- Home /
Darkness system
Is there a way to check if mesh/object is covered by shadow or something like that? I would like to achieve effect that you can hide in shadows, or hide bodies. I would prefer to do it realtime - not by predefinied areas, because i would like to include flashlights etc.
Raycast from the light source to the player or the object to hide. If light hits something before the object than it's in shadow.
It might work but i would have to include additional things, like spotlight angle and distance. I'd also have to add some kind of variable which counts number of lights that cover player and if its value is 0 then player/object is hidden.
Something like this should do for a start
using UnityEngine;
using System.Collections;
public class LightCheck: $$anonymous$$onoBehaviour {
public new Light[] light;
public GameObject[] go;
public static bool inLight;
void Start () {
go = GameObject.FindGameObjectsWithTag("Shadowed");
light = (Light[])FindObjectsOfType (typeof(Light));
}
void Update () {
RaycastHit raycastHit;
foreach (GameObject i in go) {
foreach (Light l in light) {
Vector3 rayDirection = i.transform.position - this.transform.position;
if (Physics.Raycast (this.transform.position, rayDirection, out raycastHit, l.range)) {
if (raycastHit.transform.tag == "Shadowed") {
inLight = true;
} else {
inLight = false;
}
}
}
}
}
}
and then
if (LightCheck.inLight) {
//Do Something
}else{
//Do Something else
}
Thanks for help, but what should i do with directional lights? It might not work this way.
Answer by calmcarrots · Jul 17, 2015 at 08:00 PM
Use light probes
And what should i do with them? Is there a way to check if player is covered by shadow using them? If yes, then how?
Did you look at the link I gave you? Read it all. Then look at the scripting options you have for light probes. They're dead simple and I don't think it requires much explanation. Do some more research on light probes. But no one is going to code a whole entire darkness system.
http://docs.unity3d.com/ScriptReference/LightProbes.html
Also, here are some extra links
https://www.youtube.com/watch?v=O3DT1QDGelo https://www.youtube.com/watch?v=eGu9_8HS2uI
Answer by Lahzar · Jul 18, 2015 at 06:37 PM
Are you doing this in 2D or 3D? 2D is much easier to do this.
I use something similar in my AI, but its not exactly perfect yet. I have a rendertexture which renders the player and a shadow layer from a camera placed on the AI. Every X seconds (I use 0.1) it starts a coroutine which gets the rendertexture and converts it to a texture and then convert that to a 2D array of colors. Then I have a thread which compares the grayscale value of every single pixel in said array to a predefined color set in the inspector. And then it uses some more advanced stuff to change how my AI reacts, but thats seems offtopic for this. The shadow layer is the entire map copy-pasted to a new layer and with all the mesh renderes turned to shadows only, so they wont render, just cast/receive shadows.
It works great and I have not noticed any performance drops, but its not perfect. Im not a lighting expert, so my scene is very poorly lit, just 1 directional light and a few point lights here and there. So when the player stands between the AI and the player, the AI doesn't see the player because all the light hits the back of the player... I am in the process of trying to fix this, but you know...
It might not work perfectly for your scenario, but its atleast a reasonable dynamic shadow detection system.
//Made by Imre Angelo aka Lahzar on the unity forums
//You should be able to fill in the blanks/predefined variables
//Also remember to play around with ambient occlusion. Can make things better & easier! Enjoy!
static bool processing = false;
void Start() {
#region Create RenderTexture
RenderTexture template = GetComponentInChildren<Camera>().targetTexture;
rt = new RenderTexture(template.width, template.height, template.depth);
rt.Create();
GetComponentInChildren<Camera>().targetTexture = rt;
#endregion
}
#region Shadow Detection
IEnumerator RenderToTexture() {
processing = true;
yield return new WaitForEndOfFrame();
RenderTexture.active = rt;
text = new Texture2D(rt.width, rt.height);
text.ReadPixels(new Rect(0,0,rt.width,rt.height),0,0);
text.Apply();
pixels = text.GetPixels();
DebugMaterial.mainTexture = text;
yield return new WaitForSeconds(freq);
Thread t = new Thread(ComparePixels);
t.Start();
t.Join();
processing = false;
yield return visible;
}
void ComparePixels() {
foreach(Color p in pixels) {
if(p.grayscale >= bright.grayscale) {
visible = true;
break;
} else {
visible = false;
}
}
}
#endregion
Your answer
Follow this Question
Related Questions
Light creates distortion on mesh? 1 Answer
No shadows. 2 Answers
Light with minimum bias not sufficient 1 Answer
Casting sharp 2D shadows side on 0 Answers
How can i make the game area pitch black 2 Answers