lightmode forwardbase forwardadd

now, somebody do not understand how to use the key words forwardbase and forwardadd.
base, in my opition means the basic light effect.
add, means the addtional light effect.

in this blog, you will learn how to use the base pass and addtional pass to render a object. and also u will lern the setting of default number of pixel light in unity.

next, u will known there methods to cope a light in forward render method.
vertex
pixel
sphereical harmonics, short for SH
pixel and vertex ways have higher performance. but pixel method will have lower performance.

there are some rules when when choose the vertex or pixel or sphereical harmonics, that means which ways to a light will take to render accroding to your settings.

more explanations will appear larter.

let me give a first example to render a light in base pass.
the base pass will use render the first direction light color to the plane, and the additional pass will render the second pixel light.

Shader "Course/LightColor0"
{
    SubShader
    {
        Pass
        {
            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase  // this code is usually follow by "LightMode"="ForwardBase" tags.
            #include "Lighting.cginc"

            struct a2v
            {
                float4 localPosition:POSITION;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                return v;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed4 color = fixed4(_LightColor0.rgb, 1);
                return color;
            }
            ENDCG
        }
    }
}

now there is only one light in the scene like this:
lightmode forwardbase forwardadd_第1张图片
and the configuration of the direcitonal light is :

lightmode forwardbase forwardadd_第2张图片

the render mode is important, and there is just only one light in the scene, so this light is rendered in the base pass.
the base pass can render only one pixel light and other vertex and sh light.
other pixel light will be rendered in the additional pass.
whether a light is pixel light or vertex light or sh light. there are some rules to limit it.
if you assigin a light mode of the light is important, this light is pxiel light. in this case you should not care about the default value of unity’s project settings.
there is a default value of pixel light number in unity.
it is in Unity->Edit->Project Settings->Quality
lightmode forwardbase forwardadd_第3张图片

how does this default value affect the light’s pixel or not pixel. if the number of the light in the scene not more than this value, and light which mode is setted to auto. and this light will be rendered as pixel light.
next we will add another pass to show the second pixel light effect.

Shader "Course/LightColor0"
{
    SubShader
    {
        Pass
        {
            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase  // this code is usually follow by "LightMode"="ForwardBase" tags.
            #include "Lighting.cginc"

            struct a2v
            {
                float4 localPosition:POSITION;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                return v;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed4 color = fixed4(_LightColor0.rgb, 1);
                return color;
            }
            ENDCG
        }

        Pass
        {
            Tags{"LightMode"="ForwardAdd"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdadd  // this code is usually follow by "LightMode"="ForwardAdd" tags.
            #include "Lighting.cginc"

            struct a2v
            {
                float4 localPosition:POSITION;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                return v;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed4 color = fixed4(_LightColor0.rgb, 1);
                return color;
            }
            ENDCG
        }
    }
}

we add another light in the scene , and assin the mode of light to important.
lightmode forwardbase forwardadd_第4张图片
lightmode forwardbase forwardadd_第5张图片

and the effect is :
lightmode forwardbase forwardadd_第6张图片

why ther is only the second light color of the plane. yep, let me anaylse it. the first light use the base pass. and the second light use the additional pass. there is no operation tell me how to blend these two effects. so here, we should tell unity how to blend the the addional color and the base color. that is the blend operation’s function.
we will introuduce this key word in the furture.
now, let add the blend one one to the additioanl pass.

Shader "Course/LightColor0"
{
    SubShader
    {
        Pass
        {
            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase  // this code is usually follow by "LightMode"="ForwardBase" tags.
            #include "Lighting.cginc"

            struct a2v
            {
                float4 localPosition:POSITION;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                return v;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed4 color = fixed4(_LightColor0.rgb, 1);
                return color;
            }
            ENDCG
        }

        Pass
        {
            Tags{"LightMode"="ForwardAdd"}
            Blend one one

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdadd  // this code is usually follow by "LightMode"="ForwardAdd" tags.
            #include "Lighting.cginc"

            struct a2v
            {
                float4 localPosition:POSITION;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                return v;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed4 color = fixed4(_LightColor0.rgb, 1);
                return color;
            }
            ENDCG
        }
    }
}

blend one one. means we blend base color and additional color by the percent of 1:1.
you can change the color of the light to get the different blending effect.

你可能感兴趣的:(Unity,fowardbase,and,forwardadd,pass)