【问题标题】:reconstruct worldposition.xyz from depth从深度重建 worldposition.xyz
【发布时间】:2022-03-25 11:33:58
【问题描述】:

我想从渲染图像的任何像素恢复 worldposition.xyz 以进行后处理。在 three.js 示例的帮助下,我重建了深度值。我认为我已经接近我的目标了。有谁知道我如何根据 vUv 和深度值重建世界位置?

depthShader = {

    uniforms: {

       'tDiffuse': { value: null }, 
       'tDepth': { value: null },
       'cameraNear': { value: 0 },
       'cameraFar': { value: 0 },            
    },

    vertexShader:`
        
       varying vec2 vUv;
        
       void main() {
                
          vUv = uv;
            
          vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
          gl_Position = projectionMatrix * modelViewPosition;
    
       }`,

    fragmentShader:`
       #include <packing>

       uniform sampler2D tDiffuse;
       uniform sampler2D tDepth;
       uniform float cameraNear;
       uniform float cameraFar; 
       varying vec2 vUv;

       float readDepth( sampler2D depthSampler, vec2 coord ) {
          float fragCoordZ = texture2D( depthSampler, coord ).x;
          float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
          return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
       }


       void main() {

          float depth = readDepth(tDepth, vUv);
          vec4 color = texture2D(tDiffuse, vUv);
        
          gl_FragColor.rgb = 1.0 - vec3( depth );
            
       }`

};

【问题讨论】:

    标签: three.js shader


    【解决方案1】:
    float clipW = cameraProjection[2][3] * viewZ + cameraProjection[3][3];
    vec4 clipPosition = vec4( ( vec3( gl_FragCoord.xy / viewport.zw, depth ) - 0.5 ) * 2.0, 1.0 );
    clipPosition *= clipW;
    vec4 viewPosition = inverseProjection * clipPosition;
    vec4 vorldPosition = cameraMatrixWorld * vec4( viewPosition.xyz, 1.0 );
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      相关资源
      最近更新 更多