Why this shader doesnt work?

I dont know why this line “col = lerp(col, proj, 0.5);” doesnt work in the following shader:

Shader "Water-Still" {

	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_BumpMap ("Normalmap", 2D) = "bump" {}
	}
	
	SubShader {
		Tags { "Queue"="Transparent" "RenderType"="Opaque" }
		LOD 200
		
		GrabPass { }
		
		CGPROGRAM
		#pragma surface surf BlinnPhong vertex:vert
		#include "UnityCG.cginc"
		
		struct Input {
			float2 uv_BumpMap;
			float4 uv_GrabTex;
			float2 uv_MainTex;
		};
		
		void vert (inout appdata_full v, out Input o) {
			o.uv_GrabTex = ComputeScreenPos(v.vertex);
		}
		
		sampler2D _MainTex;
		sampler2D _BumpMap;
		sampler2D _GrabTexture;

		void surf (Input IN, inout SurfaceOutput o) {
			half4 col;
			half4 proj;
			col = tex2D (_MainTex, IN.uv_MainTex);
			o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
			proj = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.uv_GrabTex));
			col = lerp(col, proj, 0.5);

			o.Albedo = col.rgb;
			o.Alpha = 0.0;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Any help appreciated.

this is very frustrating.
Why even this simple thing doesnt work?

		GrabPass {}
		
		CGPROGRAM
		#pragma surface surf Lambert
		#include "UnityCG.cginc"

		sampler2D _MainTex;
		sampler2D _GrabTexture;

		struct Input {
			float2 uv_MainTex;
			float4 uv_GrabTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			//half4 c1 = tex2D (_MainTex, IN.uv_MainTex);
			half4 refrColor  = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.uv_GrabTex));
			half4 col = refrColor;
			o.Albedo = col.rgb;
		}
		ENDCG

When you say ‘doesn’t work’, you mean it seems to have no effect, or there’s a compile error?

Also, you use uv_GrabTex as projection coordinates when they are actually like your usual color map uvs.
You should make a little vertex shader code (see Normal Extrusion with Vertex Modifier in Shader Lab’s Surface Shader Examples) to multiply your uv_GrabTex coordinates by a proper projection matrix.

Yeah, this is pretty wierd…

This compiles:

float3 refr = float3(0,0,0);
o.Emission = c.rgb * refr;

but this doesn’t:
float3 refr = tex2Dproj(_GrabTexture, IN.uv_GrabTex);
o.Emission = c.rgb * refr;

I also tried messing around trying to get it to work for a bit. If I had to guess, the problem occurs when the surface shader is expanded out into vertex and pixel shaders, so I don’t think it’s a problem with your code, but rather a bug in Unity. I have no idea how this would be fixed or worked around. Sorry I couldn’t help, but at least you know the bug is reproducible. :frowning:

Okay, the answer is; you simply DONT use uv_GrabTex but use grabTex or anything else without “uv_”

2 Likes