UnityShader

【Unity Shader入门精要学习】Unity中的基础光照(

2019-08-24  本文已影响0人  小王子称号发放NPC

高光反射

Phong

image.png

1、逐顶点

Shader "Unlit/Phong-Vertex"
{
    Properties
    {
        _Diffuse("Diffuse",Color)=(1.0,1.0,1.0,1.0)
        _Specular("Specular",Color)=(1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(1.0,100.0))=30
    }

    SubShader
    {
        Pass
        {
            Tags{"LightModel"="ForwardBase"}

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

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

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

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

            v2f vert(a2v a)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(a.pos);

                float3 worldNormal = normalize(mul(a.normal,(float3x3)unity_WorldToObject));
                float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                float3 worldReflectionDir = normalize(reflect(-worldLightDir,worldNormal));
                float3 worldViewDir = normalize(_WorldSpaceCameraPos.xyz-mul(unity_ObjectToWorld,a.pos).xyz);

                //ambient
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;

                //diffuse
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));

                //specular
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldViewDir,worldReflectionDir)),_Gloss);

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

            fixed4 frag(v2f v):SV_TARGET
            {
                return fixed4(v.color,1.0);
            }

            ENDCG

        }
    }
}

2、逐像素

Shader "Unlit/Phong-Pixel"
{
    Properties
    {
        _Diffuse("Diffuse",Color)=(1.0,1.0,1.0,1.0)
        _Specular("Specular",Color)=(1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(1.0,100.0))=30.0
    }

    SubShader
    {
        Pass
        {
            Tags{"LightModel"="ForwardBase"}

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

            float4 _Diffuse;
            float4 _Specular;
            float _Gloss;

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

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

            v2f vert(a2v a)
            {
                v2f o;
                o.clipPos = UnityObjectToClipPos(a.pos);
                o.worldNormal = mul(a.normal,(float3x3)unity_WorldToObject);
                o.worldPos = mul(unity_ObjectToWorld,a.pos);

                return o;
            }

            fixed4 frag(v2f v):SV_TARGET
            {
                float3 worldNormal = normalize(v.worldNormal);
                float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                float3 worldReflectionDir = normalize(reflect(-worldLightDir,worldNormal));
                float3 worldViewDir = normalize(_WorldSpaceCameraPos.xyz-v.worldPos.xyz);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;
                
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldViewDir,worldReflectionDir)),_Gloss);

                return fixed4(ambient+diffuse+specular,1.0);
            }


            ENDCG
        }
    }
}

Blinn-Phong

1、逐顶点

Shader "Unlit/BlinnPhong-Vertex"
{
    Properties
    {
        _Diffuse("Diffuse",Color)=(1.0,1.0,1.0,1.0)
        _Specular("Specular",Color)=(1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(1.0,100.0))=30.0
    }

    SubShader
    {
        Pass
        {
            Tags{"LightModel"="ForwardBase"}

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

            float4 _Diffuse;
            float4 _Specular;
            float _Gloss;

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

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

            v2f vert(a2v a)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(a.pos);

                float4 worldPos = mul(unity_ObjectToWorld,a.pos);
                float3 worldNormal = normalize(UnityObjectToWorldNormal(a.normal));
                float3 worldLightDir = normalize(UnityWorldSpaceLightDir(_WorldSpaceLightPos0.xyz));
                float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos.xyz));
                float3 worldHalfDir = normalize(worldLightDir + worldViewDir);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal,worldHalfDir)),_Gloss);

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


            fixed4 frag(v2f v):SV_TARGET
            {
                return fixed4(v.color,1.0);
            }

            ENDCG
        }
    }
}

2、逐像素

Shader "Unlit/BlinnPhong-Pixel"
{
    Properties
    {
        _Diffuse("Diffuse",Color)=(1.0,1.0,1.0,1.0)
        _Specular("Specular",Color)=(1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(1.0,100.0))=30.0
    }

    SubShader
    {
        Pass
        {
            Tags{"LightModel"="ForwardBase"}
            CGPROGRAM
            #include "Lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag

            float4 _Diffuse;
            float4 _Specular;
            float _Gloss;

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

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

            v2f vert(a2v a)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(a.pos);
                o.worldPos = mul(unity_ObjectToWorld,a.pos);
                o.worldNormal = UnityObjectToWorldNormal(a.normal);

                return o;
            }

            fixed4 frag(v2f v):SV_TARGET
            {
                float3 normalVertexWorldNormal = normalize(v.worldNormal);
                float3 worldLightDir = normalize(UnityWorldSpaceLightDir(_WorldSpaceLightPos0.xyz));
                float3 worldViewDir = normalize(UnityWorldSpaceViewDir(v.worldPos.xyz));
                float3 halfDir = normalize(worldViewDir+worldLightDir);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;

                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(normalVertexWorldNormal,worldLightDir));
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(normalVertexWorldNormal,halfDir)),_Gloss);

                return fixed4(ambient+diffuse+specular,1.0);
            }

            ENDCG
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读