Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

Unity Shader 21.05.15. Diffuse Warping 본문

Unity/복습

Unity Shader 21.05.15. Diffuse Warping

HappyFrog 2021. 5. 15. 00:53
Shader "Custom/Warp"
{
    Properties
    {       
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _RampTex("RampTexture", 2D) = "white" {}
    _BumpTex("BumpTexture", 2D) = "bump" {}
       
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
       
        #pragma surface surf _Warp noambient
               
        sampler2D _MainTex;
    sampler2D _RampTex;
    sampler2D _BumpTex;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_BumpTex));
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
           
            o.Alpha = c.a;
        }

        float4 Lighting_Warp(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
        {
            float ndotl = saturate(dot(s.Normal, lightDir))*0.5 + 0.5;
            float4 ramp = tex2D(_RampTex, float2(ndotl, 0.5));
            float4 final;
            final.rgb = ramp.rgb * s.Albedo;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

아직 반사광은 표현되지 않은 모습이다. 빛효과는 하프램버트를 이용했다.

 

여기서 halfVector를 사용하여 spec을 구하고 이를 ramp텍스쳐의 v좌표에 대입시킨다면 어떨까.

Shader "Custom/Warp"
{
    Properties
    {       
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _RampTex("RampTexture", 2D) = "white" {}
    _BumpTex("BumpTexture", 2D) = "bump" {}
       
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
       
        #pragma surface surf _Warp noambient
               
        sampler2D _MainTex;
    sampler2D _RampTex;
    sampler2D _BumpTex;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_BumpTex));
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
           
            o.Alpha = c.a;
        }

        float4 Lighting_Warp(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
        {
            float ndotl = saturate(dot(s.Normal, lightDir)) * 0.5 + 0.5;
            float3 hVec = normalize(lightDir + viewDir);
            float spec = dot(hVec, s.Normal);
            float4 ramp = tex2D(_RampTex, float2(ndotl, spec));
            float4 final;
            final.rgb = ramp.rgb * s.Albedo;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

이렇게 반사광이 적절하게 생긴다.