Notifications
Clear all

[Closed] Is object in shadow?

Does anyone know how to check if an object is in shadow without rendering?

anyhelp would be much appreciated

kind regards

Nebille

3 Replies

I think there’s no easy way no know that.

In fact the only way I can think of to know if an object is exactly in shadow or not is casting rays from the light to the center of each face that faces the light. So if the ray intersects with any other object in the scene closer to the light than the first object, that object will be in shadow. Obviously, not all lights will cast rays in the same way. I mean, an omni or spot light will cast rays from the light position itself to the face center of the object (perspective projection), however, a directional light will cast rays parallel to the light view direction (orthographic projection). Another thing to keep in mind is that the light could have some kind of attenuation so not all the rays will be inside the light volume. Imagine an omni light with some radius. If a ray exceed this radius before hitting the object, it’s not a valid ray anymore.

As you can imagine, this method could be very slow, depending on the scene complexity. Also, this method doesn’t take into account transparent objects.

There’s another method that is faster but it not assures you that an object is exactly in shadow. Consists in projecting the world-space bounding box of the object to be tested, to the light clip-space (so now the bounding box happens to be a rectangle) and do the same thing to the other objects to be tested against the first object. So now it’s a matter of testing if two rectangles overlap or not. To project the world-space bounding box to light clip-space, youl’ll need the light view*projection combined matrix.

Anyway, remember that this method doesn’t grants you that the object is exactly in shadow.

Hope that helps in some way.

I believe Bobo (Borislav Petrov) wrote a script a long time ago… WHo Lights Me… perhaps you can work with that to find out what is not lit, because objects in shadow are not directly lit. If you mean at all in shadow, as in partially in shadow even, not just entirely in shadow, then you have more work to do.

Maneswar

There’s another problem with the raycasting method. If the geometry is not enough tessellated, you could be missing shadows.

Imagine a big cube built up by 8 faces. If you cast a ray from the light to the center of one of the faces of the cube, it’s possible that the ray doesn’t intersect any object but still receiving shadows. One could think that casting rays to the object vertices would be enough but even then, you could still being missing shadows.

So the only way to guarantee 100% an object being in shadows is casting rays at a per-pixel basis.

Greets.