-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparticles_emit_gpu.py
More file actions
180 lines (141 loc) · 4.72 KB
/
particles_emit_gpu.py
File metadata and controls
180 lines (141 loc) · 4.72 KB
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
from mewnala import *
import math
p = None
particle = None
mat = None
spawn = None
motion = None
CAPACITY = 40000
BURST = 120
DT = 1.0 / 60.0
TTL = 2.5
GRAVITY = 9.8
SPEED = 5.0
SPAWN_SHADER = """
struct Spawn {
pos: vec4<f32>,
speed: vec4<f32>,
}
@group(0) @binding(0) var<storage, read_write> position: array<f32>;
@group(0) @binding(1) var<storage, read_write> velocity: array<f32>;
@group(0) @binding(2) var<storage, read_write> color: array<f32>;
@group(0) @binding(3) var<storage, read_write> scale: array<f32>;
@group(0) @binding(4) var<storage, read_write> age: array<f32>;
@group(0) @binding(5) var<storage, read_write> dead: array<f32>;
@group(0) @binding(6) var<uniform> spawn: Spawn;
@group(0) @binding(7) var<uniform> emit_range: vec4<f32>;
fn hash(n: u32) -> u32 {
var x = n;
x = (x ^ 61u) ^ (x >> 16u);
x = x + (x << 3u);
x = x ^ (x >> 4u);
x = x * 0x27d4eb2du;
x = x ^ (x >> 15u);
return x;
}
fn hash_unit(n: u32) -> f32 {
return f32(hash(n)) / f32(0xffffffffu);
}
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let local_i = gid.x;
if local_i >= u32(emit_range.y) { return; }
let base = u32(emit_range.x);
let cap = u32(emit_range.z);
let slot = (base + local_i) % cap;
let seed = base + local_i;
let theta = hash_unit(seed) * 6.2831853;
let r = sqrt(hash_unit(seed * 2u + 1u));
let dirxz = vec2<f32>(cos(theta), sin(theta)) * r;
let dy = 0.7 + 0.3 * hash_unit(seed * 3u + 7u);
let v = vec3<f32>(dirxz.x, dy, dirxz.y) * spawn.speed.x;
position[slot * 3u + 0u] = spawn.pos.x;
position[slot * 3u + 1u] = spawn.pos.y;
position[slot * 3u + 2u] = spawn.pos.z;
velocity[slot * 3u + 0u] = v.x;
velocity[slot * 3u + 1u] = v.y;
velocity[slot * 3u + 2u] = v.z;
let h = fract(hash_unit(seed * 5u + 11u));
color[slot * 4u + 0u] = 0.5 + 0.5 * sin(h * 6.28);
color[slot * 4u + 1u] = 0.5 + 0.5 * sin(h * 6.28 + 2.094);
color[slot * 4u + 2u] = 0.5 + 0.5 * sin(h * 6.28 + 4.189);
color[slot * 4u + 3u] = 1.0;
scale[slot * 3u + 0u] = 1.0;
scale[slot * 3u + 1u] = 1.0;
scale[slot * 3u + 2u] = 1.0;
age[slot] = 0.0;
dead[slot] = 0.0;
}
"""
MOTION_SHADER = """
struct Params {
dt: f32,
ttl: f32,
gravity: f32,
_pad: f32,
}
@group(0) @binding(0) var<storage, read_write> position: array<f32>;
@group(0) @binding(1) var<storage, read_write> velocity: array<f32>;
@group(0) @binding(2) var<storage, read_write> scale: array<f32>;
@group(0) @binding(3) var<storage, read_write> age: array<f32>;
@group(0) @binding(4) var<storage, read_write> dead: array<f32>;
@group(0) @binding(5) var<uniform> params: Params;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let i = gid.x;
let count = arrayLength(&age);
if i >= count { return; }
if dead[i] != 0.0 { return; }
age[i] = age[i] + params.dt;
velocity[i * 3u + 1u] = velocity[i * 3u + 1u] - params.gravity * params.dt;
position[i * 3u + 0u] = position[i * 3u + 0u] + velocity[i * 3u + 0u] * params.dt;
position[i * 3u + 1u] = position[i * 3u + 1u] + velocity[i * 3u + 1u] * params.dt;
position[i * 3u + 2u] = position[i * 3u + 2u] + velocity[i * 3u + 2u] * params.dt;
let life = clamp(1.0 - age[i] / params.ttl, 0.0, 1.0);
let s = life * life;
scale[i * 3u + 0u] = s;
scale[i * 3u + 1u] = s;
scale[i * 3u + 2u] = s;
if age[i] > params.ttl { dead[i] = 1.0; }
}
"""
def setup():
global p, particle, mat, spawn, motion
size(900, 700)
mode_3d()
directional_light((0.95, 0.9, 0.85), 800.0)
particle = Geometry.sphere(0.12, 8, 6)
velocity_attr = Attribute("velocity", AttributeFormat.Float3)
age_attr = Attribute("age", AttributeFormat.Float)
p = Particles(
capacity=CAPACITY,
attributes=[
Attribute.position(),
Attribute.color(),
Attribute.scale(),
Attribute.dead(),
velocity_attr,
age_attr,
],
)
# park unemitted slots until the spawn kernel fills them.
dead_buf = p.buffer(Attribute.dead())
dead_buf.write([1.0] * CAPACITY)
color_buf = p.buffer(Attribute.color())
mat = Material.pbr(albedo=color_buf)
spawn = Compute(Shader(SPAWN_SHADER))
motion = Compute(Shader(MOTION_SHADER))
def draw():
camera_position(0.0, 4.0, 16.0)
camera_look_at(0.0, 2.0, 0.0)
background(10, 10, 18)
use_material(mat)
particles(p, particle)
t = elapsed_time
sx = math.cos(t) * 0.4
sz = math.sin(t) * 0.4
spawn.set(pos=[sx, 7.0, sz, 0.0], speed=[SPEED, 0.0, 0.0, 0.0])
p.emit_gpu(BURST, spawn)
motion.set(dt=DT, ttl=TTL, gravity=GRAVITY)
p.apply(motion)
run()