I’m using the Projector example as a template, found here on wikibooks:
http://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors
I’m trying to add a Falloff to it, so it behaves like Unity’s “Projector Light” shader. I’m basically trying to create a CG equivalent of Unity’s fixed-function shader, But I’m having trouble applying the falloff to the ShadowTex. Here’s what I got so far:
Shader "Custom/FX/Projector Light" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_ShadowTex ("Cookie", 2D) = "" { TexGen ObjectLinear }
_FalloffTex("FallOff", 2D) = "" { TexGen ObjectLinear }
}
SubShader {
Pass {
ZWrite off
Fog { Color (0, 0, 0) }
ColorMask RGB
Blend DstColor One
Offset -1, -1
//Blend One One // add color of _ShadowTex to the color in the framebuffer
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// User-specified properties
uniform fixed4 _Color;
uniform sampler2D _ShadowTex;
uniform sampler2D _FalloffTex;
// Projector-specific uniforms
uniform float4x4 _Projector; // transformation matrix from object space to projector space
uniform float4x4 _ProjectorClip;
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 posProj : TEXCOORD0; // position in projector space
float4 uv : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.posProj = mul(_Projector, input.vertex);
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.uv = mul(_ProjectorClip, input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
if (input.posProj.w > 0.0) // in front of projector?
{
// float4 shadowTex = tex2D(_ShadowTex, float2(input.posProj) / input.posProj.w) * _Color;
// float4 fallOff = tex2D(_FalloffTex, float2(input.uv) / input.uv.w);
/* Alternatively */
float4 shadowTex = tex2Dproj(_ShadowTex, input.posProj) * _Color;
float4 fallOff = tex2Dproj(_FalloffTex, input.uv);
//float4 col = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(input.posProj));
//float4 fall = tex2Dproj (_FalloffTex, UNITY_PROJ_COORD(input.uv));
//return float4( shadowTex.rgb, float4(input.posProj / input.posProj.w).a);
//return float4( shadowTex.rbg, UNITY_PROJ_COORD(input.posProj) );
//return lerp( fall, col, UNITY_PROJ_COORD(input.posProj));
//return tex2Dproj( _ShadowTex, float4(input.posProj));
//return shadowTex;
return float4(shadowTex.rgb, fallOff.a);
//return lerp( shadowTex, fallOff, 1 - _Color.a ); // manually set falloff with Color's alpha
}
else // behind projector
{
return float4(0.0);
}
}
ENDCG
}
}
// Fixed-Function fallback, Unity's "Projector Light"
Subshader {
Pass {
ZWrite off
Fog { Color (0, 0, 0) }
Color [_Color]
ColorMask RGB
Blend DstColor One
Offset -1, -1
SetTexture [_ShadowTex] {
combine texture * primary, ONE - texture
Matrix [_Projector]
}
SetTexture [_FalloffTex] {
constantColor (0,0,0,0)
combine previous lerp (texture) constant
Matrix [_ProjectorClip]
}
}
}
}
I found some related posts, so I think I’m close, just not sure what to do next.
http://forum.unity3d.com/threads/69892-_Projector-Projector-shader-and-CG
http://answers.unity3d.com/questions/40357/projector-projector-shader-and-cg.html