记录Unity Shader学习进度【基础光照】

基础光照


兰伯特光照模型
逐顶点光照

Shader "Unlit/Lambert"
{
    Properties
    {
        _Color("Color", color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        LOD 100

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

            fixed4 _Color;

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //计算世界法线方向
                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                //计算世界灯光方向
                fixed3 worldLight = normalize(UnityWorldSpaceLightDir(v.vertex));
                //计算兰伯特光照
                fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldLight, worldNormal));

                o.color = ambient + diffuse;
                return o;
            }

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

逐像素光照

Shader "Unlit/Lambert"
{
    Properties
    {
        _Color("Color", color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        LOD 100

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

            fixed4 _Color;

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //计算世界法线方向
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                //计算世界灯光方向
                o.worldLight =UnityWorldSpaceLightDir(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //归一化世界法线方向
                fixed3 worldNormalDir = normalize(i.worldNormal);
                //归一化世界灯光方向
                fixed3 worldLightDir = normalize(i.worldLight);
                //计算兰伯特光照
                fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldLightDir, worldNormalDir));

                fixed3 color = ambient + diffuse;
                return fixed4(color, 1);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

半兰伯光照模型

Shader "Unlit/HalfLambert"
{
    Properties
    {
        _Color("Color", color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        LOD 100

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

            fixed4 _Color;

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //计算世界法线方向
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                //计算世界灯光方向
                o.worldLight = UnityWorldSpaceLightDir(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //归一化世界法线方向
                fixed3 worldNormalDir = normalize(i.worldNormal);
                //归一化世界灯光方向
                fixed3 worldLightDir = normalize(i.worldLight);
                //计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
                fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
                fixed3 diffuse = _LightColor0.rgb * _Color.rgb * halfLambert;

                fixed3 color = ambient + diffuse;
                return fixed4(color, 1);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

逐像素高光

Shader "Unlit/Speculart"
{
    Properties
    {
        _Diffuse("Diffuse", color) = (1,1,1,1)
        _Specular("Specular", color) = (1,1,1,1)
        _Gloss("Gloss", Range(8, 256)) = 20
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        LOD 100

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

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //计算世界法线方向
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                //计算顶点世界位置
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //归一化世界法线方向
                fixed3 worldNormalDir = normalize(i.worldNormal);
                //计算世界灯光方向
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                //计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
                fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;

                //计算反射光,用到reflect函数
                fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormalDir));
                //计算世界空间下视角方向(世界空间下摄像机位置减去世界空间下顶点位置)
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                //计算高光
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(viewDir, reflectDir)), _Gloss);

                return fixed4(ambient + diffuse + specular, 1);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

Blinn-Phong光照模型

Shader "Unlit/Blinn-Phong"
{
    Properties
    {
        _Diffuse("Diffuse", color) = (1,1,1,1)
        _Specular("Specular", color) = (1,1,1,1)
        _Gloss("Gloss", Range(8, 256)) = 20
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }
        LOD 100

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

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //计算世界法线方向
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                //计算顶点世界位置
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //归一化世界法线方向
                fixed3 worldNormalDir = normalize(i.worldNormal);
                //计算世界灯光方向
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                //计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
                fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;

                //计算世界空间下视角方向
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
                //计算视角方向和灯光方向的半角向量
                fixed3 halfDir = normalize(worldLightDir + viewDir);
                //计算高光
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormalDir, halfDir)), _Gloss);

                return fixed4(ambient + diffuse + specular, 1);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值