I’m doing a simple bumpmapping using a Surface Shader, and everything works ok… as expected.
however, when doing the exact same bumpmapping in pure Fragment Shader… it behaves oddly… if I render a cube, all faces of that cube get lit when the light is facing at one point, and get darker when it faces the other way (instead of just the faces where the light is pointing at).
so… is there any particular difference between doing this in a Surface Shader:
Shader "Test/SurfaceShaderBump" {
Properties {
_Diffuse ("Diffuse",2D) = "white" {}
_NormalMap ("Normal Map", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Custom
half4 LightingCustom( SurfaceOutput s, half3 lightDir, half3 viewDir, half atten )
{
half pxlAtten = dot( lightDir, s.Normal );
return half4(s.Albedo * pxlAtten,1.0);
}
sampler2D _Diffuse;
sampler2D _NormalMap;
struct Input {
float2 uv_Diffuse;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_Diffuse, IN.uv_Diffuse).rgb;
o.Normal = UnpackNormal( tex2D(_NormalMap, IN.uv_Diffuse) );
}
ENDCG
}
}
and doing this in a Fragment Shader:
Shader "Test/FragmentShaderBump" {
Properties {
_Diffuse ("Diffuse",2D) = "white" {}
_NormalMap ("Normal Map", 2D) = "bump" {}
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
vertexOutput vert( vertexInput v )
{
vertexOutput o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
return o;
}
sampler2D _Diffuse;
sampler2D _NormalMap;
half4 frag( vertexOutput i ) : COLOR
{
float3 lightDir = normalize( float3(_WorldSpaceLightPos0 ) );
half3 Albedo = tex2D(_Diffuse, i.uv.xy ).rgb;
half3 pNormal = UnpackNormal( tex2D(_NormalMap, i.uv.xy ) );
half pxlAtten = dot( half3(pNormal.rgb), lightDir );
half3 diff = Albedo * pxlAtten;
return half4( diff, 1 );
}
ENDCG
}
}
}
to my understanding they should be doing exactly the same thing… however on this last one, all faces of the cube get lit at the same time (when the light is pointing at some point) and all faces get unlit at the same time (when turning it away from that point).
Any ideas of what might I be doing wrong?
Thanks!