My apologies if this has already been answered, I tried searching on here and didn’t find anything.
Also apologies if this is a rather stupid question, I am not a “shader maker” by any means, but I have a special case that I’d like to use them for.
I basically need to grab the amount of light that is falling on an object and pass that back to a script. It seems like the “primary” variable inside of the SetTexture call would be great for this. However, I need to get that information back OUT to a normal Unity JS script.
Is this possible? I thought about trying to set a custom variable on the Shader and updating that, but I can’t seem to find the syntax that allows me to update that variable in the SetTexture or even Pass blocks.
Any help or a pointer in the right direction would be greatly appreciated!
In a word: no. For the most part, 3D graphics hardware is one-way. Generally the only way you can get information back from them is in the form of pictures, and only relatively slowly.
Shaders probably won’t help you do what you want. However, can you describe what you want in more detail? Perhaps someone can help you come up with another solution.
What I’d like to know is how you’d like to measure light. Total light energy absorbed? Total reflected? What is blocking or attenuating the light? Might you be able to come up with an analytical solution?
I’m not looking to do anything super-complicated (at least, in my mind, it’s not that complicated). I basically want a quick and easy way to detect a general measure of how much light is falling on the player. Similar to the light gauge in the Thief series of games, if you will.
Lightmaps on the terrain/level are an option, of course, but they it wouldn’t take into account things like guards walking around with torches, the ability to extinguish a torch on a wall, etc. So I think a real-time solution is the only one that will fully “work”.
My original thought was simply to access a pixel on the player’s texture or material and then watch for changing values. However it seems that the GetPixel data isn’t updated in real-time.
Then I started reading the Shader examples, and noticed that the “primary” variable in the SetTexture code seemed to be used as a measure of the amount of light shining on the object. I thought maybe I could harness this for my purposes.
Again, it doesn’t have to be perfect, but if there were just some way I could detect “Player is mostly Shadowed”, “Player is semi-exposed”, “Player is fully exposed”, that would be great.
You’d be much better off casting rays at dynamic lights from a few positions on the player to check for occlusion. You can use raycasts around the player to look up light map values, too.
Thanks for the advice.
So I would basically put my dynamic lights on a separate layer, and then cast rays off of the player in random directions?
I don’t know that much about raycasting, but my understanding is that you are just getting one line (between two points) in space…meaning I could easily miss something as small as a light, correct? Or can I just shoot off a ton of them because they are relatively inexpensive?
Thanks in advance for your help, sorry to be such a newbie :-).
EDIT: After some further thought, maybe I wouldn’t want to put them on a separate layer…because I would want to take into account level geometry that’s in the way was well…I’m also thinking this would only work well if the all the dynamic lights I used (such as torches) had a standard “light radius”, so I know how far to check out from the character? Or would I be able to access that value through the transform of the RayCastHit object? (Meaning does that get me the actual full object I hit, or is that literally just the transform values?)
Ah, this is starting to make some sense.
So I could use a SphereCollider around each light representing the radius of the projected light (not taking into account things like geometry). Then put a trigger on that, when the player is inside the sphere, the light starts raycasting toward the player. If it hits the player, the player is “lit”.
Does that sound like it should work?
I also love the advice of putting some of the information into AI pathnodes.
Thanks so much for all of the help!