Shader6-基礎光照

 

光學概念簡單介紹

1.輻射照度

量化光的單位(W/m²)

2.光線與物體相交,要麼被散射要麼被吸收

1.散射:(改變光線方向,不改變光線密度和顏色)

  • 散射到物體內部,稱爲折射或透射。折射到物體內部的光線,有些還會重新發射出物體表面。

  • 另一種散射到物體外部,稱爲反射。

2.吸收:(改變光線密度和顏色,不改變光線方向)


光照模型

1.標準光照模型(Blinn Phong)

把進入攝像機範圍內的光線分爲4個部分:

1.1 自發光(C_{missive}) - 物體表面向某個方向發射的輻射量,如果沒有全局光照技術,自發光表面不會照亮周圍物體,只是本身看起來更亮而已。標準光照模型中

                                 C_{missive}m_{emissive}(材質自發光顏色)

1.2 高光反射(C_{specular}) - 當光源光線照到模型表面時,該表面在完全鏡面反射方向散射的輻射量。

求反射光線:

Phong模型計算高光反射:

                             

\large m_{gloss} = 光澤度(gloss) or 反光度(shininess)      \large m_{spscular} = 材質高光反射顏色      \large C_{light} = 光源顏色和強度

計算反射光線耗時,Blinn提出用 I 和 v取平均後,再歸一化得到。

 

Blinn模型計算高光反射:

                          

1.3 漫反射(C_{diffuse}) - 當光源光線照到模型表面時,該表面向每個方向散射的輻射量。

蘭伯特光照模型:反射光線強度和物體表面法線與光源方向的餘弦值成正比。防止點乘爲負,加個max限制一下

                          

半蘭伯特光照模型:使用a和b參數將n和I的點積從[-1,1]映射到[0,1]範圍內。

                       

 C_{light} = 光源顏色     m_{diffuse} = 漫反射顏色       n表面法線       I指向光源的單位矢量   

1.4 環境光(C_{ambient}) - 描述其他間接光照。標準光照模型中所有物體都使用一個環境光

                                         C_{ambient} = g_{ambient}

Unity設置環境光,Shader設置通過變量可直接獲取,UNITY_LIGHTMODEL_AMBIENT

逐像素光照(Phong着色) 

基於每像素,用每個像素的法線進行光照計算,每個像素的法向量是通過頂點的法向量插值得到的。

逐頂點光照(高洛德着色 Gouraud)

基於頂點計算光照計算,然後計算得到的顏色插值頂點所在三角面。


實踐1

1.蘭伯特逐頂點計算的漫反射

