Ver código fonte

fix bug in invClipZ unpacking glsl code. (#8600)

Ben Houston (Clara.io) 9 anos atrás
pai
commit
86cc5819dc

+ 7 - 11
examples/webgl_depth_texture.html

@@ -35,7 +35,7 @@
         padding: 5px;
         display: inline-block;
       }
-      
+
       #error {
         margin: auto;
         margin-top: 40px;
@@ -45,7 +45,7 @@
         background: #CE0808;
       }
     </style>
-    
+
     <script id="post-vert" type="x-shader/x-vertex">
       varying vec2 vUv;
 
@@ -56,24 +56,20 @@
     </script>
     <script id="post-frag" type="x-shader/x-fragment">
       #include <packing>
-    
+
       varying vec2 vUv;
       uniform sampler2D tDiffuse;
       uniform sampler2D tDepth;
       uniform float cameraNear;
       uniform float cameraFar;
 
+
       float readDepth (sampler2D depthSampler, vec2 coord) {
-        float cameraFarPlusNear = cameraFar + cameraNear;
-        float cameraFarMinusNear = cameraFar - cameraNear;
-        float cameraCoef = 2.0 * cameraNear;
-        return cameraCoef / (cameraFarPlusNear - texture2D(depthSampler, coord).x * cameraFarMinusNear);
+        float fragCoordZ = texture2D(depthSampler, coord).x;
+        float viewZ = invClipZToViewZ( fragCoordZ, cameraNear, cameraFar );
+        return viewZToLinearClipZ( viewZ, cameraNear, cameraFar );
       }
 
-      // float depthTexel = texture2D(tDepth, vUv).x;
-      // float depth = invClipZToViewZ(depthTexel, cameraNear, cameraFar);
-      // float linearClipZ = viewZToLinearClipZ(depth, cameraNear, cameraFar);
-
       void main() {
         vec3 diffuse = texture2D(tDiffuse, vUv).rgb;
         float depth = readDepth(tDepth, vUv);

+ 1 - 1
src/renderers/shaders/ShaderChunk/packing.glsl

@@ -32,5 +32,5 @@ float viewZToInvClipZ( const in float viewZ, const in float near, const in float
   return (( near + viewZ ) * far ) / (( far - near ) * viewZ );
 }
 float invClipZToViewZ( const in float invClipZ, const in float near, const in float far ) {
-  return ( near * far ) / ( ( near - far ) * invClipZ - far );
+  return ( near * far ) / ( ( far - near ) * invClipZ - far );
 }