The image below is showing what is happening. I’m simply trying to write a shader that does diffuse and specular lighting. I’m following this tutorial.
However I noticed that for diffuse, it doesn’t take range into account, and doesn’t behave exactly like the default shader in unity. And we have the edges shown below. I don’t know how to actually access the range information so i’m stuck. I’ve searched, but I have not found any good documentation that tells me how to avoid this. Basically what should I do to fix this.
Herese my messy shader code :
Shader "Custom/LightMap Blend Diffuse Specular Transparency" {
Properties {
_Color ("MainColor", Color) = (1, 1, 1, 1)
_SpecColor ("Specular Color", Color) = (1, 1, 1, 1)
_Shininess ("Shininess", Float) = 10
_MainTex ("Base (RGB)", 2D) = "white" {}
_LightMap01 ( "LightMap 1", 2D ) = "white" {}
_LightMap02 ( "LightMap 2", 2D ) = "white" {}
_Trans ("Transparency", Range(0, 1)) = 0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _LightColor0;
sampler2D _MainTex;
float4 _Color;
float4 _SpecColor;
float _Shininess;
struct vertexIn
{
float4 pos : POSITION;
float3 norm : normal;
};
struct vertexOut
{
float4 pos : SV_POSITION;
float4 col : COLOR;
float3 posWorld : TEXCOORD0;
float3 norm : TEXCOORD1;
};
vertexOut vert(vertexIn i)
{
vertexOut o;
float4x4 modelMatrix = _Object2World;
float4x4 modelMatrixInverse = _World2Object;
o.posWorld = mul(modelMatrix, i.pos);
o.norm = normalize( mul(float4(i.norm, 0), modelMatrixInverse).xyz );
o.col = float4 (0, 0, 0, 0);
o.pos = mul( UNITY_MATRIX_MVP, i.pos);
return o;
}
float4 frag(vertexOut o) : COLOR
{
float3 normalDirection = normalize(o.norm);
float3 viewDirection = normalize(_WorldSpaceCameraPos - o.posWorld.xyz);
float3 lightDirection = float3(0, 0, 0);
float attenuation;
lightDirection = normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
float3 PointLightDirection = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
float PointLightLength = length(PointLightDirection);
lightDirection += normalize(PointLightDirection) * _WorldSpaceLightPos0.w;
attenuation = 1.0 / PointLightLength;
float3 diffuseColor = lerp(_LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection)),
_LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection) * attenuation), _WorldSpaceLightPos0.w);
float3 specularColor = (_LightColor0.rgb * _Color.rgb * max(0, dot(reflect(-lightDirection, normalDirection), viewDirection)) )
* max(0, dot (normalDirection, lightDirection));
return float4((UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb) + diffuseColor, 1);
}
ENDCG
}
Pass
{
Tags { "LightMode" = "ForwardAdd" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _LightColor0;
sampler2D _MainTex;
float4 _Color;
float4 _SpecColor;
float _Shininess;
struct vertexIn
{
float4 pos : POSITION;
float3 norm : normal;
};
struct vertexOut
{
float4 pos : SV_POSITION;
float4 col : COLOR;
float3 posWorld : TEXCOORD0;
float3 norm : TEXCOORD1;
};
vertexOut vert(vertexIn i)
{
vertexOut o;
float4x4 modelMatrix = _Object2World;
float4x4 modelMatrixInverse = _World2Object;
o.posWorld = mul(modelMatrix, i.pos);
o.norm = normalize( mul(float4(i.norm, 0), modelMatrixInverse).xyz );
o.col = float4 (0, 0, 0, 0);
o.pos = mul( UNITY_MATRIX_MVP, i.pos);
return o;
}
float4 frag(vertexOut o) : COLOR
{
float3 normalDirection = normalize(o.norm);
float3 viewDirection = normalize(_WorldSpaceCameraPos - o.posWorld.xyz);
float3 lightDirection = float3(0, 0, 0);
float attenuation;
lightDirection = normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
float3 PointLightDirection = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
float PointLightLength = length(PointLightDirection);
lightDirection += normalize(PointLightDirection) * _WorldSpaceLightPos0.w;
attenuation = 1.0 / PointLightLength;
float3 diffuseColor = lerp(_LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection)),
_LightColor0.rgb * _Color.rgb * max(0, dot(normalDirection, lightDirection) * attenuation), _WorldSpaceLightPos0.w);
float3 specularColor = (_LightColor0.rgb * _Color.rgb * max(0, dot(reflect(-lightDirection, normalDirection), viewDirection)) )
* max(0, dot (normalDirection, lightDirection));
return float4( (UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb) + diffuseColor, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
by the way. I’m also calculating specular, but I’m not using the values in the output, because I wanted to isolate diffuse, which is why my code is a mess.