Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
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
Archives
Today
Total
관리 메뉴

행복한 개구리

Unity Shader 21.05.07. 수업내용 - Wraped Diffuse 본문

Unity/수업내용

Unity Shader 21.05.07. 수업내용 - Wraped Diffuse

HappyFrog 2021. 5. 7. 16:24

사용한 Ramp텍스쳐. 흰색이 필요해서 사진 위쪽에 그림판으로 흰색을 만들어서 사용했다.

 

 

**핵심 구현기법은 빛공식으로 쓰이는 Normal과 Light Vector의 내적을 UV로 사용한다.

wrap가 적용된 모습. 빛의 방향에 달라진다.

** 주의 : wrap를 할 때 쓰는 텍스쳐의 경우는 Input에서 UV좌표를 구하지 않고 쓴다. -> 텍스쳐의 UV좌표를 그대로 써야하는게 아니라 조정해서 써야하기때문에 위 코드를 보면 tex2D(램프텍스쳐, 좌표값)이라고 사용된다.

 

조명의 x축 회전값 90//180

여기에 알베도까지 적용시킨다면 이런 모습이 된다.

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

        CGPROGRAM
        
        #pragma surface surf Wrap noambient

        sampler2D _RampTex;
        sampler2D _MainTex;

        struct Input
        {            
            float2 uv_MainTex;
        };

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

        float4 LightingWrap(SurfaceOutput s, float3 lightDir, float attten)
        {
            float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;
            float4 ramp = tex2D(_RampTex, float2(ndotl, 0.5));

            float4 final;
            final.rgb = s.Albedo * ramp.rgb;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

참고로 옵션에서의 Wrap는 내장함수가 아니라 그냥 이름이다.

 

wrap 텍스쳐의 윗부분에 흰색이 할당돼어있다면 이렇게 specular가 움직이는 모습을 볼 수 있다.

 

오늘 배운 내용까지 합쳐보았다.

Shader "Custom/Test"
{
    Properties
    {    
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _RampTex ("RampTexture", 2D) = "white" {}
        _BumpTex ("BumpTexture", 2D) = "bump" {}
        _Thickness ("Thickness", Float) = 0
        _LineColor ("LineColor", Color) = (0,0,0,0)    
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        Cull Front

        CGPROGRAM    
        #pragma surface surf _Test vertex:vert noambient noshadow

        float _Thickness;
        float4 _LineColor;

        void vert(inout appdata_full v)
        {
            v.vertex.xyz = v.vertex.xyz + v.vertex.xyz * _Thickness;
        }

        struct Input{float2 uv_MainTex;};
        void surf (Input IN, inout SurfaceOutput o){}

        float4 Lighting_Test(SurfaceOutput s, float3 lightDir, float atten)
        {                        
            return _LineColor;
        }
        ENDCG


        Cull Back
        CGPROGRAM    
        #pragma surface surf _Test 
            
        sampler2D _MainTex;
        sampler2D _RampTex;
        sampler2D _BumpTex;

        struct Input
        {
            float2 uv_BumpTex;
            float2 uv_MainTex;
        };

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

        float4 Lighting_Test(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
        {
            float ndotl = 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 = s.Albedo.rgb * ramp.rgb;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Bump와 Wrap모두 사용하였고, 2pack을 사용하여 외곽선을 잡았다.

코드를 보다보니 Ramp텍스쳐를 적용할 때, UV좌표값에 U에는 ndotl, V에는 spec을 적용시키니 둘의 특성이 모두 드러나는 듯 하다.