Unity 3d Sprite Shader (How do I limit Max Brightness to 1 with Multiple Lights Hitting)

杀马特。学长 韩版系。学妹 提交于 2019-12-06 08:43:30

You can't really do this with surface shaders, but you can do it very efficiently with vertex fragment shaders. Unity stores the 4 closest point lights in a set of vectors to be used for per-vertex (non-important) lights. Fortunately, these are also accessible in the fragment shader, so you can use them to shade all 4 lights at once in a single pass! When you have all lights summed together, make sure that their intensity can't go above 1. Here is a quick shader i threw together for you:

Shader "Unlit/ToonTest"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Name "FORWARD"
        Tags { "LightMode" = "ForwardBase" "RenderType" = "TransparentCutout" "Queue"="AlphaTest"}
        Cull Off
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #pragma multi_compile_fwdbase

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                half3 normal : NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 worldPos : TEXCOORD1;
                float3 ambient : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.ambient = ShadeSH9(mul(unity_ObjectToWorld, float4(v.normal, 0.0 ))); // Ambient from spherical harmonics
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            float3 Shade4Lights (    
                float4 lightPosX, float4 lightPosY, float4 lightPosZ,
                float3 lightColor0, float3 lightColor1, float3 lightColor2, float3 lightColor3,
                float4 lightAttenSq,
                float3 pos)
            {
                // to light vectors
                float4 toLightX = lightPosX - pos.x;
                float4 toLightY = lightPosY - pos.y;
                float4 toLightZ = lightPosZ - pos.z;
                // squared lengths
                float4 lengthSq = 0;
                lengthSq += toLightX * toLightX;
                lengthSq += toLightY * toLightY;
                lengthSq += toLightZ * toLightZ;
                // don't produce NaNs if some vertex position overlaps with the light
                lengthSq = max(lengthSq, 0.000001);

                // attenuation
                float4 atten = 1.0 / (1.0 + lengthSq * lightAttenSq);
                float4 diff = atten; //ndotl * atten;
                // final color
                float3 col = 0;
                col += lightColor0 * diff.x;
                col += lightColor1 * diff.y;
                col += lightColor2 * diff.z;
                col += lightColor3 * diff.w;
                return col;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                half3 intensity = Shade4Lights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor[0], unity_LightColor[1], unity_LightColor[2], unity_LightColor[3], unity_4LightAtten0, i.worldPos);
                intensity = min((half3)1, i.ambient + intensity);

                col.rgb *= intensity;

                clip(col.a - 0.5);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

The "Shade4Lights" function is a modified version of Unity's "Shade4PointLights", with diffuse lambert lighting removed (attenuation only). You'll also have to add your RetroAA function to the texture sampling. Your cutoff value is the "- 0.5" inside the "clip" funciton - you can expose this if you need it. If you need shadow casting for this shader, you can copy/paste the shadow pass from Unity's standard shader (you can download the source code from their page). For shadow receiving, you need to add a few lines to the shader - again check the source code for this.

You can read more about built-in shader variables here:

https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!