Shader "Chan/Chapter6_DiffuseVertexLevel" {
    Properties
    {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
    }
    SubShader
    {
        Pass
        {
            Tags{"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
            fixed4 _Diffuse;

            //輸入結構體
            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            //輸出結構體
            struct v2f
            {
                float4 pos:SV_POSITION;
                fixed3 color:COLOR;
            };

            //頂點着色器
            v2f vert(a2v v)
            {
                v2f o;
                //頂點從模型空間轉換到裁剪空間
                o.pos = UnityObjectToClipPos(v.vertex);
                //獲取環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //頂點法線轉換到模型空間轉化到世界空間 
                fixed3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
                //世界空間下的光源方向 _WorldSpaceLightPos0(只有一個光源,並且是平行光時)
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                //蘭伯特光照模型計算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLight));
                //環境光 + 漫反射 = 最終顏色
                o.color = ambient + diffuse;
                return o;

            }

            //片元着色器 - 輸出頂點顏色
            fixed4 frag(v2f i):SV_Target
            {
                return fixed4(i.color,1.0);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

2.蘭伯特逐像素計算的漫反射

Shader "Chan/Chapter6_DiffusePixelLevel" {
    Properties
    {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
    }
    SubShader
    {
        Pass
        {
            Tags{"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
            fixed4 _Diffuse;

            //輸入結構體
            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            //輸出結構體
            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
            };

            //頂點着色器
            v2f vert(a2v v)
            {
                v2f o;
                //頂點從模型空間轉換到裁剪空間
                o.pos = UnityObjectToClipPos(v.vertex);
                //頂點法線轉換到模型空間轉化到世界空間 
                o.worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
                return o;
            }

            //片元着色器 - 輸出頂點顏色
            fixed4 frag(v2f i):SV_Target
            {
                //獲取環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //世界空間下的光源方向 _WorldSpaceLightPos0(只有一個光源,並且是平行光時)
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                //世界空間下歸一化的法線
                fixed3 worldNormal = normalize(i.worldNormal);
                //蘭伯特光照模型計算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLight));
                fixed3 color = ambient + diffuse;
                return fixed4(color,1.0);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

3.半蘭伯特逐像素計算的漫反射

 

Shader "Chan/Chapter6_HalfLambert" {
    Properties
    {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
    }
    SubShader
    {
        Pass
        {
            Tags{"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
            fixed4 _Diffuse;

            //輸入結構體
            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            //輸出結構體
            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
            };

            //頂點着色器
            v2f vert(a2v v)
            {
                v2f o;
                //頂點從模型空間轉換到裁剪空間
                o.pos = UnityObjectToClipPos(v.vertex);
                //頂點法線轉換到模型空間轉化到世界空間 
                o.worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
                return o;
            }

            //片元着色器 - 輸出頂點顏色
            fixed4 frag(v2f i):SV_Target
            {
                //獲取環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //世界空間下的光源方向 _WorldSpaceLightPos0(只有一個光源,並且是平行光時)
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                //世界空間下歸一化的法線
                fixed3 worldNormal = normalize(i.worldNormal);
                //
                fixed halfLambert = dot(worldNormal,worldLight) * 0.5 + 0.5;
                //半蘭伯特光照模型計算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;
                fixed3 color = ambient + diffuse;
                return fixed4(color,1.0);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

 

實踐2

1.逐頂點的Phong光照模型計算

Shader "Chan/Chapter6_SpecularVertexLevel" {
    Properties {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
        _Specular("Specular",Color) = (1,1,1,1)//高光反射係數
        _Gloss("Gloss",Range(8,256)) = 20//光澤度or反光度
    }
    SubShader {
        Pass
        {
            Tags{"LightMode" = "Forwardbase"}
            
            CGPROGRAM
            #include "Lighting.cginc"

            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                fixed3 color:COLOR;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                //環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //法線從模型空間轉化到世界空間
                fixed3 worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
                //世界空間下的光源方向 _WorldSpaceLightPos0(只有一個光源,並且是平行光時)
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                //蘭伯特光照模型計算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
                //光源方向和法線,求得反射光線方向 
                //-----worldLightDir = 交點指向光源方向,公式需要光源指向交點,因此取反-----
                fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));
                //世界空間下的攝像機座標 - 世界空間下的頂點座標 = 頂點的的視角方向
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld,v.vertex).xyz);
                //Phong光照模型高光反射
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
                //環境光 + 漫反射 + 高光反射 = 最終顏色
                o.color = ambient + diffuse + specular;
                return o;
            }

            fixed4 frag(v2f i):SV_Target
            {
                return fixed4(i.color,1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

2.逐像素的Phong光照模型

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'

Shader "Chan/Chapter6_SpecularPixelLevel" {
    Properties {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
        _Specular("Specular",Color) = (1,1,1,1)//高光反射係數
        _Gloss("Gloss",Range(8,256)) = 20//光澤度or反光度
    }
    SubShader {
        Pass
        {
            Tags{"LightMode" = "Forwardbase"}
            
            CGPROGRAM
            #include "Lighting.cginc"

            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                fixed3 worldPos:TEXCOORD1;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                //得到世界空間下的法線和頂點
                //unity_WorldToObject = 模型空間到世界空間的變換矩陣的逆矩陣
                o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }

            fixed4 frag(v2f i):SV_Target
            {
                //環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                float3 worldNormal = normalize(i.worldNormal);
                //世界空間下的光源方向 _WorldSpaceLightPos0(只有一個光源,並且是平行光時)
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                //蘭伯特光照模型計算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
                //光源方向和法線,求得反射光線方向 
                //-----worldLightDir = 交點指向光源方向,公式需要光源指向交點,因此取反-----
                fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));
                //世界空間下的攝像機座標 - 世界空間下的頂點座標 = 頂點的的視角方向
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                //Phong光照模型高光反射
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
                //環境光 + 漫反射 + 高光反射 = 最終顏色
                return fixed4(ambient + diffuse + specular,1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

實踐3

1.Binn-Phong光照模型

Shader "Chan/Chapter6_BlinnPhong" {
    Properties {
        _Diffuse("Diffuse",Color) = (1,1,1,1)
        _Specular("Specular",Color) = (1,1,1,1)//高光反射係數
        _Gloss("Gloss",Range(8,256)) = 20//光澤度or反光度
    }
    SubShader {
        Pass
        {
            Tags{"LightMode" = "Forwardbase"}
            
            CGPROGRAM
            #include "Lighting.cginc"

            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                fixed3 worldPos:TEXCOORD1;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }

            fixed4 frag(v2f i):SV_Target
            {
                //環境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                float3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
                fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));         
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);          
                fixed3 halfDir = normalize(worldLightDir + viewDir);
                
                //Blinn-Phong光照模型高光反射
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal,halfDir)),_Gloss);

                return fixed4(ambient + diffuse + specular,1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章