- Home /
Question by
EPITECH-MARSEILLE · Oct 24, 2017 at 10:10 PM ·
shaderrendertextureblit
How to alter/modify a RenderTexture with a shader,How can I modify/"post process" a RenderTexture using a shader
Hi, I'm using a RenderTexture and I need it to fade it continually with a color.
I'm not sure if I'm using Blit correctly and/or if it can be used for this purpose, here's my code:
// - BumpTexture is the RenderTexture
// - blendColor is the targeted Color for the blending
// - ReturnToNormalMat is a material containing the shader
this.ReturnToNormalMat.SetTexture("_MainTex", this.BumpTexture);
this.ReturnToNormalMat.SetColor("_Color", blendColor);
this.ReturnToNormalMat.SetFloat("_LerpValue", lerpValue);
Graphics.Blit (this.BumpTexture, this.BumpTexture, this.ReturnToNormalMat);
Shader:
Shader "Hidden/LerpToColor"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_LerpValue ("Lerp Value", Range (0, 1)) = 0.1
_Color ("Color", Color) = (0.5,0.5,0.5,1.000000)
}
SubShader
{
// No culling or depth
Lighting Off Cull Off ZTest Always ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _Color;
float _LerpValue;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return lerp(col, _Color, _LerpValue);
}
ENDCG
}
}
}
What I get is that the renderTexture turns black, "col" seems to always be black. If I return for instance float4(col.r, col.g, col.b, 1) I get black. Maybe it has to do with how uvs are set ?
I could convert the renderTexture to a Texture2D and use GetPixels/SetPixels but it sounds like an overkill ? Thanks !
Comment