Hi all. Trying to improve the visual quality of our projectors, I wanted to modify the shader we use for the projectors material. This is what we and probably most people are using: Code (csharp): Shader "Projector/Projector Multiply" { Properties { _Color ("Main Colour", Color) = (1,1,1,0) _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear } } Subshader { Tags { "RenderType"="Transparent" "Queue"="Transparent+100"} Pass { ZWrite Off Offset -1, -1 Fog { Mode Off } //AlphaTest Greater .1 AlphaTest Less 1 ColorMask RGB Blend One SrcAlpha SetTexture [_ShadowTex] { constantColor [_Color] combine texture * constant, One - texture Matrix [_Projector] } } } } We slightly modified it so that it can take a color and removed the falloff texture, but that's pretty much what you'll find in the Projectors package BlobShadow prefab shader. Since I want way better control about the way this is all rendered, I wanted to move it to CG code. My puny and failed attempt at simply reproducing what the fixed function shader was doing looks like this: Code (csharp): Shader "Projector/Projector Multiply" { Properties { _Color ("Main Colour", Color) = (1,1,1,0) _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear } } Subshader { Tags { "RenderType"="Transparent" "Queue"="Transparent+100"} Pass { ZWrite Off Offset -1, -1 Fog { Mode Off } //AlphaTest Greater .1 AlphaTest Less 1 ColorMask RGB Blend One SrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_fog_exp2 #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float2 uv_Main : TEXCOORD0; }; sampler2D _ShadowTex; float4 _Color; float4x4 _Projector; v2f vert(appdata_tan v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv_Main = mul (_Projector, v.texcoord).xy; return o; } half4 frag (v2f i) : COLOR { half4 tex = tex2D(_ShadowTex, i.uv_Main) * _Color; tex.a = 1-tex.a; return tex; } ENDCG } } } This doesn't work, it looks like the projection is being done wrong, as it seems to take only a very little part of the texture I'm passing in: (I didn't use the same texture since the other one was simply giving invisible results, whereas this one shows a little gradient on the right coming from the texture, God knows from what part of it though) (The receiving geometry in the back is a Terrain in both cases) Anyone knows projectors in great details, someone from Unity who worked on it? Someone like, oh I don't knooow, let's saaaaay, Aras?
Still in need of an answer for that. Also, I'd like to be able to control the projection's transparency by the main color's alpha channel, with the old shader everyone uses. If I do something like Code (csharp): combine texture * constant, One - texture * constant and set the main color's alpha to 0.5 (128), all I get is instead of if I set the main color's alpha back to 1, which isn't the result I was expecting. Thanks for any help. At all. Please.
Concerning the ability to fade a projector in transparency, this shader works: Code (csharp): Subshader { Tags { "RenderType"="Transparent" "Queue"="Transparent-100" } Pass { ZWrite Off Fog { Mode Off } ColorMask RGB Blend SrcAlpha OneMinusSrcAlpha SetTexture [_ShadowTex] { constantColor [_Color] combine texture * constant, texture * constant Matrix [_Projector] } } } But I'd really rather do those things in CG. Thanks!
Got it: Code (csharp): Shader "Projector/Projector MultiplyCG" { Properties { _Color ("Main Colour", Color) = (1,1,1,0) _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear } } Subshader { Tags { "RenderType"="Transparent" "Queue"="Transparent+100"} Pass { ZWrite Off Offset -1, -1 Fog { Mode Off } AlphaTest Less 1 ColorMask RGB Blend One SrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_fog_exp2 #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float[COLOR="red"]4[/COLOR] uv_Main : TEXCOORD0; }; sampler2D _ShadowTex; float4 _Color; float4x4 _Projector; v2f vert(appdata_tan v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv_Main = [COLOR="red"]mul (_Projector, v.vertex);[/COLOR] return o; } half4 frag (v2f i) : COLOR { half4 tex = [COLOR="red"]tex2Dproj[/COLOR](_ShadowTex, [COLOR="red"]UNITY_PROJ_COORD(i.uv_Main)[/COLOR]) * _Color; tex.a = 1-tex.a; return tex; } ENDCG } } } Serves me right for completely overlooking TexGen ObjectLinear in the texture declaration We now have a CG version of the projector shader to which we can do more easily some nasty though cool and expensive things! All my thanks to Aras
I am looking to make my own projector shader. The code posted above throws many errors in Unity 4. Are there any examples or working unity cg shaders for projectors out there?
This might work with Unity4 (not sure): http://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors Let me know if it doesn't.
Many thanks, Martin! It compiles, though it has some issues with flickering and applying the blob shadow to objects that are not in the projectors FOV. I will tinker around and see if I can get it working correctly before posting again.
I am thankfull from the deep of my heart, this code saved my life! The code works, just remove any tag between "[ ]" like "[Color = "red"]" this is probably due to the code editor in here...