ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 3강 Geometry, Texture, String
    [DSU] 게임엔진활용 2026. 3. 30. 01:58
    728x90

    1. 더블 버퍼링, 해상도 (RenderView.cs => MainCamera.cs setViewport)

    지난 시간 이어 설명 ...

     

    2. OnGUI

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
        void OnGUI()
        {
            if( Event.current.type != EventType.Repaint )
                return;
     
            RenderTexture texBack = RenderTexture.active;
            RenderTexture.active = mc.rt;
            Matrix4x4 matBack = GUI.matrix;
            GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
     
            // draw something ...
     
            RenderTexture.active = texBack;
            GUI.matrix = matBack;
            GL.Clear(truetrue, Color.black);
            GUI.matrix = Matrix4x4.TRS(
                new Vector3(mc.viewport.x, mc.viewport.y, 0),
                Quaternion.identity,
                new Vector3((float)(Screen.width - mc.viewport.x * 2/ mc.devWidth,
                            (float)(Screen.height - mc.viewport.y * 2/ mc.devHeight, 1.0f));
            drawImage(mc.rt, 000);
     
        }
     
    cs

     

    3. Texture

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
        public const int TOP = 1, BOTTOM = 2, VCENTER = 4,
                            LEFT = 8, RIGHT = 16, HCENTER = 32;
        public const int REVERSE_NONE = 0, REVERSE_WIDTH = 1, REVERSE_HEIGHT = 2;
     
        Texture createImage(string path)
        {
            return Resources.Load<Texture>(path);
        }
     
        void drawImage(Texture tex, float x, float y, int anc = TOP | LEFT )
        {
            drawImage(tex, x, y, 00, tex.width, tex.height, anc, 1.0f, 1.0f, 20, REVERSE_NONE);
        }
     
        void drawImage(Texture tex, float x, float y,
            float tx, float ty, float tw, float th, int anc = TOP | LEFT,
            float rx = 1f, float ry = 1f, int xyz = 2, float degree = 0f, int reverse = REVERSE_NONE)
        {
            int w = (int)(tw * rx);
            int h = (int)(th * ry);
     
            switch(anc)
            {
                case TOP | LEFT:                                 break;
                case TOP | RIGHT:        x -= w;                break;
                case TOP | HCENTER:      x -= w / 2;          break;
                case BOTTOM | LEFT:                  y -= h;  break;
                case BOTTOM | RIGHT:     x -= w;     y -= h;  break;
                case BOTTOM | HCENTER:   x -= w / 2; y -= h;   break;
                case VCENTER | LEFT:                 y -= h / 2break;
                case VCENTER | RIGHT:    x -= w;     y -= h / 2break;
                case VCENTER | HCENTER:  x -= w / 2; y -= h / 2break;
            }
     
            Matrix4x4 matrixPrjection = GUI.matrix;
     
            Matrix4x4 matrixModelview = Matrix4x4.TRS(
                new Vector3(x + w / 2, y + h / 20),
                Quaternion.Euler(xyz == 0 ? degree : 0,
                                xyz == 1 ? degree : 0,
                                xyz == 2 ? degree : 0),
                Vector3.one);
            GUI.matrix = GUI.matrix * matrixModelview;
     
            if ((reverse & REVERSE_WIDTH) == REVERSE_WIDTH)
            {
                Matrix4x4 m0 = Matrix4x4.TRS(
                    new Vector3(000),
                    Quaternion.Euler(01800),
                    new Vector3(111)
                    );
                GUI.matrix = GUI.matrix * m0;
            }
            else if ((reverse & REVERSE_HEIGHT) == REVERSE_HEIGHT)
            {
                Matrix4x4 m1 = Matrix4x4.TRS(
                    new Vector3(000),
                    Quaternion.Euler(18000),
                    new Vector3(111)
                    );
                GUI.matrix = GUI.matrix * m1;
            }
     
            Material m = material[0];
            m.SetColor("_BaseColor", Color.white);
            m.SetTexture("_BaseMap", tex);
     
            Graphics.DrawTexture(
                new Rect(-/ 2-/ 2, w, h),
                tex,
                new Rect(tx / tex.width, ty / tex.height, tw / tex.width, th / tex.height),
                0000, m);
     
            GUI.matrix = matrixPrjection;
        }
     
     
     
    cs

     

     

    알파 채널을 사용하기 위해, Shader에서 Blend를 설정

     

     

    4. Line, Rect

    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
    Texture texDot;
    Color color;
    float lineWidth;
     
    void init()
    {
        Texture2D tex = new Texture2D(11);
        tex.SetPixel(00, Color.white);
        tex.Apply();
        texDot = tex;
    }
     
    public void setRGBA(float r, float g, float b, float a)
    {
        color.r = r;
        color.g = g;
        color.b = b;
        color.a = a;
    }
     
    public void setLineWidth(float width)
    {
        lineWidth = width;
    }
     
    public void drawLine(float sx, float sy, float ex, float ey)
    {
        float cx = (sx + ex) / 2;
        float cy = (sy + ey) / 2;
        float dx = ex - sx;
        float dy = ey - sy;
        float len = Mathf.Sqrt(dx * dx + dy * dy);
     
        float degree = Mathf.Atan2(ey - sy, ex - sx) * Mathf.Rad2Deg;
        drawImage(texDot, cx, cy, 0011, VCENTER | HCENTER,
            len, lineWidth, 2, degree, REVERSE_NONE);
    }
     
    public void drawRect(float x, float y, float width, float height)
    {
        // top & bottom
        drawLine(x, y, x + width, y);
        drawLine(x, y + height - lineWidth, 
                 x + width, y + height - lineWidth);
        // left & right
        drawLine(x, y + lineWidth, x, y + height - lineWidth);
        drawLine(x + width - lineWidth, y + lineWidth, 
                 x + width - lineWidth, y + height - lineWidth);
    }
     
    public void fillRect(float x, float y, float width, float height)
    {
        drawImage(texDot, x, y, 0011, TOP | LEFT,
            width, height, 20, REVERSE_NONE);
    }
    cs

     

    5. String

    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
    60
    61
    62
    63
    64
    65
    66
        string stringName = null;
        float stringSize = 20f;
        Color stringColor = Color.white;
     
        public string getStringName()
        {
            return stringName;
        }
        public void setStringName(string name)
        {
            stringName = name;
        }
     
        public float getStringSize()
        {
            return stringSize;
        }
        public void setStringSize(float size)
        {
            stringSize = size;
        }
     
        public Color getStringRGBA()
        {
            return stringColor;
        }
        public void setStringRGBA(float r, float g, float b, float a)
        {
            stringColor = new Color(r, g, b, a);
        }
     
        public Vector2 sizeOfString(string str)
        {
            GUIStyle style = new GUIStyle(GUI.skin.label);
            style.font = Resources.Load<Font>(stringName);
            style.fontSize = (int)stringSize;
            style.fontStyle = FontStyle.Normal;
            style.normal.textColor = stringColor;
            return style.CalcSize(new GUIContent(str));
        }
     
        public void drawString(string str, float x, float y, int anc = TOP | LEFT)
        {
            GUIStyle style = new GUIStyle(GUI.skin.label);
            style.font = Resources.Load<Font>(stringName);
            style.fontSize = (int)stringSize;
            style.fontStyle = FontStyle.Normal;
            style.normal.textColor = stringColor;
            style.hover.textColor = stringColor;
            Vector2 size = style.CalcSize(new GUIContent(str));
            switch (anc)
            {
                case TOP | LEFT:                                        break;
                case TOP | RIGHT:       x -= size.x;                    break;
                case TOP | HCENTER:     x -= size.x / 2;                break;
                case BOTTOM | LEFT:                     y -= size.y;    break;
                case BOTTOM | RIGHT:    x -= size.x;    y -= size.y;    break;
                case BOTTOM | HCENTER:  x -= size.x / 2;y -= size.y;    break;
                case VCENTER | LEFT:                    y -= size.y / 2;break;
                case VCENTER | RIGHT:   x -= size.x;    y -= size.y / 2;break;
                case VCENTER | HCENTER: x -= size.x / 2;y -= size.y / 2;break;
            }
            GUI.color = stringColor;// #issue
            GUI.Label(new Rect(x, y, size.x, size.y), str, style);
        }
     
    cs

     

     

     

     

Designed by Tistory.