8

I want a threejs canvas with a transparent background. I'm creating a renderer like this:

# coffeescript
r = new THREE.WebGLRenderer alpha: true

When I call r.render(), it works as expected, with the objects appearing over a transparent background. However, when I attempt to add post-processing with EffectComposer like so:

cmp = new THREE.EffectComposer r
cmp.addPass new THREE.RenderPass scene, camera

effect = new THREE.FilmPass 0.9, 2, 2048, true
effect.renderToScreen = true
cmp.addPass effect

cmp.render 3

the new result is that the scene renders as expected (objects have the Film effect correctly applied), EXCEPT the background is no longer transparent as desired...instead it's black and opaque. Why? How can I prevent post-processing from tampering with my transparent background?

2 Answers 2

8
var width = window.innerWidth || 1;
var height = window.innerHeight || 1;
var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: false };

var renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );

cmp = new THREE.EffectComposer(r, renderTarget);

If you don't specify a rendertarget then it will create one with THREE.RGBFormat which will make you lose alpha.

2
  • 2
    Wow, thank you. Because EffectComposer is nowhere to be found in the docs, I'm having a hard time learning about stuff like renderTarget...I didn't even know it existed until now. How did you learn about EffectComposer and it's parameters?
    – zakdances
    Jan 4, 2014 at 22:03
  • @dman see my comment about renderpass and using renderPass.clear=false
    – yeahdixon
    Sep 25, 2018 at 1:53
1

RenderPass by default clears your rendertarget!!!!! and will clear your alpha too if you are not careful. Therefore the clear color will be set, not from your original renderer but from the RenderPass function itself and potentially default to full opacity. There are some options you should look at: clear,clearAlpha, clearColor,clearDepth. What worked for me was to set renderPass.clear=false . I also manually cleared the renderer so my renderer.autoClear=false If you start doing a lot of passes, it helps to have precise control over when you want your renderer to clear and call them manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.