How to modify RenderTexture used in Projector to have black border?
I am attempting to create a projector that uses a render texture as it's cookie. Everything works fine, except because the render texture does not have a black border, the Projector/Light shader will repeat the border pixels across the projected surface. The result is the projection does not have a defined border...
What I would like to do is apply a black and white texture that is multiplied onto the render texture to give a black border. So the texture would be all white with black edges, possibly feathered. I am assuming this should be a modification to the Projector/Light shader, but I don't know how to make that work.
Any ideas?
Answer by seowen1 · Aug 29, 2019 at 06:26 PM
Answered!
Modified the Projector/Light shader like so...
// Upgrade NOTE: replaced '_Projector' with 'unity_Projector'
// Upgrade NOTE: replaced '_ProjectorClip' with 'unity_ProjectorClip'
Shader "Projector/LightAlpha" {
Properties{
_Color("Main Color", Color) = (1,1,1,1)
_ShadowTex("Cookie", 2D) = "" {}
_FalloffTex("FallOff", 2D) = "" {}
_AlphaTex("Alpha",2D) = "" {}
}
Subshader{
Tags {"Queue" = "Transparent"}
Pass {
ZWrite Off
ColorMask RGB
Blend DstColor One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct v2f {
float4 uvShadow : TEXCOORD0;
float4 uvFalloff : TEXCOORD1;
UNITY_FOG_COORDS(2)
float4 pos : SV_POSITION;
};
float4x4 unity_Projector;
float4x4 unity_ProjectorClip;
v2f vert(float4 vertex : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(vertex);
o.uvShadow = mul(unity_Projector, vertex);
o.uvFalloff = mul(unity_ProjectorClip, vertex);
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 _Color;
sampler2D _ShadowTex;
sampler2D _FalloffTex;
sampler2D _AlphaTex;
fixed4 frag(v2f i) : SV_Target
{
fixed4 texS = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
fixed4 texA = tex2Dproj(_AlphaTex, UNITY_PROJ_COORD(i.uvShadow));
texS.rgb *= texA * _Color.rgb;
texS.a = 1.0 - texS.a;
fixed4 texF = tex2Dproj(_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff));
fixed4 res = texS * texF.a;
UNITY_APPLY_FOG_COLOR(i.fogCoord, res, fixed4(0,0,0,0));
return res;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
RenderTexture Scope Effect 0 Answers
problem w/ projector 0 Answers
How to make dynamic ripple with shader? 0 Answers
Projector and Shader with modified Vertices 0 Answers
PostProcess Outline Shader GVR alignment 0 Answers