summaryrefslogtreecommitdiff
path: root/shaders/godot3.4/16-6.shader_test
blob: f15523bc681786d751f22bc7d56e1af7f0384b69 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
[require]
GLSL >= 3.30

[fragment shader]
#version 330
#define GLES_OVER_GL
#define USE_FRACTIONAL_DELTA
#define USE_MATERIAL
precision highp float;
precision highp int;

// any code here is never executed, stuff is filled just so it works

#if defined(USE_MATERIAL)

layout(std140) uniform UniformData {
vec3 m_direction;
float m_spread;
float m_flatness;
float m_initial_linear_velocity;
float m_initial_angle;
float m_angular_velocity;
float m_orbit_velocity;
float m_linear_accel;
float m_radial_accel;
float m_tangent_accel;
float m_damping;
float m_scale;
float m_hue_variation;
float m_anim_speed;
float m_anim_offset;
float m_initial_linear_velocity_random;
float m_initial_angle_random;
float m_angular_velocity_random;
float m_orbit_velocity_random;
float m_linear_accel_random;
float m_radial_accel_random;
float m_tangent_accel_random;
float m_damping_random;
float m_scale_random;
float m_hue_variation_random;
float m_anim_speed_random;
float m_anim_offset_random;
float m_lifetime_randomness;
float m_emission_sphere_radius;
vec4 m_color_value;
int m_trail_divisor;
vec3 m_gravity;


};

#endif
uniform sampler2D m_color_ramp;


void main() {

	{


	}

	{


	}
}
/* clang-format on */

[vertex shader]
#version 330
#define GLES_OVER_GL
#define USE_FRACTIONAL_DELTA
#define USE_MATERIAL
precision highp float;
precision highp int;

layout(location = 0) in highp vec4 color;
/* clang-format on */
layout(location = 1) in highp vec4 velocity_active;
layout(location = 2) in highp vec4 custom;
layout(location = 3) in highp vec4 xform_1;
layout(location = 4) in highp vec4 xform_2;
layout(location = 5) in highp vec4 xform_3;

struct Attractor {
	vec3 pos;
	vec3 dir;
	float radius;
	float eat_radius;
	float strength;
	float attenuation;
};

#define MAX_ATTRACTORS 64

uniform bool emitting;
uniform float system_phase;
uniform float prev_system_phase;
uniform int total_particles;
uniform float explosiveness;
uniform float randomness;
uniform float time;
uniform float delta;

uniform int attractor_count;
uniform Attractor attractors[MAX_ATTRACTORS];
uniform bool clear;
uniform uint cycle;
uniform float lifetime;
uniform mat4 emission_transform;
uniform uint random_seed;

out highp vec4 out_color; //tfb:
out highp vec4 out_velocity_active; //tfb:
out highp vec4 out_custom; //tfb:
out highp vec4 out_xform_1; //tfb:
out highp vec4 out_xform_2; //tfb:
out highp vec4 out_xform_3; //tfb:

#if defined(USE_MATERIAL)

/* clang-format off */
layout(std140) uniform UniformData { //ubo:0
vec3 m_direction;
float m_spread;
float m_flatness;
float m_initial_linear_velocity;
float m_initial_angle;
float m_angular_velocity;
float m_orbit_velocity;
float m_linear_accel;
float m_radial_accel;
float m_tangent_accel;
float m_damping;
float m_scale;
float m_hue_variation;
float m_anim_speed;
float m_anim_offset;
float m_initial_linear_velocity_random;
float m_initial_angle_random;
float m_angular_velocity_random;
float m_orbit_velocity_random;
float m_linear_accel_random;
float m_radial_accel_random;
float m_tangent_accel_random;
float m_damping_random;
float m_scale_random;
float m_hue_variation_random;
float m_anim_speed_random;
float m_anim_offset_random;
float m_lifetime_randomness;
float m_emission_sphere_radius;
vec4 m_color_value;
int m_trail_divisor;
vec3 m_gravity;


};
/* clang-format on */

#endif

/* clang-format off */
uniform sampler2D m_color_ramp;

uint m_hash(uint m_x)
	{
		m_x=(((m_x>>16u)^m_x)*73244475u);
		m_x=(((m_x>>16u)^m_x)*73244475u);
		m_x=((m_x>>16u)^m_x);
return m_x;	}

float m_rand_from_seed(inout uint m_seed)
	{
		int m_k;
		int m_s=int(m_seed);
		if ((m_s==0))
		{
			m_s=305420679;
		}
		m_k=(m_s/127773);
		m_s=((16807*(m_s-(m_k*127773)))-(2836*m_k));
		if ((m_s<0))
		{
			m_s+=2147483647;
		}
		m_seed=uint(m_s);
return (float((m_seed%65536u))/65535.0);	}

float m_rand_from_seed_m1_p1(inout uint m_seed)
	{
return ((m_rand_from_seed(m_seed)*2.0)-1.0);	}


/* clang-format on */

uint hash(uint x) {
	x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
	x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b);
	x = (x >> uint(16)) ^ x;
	return x;
}

void main() {
#ifdef PARTICLES_COPY

	out_color = color;
	out_velocity_active = velocity_active;
	out_custom = custom;
	out_xform_1 = xform_1;
	out_xform_2 = xform_2;
	out_xform_3 = xform_3;

#else

	bool apply_forces = true;
	bool apply_velocity = true;
	float local_delta = delta;

	float mass = 1.0;

	float restart_phase = float(gl_VertexID) / float(total_particles);

	if (randomness > 0.0) {
		uint seed = cycle;
		if (restart_phase >= system_phase) {
			seed -= uint(1);
		}
		seed *= uint(total_particles);
		seed += uint(gl_VertexID);
		float random = float(hash(seed) % uint(65536)) / 65536.0;
		restart_phase += randomness * random * 1.0 / float(total_particles);
	}

	restart_phase *= (1.0 - explosiveness);
	bool restart = false;
	bool shader_active = velocity_active.a > 0.5;

	if (system_phase > prev_system_phase) {
		// restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed

		if (restart_phase >= prev_system_phase && restart_phase < system_phase) {
			restart = true;
#ifdef USE_FRACTIONAL_DELTA
			local_delta = (system_phase - restart_phase) * lifetime;
#endif
		}

	} else if (delta > 0.0) {
		if (restart_phase >= prev_system_phase) {
			restart = true;
#ifdef USE_FRACTIONAL_DELTA
			local_delta = (1.0 - restart_phase + system_phase) * lifetime;
#endif
		} else if (restart_phase < system_phase) {
			restart = true;
#ifdef USE_FRACTIONAL_DELTA
			local_delta = (system_phase - restart_phase) * lifetime;
#endif
		}
	}

	uint current_cycle = cycle;

	if (system_phase < restart_phase) {
		current_cycle -= uint(1);
	}

	uint particle_number = current_cycle * uint(total_particles) + uint(gl_VertexID);
	int index = int(gl_VertexID);

	if (restart) {
		shader_active = emitting;
	}

	mat4 xform;

#if defined(ENABLE_KEEP_DATA)
	if (clear) {
#else
	if (clear || restart) {
#endif
		out_color = vec4(1.0);
		out_velocity_active = vec4(0.0);
		out_custom = vec4(0.0);
		if (!restart)
			shader_active = false;

		xform = mat4(
				vec4(1.0, 0.0, 0.0, 0.0),
				vec4(0.0, 1.0, 0.0, 0.0),
				vec4(0.0, 0.0, 1.0, 0.0),
				vec4(0.0, 0.0, 0.0, 1.0));
	} else {
		out_color = color;
		out_velocity_active = velocity_active;
		out_custom = custom;
		xform = transpose(mat4(xform_1, xform_2, xform_3, vec4(vec3(0.0), 1.0)));
	}

	if (shader_active) {
		//execute shader

		{
			/* clang-format off */
	{
		uint m_base_number=(particle_number/uint(m_trail_divisor));
		uint m_alt_seed=m_hash(((m_base_number+1u)+random_seed));
		float m_angle_rand=m_rand_from_seed(m_alt_seed);
		float m_scale_rand=m_rand_from_seed(m_alt_seed);
		float m_hue_rot_rand=m_rand_from_seed(m_alt_seed);
		float m_anim_offset_rand=m_rand_from_seed(m_alt_seed);
		float m_pi=3.14159;
		float m_degree_to_rad=(m_pi/180.0);
		bool m_restart=false;
		float m_tv=0.0;
		if ((out_custom.y>out_custom.w))
		{
					{
			m_restart=true;
			m_tv=1.0;
		}
;
		}
		if ((restart||m_restart))
		{
					{
			uint m_alt_restart_seed=m_hash(((m_base_number+301184u)+random_seed));
			float m_tex_linear_velocity=0.0;
			float m_tex_angle=0.0;
			float m_tex_anim_offset=0.0;
			float m_spread_rad=(m_spread*m_degree_to_rad);
					{
			float m_angle1_rad=(m_rand_from_seed_m1_p1(m_alt_restart_seed)*m_spread_rad);
			m_angle1_rad+=((m_direction.x!=0.0)?atan(m_direction.y, m_direction.x):(sign(m_direction.y)*(m_pi/2.0)));
			vec3 m_rot=vec3(cos(m_angle1_rad), sin(m_angle1_rad), 0.0);
			out_velocity_active.xyz=((m_rot*m_initial_linear_velocity)*mix(1.0, m_rand_from_seed(m_alt_restart_seed), m_initial_linear_velocity_random));
		}
;
			float m_base_angle=((m_initial_angle+m_tex_angle)*mix(1.0, m_angle_rand, m_initial_angle_random));
			out_custom.x=(m_base_angle*m_degree_to_rad);
			out_custom.y=0.0;
			out_custom.w=(1.0-(m_lifetime_randomness*m_rand_from_seed(m_alt_restart_seed)));
			out_custom.z=((m_anim_offset+m_tex_anim_offset)*mix(1.0, m_anim_offset_rand, m_anim_offset_random));
			float m_s=((m_rand_from_seed(m_alt_restart_seed)*2.0)-1.0);
			float m_t=((m_rand_from_seed(m_alt_restart_seed)*2.0)*m_pi);
			float m_radius=(m_emission_sphere_radius*sqrt((1.0-(m_s*m_s))));
			xform[3].xyz=vec3((m_radius*cos(m_t)), (m_radius*sin(m_t)), (m_emission_sphere_radius*m_s));
			out_velocity_active.xyz=(emission_transform*vec4(out_velocity_active.xyz, 0.0)).xyz;
			xform=(emission_transform*xform);
			out_velocity_active.xyz.z=0.0;
			xform[3].z=0.0;
		}
;
		}
		else
		{
					{
			out_custom.y+=(local_delta/lifetime);
			m_tv=(out_custom.y/out_custom.w);
			float m_tex_linear_velocity=0.0;
			float m_tex_orbit_velocity=0.0;
			float m_tex_angular_velocity=0.0;
			float m_tex_linear_accel=0.0;
			float m_tex_radial_accel=0.0;
			float m_tex_tangent_accel=0.0;
			float m_tex_damping=0.0;
			float m_tex_angle=0.0;
			float m_tex_anim_speed=0.0;
			float m_tex_anim_offset=0.0;
			vec3 m_force=m_gravity;
			vec3 m_pos=xform[3].xyz;
			m_pos.z=0.0;
			m_force+=((length(out_velocity_active.xyz)>0.0)?((normalize(out_velocity_active.xyz)*(m_linear_accel+m_tex_linear_accel))*mix(1.0, m_rand_from_seed(m_alt_seed), m_linear_accel_random)):vec3(0.0,0.0,0.0));
			vec3 m_org=emission_transform[3].xyz;
			vec3 m_diff=(m_pos-m_org);
			m_force+=((length(m_diff)>0.0)?((normalize(m_diff)*(m_radial_accel+m_tex_radial_accel))*mix(1.0, m_rand_from_seed(m_alt_seed), m_radial_accel_random)):vec3(0.0,0.0,0.0));
			m_force+=((length(m_diff.yx)>0.0)?(vec3(normalize((m_diff.yx*vec2(-1.0,1.0))), 0.0)*((m_tangent_accel+m_tex_tangent_accel)*mix(1.0, m_rand_from_seed(m_alt_seed), m_tangent_accel_random))):vec3(0.0,0.0,0.0));
			out_velocity_active.xyz+=(m_force*local_delta);
			float m_orbit_amount=((m_orbit_velocity+m_tex_orbit_velocity)*mix(1.0, m_rand_from_seed(m_alt_seed), m_orbit_velocity_random));
			if ((m_orbit_amount!=0.0))
			{
							{
				float m_ang=(((m_orbit_amount*local_delta)*m_pi)*2.0);
				mat2 m_rot=mat2(vec2(cos(m_ang), -sin(m_ang)), vec2(sin(m_ang), cos(m_ang)));
				xform[3].xy-=m_diff.xy;
				xform[3].xy+=(m_rot*m_diff.xy);
			}
;
			}
			if (((m_damping+m_tex_damping)>0.0))
			{
							{
				float m_v=length(out_velocity_active.xyz);
				float m_damp=((m_damping+m_tex_damping)*mix(1.0, m_rand_from_seed(m_alt_seed), m_damping_random));
				m_v-=(m_damp*local_delta);
				if ((m_v<0.0))
				{
									{
					out_velocity_active.xyz=vec3(0.0,0.0,0.0);
				}
;
				}
				else
				{
									{
					out_velocity_active.xyz=(normalize(out_velocity_active.xyz)*m_v);
				}
;
				}
			}
;
			}
			float m_base_angle=((m_initial_angle+m_tex_angle)*mix(1.0, m_angle_rand, m_initial_angle_random));
			m_base_angle+=(((out_custom.y*lifetime)*(m_angular_velocity+m_tex_angular_velocity))*mix(1.0, ((m_rand_from_seed(m_alt_seed)*2.0)-1.0), m_angular_velocity_random));
			out_custom.x=(m_base_angle*m_degree_to_rad);
			out_custom.z=(((m_anim_offset+m_tex_anim_offset)*mix(1.0, m_anim_offset_rand, m_anim_offset_random))+((out_custom.y*(m_anim_speed+m_tex_anim_speed))*mix(1.0, m_rand_from_seed(m_alt_seed), m_anim_speed_random)));
		}
;
		}
		float m_tex_scale=1.0;
		float m_tex_hue_variation=0.0;
		float m_hue_rot_angle=((((m_hue_variation+m_tex_hue_variation)*m_pi)*2.0)*mix(1.0, ((m_hue_rot_rand*2.0)-1.0), m_hue_variation_random));
		float m_hue_rot_c=cos(m_hue_rot_angle);
		float m_hue_rot_s=sin(m_hue_rot_angle);
		mat4 m_hue_rot_mat=((mat4(0.299,0.587,0.114,0.0,0.299,0.587,0.114,0.0,0.299,0.587,0.114,0.0,0.0,0.0,0.0,1.0)+(mat4(0.701,-0.587,-0.114,0.0,-0.299,0.413,-0.114,0.0,-0.3,-0.588,0.886,0.0,0.0,0.0,0.0,0.0)*m_hue_rot_c))+(mat4(0.168,0.33,-0.497,0.0,-0.328,0.035,0.292,0.0,1.25,-1.05,-0.203,0.0,0.0,0.0,0.0,0.0)*m_hue_rot_s));
		out_color=((m_hue_rot_mat*textureLod(m_color_ramp, vec2(m_tv, 0.0), 0.0))*m_color_value);
		xform[0]=vec4(cos(out_custom.x), -sin(out_custom.x), 0.0, 0.0);
		xform[1]=vec4(sin(out_custom.x), cos(out_custom.x), 0.0, 0.0);
		xform[2]=vec4(0.0,0.0,1.0,0.0);
		float m_base_scale=(m_tex_scale*mix(m_scale, 1.0, (m_scale_random*m_scale_rand)));
		if ((m_base_scale<1e-06))
		{
					{
			m_base_scale=1e-06;
		}
;
		}
		xform[0].xyz*=m_base_scale;
		xform[1].xyz*=m_base_scale;
		xform[2].xyz*=m_base_scale;
		out_velocity_active.xyz.z=0.0;
		xform[3].z=0.0;
		if ((out_custom.y>out_custom.w))
		{
					{
			shader_active=false;
		}
;
		}
	}


			/* clang-format on */
		}

#if !defined(DISABLE_FORCE)

		if (false) {
			vec3 force = vec3(0.0);
			for (int i = 0; i < attractor_count; i++) {
				vec3 rel_vec = xform[3].xyz - attractors[i].pos;
				float dist = length(rel_vec);
				if (attractors[i].radius < dist)
					continue;
				if (attractors[i].eat_radius > 0.0 && attractors[i].eat_radius > dist) {
					out_velocity_active.a = 0.0;
				}

				rel_vec = normalize(rel_vec);

				float attenuation = pow(dist / attractors[i].radius, attractors[i].attenuation);

				if (attractors[i].dir == vec3(0.0)) {
					//towards center
					force += attractors[i].strength * rel_vec * attenuation * mass;
				} else {
					force += attractors[i].strength * attractors[i].dir * attenuation * mass;
				}
			}

			out_velocity_active.xyz += force * local_delta;
		}
#endif

#if !defined(DISABLE_VELOCITY)

		if (true) {
			xform[3].xyz += out_velocity_active.xyz * local_delta;
		}
#endif
	} else {
		xform = mat4(0.0);
	}

	xform = transpose(xform);

	out_velocity_active.a = mix(0.0, 1.0, shader_active);

	out_xform_1 = xform[0];
	out_xform_2 = xform[1];
	out_xform_3 = xform[2];

#endif //PARTICLES_COPY
}

/* clang-format off */