Hi Guys
I am trying to detect real light source with a simple camera. Any help?
“when the camera see the light source
detect the location of that and return
back the position.”
Light[] lights;
// Use this for initialization
void Start () {
lights = FindObjectsOfType<Light>();
}
// Update is called once per frame
void Update () {
Vector3 lightPos = GetVisibleLightSourcePosition();
if (!Vector3.Equals(lightPos, Vector3.zero))
{
// light source is visible
Debug.Log("Light source position = " + lightPos);
}
}
Vector3 GetVisibleLightSourcePosition()
{
foreach (Light light in lights)
{
//is the camera facing towards the light source?
Vector3 screenPoint = Camera.main.WorldToScreenPoint(light.transform.position);
if (screenPoint.x >= 0 && screenPoint.x <= Screen.width &&
screenPoint.y >= 0 && screenPoint.y <= Screen.height)
{
// is the view of the light source being obstructed?
Vector3 dir = Camera.main.transform.position - light.transform.position;
float distance = Vector3.Distance(Camera.main.transform.position, light.transform.position);
if (!Physics.Raycast(Camera.main.transform.position, dir, distance))
{
// light source is visible to camera
// TODO: Return array of positions if more lights need to be detected
return light.transform.position;
}
}
}
return Vector3.zero;
}