简体   繁体   English

Three.js和sao

[英]Three.js and ssao

I can't manage to use ssao with three.js. 我无法在Three.js中使用ssao。 I tried to follow the webgl_postprocessing_dof.html example : here is the function initPostprocessing 我尝试遵循webgl_postprocessing_dof.html示例:这是initPostprocessing函数

function initPostprocessing() {
    postprocessing.scene = new THREE.Scene();

    postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2,  window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
    postprocessing.camera.position.z = 100;             

    postprocessing.scene.add( postprocessing.camera );

    var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
    postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );  //modifier 500
    postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );

    var ssao_shader = new THREE.ShaderMaterial(THREE.ShaderExtras[ "ssao" ]);  //modification

    postprocessing.ssao_uniforms = THREE.UniformsUtils.clone( ssao_shader.uniforms );
    postprocessing.ssao_uniforms[ "tDepth" ].value=1;
    postprocessing.ssao_uniforms[ "tDiffuse" ].value=1;
    postprocessing.ssao_uniforms[ "fogEnabled" ].value=1;
    postprocessing.ssao_uniforms[ "fogFar" ].value=100;
    postprocessing.ssao_uniforms[ "fogNear" ].value=0;
    postprocessing.ssao_uniforms[ "onlyAO" ].value=0;
    postprocessing.ssao_uniforms[ "aoClamp" ].value=0.1;
    postprocessing.ssao_uniforms[ "lumInfluence" ].value=0.1;

    postprocessing.materialSSAO = new THREE.ShaderMaterial( {
        uniforms: postprocessing.ssao_uniforms,
        vertexShader: ssao_shader.vertexShader,
        fragmentShader: ssao_shader.fragmentShader
    });

}

and the render function : 和render函数:

function render() {
    renderer.clear();

    // Render depth into texture                    
    scene.overrideMaterial=material_depth;
    renderer.render( scene, camera, postprocessing.rtTextureDepth, true );

    // Render color into texture
    scene.overrideMaterial = null;
    renderer.render( scene, camera, postprocessing.rtTextureColor);

    // 
    postprocessing.materialSSAO.uniforms[ "tDepth" ].texture=postprocessing.rtTextureDepth;
    postprocessing.materialSSAO.uniforms[ "tDiffuse" ].texture=postprocessing.rtTextureColor;
    postprocessing.scene.overrideMaterial = postprocessing.materialSSAO;
    renderer.render( postprocessing.scene, postprocessing.camera );
}

Maybe I misunderstood something. 也许我误会了一些东西。

I don't believe you can use the SSAO shader as a material in the way that you are. 我不相信您可以按照自己的方式使用SSAO着色器作为材质。 Materials are combined with geometry to draw meshes. 将材料与几何体结合以绘制网格。 Where as the SSAO shader is meant to draw its output not on top of multiple geometries but to a screen aligned quad. 其中,SSAO着色器旨在将其输出绘制在多个几何图形之上而不是在多个几何图形之上。

typically you'd use the effect composer class to accomplish this. 通常,您将使用效果作曲家类来完成此任务。

composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( postprocessing.scene, postprocessing.camera ) );

then instead of creating a material the SSAO is added as a shader pass to the composer and rendered to the screen 然后不创建材质而是将SSAO作为着色器添加到作曲家并渲染到屏幕

var effect = new THREE.ShaderPass( THREE.SSAOShader );
effect.uniforms[ 'tDepth' ].value = postprocessing.rtTextureDepth;
effect.uniforms[ 'size' ].value.set( window.innerWidth, window.innerHeight );
effect.uniforms[ 'cameraNear' ].value = postprocessing.camera.near;
effect.uniforms[ 'cameraFar' ].value = postprocessing.camera.far;
effect.renderToScreen = true;
composer.addPass( effect );

and finally in the render function you use the composer to render as opposed to the renderer 最后,在render函数中,使用composer进行渲染,而不是使用renderer

function render(){
    scene.overrideMaterial = material_depth;
    renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureDepth );
    scene.overrideMaterial = null;
    composer.render();
}

this also removes the necessity to have a seperate diffuse render target since the composer takes care of that for you with the render pass. 这也消除了使用单独的漫反射对象的必要,因为作曲者会通过渲染过程为您处理。

for a complete example of SSAO without the plugin see this one by alteredqualia: http://bit.ly/ZIPj2J 有关不带插件的SSAO的完整示例,请通过alteredqualia查看此示例: http ://bit.ly/ZIPj2J

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM