如何渲染具有位移顶点的对象的深度缓冲?

时间:2013-10-16 20:32:55

标签: javascript html5 three.js webgl depth-buffer

我修改了这个例子:http://stemkoski.github.io/Three.js/Shader-Fireball.html

并插入:

var depthShader = THREE.ShaderLib["depthRGBA"];
var depthUniforms = THREE.UniformsUtils.clone(depthShader.uniforms);
depthMaterial = new THREE.ShaderMaterial({
     fragmentShader : depthShader.fragmentShader,
     vertexShader : depthShader.vertexShader,
     uniforms : depthUniforms
});
depthMaterial.blending = THREE.NoBlending;

depthTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, {
    minFilter : THREE.NearestFilter,
    magFilter : THREE.NearestFilter,
    format : THREE.RGBAFormat
});

quadCamera = new THREE.OrthographicCamera(window.innerWidth / -2, window.innerHeight / 2, window.innerWidth / 2, window.innerHeight / -2, -1000, 2000);
quadCamera.position.z = 100;

var shader = THREE.UnpackDepthRGBAShader;
var uniforms = new THREE.UniformsUtils.clone(shader.uniforms);
uniforms.tDiffuse.value = depthTarget;
quadMaterial = new THREE.ShaderMaterial({
    vertexShader : shader.vertexShader,
    fragmentShader : shader.fragmentShader,
    uniforms : uniforms
});

var mesh = new THREE.Mesh(new THREE.PlaneGeometry(window.innerWidth, window.innerHeight*1.5), quadMaterial);
mesh.position.z = -500;
mesh.position.y = 200;
quadScene = new THREE.Scene();
quadScene.add(mesh);

并将渲染功能更改为:

function render() {
   renderer.overrideMaterial = depthMaterial;
   renderer.render(scene, camera, depthTarget, true);
   renderer.overrideMaterial = null;
   renderer.render(quadScene, quadCamera);
}

看起来像是:http://i.imgur.com/hiHLc8g.png

  1. 如何让深度缓冲区看起来像深度缓冲区而不是黑色?
  2. 取代顶点的自定义ShaderMaterials会正确写入深度缓冲区吗?因为我有另一个项目,其中包含位移顶点的对象,深度不考虑位移。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

嗯,我觉得这架飞机也很黑,这很奇怪。因为在你的理论中,如果只是为了移位顶点,平面是否会被正确着色?

相机的近距离和远距离剪裁平面值是否已关闭?因此,它无法正确使用深度。您是否检查过近端和远端值是否在理智范围内?