CookTorranceSurface Temp Note

 Shader "Custom/CookTorranceSurface"

{

    Properties

    {

        _MainTex("Base (RGB)", 2D) = "white" {}

        _ColorTint("Color", Color) = (1,1,1,1)

        _SpecColor("Specular Color", Color) = (1,1,1,1)

        _BumpMap("Normal Map", 2D) = "bump"

        _Roughness("Roughness", Range(0,1)) = 0.5

        _Subsurface("Subsurface", Range(0,1)) = 0.5

    }

    SubShader

    {

        Tags { "RenderType"="Opaque" }

        LOD 200


        CGPROGRAM

        // CookTorrance Light model

        #pragma surface surf CookTorrance


        // Use shader model 3.0 target, to get nicer looking lighting

        #pragma target 3.0


        struct Input

        {

            float2 uv_MainTex;

        };


#define PI 3.14159265358979323846F


        sampler2D _MainTex;

        sampler2D _BumpMap;

        half _Roughness;

        float _Subsurface;

        fixed4 _ColorTint;



        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.

        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.

        // #pragma instancing_options assumeuniformscaling

        UNITY_INSTANCING_BUFFER_START(Props)

            // put more per-instance properties here

        UNITY_INSTANCING_BUFFER_END(Props)


        struct SurfaceOutputCustom {

            fixed3 Albedo;

            fixed3 Normal;

            fixed3 Emission;

            fixed Alpha;

        }



        void surf (Input IN, inout SurfaceOutputStandard o)

        {

            // Albedo comes from a texture tinted by color

            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;

            o.Albedo = c.rgb;

            o.Normal = UnpackNormal(tex2D(_BumpMap, IN, uv_MainTex));

            o.Alpha = c.a;

        }


        inline float4 LightingCookTorrance(SurfaceOutputCustom s, float3 viewDir, UnityGI gi) {

            UnityLight light = gi.light;


            viewDir = normalize(viewDir);

            float3 lightDir = normalize(light.dir);

            s.Normal = normalize(s.Normal);


            float3 halfV = normalize(lightDir + viewDir);

            float NdotL = saturate(dot(s.Normal, lightDir));

            float NdotH = saturate(dot(s.Normal, halfV));

            float NdotV = saturate(dot(s.Normal, viewDir));

            float VdotH = saturate(dot(viewDir, halfV));

            float LdotH = saturate(dot(lightDir, halfV));



            //BRDFs

            float3 diff = DisneyDiff(s.Albedo, NdotL, LdotH, _Roughness, _SpecColor);

            float3 spec = CookTorrance(NdotL, LdotH, NdotV, _Roughness, _SpecColor);


            // add diffuse, speculat and tints 


            float3 firstLayer = (diff + spec * _SpecColor) * _LightColor0.rgb;

            float4 c = float4(firstLayer, s.Alpha);


#ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT

            c.rgb += s.Albedo * gi.indirect.diffuse;

#endif


            return c;

        }



        float sqr(float a) {

            return a * a;

        }


        float schlickFresnel(float a) {

            return pow(clamp(1 - a, 0, 1), 5);

        }


        float G1(float k, float x) {

            return x / (x * (1 - k) + k);

        }


        inline float3 CookTorranceSpec(float NdotL, float LdotH, float NdotH, float roughness, float F0) {

            float alpah = sqr(roughness);

            float F, D, G;


            // D (Distribution Function)


            float alphaSqr = sqr(alpha);

            float denominator = sqr(NdotH) * (alphaSqr - 1.0f) + 1.0f;

            D = alphaSqr / (PI * sqr(denominator));


            // F (Fresnel Function)

            float LdotH5 = ShlickFresnel(LdotH);

            F = F0 + (1.0f - F0) * LdotH5;


            // G (Geometry trem, Schlick's Approximation of Smith)

            float r = _Roughness + 1;

            float k = sqr(r) / 8;

            float glL = G1(k, NdotL);

            float glV = G1(k, NdotV);

            G = glL * glV;


            float specular = NdotL * D * F * G;

            return specular;



        }



        ENDCG

    }

}

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

C++ 생성자 위임 (delegating constructor)