본문 바로가기

Unity3D C#/유니티 쉐이더 공부

버텍스 컬러를 이용한 텍스쳐 블렌딩에 노멀맵 적용하기

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
32
33
34
35
36
37
38
39
40
41
Shader "Custom/vertexcolor" {
    Properties {
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _MainTex2("Albedo (RGB)", 2D) = "white" {}
        _MainTex3("Albedo (RGB)", 2D) = "white" {}
        _MainTex4("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Standard nomabient
 
        sampler2D _MainTex;
        sampler2D _MainTex2;
        sampler2D _MainTex3;
        sampler2D _MainTex4;
 
        struct Input {
            float2 uv_MainTex;
            float2 uv_MainTex2;
            float2 uv_MainTex3;
            float2 uv_MainTex4;
            float4 color:COLOR;
        };
        
        void surf (Input IN, inout SurfaceOutputStandard o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D(_MainTex2, IN.uv_MainTex);
            fixed4 e = tex2D(_MainTex3, IN.uv_MainTex);
            fixed4 f = tex2D(_MainTex4, IN.uv_MainTex);
            o.Albedo = lerp(c.rgb, d.rgb, IN.color.r);
            o.Albedo = lerp(o.Albedo, e.rgb, IN.color.g);
            o.Albedo = lerp(o.Albedo, f.rgb, IN.color.b);
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
 
cs


기존 버텍스컬러를 이용한 텍스쳐 블렌딩 코드에 NormalMap 추가

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Shader "Custom/vertexcolor" {
    Properties {
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _MainTex2("Albedo (RGB)", 2D) = "white" {}
        _MainTex3("Albedo (RGB)", 2D) = "white" {}
        _MainTex4("Albedo (RGB)", 2D) = "white" {}
        _BumpMap("NormalMap", 2D) = "bump"{}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Standard nomabient
 
        sampler2D _MainTex;
        sampler2D _MainTex2;
        sampler2D _MainTex3;
        sampler2D _MainTex4;
        sampler2D _BumpMap;
 
        struct Input {
            float2 uv_MainTex;
            float2 uv_MainTex2;
            float2 uv_MainTex3;
            float2 uv_MainTex4;
            float4 color:COLOR;
            float2 uv_BumpMap;
        };
        
        void surf (Input IN, inout SurfaceOutputStandard o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D(_MainTex2, IN.uv_MainTex);
            fixed4 e = tex2D(_MainTex3, IN.uv_MainTex);
            fixed4 f = tex2D(_MainTex4, IN.uv_MainTex);
            o.Albedo = lerp(c.rgb, d.rgb, IN.color.r);
            o.Albedo = lerp(o.Albedo, e.rgb, IN.color.g);
            o.Albedo = lerp(o.Albedo, f.rgb, IN.color.b);
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
 
cs

NormalMap의 울퉁불퉁한 질감은 빛의 음영에 따라 느끼게 되는 것이어서 

빛의 각도에 따라 잘 보일 수도 아닐 수도 있음.


그리고 모든 질감은 어느 정도의 스페큘러를 가지고 있음. 우리가 질감을 느끼게 되는 요소는 거의 스페큘러이며, 그래서 스페큘러가 없으면 거의 질감을 느끼기가 힘든 것. 코드에 Metallic과 Smoothness 추가


            o.Metallic = 0;
            o.Smoothness = 0.5;


일부만 젖은 것처럼 표현하고 싶다면, Smoothness에 0이 들어가면 매우 둔탁해지고, 1이 들어가면 매우 반짝거리게 되는 것을 이용하여 4번째 텍스쳐에 해당하는 버텍스 컬러 b 채널을 수정


o.Smoothness = IN.color.b; 

버텍스 컬러의 b부분을 반짝거리게 수정


o.Smoothness = IN.color.b + 0.3;

버텍스 컬러에 조금 수치를 더해서 영역 확대


o.Smoothness = IN.color.b * 0.5 + 0.3;

0.5를 곱해줌으로써 조금 덜 반짝거리게 함


'Unity3D C# > 유니티 쉐이더 공부' 카테고리의 다른 글

Lambert(램버트) 라이팅 만들기  (0) 2018.01.18
유니티에 내장된 라이팅 구조  (0) 2018.01.17
Occlusion(오클루젼)  (0) 2018.01.15
NormalMap의 함수 적용  (0) 2018.01.14
Normal map(노멀맵)  (0) 2018.01.13