ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [게임엔진활용] 10강 파티클 시스템
    [DSU] 게임엔진활용 2026. 6. 8. 01:16
    728x90

    파티클 시스템 제작

     

    브랜딩 방식 변경 Src : SrcAlpha Dst : One

     

    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    using System;
    using UnityEngine;
     
    public class GamePS : RenderView
    {
        Texture texPS;
        Vector2 position;
        float degree;
     
        class PS
        {
            public PS()
            {
                life = 0f;
            }
            public float life, _life;
            public Vector2 p, v;
            public Color cs, ce;
            public float ss, se;
        }
        PS[] _ps;// 활성화 + 비활성화 가리지 모든 입자
        PS[] ps;// 활성화 입자
        int psNum;
     
        float psDelta;
     
        public override void load()
        {
            materialIndex = 5;
     
            Texture2D tex = new Texture2D(3232);
            for (int i = 0; i < 32; i++)
            {
                for (int j = 0; j < 32; j++)
                {
                    float d = (float)Math.Sqrt((16 - i) * (16 - i) + (16 - j) * (16 - j)) / 16;
                    if (d > 1)
                        d = 1;
                    float a = 1 - d;
                    //a *= a;
                    a = (float)Math.Sin(a * 90 * Math.PI / 180);
                    tex.SetPixel(i, j, new Color(111, a));
                }
            }
            tex.Apply();
            texPS = tex;
     
            _ps = new PS[200];
            for (int i = 0; i < 200; i++)
                _ps[i] = new PS();
            ps = new PS[200];
            psNum = 0;
        }
     
        public override void free()
        {
        }
     
        public override void draw(float dt)
        {
            setRGBA(0000.7f);
            fillRect(00, mc.devWidth, mc.devHeight);
            setRGBA(1111);
     
            materialIndex = 5;
            //drawImage(texPS, 300, 0, 0, 0, texPS.width, texPS.height, TOP | LEFT, 10, 10);
            for (int i = 0; i < psNum; i++)
            {
                PS p = ps[i];
                float r = p.life / p._life;
                p.p += p.v * dt;
                Color c = p.cs * (1 - r) + p.ce * r;
                float s = p.ss * (1 - r) + p.se * r;
                setRGBA(c.r, c.g, c.b, c.a);
                drawImage(texPS, p.p.x, p.p.y,
                    00, texPS.width, texPS.height, TOP | LEFT, s, s);
     
                p.life += dt;
                if (p.life > p._life)
                {
                    p.life = 0;
                    psNum--;
                    ps[i] = ps[psNum];
                    i--;
                }
            }
            setRGBA(1111);
     
            psDelta += dt;
            while (psDelta > 0.015f)
            {
                psDelta -= 0.015f;
     
                createPS((int)position.x, (int)position.y);
            }
     
            materialIndex = 0;
        }
     
        void createPS(int x, int y)
        {
            PS p = null;
            for (int i = 0; i < _ps.Length; i++)
            {
                p = _ps[i];
                if (p.life == 0)
                    break;
            }
            if (p == null)
                return;
     
            p._life = 0.5f + 0.5f * (getRandom() % 100/ 99//0.5 ~1.0f
            p.life = 0.000001f;
            p.p = new Vector2(
                (int)(x - 30 + 60f * (getRandom() % 100/ 99),
                (int)(y - 30 + 60f * (getRandom() % 100/ 99));
            p.v = new Vector2(-30 + 60 * (getRandom() % 100/ 99-160);
            p.cs = new Color(1001);
            p.ce = new Color(1100.5f * (getRandom() % 100/ 99);
            p.ss = 2 + 1.0f * (getRandom() % 100/ 99;// 2~3
            p.se = 0.1f + 0.4f * (getRandom() % 100/ 99;// 0.1~0.5
     
            ps[psNum] = p;
            psNum++;
        }
     
        Vector2 prevPosition;
        public override void mouse(int state, float x, float y)
        {
            if (state == 0)
            {
                prevPosition = position;
                position = new Vector2(x, y);
                degree = (float)Math.Atan2(prevPosition.y - position.y, prevPosition.x - position.x);
            }
        }
    }
    cs

     

     

Designed by Tistory.