uniform float zNear;// = 0.1; uniform float zFar;// = 500.0; float mapRange(float x, float minIn, float maxIn, float minOut, float maxOut) { x -= minIn; x = x * (maxOut - minOut) / (maxIn - minIn); x += minOut; x = clamp(x, minOut, maxOut); return x; } float linearDepth(float depthSample) { depthSample = 2.0 * depthSample - 1.0; float zLinear = 2.0 * zNear * zFar / (zFar + zNear - depthSample * (zFar - zNear)); return zLinear; } float adepthSample(float linearDepth) { float nonLinearDepth = (zFar + zNear - 2.0 * zNear * zFar) / (linearDepth * (zFar - zNear)); nonLinearDepth = (zFar + zNear - 2.0 * zNear * zFar / linearDepth) / (zFar - zNear); nonLinearDepth = (nonLinearDepth + 1.0) / 2.0; return nonLinearDepth; }