ThreeJS后期多场景渲染
需求:场景模型需要Bloom,ssao等后期特效,但人物模型不需要。
之前做过类似的需求,一个场景伽马校正,另一个不需要,实现方式是用同一个renderer和camera, 多个scene,将renderer.autoClear = false,然后分别渲染scene1,scene2
尝试了下这套方案不可取,因为现在renderer通过后期混合器调用来渲染,然后就是一顿上网查,国内没有相关信息,google一下发现5,6年前在stackoverflow上有人问过,但不了了之..
看下了renderPass的源码:
var RenderPass = function ( scene, camera, overrideMaterial, clearColor, clearAlpha ) {
Pass.call( this );
this.scene = scene;
this.camera = camera;
this.overrideMaterial = overrideMaterial;
this.clearColor = clearColor;
this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 0;
this.clear = true;
this.clearDepth = false;
this.needsSwap = false;
};
发现RenderPass有clear属性可以利用下,新建一个renderPass在ssao后面,通过叠加的方式,可以实现需要的效果
function initEffect() {
let renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: true };
let renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, renderTargetParameters);
composer = new EffectComposer(renderer, renderTarget);
renderPass = new RenderPass(scene, camera);
composer.addPass(renderPass);
var ssaoPass = new SSAOPass(scene, camera, window.innerWidth, window.innerHeight);
ssaoPass.kernelRadius = 16;
ssaoPass.maxDistance = 0.03;
ssaoPass.maxDistance = 0.1;
composer.addPass(ssaoPass);
renderPass2 = new RenderPass(scene2, camera);
renderPass2.clear = false
composer.addPass(renderPass2);
var bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight));
bloomPass.threshold = 0;
bloomPass.strength = 0.1;
bloomPass.radius = 0;
composer.addPass(bloomPass);
}
done:)