question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CopyShader opacity does not work as expected

See original GitHub issue
Description 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
WestLangleycommented, Jun 25, 2022

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.

1reaction
WestLangleycommented, Jun 3, 2020

Yes, likely.

One needs to check the three.js examples where this is used – used both directly and indirectly.

Check BlendShader.js, too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found