Hello everyone,
I have a surface refraction shader which works great when my camera is set to deferred lighting. Weirdly enough though, as soon as I switch to forward rendering, the shader refracts the things behind it upside down. I don’t know much about shaders, but I’m pretty sure it has something to do with the “Render Texture Coordinates” section in this post.
Here is the shader:
Shader "Effects/Distort" {
Properties{
_Refraction("Refraction", Range(0.00, 100.0)) = 1.0
_DistortTex("Base (RGB)", 2D) = "white" {}
_ObjectVisibility("Visibility", Range(0.00, 1.00)) = 0.05
}
SubShader{
Tags{ "RenderType" = "Transparent" "Queue" = "Overlay" }
LOD 100
GrabPass
{
}
CGPROGRAM
#pragma surface surf NoLighting
#pragma vertex vert
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten) {
fixed4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
sampler2D _GrabTexture : register(s0);
sampler2D _DistortTex : register(s2);
float _Refraction;
float _ObjectVisibility;
float4 _GrabTexture_TexelSize;
struct Input {
float2 uv_DistortTex;
float3 color;
float3 worldRefl;
float4 screenPos;
INTERNAL_DATA
};
struct appdata_t {
float4 vertex : POSITION;
};
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color;
}
void surf(Input IN, inout SurfaceOutput o) {
float3 distort = tex2D(_DistortTex, IN.uv_DistortTex) * float3(IN.color.r,IN.color.g,IN.color.b);
float2 offset = distort * _Refraction * _GrabTexture_TexelSize.xy;
IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy;
float4 refrColor = tex2Dproj(_GrabTexture, IN.screenPos);
o.Alpha = refrColor.a;
o.Emission = refrColor.rgb * (1+_ObjectVisibility);
}
ENDCG
}
}
The previously linked post talks about inserting the following snippet somewhere:
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
uv.y = 1-uv.y;
#endif
I probably have to change _MainTex_TexelSize to _GrabTexture_TexelSize, but that’s pretty muc all I know. Where do I need to insert this snippet to get it working?
Thanks for your help in advance!