1
\$\begingroup\$

I want to determine if a mesh is lit by the directional light on my scene in editor to automatically disable its "dynamic shadow casting" property (let's say I have one light source in my scene which is this directional light).

The mesh is a static object: it can't move and it's not animated. It has a bounding box and a bounding sphere. All I need to do is to provide a plausible (not 100% accurate) answer to the following question: "is my static mesh fully occluded by some other static meshes?". I am not going to do that in real time, it's just an editor check, but my scene has several thousand static meshes, so it shouldn't be dead slow.

I'm afraid that the only way to do that is by performing a ray (or line) tracing in the direction of my light source. I can just trace the vertices of my bounding box, but the mesh can be partially occluded (e.g. vertices are, the centre of the bounding box is not) so my guess is that I need to trace a projection of the bounding box in the direction of my light source but I don't know how to calculate things like that and my idea may not be correct. :(

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Here's a wild solution:

Step.1

backup your scene.

Step.2

Gives all meshes in the scene a new material, it will just return the set color. the shader just like:

float4 frag (v2f i) : COLOR
{
    return _Color;
}

Step.3

Number each mesh and give it a color according to certain rules.(id->rgb, alpha=1)

Step.4

Create a orthographic camera, set its direction to be the same as the light. Set its size so that it sees all meshes in the scene. Turn off antialiasing.

Step.5

Set the camera's target texture, Iterate over all pixels, check if color "x" exists.This way you can tell if an object is illuminated. The accuracy depends on texture resolution.

\$\endgroup\$
4
  • \$\begingroup\$ Doesn’t sound wild at all! It’s actually very clever and quite simple idea. I think I also can use speep tracing to find all possible occluders “above” my object and check using this technique. I think that the rotation of my “camera” should be the same as the direction of light source. Do you know how could I calculate the “view” matrix to “focus” on my object? \$\endgroup\$
    – Netherwire
    Commented Apr 26, 2022 at 0:30
  • \$\begingroup\$ To simulate directional light, the camera should be orthographic. So just let the camera's orthographicSize is greater than the object's bounding sphere radius. The position of camera can be calculated as (position of object) + (-x)*(normalized light direction). The direction of camera is the same as the light's. You can convert a quaternion to a direction. \$\endgroup\$
    – Mangata
    Commented Apr 26, 2022 at 2:03
  • \$\begingroup\$ Cool, thanks! What is x in the equation above? \$\endgroup\$
    – Netherwire
    Commented Apr 26, 2022 at 21:05
  • \$\begingroup\$ The distance from the camera to the object, float. \$\endgroup\$
    – Mangata
    Commented Apr 27, 2022 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .