我无法使用ssao和three.js。 我试着关注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
});
}
和渲染功能:
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 );
}
也许我误会了什么。
答案 0 :(得分:4)
我不相信你可以像你一样使用SSAO着色器作为材料。材质与几何体结合以绘制网格。 SSAO着色器的意思是将其输出绘制在多个几何图形之上,而不是屏幕对齐的四边形。
通常您会使用效果编辑器类来完成此任务。
composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( postprocessing.scene, postprocessing.camera ) );
然后代替创建素材,将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 );
最后在渲染函数中使用composer渲染而不是渲染器
function render(){
scene.overrideMaterial = material_depth;
renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTextureDepth );
scene.overrideMaterial = null;
composer.render();
}
这也消除了有一个单独的漫反射渲染目标的必要性,因为作曲家会使用渲染过程来处理它。
有关没有插件的SSAO的完整示例,请参阅changedqualia:http://bit.ly/ZIPj2J