Calculate amount of light received by GameObject to change its behavior

I need to calculate the amount of light received by a GameObject on the scene to change its behavior.

For example, the “spheres” in my scene (see screenshot - Bud GameObject) have three states: “Awake, Asleep, Dead”. The default state is “Awake”.
I need to change this state, for example from “Asleep” to “Awake” or vice versa, in function of the light amount on the sphere.

Another example to better explain what I’m trying to do. If, for instance, I put a wall between the light source and my structure, every sphere in the shadow must be switched to “Asleep” state. If a sphere or more than one sphere receives more light (let’s say, the wall or the light source is moved), the state must be switched to “Awake”.

The trigger for the switch should be a certain value of the amount of light.

0 = complete shadow
1 = full light

trigger value to switch from Awake to Asleep or vice versa = 0.5 (

Any help is really appreciated.

You won’t be able to pull this data out of the rendering pipeline. You’ll probably need to just do your own Raycasts from the spheres to the lights in your scene to see if they’re obstructed. Then calculate how much light each is getting from the intensity and distance to any unobstructed lights. How many lights/spheres are you going to have in the scene?

3 Likes

Right now I need one light source (i.e. the sun) that can move, and several spheres in the scene. See the spheres as nodes of a tree branches so the exact number depends on how long I run the simulation and/or the tree species. By the way, I’m developing a tree growth simulation.

If it’s just one light and < 100 of the spheres, you shouldn’t have any trouble at all performance-wise doing raycasts from every sphere to the light every frame.

If you start adding more lights and spheres to the scene and start to notice performance problems, you have some good options. For example:

  • Don’t raycast every sphere every frame. Use a round-robin method to only raycast from some rotating subset of the spheres each frame. This will make your simulation less perfect in that it may take some extra frames for spheres to “realize” that they have entered or left a shadow, but probably not noticeably so.
  • Explore Unity’s Job system to allow raycasts to be performed on a separate thread to increase performance a lot. See https://docs.unity3d.com/ScriptReference/RaycastCommand.html
1 Like

Doing a raycast, for what I understood, I can check if a sphere is light occluded but not HOW MUCH. Some spheres can be partly in the light and partly in the shadow. Also, raycast starts from a single point, from which point of the sphere I have to cast a ray?
Sorry, newbie here

Yes you’re correct, it will only tell you about one point on the sphere being occluded. Your spheres in the image look quite small, so it may be a “good enough” approximation. If it’s not good enough for what you’re trying to do, one approach may be to do several raycasts from several points on your sphere.

If you go with a single raycast, I think it makes the most sense to start the raycast from the center of the sphere, or the point on the surface of the sphere nearest to the light in question, which would be something like sphereCenter + ((lightPosition - spherePosition).normalized * sphereRadius)

1 Like

I would try if you can get this information from LightProbes:

Get LightProbe at position and evaluate the incoming light using the spherical harmonic data:

3 Likes

Thanks both of you, I’m going to check and eventually come back here for help

1 Like