0
\$\begingroup\$

I'm working on a shader for dynamic lighting in a 2 unity game. I'm new to shaders but I've managed to get something close to the effect I was looking for, although not 100%.

As you can see in the image below, my texture maps respond to a point light and are shaded using a cel-shaded technique:

Real time lighting

However, if you look closely, you can see that a few of the pixels are somewhere between fully lit and half lit, creating diagonal lines across pixels:

enter image description here

What I would like to achieve instead, are pixels that have a single color at all times, depending on the intensity of the light reaching it.

Do I need to move away from the surface shader and implement the lighting in a vertex+fragment shader?

Below is the relevant code for the shader:

Shader "Custom/PixelCellShading"
{
    ...

    SubShader
    {
        Tags { "RenderType" = "Cutout" }

        CGPROGRAM
        #pragma surface surfaceFunction PixelCell alpha
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _NormalTex;
        float _Cels; // number of shading levels, set to 3

        struct Input
        {
            float2 uv_MainTex;
        };

        void surfaceFunction(Input IN, inout SurfaceOutput o)
        {
            float3 normalMap = UnpackNormal(tex2D(_NormalTex, IN.uv_MainTex));
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);

            o.Normal = normalMap.rgb;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

        half4 LightingPixelCell(SurfaceOutput s, half3 lightDir, half attenuation)
        {
            half NdotL = dot(s.Normal, lightDir);
            half cel = floor(NdotL * _Cels) / (_Cels);

            half4 c;
            c.rgb = s.Albedo * _LightColor0.rgb * cel * attenuation;
            c.a = s.Alpha;
            return c;
        }
        ENDCG
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.