美文网首页Unity_Shader
处理各种光源

处理各种光源

作者: _Arturia | 来源:发表于2018-08-24 11:46 被阅读10次

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"

}

相关文章

  • 处理各种光源

    Shader "_MyShader/6_Light/0_ForwardRendering"{Properties{...

  • 同时处理阴影和各种光源的光照衰减

    Shader "_MyShader/6_Light/2_ShadowAndAttenuation"{Propert...

  • URP多光源处理

    URP多光源处理 该节我们将实现在URP下接收多个光照 本系列URP不再阐述具体的效果实现逻辑与公式推导,侧重于U...

  • three.js浅谈@光源

    光源种类 承接上一节讲了各种镜头,但是光有镜头还是无法看清东西的,因为需要光源threejs中有6种光源 环境光 ...

  • 关于光源

    首先了解各种光源的光谱曲线。各种光源光谱分析http://blog.sina.com.cn/s/blog_b5d3...

  • 基础绘画教程 | 人物的阴影处理方式,通过背景让你的角色更多变化

    各种光源下人体阴影应该怎么画? 画师:君野朋成 如有侵权请私信,我们及时删除处理 以上就是在不同角度下,人物的阴影...

  • URP多光源阴影处理

    URP多光源阴影处理 本系列URP不再阐述具体的效果实现逻辑与公式推导,侧重于URP下对《Shader入门精要》中...

  • 手机摄影基础知识之光和影

    光源:包括自然光源和人造光源。 但是一般电影电视剧中我们看到的各种朦胧感、光束感、 光——塑造立体感——三大面——...

  • 各种光源下人体阴影应该怎么画?

    各种光源下人体阴影应该怎么画? 笔下的人物老是缺少立体感?画面又脏又乱或者完全没有氛围?那是因为你不会处理光影! ...

  • Ⅳ更加复杂的光照

    更加复杂的光照 前言 在之前学习中,我们场景中都只有一个光源且光源类型是平行光,但在实际开发中,我们往往需要处理数...

网友评论

    本文标题:处理各种光源

    本文链接:https://www.haomeiwen.com/subject/pvsbiftx.html