FarazzShaikh / THREE-CustomShaderMaterial

Extend Three.js standard materials with your own shaders!
Other
846 stars 54 forks source link

Cannot use both csm_DiffuseColor and csm_FragColor in if/else #48

Open tresabhi opened 8 months ago

tresabhi commented 8 months ago

When in opaque mode, my shader uses diffuse color to allow there to be lighting on the mesh. When not in opaque mode, my shader forms a neon overlay. But using both, even in an exclusive if/else statement, doesn't work. Heck, even commenting "// csm_FragColor" anywhere breaks the code.

uniform bool opaque;

void main() {
  if (opaque) {
    csm_DiffuseColor = vec4(1.0);
  else {
    csm_FragColor = vec4(1.0, 0.0, 0.0, 0.5);
  }
}
FarazzShaikh commented 8 months ago

Weird, yes the first part of your question is a bug. CSM to prove https://codesandbox.io/s/awesome-robinson-dz3p99?file=/src/App.tsx

Heck, even commenting "// csm_FragColor" anywhere breaks the code.

Not sure what you mean. Comments are stripped internally, so they shoudlnt have any effect. Here is a CSB with it working as expected with a // csm_FragColor https://codesandbox.io/s/sad-booth-ttjwg8?file=/src/App.tsx

FarazzShaikh commented 8 months ago

Temporary workaround is to use shader replacement like so: https://codesandbox.io/s/nervous-agnesi-mz8yzj?file=/src/App.tsx:897-926

Doing this does not seem to have any performance implications. However, the bug should be fixed in the next update.

tresabhi commented 8 months ago

Weird, comments did break them in my case, oh well. Glad to see such a rapid fix 😄, thanks!