标准光照模型Phong 和Bling-Phong

本文详细介绍了Phong着色模型,包括自发光、漫反射和反射等元素,并提供了逐顶点和逐像素计算漫反射与高光的Shader代码示例。同时,还讲解了Bling-Phong模型,通过改变反射光与视线夹角来模拟光线衰减,并展示了如何在图形学中近似菲涅尔效应。
摘要由CSDN通过智能技术生成

Phong着色模型:

包括元素:

自发光(emissive)

漫反射(diffuse)

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

反射(specular)

 fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));

        fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);

        fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);

环境光(ambient)

Bling-Phong模型:

反射模型时候将光线与视线的夹角改成计算反射光与法线的夹角:

半角向量:

  表示了光线的衰减为半径平方的倒数

注:无论是Bling-Phong 还是phong模型,都没有计算菲涅尔效应(当光线与所看物体表面接近平行时,反射率会增强)

菲涅尔公式:

一般在图形学中,会简化此公式,或使用粗略相等的公式替代

实现:(源码文章后面)

第一步:计算漫反射

逐顶点计算:具有明显的棱角

逐像素计算:更光滑一些

Diffuse效果

Specular效果

逐顶点高光:(具有明显的棱角)

Shader "Custom/Chapter6/SpecularVertexLevel"
{
    Properties
    {
    _Diffuse("Diffuse",Color) = (1,1,1,1)
    _Specular("Specular", Color) = (1, 1, 1, 1)
    _Gloss("Gloss", Range(8.0, 256)) = 20


    }
    Subshader{

            Pass{
            Tags{"LightMode" = "ForwardBase"}

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

    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;

        //Transfrom the normal from object space to world space;
        fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));

        //get the lightdirection from world space
        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 - mul(unity_ObjectToWorld, v.vertex).xyz);

        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"
}

逐像素高光:

Shader "Custom/Chapter6/SpecularPixelLevel"
{
    Properties
    {
    _Diffuse("Diffuse",Color) = (1,1,1,1)
    _Specular("Specular", Color) = (1, 1, 1, 1)
    _Gloss("Gloss", Range(8.0, 256)) = 20


    }
        Subshader{

                Pass{
                Tags{"LightMode" = "ForwardBase"}

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

        fixed4 _Diffuse;
        fixed4 _Specular;
        float _Gloss;

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

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




        v2f vert(a2v v) {
            v2f o;

            o.pos = UnityObjectToClipPos(v.vertex);

           o.worldNormal = normalize(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;

        //Transfrom the normal from object space to world space;
        fixed3 worldNormal = normalize(i.worldNormal);

        //get the lightdirection from world space
        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);

        fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);

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


        }
            ENDCG




        }


    }

        FallBack "Diffuse"
}

Bling-Phong模型:

Shader "Custom/Chapter6/BlingPhong"
{
    Properties
    {
    _Diffuse("Diffuse",Color) = (1,1,1,1)
    _Specular("Specular", Color) = (1, 1, 1, 1)
    _Gloss("Gloss", Range(8.0, 256)) = 20


    }
        Subshader{

                Pass{
                Tags{"LightMode" = "ForwardBase"}

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

        fixed4 _Diffuse;
        fixed4 _Specular;
        float _Gloss;

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

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




        v2f vert(a2v v) {
            v2f o;

            o.pos = UnityObjectToClipPos(v.vertex);

           o.worldNormal = normalize(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;

        //Transfrom the normal from object space to world space;
        fixed3 worldNormal = normalize(i.worldNormal);

        //get the lightdirection from world space
        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);

        fixed3 halfDir = normalize(worldLightDir + viewDir);

        fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(worldNormal, halfDir)), _Gloss);

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


        }
            ENDCG



        }


    }

        FallBack "Diffuse"
}

标准光照模型Phong 和Bling-Phong

 参考:

GAMES101: 现代计算机图形学入门​sites.cs.ucsb.edu/~lingqi/teaching/games101.html

Unity shader 入门精要 BY 冯乐乐

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值