- Home /
Accessing a shadowmap and using it in C# script to turn off colliders?
Hi. I previously asked this question: https://forum.unity3d.com/threads/dynamic-cutout-mesh-colliders-using-shadow-maps.433729/
Basically, I want to achieve the effect of "cutting"/disabling a part of an object when it is in shadow (so it doesn't collide with anything, even though it is still visually rendered).
Now I am getting closer to a solution.
The setup: I want to disable certain colliders based on whether they are in shadow or not (using a spot light in Unity).
I have a box that consists of many smaller box colliders (4x4):
Basically, I am trying to access the shadowmap/depth texture, which stores the closest point seen from the light's point of view. So far, I have managed to create a depth texture using this guide: http://williamchyr.com/2013/11/unity-shaders-depth-and-normal-textures/
However, this shadowmap/depth texture is created from the camera and not the light source (right now, they are placed in the exact same spot). Also, the camera's culling mask doesn't include the box colliders, so they won't affect the shadowmap/depth texture: http://imgur.com/9WG0L8G
Using this depth value, I want to compare it against the distance of each of the (box) colliders. If the distance from the light source to the collider is larger, then it means that the collider is in shadow (and should be turned off). Here are two examples (purple area should be turned off): http://imgur.com/AtBuZia http://imgur.com/ZL4BLVJ
Here is an image that illustrates the distance check I want to make (e.g., one of the center colliders):
Usage example: if I push a box halfway into a shadow, it should start "tumbling" as if it is tipping due to gravity. E.g., in the image below, the box is halfway in shadow, so you would expect it to start falling down into the shadow and get "absorbed" by it (because each colliders turn off when in shadow; when all are turned off, the object won't collide with the floor and therefore fall down through the shadow): http://imgur.com/nV0UzT5
So, what I need now is to be able to access this shadowmap/depth texture and look up each of the colliders and the corresponding depth value. This value should then be compared to the actual distance between the collider and the light source. However, I am having problems with doing this, since I am unsure how to use the position of a gameobject (the collider) with the shadowmap (which is being generated in a post-processing shader).
Also, I would really like to directly access the built-in shadowmap generated by each of the light sources in Unity, instead of manually having to create them by placing a camera in the same spot as the light.
Do you have any suggestions of how to do this? I assume I need to do some communication between shaders and C# scripts, since the shadowmap would have to be generated via a shader, and then a C# should be able to take that and compare the depths with Vector3.Distance() for each of the colliders (and decide to turn them on/off).
Thanks a lot in advance!