#3D Spatial Shader: Setting ALPHA in Light() Does Seemingly Nothing

1 messages · Page 1 of 1 (latest)

slim pewter
#

What I'm trying to do is have a light turn a transparent object into a solid one. I can do this just fine with "normal" lights, but I want to do this for a specific light source, which I identify by giving it a color of solid black (0, 0, 0). For this light, for whatever reason it's not working out.

Below is a part of my code, simplified:

void fragment() {
  [unrelated code]
  .
  .
  .
  ALPHA = 0.0;
}

void light() {
  if(LIGHT_COLOR == vec3(0.0, 0.0, 0.0)) {
    DIFFUSE_LIGHT += dot(NORMAL, LIGHT) * ATTENUATION * vec3(1.0, 1.0, 1.0) / PI;
    ALPHA = 1.0;
  } else {
    DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR / PI;
    ALPHA = 1.0;
  }
}

In light(), I check whether the light color is solid black to identify the special light, after which I do some operations which include changing the diffuse and alpha. If the light is not special, then I enter the else part, where I compute the lighting differently.

I know for a fact that the if/else can go both ways, since I see the lighting effects of both DIFFUSE_LIGHT statements. Yet for whatever reason, only the ALPHA assignment in the else statement ever works. For the other one, setting it to 0, 1 or any other in-between value, everything looks the same, as in as if the assignment was never there to begin with. What's going on here?

#

Some more context:
What I'm "actually" trying to do is ||have a 3D object use an entirely different material when shone upon. My current approach is to first render one material as opaque, then in a second pass render the second material as transparent, but the second material turns opaque when shone upon by the special light.||

If there is a better approach to this, that completely sidesteps the issue I'm having in OP, then that is of course much better

pine cipher
#

When you say you see the effect of both DIFFUSE_LIGHT assignments, what exactly do you mean? Specifically, are you sure that LIGHT_COLOR == vec3(0.0, 0.0, 0.0) evaluates to "true" when you expect it to, since the == can have precision issues with floats? That's basically the only thing I can think of that might be going wrong.

#

Actually, according to the docs there's also the fact that LIGHT_COLOR represents the color set by you multiplied by the light energy ... so maybe the Godot does some weird optimization with lights it interprets as having zero energy? (But I have no idea if that's actually the case).

sonic hemlock
#

I have something similar in an old test scene. Looks very similar to what you want to do

slim pewter
slim pewter
# pine cipher When you say you see the effect of both DIFFUSE_LIGHT assignments, what exactly ...

What I mean is I have only two lights in the scene, one normal, one special. The entire scene is dark (although I do have low ambient illumination).

The normal light keeps spinning around, and I can see it illuminate objects as it passes by, and also (partially) hiding/revealing objects (the specifics depend a bit on what the ALPHA assignments I use at that time).

The special light is basically a flashlight held by a player-controlled character. I can clearly see it illuminate objects as I point the light at something (which indicates that LIGHT_COLOR == vec3(0.0, 0.0, 0.0) holds), but then for whatever reason the ALPHA assignment of that part never seems to have any effect whatsoever.

sonic hemlock
slim pewter
# slim pewter

Here's a short video to show what I mean. Ignore all objects except for the prism. During this run, I set default ALPHA to 1.0 (fully opaque). The normal light is supposed to make objects it illuminates turn somewhat transparent, and the special light is supposed to do something similar.

The normal light does what it should do, which is illuminating the object (hard to see in the video because of the transparency effect) as well as turning objects invisible. The special light only illuminates objects however

slim pewter
#

So to be completely clear, with this code:

void fragment() {
  .
  .
  .
  ALPHA = 1.0;
}

void light() {
  if(LIGHT_COLOR == vec3(0.0, 0.0, 0.0)) {
    DIFFUSE_LIGHT += dot(NORMAL, LIGHT) * ATTENUATION * vec3(1.0, 1.0, 1.0) / PI;
    ALPHA = 0.0;
  } 
}```
I illuminate objects I point the special light at, but objects remain opaque (normal lights are completely out of the picture).
sonic hemlock
#

It "works" for me, I think it just looks weird since there is no fading between alpha 0 or 1

slim pewter