- Home /
How Can I Detect If An Object Is Lit By A Specific Light?
I have a scene with a number of objects in it. I want to detect if objects are lit by a specific directional light. I don't want to detect any objects that are within a shadow it casts.
One solution I was looking at was to raycast from relevant objects at an angle equal to the angle the light would be arriving from. However, this seems a little inaccurate, as it only checks if the centre of the object is lit or not. Ideally, I would detect whether any part of the object was lit by the directional light.
Is there an obvious way to improve the accuracy of my check or to straightforwardly check in C# which light is illuminating an object?
I guess you could use more than one ray cast to check a few points on the object. There's possibly a complex way using shaders I'd imagine.
You could improve your algorithm by converting the eight points of the mesh.bounds into world space and using each for your raycast. This will produce some false positives since the bounds is larger than your object.
For future visitors, and hopefully give you a few ideas, I'll link the other questions I found with a similar problem.
Detect light/shadow falling on object?
How to detect if a GameObject is hit by a light
I can't quite figure out what you'd have to do with the camera settings to simulate a directional light rather than a spot light - but the shader approach would be:
Place a camera at the light's no$$anonymous$$al position, oriented along its forward.
Create a render texture (say 256x256) and have the camera render to that
Code the vertex colours of the meshes of interest to encode an index for the item that needs to be seen. If there's only one then just have it set to 1. All the vertex colours need to be the same (you could also use UV2 or another vertex property if you are using vertex colouring for something else).
Render the camera with a replacement shader that writes the vertex colour of objects
Read back the render texture and scan it for any values that aren't 0 - these will be the indices of the items that are seen.
I think that for the case of a directional light you would need to position the camera a distance away from each object of interest in the opposite direction to the light's forward and do it once for each.
Clearly you need to be able to decode the vertex colour from a float to an int - so just remembering that each index would be 1f/255 per colour channel should do it.
Answer by gardian06 · Aug 09, 2013 at 07:18 PM
give the light a sphere collider (marked trigger) place the trigger in the interested area (where the light illuminates the ground hopefully ignoring collisions with the ground), and then give it an OnTriggerXXX() then you can determine what is colliding with that trigger, and grant you the ability to see what is inside the light.
if you want to have more accuracy you could use a capsule collider (sized to the radius of the light), and then do a Vector3.Angle() test, but I am not the most sure on this.
pre EDIT note: a Directional light (defies logic as a directional light illuminates every face with normal vector less then 90 degrees of angle from the direction of the light vector for all intents and purposes everything in the scene) instead of a SpotLight. so I wrote with regards to a SpotLight.
EDIT: as a directional Light effectively illuminates everything in the scene. instead of determining what is illuminated it would be easier to determine what is not illuminated for this I am seeing:
have a manager responsible for tracking each detectable object.
have the manager hold the negative vector of the directional light
foreach of those detectable objects take about 8-12+ points of interest (based on level of accuracy/anal-retention)
and fire a ray from those points of interest in the direction of the negative vector of the direction light, and if the ray does not hit anything within distanceX the object is visible.
That wouldn't cope with the shadows (which is what the OP is asking for).
@whydoidoit sorry about that Edited to answer question.
Your answer
Follow this Question
Related Questions
How to detect if an object is under a shadow casted by a directional light 1 Answer
What am i doing wrong? I want to make a raycast acces panel with lights 2 Answers
Directional light between scenes ( .LoadLevel) 3 Answers
using occlusion maps to reduce direct light 0 Answers
Accessing a shadowmap and using it in C# script to turn off colliders? 0 Answers