Unity中写GLSL(二十一)—— Cook-Torrance— Fresnel

参考:http://www.codinglabs.net/article_physically_based_rendering_cook_torrance.aspx

下面是效果:
其实之前的文章就写过菲涅尔反射了。
这里写图片描述

下面是代码:

Shader "Custom/FresnalShader"
{
    Properties
    {
        _DiffuseColor ("Diffuse Color", Color) = (1,1,1,1)
        _SpecColor ("Specular Color", Color) = (1,1,1,1)
        _Roughness ("Roughness", Range(0.0, 1.0)) = 0.5
        _Metallic ("Metallic", Range(0.0, 1.0)) = 0.5
        _IOR ("Indices of Refraction", Float) = 1
    }

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

            uniform vec3 _DiffuseColor;
            uniform vec3 _SpecColor;
            uniform float _Roughness;
            uniform float _Metallic;
            uniform float _IOR;

            uniform vec3 _WorldSpaceCameraPos;
            uniform mat4 _Object2World;
            uniform mat4 _World2Object;
            uniform vec4 _WorldSpaceLightPos0;

            uniform vec4 _LightColor0;

            #ifdef VERTEX
            //顶点着色器

            out vec4 worldPosition;
            out vec3 worldNormalDirection;

            void main()
            {
                mat4 modelMatrix = _Object2World;
                mat4 modelMatrixInverse = _World2Object;

                worldPosition = modelMatrix * gl_Vertex;
                worldNormalDirection = normalize(vec3(vec4(gl_Normal, 0.0) * modelMatrixInverse));

                gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            }

            #endif

            #ifdef FRAGMENT
            //片元着色器

            in vec4 worldPosition;
            in vec3 worldNormalDirection;

            //菲涅尔
            vec3 Fresnel_Schlick(float cosT, vec3 F0)
            {
                return F0 + (1 - F0) * pow(1 - cosT, 5);
            }

            void main()
            {
                vec3 normalDirection = normalize(worldNormalDirection);
                vec3 viewDirection = normalize(_WorldSpaceCameraPos - vec3(worldPosition));

                vec3 lightDirection = normalize(vec3(_WorldSpaceLightPos0));

                vec3 ambientLighting = vec3(gl_LightModel.ambient) * vec3(_DiffuseColor);

                vec3 diffuseReflection = vec3(_LightColor0) * vec3(_DiffuseColor)
                                        * max(0.0, dot(normalDirection, lightDirection));

                vec3 halfVector = normalize(lightDirection + viewDirection);

                vec3 specularReflection;
                //if (dot(normalDirection, lightDirection) < 0.0)
                //{
                //  specularReflection = vec3(0.0, 0.0, 0.0);
                //}
                //else
                //{
                    //specularReflection = vec3(_LightColor0) * vec3(_SpecColor) 
                    //                  * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Roughness);
                    //specularReflection = vec3(_LightColor0) * vec3(_SpecColor) 
                    //                  * pow(max(0.0, dot(halfVector, normalDirection)), _Roughness);

                //}

                vec3 F0 = vec3(abs((1.0 - _IOR) / (1.0 + _IOR)));
                F0 = F0 * F0;
                F0 = mix(F0, _DiffuseColor, _Metallic);

                vec3 fresnel = Fresnel_Schlick(max(0.0, dot(viewDirection, halfVector)), F0);

                //float kS = GGX_Distribution(normalDirection, halfVector, _Roughness);
                //float kD = 1 - kS;
                //gl_FragColor = vec4(kD *(ambientLighting) +  kS *specularReflection, 1.0);
                gl_FragColor = vec4(diffuseReflection + fresnel, 1.0);
            }

            #endif

            ENDGLSL
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值