Anisotropic shader compile error

when I try to use this shader from the wiki (Aras as author)

Shader "Anisotropic" { 

Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}
}

Category {
    Lod 0
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }
    
    SubShader {
        // Ambient pass
        Pass {
            Name "BASE"
            Tags {"LightMode" = "PixelOrNone"}
            Color [_PPLAmbient]
            // Make it respond less to ambient color just to make it look better - no DOUBLE part here
            SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary, texture * constant}
        }
        // Vertex lights
        Pass { 
            Name "BASE"
            Tags {"LightMode" = "Vertex"}
            Lighting On
            Material {
                Diffuse [_Color]
                Emission [_PPLAmbient]
            }
            SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
        }
        // Pixel lights
        Pass {
            Name "PPL"
            Tags { "LightMode" = "Pixel" }
            
CGPROGRAM 
// profiles arbfp1 
// vertex vert
// fragment frag
// autolight 7
    
#include "UnityCG.cginc" 
#include "AutoLight.cginc" 

struct v2f {
    V2F_POS_FOG;
    float2 uv[2] : TEXCOORD0;
    V2F_LIGHT_COORDS(TEXCOORD2);
};
struct v2f2 {
    V2F_POS_FOG;
    float2 uv[2] : TEXCOORD0;
};
 
v2f vert (appdata_base v) {
    v2f o;
        
    PositionFog( v.vertex, o.pos, o.fog );
          
    o.uv[0] = TRANSFORM_UV(0);
    
    float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));
    
    float3 halfAngle = normalize(viewDir + lightDir);
    o.uv[1].x = dot(lightDir,v.normal);
    o.uv[1].y = dot(halfAngle,v.normal);
    
    PASS_LIGHT_COORDS(2);
    
    return o;
}

sampler2D _MainTex : register(s0);
sampler2D _AnisoTex : register(s1);

float4 frag(v2f2 i, LIGHTDECL(TEXUNIT2)) : COLOR
{
    float4 texcol = tex2D( _MainTex, i.uv[0] );
    float4 aniso = tex2D( _AnisoTex, i.uv[1] );
    texcol.rgb *= _ModelLightColor[0].rgb;
    texcol.rgb *= aniso.rgb * (aniso.a * LIGHTATT * 2);
    return float4( texcol.rgb, 0 );
}

ENDCG 

            SetTexture[_MainTex] {}
            SetTexture[_AnisoTex] {}
            SetTexture [_LightTexture0] {}
            SetTexture [_LightTextureB0] {}
        } 
    } 
}

Fallback " VertexLit", 1 



}

It will read like this in the compiled version

Shader "Anisotropic" { 

Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}
}

Category {
    Lod 0
    Blend AppSrcAdd AppDstAdd
    Fog { Color [_AddFog] }
    
    SubShader {
        // Ambient pass
        Pass {
            Name "BASE"
            Tags {"LightMode" = "PixelOrNone"}
            Color [_PPLAmbient]
            // Make it respond less to ambient color just to make it look better - no DOUBLE part here
            //SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary, texture * constant}
        }
        // Vertex lights
        Pass { 
            Name "BASE"
            Tags {"LightMode" = "Vertex"}
            Lighting On
            Material {
                Diffuse [_Color]
                Emission [_PPLAmbient]
            }
            SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
        }
        // Pixel lights
        Pass {
            Name "PPL"
            Tags { "LightMode" = "Pixel" }
            
// Cg program with errors was here
Program "" { SubProgram { "!!error" } }
#LINE 89
 

            SetTexture[_MainTex] {}
            SetTexture[_AnisoTex] {}
            SetTexture [_LightTexture0] {}
            SetTexture [_LightTextureB0] {}
        } 
    } 
}

Fallback " VertexLit", 1 



}

It seems there are som errors in the Cg-program, and I have no understanding of what is going wrong.
Do I need additional libaries of some sort (I see it includes something), or what?
I have used notepad to copy the shader into the .shader file, maybe that’s the problem? I can’t seem to edit shaders directly from within Unity, as I only have a “open compiled shader” button to press, which can’t save my changes.
This is a trial version though.
Help please :cry:

That shader is designed for Unity 1.6.2 rather than 2+. There were some changes to the language in 2.0, so some lines need to be updated. If you read the errors carefully, they should explain exactly what to do to make the shader 2+ compatible.

The fixed shader should work fine in the trial version. You probably need the right source textures, which are available from the original nVidia example.

