ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 1-4강 쉐이더로 효과 만들기
    [DSU] 게임엔진활용 2026. 3. 12. 23:15
    728x90

     

    여러가지 쉐이더를 만들어서 실시간으로 적용해봅니다.

     

    RenderView.cs 파일에서 여러 material을 사용할 수 있도록 수정합니다.

    텍스쳐를 회색, 모자이크, 눈에 오는 효과를 추가해보았습니다.

    material에서 쉐이더로 넘어가는 값들은, 코드는 39 라인에 쉽게 확인할 수 있도록 매크로 묶어두었습니다.

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    using UnityEngine;
     
    public class RenderView : MonoBehaviour
    {
        public MainCamera mc;
        public int materialIndex;
        Material[] material;
     
        float playbackTime;
     
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            mc = FindFirstObjectByType<MainCamera>();
            //material = new Material(Shader.Find("Unlit/Texture"));// 기본 제공 쉐이더
            string[] strShader = new string[] { "std""grey""mozic""snow" };// 직접 만든 쉐이더
            material = new Material[strShader.Length];
            for (int i = 0; i < strShader.Length; i++)
                material[i] = new Material(Shader.Find("Custom/" + strShader[i]));
     
            playbackTime = 0f;
        }
     
        // Update is called once per frame
        void Update()
        {
        }
     
        void OnGUI()
        {
            if (mc != null && mc.rt != null)
            {
                GL.Clear(truetrue, Color.black);
     
                Texture tex = mc.rt;
                Material m = material[materialIndex];
                m.SetTexture("_BaseMap", tex);
                m.SetColor("_BaseColor", Color.white);
    #if true
                // 0~1. std/grey none
                // 2. mozic
                m.SetFloat("size"10);
                // 3. snow
                m.SetFloat("iTime", playbackTime);
                playbackTime += Time.deltaTime;
    #endif
                float r0 = (float)Screen.width / tex.width;
                float r1 = (float)Screen.height / tex.height;
                float r = (r0 < r1 ? r0 : r1);
                int w = (int)(tex.width * r),    h = (int)(tex.height * r);
                int x = (Screen.width - w) / 2,    y = (Screen.height - h) / 2;
                Graphics.DrawTexture(
                    new Rect(x, y, w, h), tex,
                    new Rect(0011),
                    0000, m);
            }
        }
    }
     
    cs

     

     

    std, grey, mozic, snow shader 파일입니다. 설명이 필요 없는 부분은 생략했습니다.

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    Shader "Custom/std"
    //Shader "Custom/grey"
    //Shader "Custom/mozic"
    //Shader "Custom/snow"
    {
        Properties
         {
            // 생략
        }
     
        SubShader
        {
            Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
     
            Pass
            {
                // 생략
     
                CBUFFER_START(UnityPerMaterial)
                    half4 _BaseColor;
                    float4 _BaseMap_ST;
                    float4 _BaseMap_TexelSize;// 추가
                CBUFFER_END
     
                Varyings vert(Attributes IN)
                {
                    // 생략
                }
     
                half4 frag(Varyings IN) : SV_Target
                {
    #if 1            // std
                    half4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor;
    #elif 1            // grey
                    half4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
                    float grey = c.r * 0.3 + c.g * 0.59 + c.b * 0.11;
                    c = float4(grey, grey, grey, _BaseColor.a);
    #elif 1            // mozic
                    float2 uv = IN.uv;
                    float sizeX = size;
                    float sizeY = size;
                    float px = sizeX * _BaseMap_TexelSize.x;
                    float py = sizeY * _BaseMap_TexelSize.y;
                    uv = float2(px * floor(uv.x/px), py * floor(uv.y/py));
                    half4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv) * _BaseColor;
    #else             // snow는 길어서 수업시간
    #endif
     
                    #ifndef UNITY_COLORSPACE_GAMMA
                        c.rgb = LinearToSRGB(c.rgb);
                    #endif
     
                    return c;
                }
                ENDHLSL
            }
        }
    }
     
    cs

     

     

    실행결과 입니다.

     

     

    2강에서는 더블버퍼링 및 키입력에 대해 알아보겠습니다.

    긴 글 읽어주셔서 감사합니다 :)

Designed by Tistory.