Hello, I made this rim lighting shader reminiscent of the lighting you see in Vagrant Story. However as you can see, the shadows are funky. Any idea of what’s happening and how I can fix it?
Shader "Custom/PixelatedRimLighting"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_RimPower ("Rim Power", Range(0.5, 10)) = 3
_RimSoftness ("Rim Softness", Range(0.1, 2)) = 1
_RimTint ("Rim Tint Strength", Range(0, 1)) = 0.5
_LightingSteps ("Lighting Steps", Range(2, 32)) = 8 // Number of pixelated lighting steps
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
// Rim lighting properties
float _RimPower;
float _RimSoftness;
float _RimTint;
float _LightingSteps;
// Global light color variable (set dynamically)
float4 _GlobalLightColor;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// Base texture
fixed4 baseColor = tex2D(_MainTex, i.uv);
// Calculate view direction
float3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
// Adjust Rim Softness to discrete steps
float adjustedSoftness = floor(_RimSoftness * 5.0) / 5.0;
// Quantized Rim Light Calculation
float rim = pow(1.0 - saturate(dot(viewDir, i.worldNormal)), _RimPower);
rim = smoothstep(0.0, adjustedSoftness, rim);
// Quantize the lighting using the Lighting Steps property
rim = floor(rim * _LightingSteps) / _LightingSteps;
// Apply global light color as rim color
fixed4 rimColor = _GlobalLightColor * rim * _RimTint;
// Combine base texture with quantized rim lighting
return baseColor + rimColor;
}
ENDCG
}
}
}