UnityShader Phong/BlinnPhong/半兰伯特/兰伯特组合学习参考代码

综合基础光照模型编写Shader功能模块(学习UnityShader代码编写新手参考)

Shader "MyShader/Texture"
{
    Properties
    {
        _MainTex("主贴图",2D)="white"{}
        _MainColor("主颜色",color)=(1,1,1,1)
        _SpecularColor("高光颜色",color)=(1,1,1,1)
        _Gloss("高光范围",float)=1.0
        [Toggle]_HalfLambert("开启半兰伯特",int)=0
        [Toggle]_BlinnPhong("开启BlinnPhong",int)=0
    }
    SubShader
    {
        Pass
        {
            Tags{"LigthMode"="ForwardBase"}

            CGPROGRAM
            #pragma vertex vert 
            #pragma fragment frag 
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _MainColor;
            fixed4 _SpecularColor;
            float _Gloss;
            bool _HalfLambert;
            bool _BlinnPhong;

            struct vertexInput
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
                float4 texcood:TEXCOORD0;
            };

            struct vertexOutput
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;
            };

            vertexOutput vert(vertexInput v)
            {
                vertexOutput o = (vertexOutput)o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv = TRANSFORM_TEX(v.vertex,_MainTex);
                return o;
            }

            float4 frag(vertexOutput i):SV_TARGET
            {
                half3 worldNormal = normalize(i.worldNormal);
                half3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                half3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
                half3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));
                half3 halfDir = normalize(viewDir+worldLightDir);
                fixed3 albedo = tex2D(_MainTex,i.uv).rgb*_MainColor.rgb;

                fixed3 diffuse = albedo*_LightColor0.rgb;
                if(!_HalfLambert)
                diffuse*=saturate(dot(worldNormal,worldLightDir));
                else
                diffuse*=dot(worldNormal,worldLightDir)*0.5+0.5;

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;

                fixed3 specular;
                if(_BlinnPhong)
                specular= _LightColor0.rgb*_SpecularColor.rgb*pow(saturate(dot(worldNormal,halfDir)),_Gloss);
                else
                specular= _LightColor0.rgb*_SpecularColor.rgb*pow(saturate(dot(viewDir,reflectDir)),_Gloss);

                fixed3 color = diffuse+ambient+specular;
                return float4(color,1.0);
            }
            ENDCG
        }
    }
}

功能模块预览:

UnityShader Phong/BlinnPhong/半兰伯特/兰伯特组合学习参考代码

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/58be5934de.html