Is it possible to tell if a fragment is in shadow in a shader?

Godot Version

4.3

Question

Hello, I made a post a couple of months ago about adding a water caustics shader to Terrain3D’s terrain material. I was able to mostly do it but, the one thing that’s been bugging me since is that it still shows up in shadows. I’m not very proficient in shaders, and I can’t figure out how to fix this. Tried sending a varying out from the light function, that isn’t allowed, tried multiple ways of getting the color of the fragment, but none of it has worked for me. Any help would be greatly appreciated. :pray: :pray: :pensive: :pensive:

Edit: Another major part of the problem is just that Terrain3D’s TerrainMaterials don’t seem to support a “next pass” material. If it did I’m fairly certain I could find a way to use the light function with alpha to have it ignore sections that are in shadow.

Original post here:

I’m not 100% sure, but I was struggling with the same question lately and even created proposal here Expose scene directional light data through the compositor effect callback · Issue #11264 · godotengine/godot-proposals · GitHub.
As far as I know the answer in No. I’d be happy to hear that I’m wrong, so let’s wait together till someone says it’s possible :slight_smile:

1 Like

:saluting_face: :saluting_face: :saluting_face: :saluting_face: :saluting_face: :saluting_face:

Update: FOUND A WAY TO DO IT (in my situation) :sunglasses::sunglasses::sunglasses::sunglasses:

Basically, there were two different things that could solve the problem, either:

  1. I get lighting data out of the light function and into the fragment function.
    or
  2. I find a way to put a second material on top of Terrain3D’s terrain material.

Neither of these are technically possible lol BUT I worked around it by just duplicating the terrain, “shrinking” it slightly by doing VECTOR+=NORMAL*0.2; (or any small number), in vertex(), then doing ALPHA *= DIFFUSE_LIGHT.r; in light(), and then applying my caustics shader on to that. This has exactly the effect I was looking for, which is making the caustics disappear when they’re in shadow.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.