UnityShader 学习笔记 7 Light

处理各种光源的Shader

  • 源码


Shader "_MyShader/6_Light/0_ForwardRendering"

{

    Properties

    {

        _DiffuseColor ("DiffuseColor",COLOR) =(1,1,1,1)

        _SpecularColor ("SpecularColor",COLOR) =(1,1,1,1)

        _SpecularRange ("SpecularRange",Range(5,100)) = 20

    }

    SubShader

    {

        Pass

        {

            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag

            #pragma multi_compile_fwdbase

            

            #include "Lighting.cginc"





            fixed4 _DiffuseColor;

            fixed4 _SpecularColor;

            float _SpecularRange;



            struct a2v {

                float4 vertex:POSITION;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                fixed3 worldNormalDir:POSITION1;

                fixed3 worldLightDir:POSITION2;

                half3 reflectDir:POSITION3;

                fixed3 worldViewDir:POSITION4;

                //Blinn-Phong

                fixed3 blinn_Phong_Dir:POSITION5;

            };



            v2f vert(a2v v){

                v2f o;

                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);



                o.worldLightDir = normalize(_WorldSpaceLightPos0);

                o.worldNormalDir = normalize(mul(v.normal,(float3x3)_World2Object));



                //计算反射向量

                o.reflectDir = normalize(reflect(-o.worldLightDir,o.worldNormalDir));

                //计算视角向量

                o.worldViewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));



                //Blinn-Phong

                o.blinn_Phong_Dir = normalize(o.worldLightDir + o.worldViewDir);



                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                //漫反射阶段

                fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;



                fixed3 diffuseColor = _LightColor0 * _DiffuseColor*saturate(dot(i.worldNormalDir,i.worldLightDir));



                //高光阶段

                fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.reflectDir,i.worldViewDir)),_SpecularRange);

                //Blinn-Phong光照

                //fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.worldNormalDir,i.blinn_Phong_Dir)),_SpecularRange);

                

                fixed atten = 1.0;



                fixed4 col = fixed4(ambientColor + (diffuseColor + specularColor) * atten,1);

                return col;

            }



            

            ENDCG

        }





        Pass

        {

            Tags{"LightMode" = "ForwardAdd"}

            Blend One One



            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag

            #pragma multi_compile_fwdadd

            

            #include "Lighting.cginc"

            #include "AutoLight.cginc"

            #include "UnityPBSLighting.cginc"





            fixed4 _DiffuseColor;

            fixed4 _SpecularColor;

            float _SpecularRange;



            struct a2v {

                float4 vertex:POSITION;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                fixed3 worldNormalDir:POSITION1;

                fixed3 worldLightDir:POSITION2;

                half3 reflectDir:POSITION3;

                fixed3 worldViewDir:POSITION4;

                //Blinn-Phong

                fixed3 blinn_Phong_Dir:POSITION5;

                float4 worldPos:POSITION6;

            };



            v2f vert(a2v v){

                v2f o;

                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);



                o.worldPos = mul(_Object2World,v.vertex);

                

            #ifdef USING_DIRECTIONAL_LIGHT

                o.worldLightDir = normalize(_WorldSpaceLightPos0);

            #else

                o.worldLightDir = normalize(_WorldSpaceLightPos0 - o.worldPos);

            #endif



                o.worldNormalDir = normalize(mul(v.normal,(float3x3)_World2Object));



                //计算反射向量

                o.reflectDir = normalize(reflect(-o.worldLightDir,o.worldNormalDir));

                //计算视角向量

                o.worldViewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));



                //Blinn-Phong

                o.blinn_Phong_Dir = normalize(o.worldLightDir + o.worldViewDir);



                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                //漫反射阶段

                //fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;



                fixed3 diffuseColor = _LightColor0 * _DiffuseColor*saturate(dot(i.worldNormalDir,i.worldLightDir));



                //高光阶段

                fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.reflectDir,i.worldViewDir)),_SpecularRange);

                //Blinn-Phong光照

                //fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.worldNormalDir,i.blinn_Phong_Dir)),_SpecularRange);

            #ifdef USING_DIRECTIONAL_LIGHT

                fixed atten = 1.0;

            #else

                UNITY_LIGHT_ATTENUATION(atten, 0, i.worldPos.xyz)

            #endif



                fixed4 col = fixed4((diffuseColor + specularColor) * atten,1);

                return col;

            }



            

            ENDCG

        }



    }

    FallBack "VertexLit"

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值