Can anyone help fix my shader code? :(

Hi all!

I tried to edit a shader by adding specular texture channel and shininess slider. Though I’ve messed up with the code somewhere being a total shader noob :frowning:

Everything seems to show up in the controls, though cant move the sliders and the model is all flat pink! Can anyone figure out what I did wrong and stop me from a head/desk injury?

Here is the shader code:

Shader “UltraShader” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
_Detail (“Detail (RGB)”, 2D) = “gray” {}
_Lightmap (“LightMap (RGB)”, 2D) = “gray” {}
_BumpMap (“Bumpmap”, 2D) = “bump” {}
_SpecMap (“Specular map”, 2D) = “black” {}
_Shininess (“Shininess”, Range (0.03, 1)) = 0.078125
_RimColor (“Rim Color”, Color) = (0.26,0.19,0.16,0.0)
_RimPower (“Rim Power”, Range(0.5,8.0)) = 3.0

}
SubShader {

Pass{

Lighting On
Fog { Mode Off }
SetTexture [_MainTex] { combine texture * primary Double, texture * primary}
//SetTexture [_Detail] { combine previous * texture Double, previous}

}
Tags { “RenderType” = “Opaque” }
CGPROGRAM
#pragma surface surf WrapLambert

half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten) {
half NdotL = dot (s.Normal, lightDir);
half diff = NdotL * 0.2 + 0.8;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
c.a = s.Alpha;
return c;
}

struct Input {
float2 uv_MainTex;
float2 uv_Detail;
float2 uv_BumpMap;
float2 uv_SpecMap;
float2 uv_Lightmap;
float3 viewDir;
};
sampler2D _MainTex;
sampler2D _Detail;
sampler2D _BumpMap;
sampler2D _SpecMap;
sampler2D _LightMap;
float4 _RimColor;
float _RimPower;

sampler2D _Lightmap;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 1;
o.Albedo = ((o.Albedo) + (tex2D (_MainTex, IN.uv_MainTex).rgb))/2;
o.Albedo *= tex2D (_Lightmap, IN.uv_Lightmap).rgb * 2;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
o.Specular = _Shininess * specTex.g;
o.Gloss = specTex.r;
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);

}
ENDCG
}
FallBack “VertexLit”, 2
}

If your shader is showing up pink, there are probably errors in the console. If you have made changes since it stopped working, you should try reverting the shader to its last working state, and then making modifications from there. As soon as you make a modification that causes it to stop working, take a look at the console and look for error messages pertaining to shader compilation.

Once you have some error messages, you can go from there.

Here is your shader, free of syntax errors. Any shader logic errors are up to you :smile:

They were all mainly careless mistakes that code of been avoided by reading your code.

And try formatting your code a bit better, make finding mistakes easier on yourself by making more use of whitespace and indentation! (The fact that you didn’t use code tags in your original post wouldn’t of helped aswell)

Shader "UltraShader" 
{
	Properties 
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Detail ("Detail (RGB)", 2D) = "gray" {}

		_Lightmap ("LightMap (RGB)", 2D) = "gray" {}

		_BumpMap ("Bumpmap", 2D) = "bump" {}

		_SpecMap ("Specular map", 2D) = "black" {}
		_Shininess ("Shininess", Range (0.03, 1)) = 0.078125

		_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
		_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
	}
	SubShader 
	{

		Pass
		{
			Lighting On
			Fog { Mode Off }
			SetTexture [_MainTex] { combine texture * primary Double, texture * primary}
		}

		Tags { "RenderType" = "Opaque" }
		CGPROGRAM
		#pragma surface surf WrapLambert

		half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten) 
		{
			half NdotL = dot (s.Normal, lightDir);
			half diff = NdotL * 0.2 + 0.8;
			half4 c;
			c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
			c.a = s.Alpha;
			return c;
		}

		struct Input 
		{
			float2 uv_MainTex;
			float2 uv_Detail;
			float2 uv_BumpMap;
			float2 uv_SpecMap;
			float2 uv_Lightmap;
			float3 viewDir;
		};

		sampler2D _MainTex;
		sampler2D _Detail;
		sampler2D _BumpMap;
		sampler2D _Lightmap;
		sampler2D _SpecMap;

		float4 _RimColor;
		float _RimPower;
		float _Shininess;

		
		void surf (Input IN, inout SurfaceOutput o) 
		{
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 1;
			o.Albedo = ((o.Albedo) + (tex2D (_MainTex, IN.uv_MainTex).rgb))/2;
			o.Albedo *= tex2D (_Lightmap, IN.uv_Lightmap).rgb * 2;
			o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));

			float4 specTex = tex2D(_SpecMap, IN.uv_MainTex);
			o.Specular = _Shininess * specTex.g;
			o.Gloss = specTex.r;
			half rim = 1.0f - saturate(dot (normalize(IN.viewDir), o.Normal));
			o.Emission = _RimColor.rgb * pow (rim, _RimPower);
		}

		ENDCG
	}

	FallBack "VertexLit"
}

Thanks smb02dunnal. I fixed the shader in the end as it was missing some strings. Though your version is much neater so will use this as a guide for my learning :slight_smile: