Hi guys, I’m working on my first Unity game (and my first foray into Javascript) and I have a mechanic I want to implement but no idea where to begin. I want to have a dark environment that can be revealed by a screech (similar to a bat). It will be almost pitch black with only outlines visible at a short distance, then when the player screeches the environment appears in a ghostly fashion. I was thinking that the screech would involve creating a cone shaped object that would reveal objects it collides with or perhaps the screech emits vectors from the character that perform the same function. I have no idea how to implement this as simply as possible and no idea how to alter the textures of environmental objects to become ghostly when they are hit by the screech. Any ideas or help would be appreciated!
You could do this with projectors. I would recommend importing the built in ‘Projectors’ package into your project and use the ‘Blob Light Projector’ as a starting point. You’ll need to replace the shader it uses with something that ignores the z buffer contents and sorts correctly. This is my quick attempt based on the ‘Projector/Light’ shader:
Shader "Custom/Echo Projector"
{
Properties
{
_Color ("Main Color", Color) = (0.1,0.1,0.1,0)
_ShadowTex ("Cookie", 2D) = "" { TexGen ObjectLinear }
_FalloffTex ("FallOff", 2D) = "" { TexGen ObjectLinear }
}
Subshader
{
Pass
{
ZTest Always // Turn off z test so things show through each other
ZWrite Off
Color [_Color]
Blend One One // Use additive blending to make rendering order independent
SetTexture [_ShadowTex]
{
combine texture * primary, ONE - texture
Matrix [_Projector]
}
SetTexture [_FalloffTex]
{
constantColor (0,0,0,0)
combine previous lerp (texture) constant
Matrix [_ProjectorClip]
}
}
}
}
You’ll need to set the ‘Main Color’ parameter to something fairly dark since it’s an additive effect.
Thanks so much! This is almost perfect. Only thing is I didn’t want objects to appear through other objects, I just wanted to project a slightly ghostly texture onto things and I can’t figure out how to affect the Z Test in your shader so that it doesn’t display objects behind other objects. Also, is there a way to edit the Cookie so that everything outside of the center is completely black and not visible at all?
To get the z test working again you just need to remove the ‘ZTest Always’ pass state. With the z test on you’ll probably need to enable triangle offset to prevent z fighting. So the pass states should look like this:
ZWrite Off
Color [_Color]
Blend One One
Offset -1, -1
You just need to make sure that the cookie texture fades to black at the edges and that it has ‘wrap mode’ set to clamp. If things are still showing up you may want to check your render settings to see if there is any ambient light in your scene.
Ok, I’ve altered the cookie texture and it works like a charm, but for some reason, even after editing the pass state I’m still seeing transparency when the projector is active. Not sure what I’m doing wrong. Tried re-saving the file, refreshing it, removing the component and re-adding it but it still shows transparency when the projector is active. Stumped!
Could you post a screenshot of the problem?
As you can see the transparency is still there. I’ve altered the shader and it doesn’t seem to change anything, it just remains as it was when I first added the shader.
Scratch that, rewrote the shader and it works perfectly. Thanks for your assistance!
