处理各种光源

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"

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Basic Shaderlab unity 使用一个叫做 shaderlab 的语言用来包装和组织整个shader...
    goteet阅读 3,145评论 0 12
  • Unity shader 官网文档全方位学习(一)What?? Shader,看起来好高级的样子,是的,这是Uni...
    狼之独步阅读 6,064评论 0 12
  • 转自:http://m.blog.csdn.net/puppet_master/article/details/7...
    树上的cat_ee3c阅读 2,133评论 0 5
  • 张正(点点头,表示赞同毛儒毅讲的话,接着又带着些许后怕和不解说):“罗森刚才到底是怎么了,怎么像失去理智一般,好像...
    樹里阅读 230评论 0 0
  • 今年三月,我来到这座小城 十多天了,雨一直下 春雨肯定窥视了这座小城的许多秘密 比你多得多吧 虽然你细数过每条小巷...
    如言阅读 137评论 0 0