overte-HifiExperiments/libraries/render-utils/src/lightClusters_drawClusterContent.slf
2018-08-03 14:58:11 -07:00

95 lines
2.8 KiB
Text

<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
// lightClusters_drawClusterContent.frag
//
// Created by Sam Gateau on 9/8/2016.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Everything about deferred buffer
<@include DeferredBufferRead.slh@>
<@include graphics/Light.slh@>
<$declareLightBuffer(256)$>
<@include LightClusterGrid.slh@>
<@include gpu/Color.slh@>
<$declareColorWheel()$>
layout(location=0) in vec2 varTexCoord0;
layout(location=0) out vec4 _fragColor;
void main(void) {
// Grab the fragment data from the uv
vec2 texCoord = varTexCoord0.st;
vec4 fragEyePos = unpackDeferredPositionFromZdb(texCoord);
vec4 fragWorldPos = getViewInverse() * fragEyePos;
// From frag world pos find the cluster
vec4 clusterEyePos = frustumGrid_worldToEye(fragWorldPos);
ivec3 clusterPos = frustumGrid_eyeToClusterPos(clusterEyePos.xyz);
int clusterIndex = frustumGrid_clusterToIndex(clusterPos);
ivec3 cluster = clusterGrid_getCluster(clusterIndex);
int numLights = cluster.x + cluster.y;
float numLightsScale = clamp(numLights * 0.05, 0.01, 1.0);
int clusterOffset = cluster.z;
ivec3 dims = frustumGrid.dims.xyz;
dims.z +=1;
ivec3 summedDims = ivec3(dims.x * dims.y, dims.x, 1);
if (clusterPos.x < 0 || clusterPos.x >= dims.x) {
_fragColor = vec4(0.0);
return;
}
if (clusterPos.y < 0 || clusterPos.y >= dims.y) {
_fragColor = vec4(0.0);
return;
}
if (clusterPos.z < 0 || clusterPos.z > dims.z) {
_fragColor = vec4(0.0);
return;
}
int numLightTouching = 0;
for (int i = 0; i < numLights; i++) {
// Need the light now
int theLightIndex = clusterGrid_getClusterLightId(i, clusterOffset);
Light light = getLight(theLightIndex);
// Clip againgst the light volume and Make the Light vector going from fragment to light center in world space
vec4 fragLightVecLen2;
vec4 fragLightDirLen;
float cosSpotAngle;
if (!lightVolume_clipFragToLightVolumePoint(light.volume, fragWorldPos.xyz, fragLightVecLen2)) {
continue;
}
// Allright we re in the light sphere volume
fragLightDirLen.w = length(fragLightVecLen2.xyz);
fragLightDirLen.xyz = fragLightVecLen2.xyz / fragLightDirLen.w;
// Check spot
if ((i >= cluster.x) && !lightVolume_clipFragToLightVolumeSpotSide(light.volume, fragLightDirLen, cosSpotAngle)) {
continue;
}
numLightTouching++;
}
_fragColor = vec4(colorRamp(1.0 - (numLightTouching / 12.0f)), (numLightTouching > 0 ? 0.5 + 0.5 * numLightsScale : 0.0));
}