So where do I read the errors? In the compiled version it just says they are there, the errors… :slight_smile:

The errors should be listed in the editor console, which you can open by clicking on whatever text (probably red or yellow) is at the bottom of the main editor window.

Compiled versions of shaders usually aren’t very helpful.

Just found it. The main error seems to be that the shader is using old light attenuation functions, which I need to update to new ones. Yeah…I’ll just do that
Well, it seems I have to rely on someone skilled enough in here to give me a hand with this. Anyone? I promise to throw lots of smileys in my next post…

Check out “Attenuation for fragment programs” on this page in the Shaderlab documentation.

Just read the documentation, and my Cg-program differs on too many levels to just be changed. I tried to implement all the “#pragma” bits, and it were not enough to save the rest. Then I took a look at the fragment program, to see where it differed from the example. And it does so in almost all aspects. So… well, gotta leave this be, this is way beyond my capabilities for now.
It’s like having the garbage boy trying to solve the apollo 13 problems, which would surely have been a disaster :slight_smile:

Yeah, that was a bit messy. The Shaderlab compiler is a bit sloppy when it comes to reporting errors in Cg programs. Here is (I think) a working version of the shader:

Shader "Anisotropic" { 
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_AnisoTex ("Aniso lookup (RGBA)", 2D) = "white" {}
	}

	Category {
		Lod 0
		Blend AppSrcAdd AppDstAdd
		Fog { Color [_AddFog] }
	
		SubShader {
			// Ambient pass
			Pass {
				Name "BASE"
				Tags {"LightMode" = "PixelOrNone"}
				Color [_PPLAmbient]
				// Make it respond less to ambient color just to make it look better - no DOUBLE part here
				SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary, texture * constant}
			}
			// Vertex lights
			Pass { 
				Name "BASE"
				Tags {"LightMode" = "Vertex"}
				Lighting On
				Material {
					Diffuse [_Color]
					Emission [_PPLAmbient]
				}
				SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
			}
			// Pixel lights
			Pass {
				Name "PPL"
				Tags { "LightMode" = "Pixel" }
				
				CGPROGRAM
					#pragma target default 
					#pragma vertex vert
					#pragma fragment frag
					#pragma multi_compile_builtin
					#pragma fragmentoption ARB_fog_exp2
					#pragma fragmentoption ARB_precision_hint_fastest
	
					#include "UnityCG.cginc" 
					#include "AutoLight.cginc" 

					struct v2f {
						V2F_POS_FOG;
						LIGHTING_COORDS
						float2 uv[2];
						float3 normal;
						float3 lightDir;
					};
 
					v2f vert (appdata_base v) {
						v2f o;
		
						PositionFog( v.vertex, o.pos, o.fog );
		  
						o.uv[0] = TRANSFORM_UV(0);
	
						float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
						float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));
	
						float3 halfAngle = normalize(viewDir + lightDir);
						o.uv[1].x = dot(lightDir,v.normal);
						o.uv[1].y = dot(halfAngle,v.normal);
	
						TRANSFER_VERTEX_TO_FRAGMENT(o);
	
						return o;
					}

					uniform sampler2D	_MainTex,
										_AnisoTex;

					float4 frag(v2f i) : COLOR
					{
						float4 texcol = tex2D( _MainTex, i.uv[0] );
						float4 aniso = tex2D( _AnisoTex, i.uv[1] );
						texcol.rgb *= _ModelLightColor0.rgb;
						texcol.rgb *= aniso.rgb * (aniso.a * LIGHT_ATTENUATION(i) * 2);
						return float4( texcol.rgb, 0 );
					}

				ENDCG
			} 
		} 
	}

	Fallback " VertexLit", 1 
}

Hej Daniel :slight_smile:

It seem to work pretty good, and I want to thank you so much for putting your time into it!
The only thing I find odd, is how the specular seem to both add highlights and darkness. Meaning, normally a specular can only add intensity to the existing shading of the object, but this shader seem to also use the darkness of the anisotropic texture to actually darken the object. I don’t know if that is a mistake on your part, or if the original shader had the same problem.

But anyways, I can use this as is, with a little tweaking, so thanks!!!

That’s just the effect of the base pass not being doubled. The pixel light pass can’t subtract anything, but because the base is darker to begin with the final result will be darker in some places than a regular diffuse shader.

I’m not sure if that was Aras’ addition or in the original shader.