CopyShader opacity does not work as expected
See original GitHub issueDescription of the problem
In examples/js/shaders/CopyShader.js, there is an issue with the fragment shader:
"void main() {",
" vec4 texel = texture2D( tDiffuse, vUv );",
" gl_FragColor = opacity * texel;",
"}"
Because texel
is multiplied by opacity
directly, all elements of the color are scaled by the opacity value. This means on a black background, the opacity is effectively applied twice. On any other background, the resulting color will end up darker than expect.
Proposed Solution:
Multiply only the alpha component by opacity:
"void main() {",
" vec4 texel = texture2D( tDiffuse, vUv );",
" texel.a = opacity * texel.a;",
" gl_FragColor = texel;",
"}"
Three.js version
- Dev
- r117
- …
Browser
- All of them
- Chrome
- Firefox
- Internet Explorer
OS
- All of them
- Windows
- macOS
- Linux
- Android
- iOS
Hardware Requirements (graphics card, VR Device, …)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6
Top Results From Across the Web
THREE.js FXAA shader not working with CopyShader
Im attempting to render multiple scenes on the same canvas and renderer. Everything works as expected until I add the FXAA ...
Read more >Question - ZWrite in shader graph - Unity Forum
When I change the unlit master node surface type to transparent, the object geometry is not occluded since the ZWrite is off.
Read more >Creating the Effect of Transparent Glass and Plastic in Three.js
Traditionally when we adjust the opacity of an element to make it transparent, its visual presence is diluted as a whole. The object...
Read more >[Solved]-THREE.js FXAA shader not working with CopyShader
Coding example for the question THREE.js FXAA shader not working with CopyShader - renders blank-three.js.
Read more >Journey of Learning Three.js (Day 4) | by James Min | Medium
The problem with importing 3d blender object into three is that all shaders and alpha transparency, that has been added in blender software, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have second thoughts on this…
I trust the original author knew what he was doing back in 2011.
CopyShader
was designed to copy render target textures, which by default, have premultiplied alpha.If you want to further scale the opacity, you need to scale all four components.
I would be inclined to revert https://github.com/mrdoob/three.js/pull/23671, and look elsewhere if there are issues.
Yes, likely.
One needs to check the three.js examples where this is used – used both directly and indirectly.
Check
BlendShader.js
, too.