armory/Shaders/std/clusters.glsl
N8n5h 8e972f75fd Fix light visual artifact when close to the camera
This fixes a current problem with lights that occurs when getting too
close to the camera. The issue is related to the clustering algorithm
and cluster near.

Cluster near explanation:

Because of the logarithmic nature of how the Z slice is calculated,
clusters slices get "burned" too quickly at the start of the frustum,
which is not ideal. To aid this, and offset is applied to the start of
the cluster frustum, and that is clusterNear.

Cluster near solves that problem but creates another one: There is a gap
between the near plane and the cluster near offset that gets completely
ignored when calculating. And this results in the visual artifact of
slices not being affected by light whatsoever.

The proposed solution is make the gap resolve to the first slice of Z
clusters.
2021-03-13 13:30:19 -03:00

19 lines
594 B
GLSL

const vec3 clusterSlices = vec3(16, 16, 16);
int getClusterI(vec2 tc, float viewz, vec2 cameraPlane) {
int sliceZ = 0;
float cnear = clusterNear + cameraPlane.x;
if (viewz >= cnear) {
float z = log(viewz - cnear + 1.0) / log(cameraPlane.y - cnear + 1.0);
sliceZ = int(z * (clusterSlices.z - 1)) + 1;
}
// address gap between near plane and cluster near offset
else if (viewz >= cameraPlane.x) {
sliceZ = 1;
}
return int(tc.x * clusterSlices.x) +
int(int(tc.y * clusterSlices.y) * clusterSlices.x) +
int(sliceZ * clusterSlices.x * clusterSlices.y);
}