Phong shading not smooth @_@

Hi guys,

I am just starter in shader writing and got issue with Phong Shader ( the real version not Blinn-Phong). The issue is that the spectacular component is not smooth. Below is my HLSL code :

PS : I promise to post the final code once the result is correct. :wink:

Shader "Custom/PhongShader" {
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct v2f
			{
			    float4 pos : SV_POSITION;
			    float3 WNor : TEXCOORD0;
			    float4 Vert : TEXCOORD1;
			}; 
			float4 _LightPos;
			v2f vert (appdata_tan v)
			{
			    v2f o;
			    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			   	o.WNor = mul(_Object2World, float4(v.normal, 0)).xyz;
			   	o.Vert = v.vertex;
			    return o;
			}
			float4 frag(v2f i) : COLOR
			{
				float3 WorldNorm = i.WNor;
				float4 WorldPos = mul(_Object2World, i.Vert);
				float3 LightDir = normalize(_LightPos.xyz - WorldPos.xyz);
				float3 ViewDir = normalize(WorldSpaceViewDir(i.Vert));
				float3 ReflDir = reflect(LightDir, WorldNorm);
				return 0.2 + 0.5 * max(dot(WorldNorm, LightDir), 0) + 0.5 * pow(max(dot(-ViewDir, ReflDir), 0), 10);
			}
            ENDCG 
        }
    } 
    FallBack "Diffuse"
}

Here is the result.

Thanks <3

You need to normalize your normal vector.

The built-in specular shaders suffer from this issue as well.

thanks Daniel

So the correct resut should be

:

Shader “Custom/PhongShader” {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include “UnityCG.cginc”
struct v2f
{
float4 pos : SV_POSITION;
float3 WNor : TEXCOORD0;
float4 Vert : TEXCOORD1;
};
float4 _LightPos;
v2f vert (appdata_tan v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.WNor = mul(_Object2World, float4(v.normal, 0)).xyz;
o.Vert = v.vertex;
return o;
}
float4 frag(v2f i) : COLOR
{
float3 WorldNorm = normalize(i.WNor);
float4 WorldPos = mul(_Object2World, i.Vert);
float3 LightDir = normalize(_LightPos.xyz - WorldPos.xyz);
float3 ViewDir = normalize(WorldSpaceViewDir(i.Vert));
float3 ReflDir = reflect(LightDir, WorldNorm);
return 0.2 + 0.5 * max(dot(WorldNorm, LightDir), 0) + 0.5 * pow(max(dot(-ViewDir, ReflDir), 0), 10);
}
ENDCG
}
}
FallBack “Diffuse”
}

Where/when does “float4 _LightPos;” get populated with data?