- Home /
Light touches Object
Hi there,
is it anyhow possible to determine wether an object is lightened up by a light source and maybe how much? For now I tried raycasting to look wether my flashlight touches an object. This works somehow, but is not perfect and stuff. So does anyone has another idea for that?
Answer by Berenger · Mar 26, 2012 at 05:13 PM
You need the attenuation formula of lights. Pseudo code :
Points lights : lerp(0, 1, distance[light, fragment] / dMax), 0 is out of range and unlit.
SpotLight : lerp(0, 1, distance[light, fragment] / dMax) * lerp(0, 1, angle[forward, direction[light,fragment]] / angleMax), 0 is out of range and unlit. It's not exactly that as there is angleMin where everything is fully lit, but enough for your problem.
Directional : box intersection (i don't have one right now), distance doesn't matter.
Can you please specify this? I am quite new to Unity and dont know yet where to put such formulas the best way.
Well, it depends when you need to know that information. Probably in Update(). Here is a function for the spotlig, to know if an object is lit or not (know the amount of light is a tiny bit more complicated, but not much). Notice that the position in question is the position of the GameObject :
function IsLit( Light L ) : bool { var dist : float = (L.transform.position - transform.position).sqr$$anonymous$$agnitude; return dist < L.range*L.range; }