Converting GPUGems2 script: Can't make sense of the errors.

Hi, I’m trying to convert a gpu gems script and I’m getting a few errors that don’t make sense to me. This is what i’m trying to convert http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html Example 19-4. The Fragment Program for Refractive/Reflective Water.

The code of the cd:

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Proj : GPU GEMS 2 DEMOS
//  File : waterVP.cg
//  Desc : water simulation demo
///////////////////////////////////////////////////////////////////////////////////////////////////

// define inputs from application
struct VpIn
{
    float4 Pos       : POSITION;
    float2 TexCoord0 : TEXCOORD0;      
    float3 Normal    : TEXCOORD1;  
    float3 Tangent   : TEXCOORD2; 
};

// define outputs from vertex shader
struct VpOut
{
    float4 HPos       : POSITION;    
    float4 Eye        : TEXCOORD0;
    float4 Wave0      : TEXCOORD1;
    float2 Wave1      : TEXCOORD2;
    float2 Wave2      : TEXCOORD3;
    float2 Wave3      : TEXCOORD4;        
    float4 ScreenPos  : TEXCOORD5;            
};

float3x3 GetTangentSpaceBasis(float3 T, float3 N)
{
   float3x3 objToTangentSpace;
   
   objToTangentSpace[0]=T;           // tangent
   objToTangentSpace[1]=cross(T, N); // binormal
   objToTangentSpace[2]=N;           // normal  
   
   return objToTangentSpace;
}

