[SOLVED] Can't add ambient light to fur shader

Hi there, I’m trying to update this shader and also add to it ambient lighting using this shader as a reference, mostly the “Diffuse lighting with ambient” section. But I don’t seem able to make the ambient lighting work.


This is what I got so far.

Shader "Custom/Fur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _FurTex ("Fur pattern", 2D) = "white" {}
        _Diffuse ("Diffuse value", Range(0, 1)) = 1
        _FurLength ("Fur length", Range(0.0, 1)) = 0.5
        _CutOff ("Alpha cutoff", Range(0, 1)) = 0.5
        _Blur ("Blur", Range(0, 1)) = 0.5
        _Thickness ("Thickness", Range(0, 0.5)) = 0
    }
    CGINCLUDE
        fixed _Diffuse;
      
    ENDCG
    SubShader
    {
        Tags { "RenderType"="Transparent" "IgnoreProjector"="True" "Queue"="Transparent"}
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            //#pragma target 3.0
            #include "UnityCG.cginc"
            #include "UnityLightingCommon.cginc"
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                fixed4 diff : COLOR;
            };
            v2f vert (appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord;
                // get vertex normal in world space
                half3 worldNormal = UnityObjectToWorldNormal(v.normal);
                // dot product between normal and light direction for
                // standard diffuse (Lambert) lighting
                half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
                // factor in the light color
                o.diff = nl * _LightColor0;
                // in addition to the diffuse lighting from the main light,
                // add illumination from ambient or light probes
                // ShadeSH9 function from UnityCG.cginc evaluates it,
                // using world space normal
                o.diff.rgb += ShadeSH9(half4(worldNormal,1));
                return o;
            }

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                col.rgb *= i.diff;
                return col;
            }
            ENDCG
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define FURSTEP 0.20
            #include "FurHelper.cginc"
            ENDCG
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define FURSTEP 0.40
            #include "FurHelper.cginc"
            ENDCG
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define FURSTEP 0.60
            #include "FurHelper.cginc"
            ENDCG
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define FURSTEP 0.80
            #include "FurHelper.cginc"
            ENDCG
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define FURSTEP 1
            #include "FurHelper.cginc"
            ENDCG
        }
    }
}
//#pragma target 3.0
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
struct v2f {
    float4 vertex : SV_POSITION;
    half2 uv : TEXCOORD0;
    half2 uv1 : TEXCOORD1;
    fixed4 diff : COLOR;
};
float _FurLength;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _FurTex;
float4 _FurTex_ST;
float _Blur;
v2f vert(appdata_base v) {
    v2f o;
    v.vertex.xyz += v.normal * _FurLength * FURSTEP;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.uv1 = TRANSFORM_TEX(v.texcoord, _FurTex);

    half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    // dot product between normal and light direction for
    // standard diffuse (Lambert) lighting
    half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    // factor in the light color
    o.diff = nl * _LightColor0;
    // in addition to the diffuse lighting from the main light,
    // add illumination from ambient or light probes
    // ShadeSH9 function from UnityCG.cginc evaluates it,
    // using world space normal
    o.diff.rgb += ShadeSH9(half4(worldNormal,1));
    o.diff.a = 1 - (FURSTEP * FURSTEP);
    return o;
}
float _CutOff;
float _Thickness;
fixed4 frag(v2f i) : SV_Target {
    fixed4 col = tex2D(_MainTex, i.uv);
    fixed alpha = tex2D(_FurTex, i.uv1).r;
    col *= i.diff;
    col.a *= step(lerp(_CutOff, _CutOff + _Thickness, FURSTEP), alpha);
    return col;
}

Any idea to why ambient does not seem to work?

Cheers!

Alright I got it. I forgot to add the tag “LightMode”=“ForwardBase”…

2 Likes