Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/data_extern/programs/GLSL150/ParticleGS_GenerateGS.glsl @ 12232

Last change on this file since 12232 was 12115, checked in by wiesep, 6 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 7.0 KB
Line 
1#version 150
2
3//
4// Explanation of different particle types.
5//
6// Firework Launcher - launches a PT_SHELL every so many seconds.
7#define PT_LAUNCHER 0
8// Unexploded shell - flies from the origin and explodes into many PT_EMBERX's.
9#define PT_SHELL    1
10// Basic particle - after it's emitted from the shell, it dies.
11#define PT_EMBER1   2
12// After it's emitted, it explodes again into many PT_EMBER1's.
13#define PT_EMBER2   3
14// Just a different colored ember1.
15#define PT_EMBER3   4
16#define P_SHELLLIFE 3.0
17#define P_EMBER1LIFE 2.5
18#define P_EMBER2LIFE 1.5
19#define P_EMBER3LIFE 2.0
20
21// These two were originally shader params, but they caused runtime errors.
22#define NUM_EMBER_1S 30
23#define NUM_EMBER_2S 15
24#define NUM_EMBER_3S 10
25// This one was originally a variant, but also causes runtime errors.
26//#define MAX_EMBER_2S 15.0
27
28layout(points) in;
29layout(points, max_vertices = 60) out;
30
31in block {
32    vec3 Pos;
33    float Timer;
34    float Type;
35    vec3 Vel;
36} FireworkData[];
37
38// out vec3 outputPos;
39// out float outputTimer;
40// out float outputType;
41// out vec3 outputVel;
42
43// out vec3 gl_Position;
44out vec3 oPos;
45out float oUv0; // Timer
46out float oUv1; // Type
47out vec3 oUv2;  // Velocity
48
49uniform sampler1D randomTexture;
50uniform vec3 frameGravity;
51uniform float globalTime;
52uniform float elapsedTime;
53uniform float secondsPerFirework;
54
55//
56// Generic particle motion handler
57//
58void GSGenericHandler( vec3 Pos, vec3 Vel, float Timer, float Type,
59                       float elapsedTime,
60                       vec3 frameGravity)
61{
62    Vel += frameGravity;
63    Timer -= elapsedTime;
64
65    if (Pos.y > -100)
66    {
67        oPos = Pos + (Vel * elapsedTime);
68        oUv0 = Timer;
69        oUv1 = Type;
70        oUv2 = Vel;
71        EmitVertex();
72    }
73}
74
75//
76// Sample a random direction from our random texture
77//
78vec3 RandomDir(float fOffset, float globalTime, sampler1D randomTex)
79{
80    float tCoord = (globalTime + fOffset) / 300.0;
81    return texture(randomTex, tCoord).rgb;
82}
83
84//
85// Launcher type particle handler
86//
87void GSLauncherHandler( vec3 Pos, vec3 Vel, float Timer, float Type,
88                        float elapsedTime,
89                        float globalTime,
90                        sampler1D randomTex,
91                        float secondsPerFirework)
92{
93    if (Timer <= 0)
94    {
95        vec3 vRandom = normalize(RandomDir(Type, globalTime, randomTex));
96        // Give it more of an up bias.
97        vRandom = normalize(vRandom + vec3(0, 2.5, 0));
98
99        // Time to emit a new SHELL.
100        oPos = Pos + Vel * elapsedTime;
101        oUv2 = Vel + vRandom * 35.0;
102        oUv0 = P_SHELLLIFE + vRandom.y * 0.5;
103        oUv1 = PT_SHELL;
104        EmitVertex();
105
106        // Reset our timer.
107        Timer = secondsPerFirework + vRandom.x * 0.4;
108    }
109    else
110    {
111        Timer -= elapsedTime;
112    }
113
114    // Emit ourselves to keep us alive.
115    oPos = Pos;
116    oUv2 = Vel;
117    oUv0 = Timer;
118    oUv1 = Type;
119    EmitVertex();
120}
121
122//
123// Shell type particle handler
124//
125void GSShellHandler( vec3 Pos, vec3 Vel, float Timer, float Type,
126                     float elapsedTime,
127                     float globalTime,
128                     sampler1D randomTex,
129                     vec3 frameGravity)
130{
131    if (Timer <= 0)
132    {
133        vec3 vRandom = vec3(0,0,0);
134
135        // Time to emit a series of new Ember1s.
136        for (int i = 0; i < NUM_EMBER_1S; i++)
137        {
138            vRandom = normalize(RandomDir(Type + i, globalTime, randomTex));
139            oPos = Pos + Vel * elapsedTime;
140            oUv2 = Vel + vRandom * 15.0;
141            oUv0 = P_EMBER1LIFE;
142            oUv1 = PT_EMBER1;
143            EmitVertex();
144        }
145
146        // Find out how many Ember2s to emit.
147        // Not doing this because it causes a runtime error.
148        //int numEmber2s = abs(vRandom.x)*MAX_EMBER_2S;
149        for (int i = 0; i < NUM_EMBER_2S; i++)
150        {
151            vRandom = normalize(RandomDir(Type, globalTime, randomTex));
152            oPos = Pos + Vel * elapsedTime;
153            oUv2 = Vel + vRandom * 10.0;
154            oUv0 = P_EMBER2LIFE + 0.4 * vRandom.x;
155            oUv1 = PT_EMBER2;
156            EmitVertex();
157        }
158
159    }
160    else
161    {
162        GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity);
163    }
164}
165
166//
167// Ember1 and Ember3 type particle handler.
168//
169void GSEmber1Handler( vec3 Pos, vec3 Vel, float Timer, float Type,
170                      float elapsedTime,
171                      vec3 frameGravity)
172{
173    if (Timer > 0)
174    {
175        GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity);
176    }
177}
178
179//
180// Ember2 type particle handler.
181//
182void GSEmber2Handler( vec3 Pos, vec3 Vel, float Timer, float Type,
183                      float elapsedTime,
184                      float globalTime,
185                      sampler1D randomTex,
186                      vec3 frameGravity)
187{ 
188    if (Timer <= 0)
189    {
190        // Time to emit a series of new Ember3's.
191        for (int i = 0; i < NUM_EMBER_3S; i++)
192        {
193            oPos = Pos + Vel * elapsedTime;
194            oUv2 = Vel + normalize(RandomDir(Type + i, globalTime, randomTex)) * 10.0;
195            oUv0 = P_EMBER3LIFE;
196            oUv1 = PT_EMBER3;
197            EmitVertex();
198        }
199    }
200    else
201    {
202        GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity);
203    }
204}
205
206void main()
207{
208    if (FireworkData[0].Type == PT_LAUNCHER)
209        GSLauncherHandler(FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type,
210                           elapsedTime, globalTime, randomTexture, secondsPerFirework);
211    else if (FireworkData[0].Type == PT_SHELL)
212        GSShellHandler(FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type,
213                        elapsedTime, globalTime, randomTexture, frameGravity);
214    else if (FireworkData[0].Type == PT_EMBER1 ||
215             FireworkData[0].Type == PT_EMBER3)
216        GSEmber1Handler(FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type,
217                         elapsedTime, frameGravity);
218    else if (FireworkData[0].Type == PT_EMBER2)
219        GSEmber2Handler(FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type,
220                         elapsedTime, globalTime, randomTexture, frameGravity);
221    EndPrimitive();
222
223    // // gl_Position.xyz = FireworkData[0].Pos;// + FireworkData[0].Vel * elapsedTime;
224    // // gl_Position.w = 1;
225    // gl_Position = FireworkData[0].Pos + 1;// + FireworkData[0].Vel * elapsedTime;
226    // // gl_Position = Pos[0];// + FireworkData[0].Vel * elapsedTime;
227    // //gl_Position = vec4(10, 10, 10, 1);
228    // // oUv0 = P_SHELLLIFE + 0.5;
229    // oUv0 = FireworkData[0].Timer;
230    // // oUv0 = Timer[0];
231    // oUv1 = FireworkData[0].Type;
232    // // oUv1 = Type[0];
233    // oUv2 = FireworkData[0].Vel;
234    // // oUv2 = Vel[0];
235    // //oUv2 = vec3(4, 5, 35.0);
236    // EmitVertex();
237    // EndPrimitive();
238    // // // gl_Position = vec4(10, 20, 30, 1);
239    // gl_Position = vec3(10, 20, 30);
240    // oUv0 = P_EMBER3LIFE;
241    // oUv1 = PT_SHELL;
242    // oUv2 = vec3(40, 50, 60);
243    // EmitVertex();
244    // EndPrimitive();
245}
Note: See TracBrowser for help on using the repository browser.