diff --git a/libraries/render-utils/src/lightClusters_drawGrid.slv b/libraries/render-utils/src/lightClusters_drawGrid.slv index db7006322c..4c32be6d5c 100644 --- a/libraries/render-utils/src/lightClusters_drawGrid.slv +++ b/libraries/render-utils/src/lightClusters_drawGrid.slv @@ -33,6 +33,18 @@ uniform frustrumGridBuffer { FrustrumGrid frustrumGrid; }; +float getProjectionNear(mat4 projection) { + float planeC = projection[2][3] + projection[2][2]; + float planeD = projection[3][2]; + return planeD / planeC; +} +float getProjectionFar(mat4 projection) { + //float planeA = projection[0][3] - projection[0][2]; + //float planeB = projection[1][3] - projection[1][2]; + float planeC = projection[2][3] - projection[2][2]; + float planeD = /*projection[3][3]*/ - projection[3][2]; + return planeD / planeC; +} //uniform ivec4 inClusterLocation; out vec4 varColor; @@ -66,14 +78,24 @@ void main(void) { vec4 pos = UNIT_BOX[UNIT_BOX_LINE_INDICES[gl_VertexID]]; // pos.z -= 10.0f; + vec3 cpos = vec3(1, 0, 1); + // float z = frustrumGrid.nearFarRange.y * (1 - pos.z) + frustrumGrid.nearFarRange.z * (pos.z); + vec3 gridScale = vec3(0.5, 0.5, 1.0) / vec3(frustrumGrid.dims.xyz); - vec4 eyePos = frustrumGrid.eyeToGridProjInv * (pos); + vec3 cvpos = cpos + pos.xyz; + + vec3 clusterStart = vec3(-1.0 + cvpos.x * gridScale.x, -1.0 + cvpos.y * gridScale.y, cvpos.z * gridScale.z); + + float z = getProjectionNear(frustrumGrid.eyeToGridProj) * (1 - clusterStart.z) + getProjectionFar(frustrumGrid.eyeToGridProj) * (clusterStart.z); + + vec4 eyePos = frustrumGrid.eyeToGridProjInv * (vec4(clusterStart.xy, 1.0, 1.0) * z); eyePos.xyz *= eyePos.w; vec4 worldPos = frustrumGrid.eyeToWorldMat * vec4(eyePos.xyz, 1.0); + /* int cellIsEmpty = sign(inClusterLocation.w); ivec4 cellLocation = ivec4(inClusterLocation.xyz, (inClusterLocation.w < 0 ? -inClusterLocation.w : inClusterLocation.w));