// vertex program
VpOut main(VpIn IN, uniform float4x4 ModelViewProj, uniform float4 vCameraPos, uniform float4 vWaveParams)
{
    VpOut OUT;
    
    float4 vPos=IN.Pos;
    OUT.HPos = mul(ModelViewProj, vPos);
    
    float2 fTranslation=float2(fmod(vWaveParams.w, 100)*0.01, 0);
    float2 vTexCoords=vPos.xy*0.001;
    
    // Output bump layers texture coordinates    
    float fSinTranslation=sin(fTranslation*100)*0.005;
    float2 vTranslation0=fTranslation+fSinTranslation;
    float2 vTranslation1=fTranslation-fSinTranslation;
    float2 vTranslation2=fTranslation;
    
    // Scale texture coordinates to get mix of low/high frequency details
    OUT.Wave0.xy = vTexCoords.xy+fTranslation*2.0;
    OUT.Wave1.xy = vTexCoords.xy*2.0+fTranslation*4.0;
    OUT.Wave2.xy = vTexCoords.xy*4.0+fTranslation*2.0;
    OUT.Wave3.xy = vTexCoords.xy*8.0+fTranslation;                
                  
    // perspective corrected projection      
    float4 vHPos = mul(ModelViewProj, vPos);         	
    OUT.Wave0.zw=vHPos.w;
    		  
	  vHPos.y = -vHPos.y;
    OUT.ScreenPos.xy = (vHPos.xy + vHPos.w)*0.5;    
    OUT.ScreenPos.zw =  float2(1, vHPos.w);      
        
    // get tangent space basis    
    float3x3 objToTangentSpace=GetTangentSpaceBasis(IN.Tangent.xyz, IN.Normal.xyz);
            
    float3 EyeVec=vCameraPos.xyz-vPos;        
    OUT.Eye.xyz = mul(objToTangentSpace, EyeVec);
                                                 
    return OUT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//  Proj : GPU GEMS 2 DEMOS
//  File : RefractiveFP.cg
//  Desc : Water simulation fragment program
///////////////////////////////////////////////////////////////////////////////////////////////////

#include "utilsFP.cg"

half4 main(float3 Eye : TEXCOORD0,
           float4 Wave0 : TEXCOORD1, float2 Wave1 : TEXCOORD2,
           float2 Wave2 : TEXCOORD3, float2 Wave3 : TEXCOORD4,
           float4 ScreenPos : TEXCOORD5,
           uniform sampler2D tex0, uniform sampler2D tex1, uniform sampler2D tex2) : COLOR
{

  half3 vEye = normalize(Eye);

  // Get bump layers
  half3 vBumpTexA = tex2D(tex0, Wave0.xy).xyz;
  half3 vBumpTexB = tex2D(tex0, Wave1.xy).xyz;
  half3 vBumpTexC = tex2D(tex0, Wave2.xy).xyz;
  half3 vBumpTexD = tex2D(tex0, Wave3.xy).xyz;
  
  // Average bump layers
  half3 vBumpTex=normalize(2.0 * (vBumpTexA.xyz + vBumpTexB.xyz + vBumpTexC.xyz + vBumpTexD.xyz)-4.0);
  
  // Apply individual bump scale for refraction and reflection
  half3 vRefrBump = vBumpTex.xyz * half3(0.02, 0.02, 1.0);
  half3 vReflBump = vBumpTex.xyz * half3(0.1, 0.1, 1.0);  
     
  // Compute projected coordinates
  half2 vProj = (ScreenPos.xy/ScreenPos.w);
  half4 vReflection = tex2D(tex2, vProj.xy+ vReflBump.xy);
  half4 vRefrA = tex2D(tex1, vProj.xy + vRefrBump.xy);
  half4 vRefrB = tex2D(tex1, vProj.xy);
  
  // Mask occluders from refraction map
  half4 vRefraction = vRefrB * vRefrA.w + vRefrA * (1 - vRefrA.w);

  // Compute Fresnel term
  half NdotL = max(dot(vEye, vReflBump), 0);
  half facing = (1.0 - NdotL);
  half fresnel = Fresnel(NdotL, 0.2, 5.0);

  // Use distance to lerp between refraction and deep water color
  half fDistScale = saturate(10.0/Wave0.w);
  half3 WaterDeepColor = (vRefraction.xyz * fDistScale + (1 - fDistScale) * half3(0, 0.1, 0.125));  
  
  // Lerp between water color and deep water color
  half3 WaterColor = half3(0, 0.1, 0.15);
  half3 waterColor = (WaterColor * facing + WaterDeepColor * (1.0 - facing));
  half3 cReflect = fresnel * vReflection;

  // final water = reflection_color * fresnel + water_color
  return half4(cReflect + waterColor, 1);  
}

And this is what I’ve got:

Shader "Water Gems" 
{
	Properties 
	{	    
	    _Tex0 ("Tex0", 2D) = "white" {}
	    _Tex1 ("Tex1", 2D) = "white" {}
	    _Tex2 ("Tex2", 2D) = "white" {}
	    _vWaveParams ("_vWaveParams",Vector) = (0,0,0,0)
	    
	}
	SubShader 
    {
    
    Pass 
    {

     CGPROGRAM
//	#pragma target 3.0 
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

float4 _vWaveParams;
sampler2D _Tex0 ,Tex1, Tex2;
float4 _Tex0_ST; 
float4 _Tex1_ST;
float4 _Tex2_ST;

struct v2f 
{
    float4 HPos       : POSITION;    
    float4 Eye        : TEXCOORD0;
    float4 Wave0      : TEXCOORD1;
    float2 Wave1      : TEXCOORD2;
    float2 Wave2      : TEXCOORD3;
    float2 Wave3      : TEXCOORD4;        
    float4 ScreenPos  : TEXCOORD5;    
    float4 Color : COLOR; 
};

float3x3 GetTangentSpaceBasis(float3 T, float3 N)
{
   float3x3 objToTangentSpace;
   
   objToTangentSpace[0]=T;           // tangent
   objToTangentSpace[1]=cross(T, N); // binormal
   objToTangentSpace[2]=N;           // normal  
   
   return objToTangentSpace;
}

half Fresnel(half NdotL, half fresnelBias, half fresnelPow)
{
  half facing = (1.0 - NdotL);
  return max(fresnelBias + (1.0 - fresnelBias) * pow(facing, fresnelPow), 0.0);
}

v2f vert (appdata_tan i)
{
    v2f o;
    
	float4 vPos = i.vertex;
    o.HPos = mul(UNITY_MATRIX_MVP, vPos);  
      
    float2 fTranslation = float2(fmod(_vWaveParams.w, 100)*0.01, 0);
    float2 vTexCoords = vPos.xy*0.001;
    
    float fSinTranslation = sin(fTranslation*100)*0.005;
    float2 vTranslation0 = fTranslation+fSinTranslation;
    float2 vTranslation1 = fTranslation-fSinTranslation;
    float2 vTranslation2 = fTranslation;
    
//    o.Wave0 = TRANSFORM_TEX (i.texcoord, _Tex0);
//    o.Wave1 = TRANSFORM_TEX (i.texcoord, _Tex0);
//    o.Wave2 = TRANSFORM_TEX (i.texcoord, _Tex0);
//    o.Wave3 = TRANSFORM_TEX (i.texcoord, _Tex0);
    
    o.Wave0.xy = vTexCoords.xy+fTranslation*2.0;
    o.Wave1.xy = vTexCoords.xy*2.0+fTranslation*4.0;
    o.Wave2.xy = vTexCoords.xy*4.0+fTranslation*2.0;
    o.Wave3.xy = vTexCoords.xy*8.0+fTranslation; 
    
    float4 vHPos = mul(UNITY_MATRIX_MVP, vPos);         	
    o.Wave0.zw = vHPos.w;
    
    vHPos.y = -vHPos.y;
    o.ScreenPos.xy = (vHPos.xy + vHPos.w)*0.5;    
    o.ScreenPos.zw =  float2(1, vHPos.w);      
    
    // get tangent space basis    
    float3x3 objToTangentSpace = GetTangentSpaceBasis(i.tangent.xyz, i.normal.xyz);
            
    float3 EyeVec = _WorldSpaceCameraPos.xyz-vPos.xyz;        
    o.Eye.xyz = mul(objToTangentSpace, EyeVec);
    
    return o;
}

half4 frag (v2f i) : COLOR
{
	half3 vEye = normalize(i.Eye.xyz);
	
	half3 vBumpTexA = tex2D(_Tex0, i.Wave0.xy).xyz;
	half3 vBumpTexB = tex2D(_Tex0, i.Wave1.xy).xyz;
	half3 vBumpTexC = tex2D(_Tex0, i.Wave2.xy).xyz;
	half3 vBumpTexD = tex2D(_Tex0, i.Wave3.xy).xyz;
	
	// Average bump layers
    half3 vBumpTex = normalize(2.0 * (vBumpTexA.xyz + vBumpTexB.xyz + vBumpTexC.xyz + vBumpTexD.xyz)-4.0);
    
	// Apply individual bump scale for refraction and reflection
	half3 vRefrBump = vBumpTex.xyz * half3(0.02, 0.02, 1.0);
	half3 vReflBump = vBumpTex.xyz * half3(0.1, 0.1, 1.0);  
	
	// Compute projected coordinates
	half2 vProj = (_ScreenParams.xy/_ScreenParams.w);
	half4 vReflection = tex2D(_Tex2, vProj.xy+ vReflBump.xy);
	half4 vRefrA = tex2D(_Tex1, vProj.xy + vRefrBump.xy);
	half4 vRefrB = tex2D(_Tex1, vProj.xy);
	
   // Mask occluders from refraction map
   half4 vRefraction = vRefrB * vRefrA.w + vRefrA * (1 - vRefrA.w);
   
   // Compute Fresnel term
   half NdotL = max(dot(vEye, vReflBump), 0);
   half facing = (1.0 - NdotL);
   half fresnel = Fresnel(NdotL, 0.2, 5.0);
   
   // Use distance to lerp between refraction and deep water color
   half fDistScale = saturate(10.0/_Wave0.w);
   half3 WaterDeepColor = (vRefraction.xyz * fDistScale + (1 - fDistScale) * half3(0, 0.1, 0.125));
     
   // Lerp between water color and deep water color
   half3 WaterColor = half3(0, 0.1, 0.15);
   half3 waterColor = (WaterColor * facing + WaterDeepColor * (1.0 - facing));
   half3 cReflect = fresnel * vReflection;
   
   // final water = reflection_color * fresnel + water_color
   return half4(cReflect + waterColor, 1);  
  // return i.color = cReflect + waterColor;
}

ENDCG

    }
}

}

And the error I’m getting

Shader error in 'Water Gems': GLSL vertex shader: ERROR: 0:359: '_Tex2' : undeclared identifier  at line 19
(Filename: Water Gems Line: 19)

Shader error in 'Water Gems': GLSL vertex shader: ERROR: 0:362: '_Tex2' : undeclared identifier  at line 19
(Filename: Water Gems Line: 19)

Shader error in 'Water Gems': Shader program had errors at line 20
(Filename: Water Gems Line: 20)

Shader warning in 'Water Gems': Program 'frag', implicit cast from "float2" to "float" at line 70
(Filename: Water Gems Line: 70)

Shader error in 'Water Gems': Program 'frag', unable to find compatible overloaded function "tex2D(error, half2)" at line 119
(Filename: Water Gems Line: 119)

Shader error in 'Water Gems': Program 'frag', undefined variable "_Tex2" at line 119
(Filename: Water Gems Line: 119)

Shader error in 'Water Gems': Program 'frag', unable to find compatible overloaded function "tex2D(error, half2)" at line 120
(Filename: Water Gems Line: 120)

Shader error in 'Water Gems': Program 'frag', undefined variable "_Tex1" at line 120
(Filename: Water Gems Line: 120)

Shader error in 'Water Gems': Program 'frag', unable to find compatible overloaded function "tex2D(error, half2)" at line 121
(Filename: Water Gems Line: 121)

Shader error in 'Water Gems': Program 'frag', undefined variable "_Tex1" at line 121
(Filename: Water Gems Line: 121)

Shader error in 'Water Gems': Program 'frag', unable to find compatible overloaded function "saturate(error)" at line 132
(Filename: Water Gems Line: 132)

Shader error in 'Water Gems': Program 'frag', undefined variable "_Wave0" at line 132
(Filename: Water Gems Line: 132)

Shader warning in 'Water Gems': Program 'frag', implicit cast from "half4" to "half3" at line 138
(Filename: Water Gems Line: 138)

Any help would be much appriecated.

Worked it out, just dumb errors. Although it doesn’t do anything so I think theres much more to it.

Hm, maybe you should start with a simpler example to learn how to debug shaders?

@efreet : how’s the result?

I am new to this and I am having the same base error in the Car tutorial (original and unmodified), how you fixed the syntax error would be appreciated.