VioletaBabel

54. Shader 본문

BCA/4. Unity
54. Shader
Beabletoet 2018. 7. 16. 11:17

셰이더는 예쁘게 꾸미기 위해 병렬처리를 하는 것



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Shader "Game/SampleShader"//디렉토리와 이름
{
    Properties 
    {//내부상의이름("인스펙터에뜰이름",지정된타입) = 밸류
        _Color ("Color", Color) = (1,0,0,1//컬러값
        _MainTex ("Albedo (RGB)", 2D) = "white" {}//텍스쳐
    }
    SubShader
    {
        Pass
        {
            Material
            {
                Diffuse[_Color] //재질에 디퓨즈 속성으로 컬러를 넣은 것. 
                                //재질에 반사되는 빛이 컬러색이라고 보면 된다.
                Ambient[_Color] //디퓨즈가 정면으로 오는 빛에 대한 것이면 이 녀석은 주변 반사광에 대한 것
                                //얘를 넣으니 위 코드 기준으로는 조금 밝아짐
            }
            Lighting On //빛을 받는다. 이게 off면 diffuse로 한 게 안들어간다(빛이 안들어가기 때문.)
                        //원래 색만 나오므로 하얗게 되거나 할거임
        }
    }
    FallBack "Diffuse"
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Shader "Game/SampleShader"
{
    Properties 
    {
        _Color ("Color", Color) = (1,0,0,1
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            Material
            {
                Diffuse[_Color]
                Ambient[_Color]
            }
            Lighting On
            SetTexture[_MainTex] //이게 있으면 위의 값 위에 색이 다 텍스쳐에 덮어져버림(빛을 안받는 셈)
                                 //없으면 위 코드 기준으로는 뻘건 색이 나옴.
    }
    FallBack "Diffuse"
}
cs
위 코드부터는 인스펙터에서 텍스쳐를 아무 이미지나 하나 넣어줬다.



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
Shader "Game/SampleShader"
{
    Properties 
    {
        _Color ("Color", Color) = (1,0,0,1
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            Material
            {
                Diffuse[_Color] 
                Ambient[_Color] 
            }
            Lighting On
            SetTexture[_MainTex]{Combine texture * primary DOUBLE }
                //texture = 텍스쳐의 컬러 값, primary = 라이팅 결과 값
                //단순히 Combine에 texture*primary하면 소수점*소수점이라 
                //어두워짐. 근데 뒤에 DOUBLE을 붙이면 x2된다.
        }
    }
    FallBack "Diffuse"
}
cs



노멀 맵은 평면을 입체적으로 보이게 눈속임하기 위한 것.


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
Shader "Game/SampleShader"
{
    Properties 
    {
        _Color ("Color", Color) = (1,0,0,1
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _NormalTex ("Bumped (RGB)", 2D) = "white" {}//노멀 맵을 넣기 위한 텍스쳐 프로퍼티
    }
    SubShader
    {
        Pass
        {
            Material
            {
                Diffuse[_Color]
                Ambient[_Color]
            }
            Lighting On
            SetTexture[_NormalTex] //노멀맵도 넣는다. 이건 인스펙터에서 미리 넣어두고 하면 됨.
            {//텍스쳐에 빛의 내적을 이용해 빛의 방향과 세기에 따라 상수로 돌려주는 방향을 고정..??
                ConstantColor[_DotLightDirection]    //ConstantColor = 컬러 상수를 등록해주는 키워드.
                                                    //_DotLightDirection = 유니티에 고정된 함수 셰이더 변수
                                                    //노멀 벡터와 빛의 방향 벡터를 Dot 연산으로 내적한 결과 값
                Combine texture dot3 constant //dot3는 3차원 벡터 외적(벡터곱) 연산
            }
            SetTexture[_MainTex]
            {
                Combine texture * primary DOUBLE //texture+primary = 밝아짐, texture-primary 어두워짐
            }
        }
    }
    FallBack "Diffuse"
}
cs



MainTex2에는 다른 텍스쳐를 넣어보자.

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
Shader "Game/SampleShader"
{
    Properties 
    {
        _Color ("Color", Color) = (1,0,0,1
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _MainTex2("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            Material
            {
                Diffuse[_Color]
                Ambient[_Color]
            }
            Lighting On
            SetTexture[_MainTex]
            {
                Combine texture * primary DOUBLE 
            }
            SetTexture[_MainTex2]
            {
                Combine texture * previous // 이전거에 현 텍스쳐를 곱해준다.
            }
        }
    }
    FallBack "Diffuse"
}
cs



==========================

새로 큐브를 하나 만들어 사용하자.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 Shader "Example/Diffuse Simple" {
    SubShader {
      Tags { "RenderType" = "Opaque" }     // 렌더링 타입.
                                        // Opaque는 보통 색깔, 조명, 반사에 쓰임
                                        // Transparent는 반투명 오브젝트
                                        // Cutout은 마스크할 때 사용함
      CGPROGRAM
      #pragma surface surf Lambert
      struct Input {
          float4 color : COLOR;//지금은 구조체에 컬러만 있지만, 노멀이나 자체발광같은 것도 넣을 수 있다.
      };
      void surf (Input IN, inout SurfaceOutput o) { // 표면 쉐이더에 관해 사용한다고 정해진 함수
          o.Albedo = 1// 흰색 지정. 0.5f로 바꾸면 회색이 된다.
      }
      ENDCG
    }
    Fallback "Diffuse"
  }
cs

이러면 그냥 하얀 큐브임



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Shader "Example/Diffuse Simple" {
    SubShader{
        Tags{ "RenderType" = "Opaque" }
        CGPROGRAM
#pragma surface surf Lambert
        struct Input {
        float4 color : COLOR;
    };
    void surf(Input IN, inout SurfaceOutput o) {
        o.Albedo = 0;
        o.Albedo.r = 1// Albedo에는 alpha가 없기에 빨간색이 된다.
    }
    ENDCG
    }
        Fallback "Diffuse"
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Shader "Example/Diffuse Texture" {
    Properties{
        _MainTex("Texture", 2D) = "white" {}
    }
        SubShader{
        Tags{ "RenderType" = "Opaque" }
        CGPROGRAM
#pragma surface surf Lambert
        struct Input {
        float2 uv_MainTex; // 구조체에서 컬러 대신 텍스쳐를 담는 것으로 바꿈
    };
    sampler2D _MainTex;
    void surf(Input IN, inout SurfaceOutput o) {
        o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb; // Albedo로 텍스쳐의 값을 가져오는 것으로 바꿈
    }
    ENDCG
    }
        Fallback "Diffuse"
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Shader "Example/Diffuse Bump" {
    Properties{
        _MainTex("Texture", 2D) = "white" {}
    _BumpMap("Bumpmap", 2D) = "bump" {}
    }
        SubShader{
        Tags{ "RenderType" = "Opaque" }
        CGPROGRAM
#pragma surface surf Lambert // 램버트 조명을 사요안다는 뜻
        struct Input {//Input은 프로퍼티에 따라 자동으로 들어가는데 프로퍼티의 순서와 똑같이 맞춰나야하고 이름은 바뀌어도 됨
        float2 uv_MainTex;
        float2 uv_BumpMap;
    };
    sampler2D _MainTex;
    sampler2D _BumpMap;
    void surf(Input IN, inout SurfaceOutput o) {
        o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;//tex2D는 텍스쳐를 새로 만든 것. 텍스쳐를 만들어 그 rgb를 o에 넣어줌
        o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); //노멀 맵 압축을 풀어주는 함수
    }
    ENDCG
    }
        Fallback "Diffuse"
}
cs




큐브 맵은 주변을 반사하는 것

텍스쳐 타입을 디폴트, 셰이프를 큐브로 바꾸면 큐브맵이 됨.



데칼은 텍스쳐 위에 하나의 이미지를 더 붙이는 것.



===




'BCA > 4. Unity' 카테고리의 다른 글

66. A*를 응용한 이동  (0) 2018.08.14
65. A* (유니티)  (0) 2018.08.13
47. 네모로직+지뢰찾기  (0) 2018.06.21
40. 옵션창  (0) 2018.06.11
39. 미니맵  (0) 2018.06.07
Comments