- Home /
How to use Camera.RenderWithShader() to modify rendered image?
Hi
Can anybody please help me understand how to work with the Camera.RenderWithShader() function?
I am rendering one of my cameras into a RenderTexture and everything works as it should, however I desperately need to "trim" the rendered image by cutting off (via alpha), lets say, 10% of pixels from left, right, top and bottom sides...
I believe I can cut off parts of the texture with the help of the following shader, however it requires a texture _TestTex to work... And I have no idea how to pass a texture to this shader :( I tried calling
Shader.SetGlobalTexture("_TestTex", myCamera.targetTexture);
and
Shader.SetGlobalTexture("_TestTex", myCamera.renderer.material.mainTexture);,
but I'm definitely doing something wrong... Please help me solve my dilemma!
Shader "Test/TestShader" { Properties { _TestTex ("Base (RGB)", 2D) = "white" {} }
SubShader
{
Tags
{
"RenderType" = "Opaque"
"Queue" = "Transparent"
"IgnoreProjector" = "True"
}
LOD 100
Pass
{
Cull Off
Lighting Off
ZWrite Off
Offset -1, -1
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
sampler2D _TestTex;
struct appdata_t
{
fixed4 vertex : POSITION;
fixed4 color : COLOR;
fixed2 texcoord : TEXCOORD0;
};
struct v2f
{
fixed4 vertex : POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color.xy = v.texcoord.xy;
o.color.zw = o.vertex.xy;
return o;
}
fixed4 frag (v2f IN) : COLOR
{
fixed4 result = fixed4(0, 0, 0, 0);
if (IN.color.z < 0.1 || IN.color.z > 0.8 || IN.color.w < 0.1 || IN.color.w > 0.8)
{
result.rgb = tex2D(_TestTex, IN.color.xy).rgb;
result.a = 1;
}
return result;
}
ENDCG
}
}
}
Have you tried looking it up? Does the search I just did help you out?
http://docs.unity3d.com/Documentation/ScriptReference/Camera.RenderWithShader.html
http://answers.unity3d.com/questions/18919/whats-the-good-usage-of-renderwithshader.html