GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — libprocessing/examples/particles_emit_gpu.rs at python-api · processing/libprocessing

Expand file tree

/

particles_emit_gpu.rs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
218 lines (181 loc) · 6.64 KB
/

particles_emit_gpu.rs

Copy path

File metadata and controls

218 lines (181 loc) · 6.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
use processing_glfw::GlfwContext;
use std::time::Instant;
use bevy::math::Vec3;
use processing::prelude::*;
use processing_render::geometry::AttributeFormat;
use processing_render::render::command::DrawCommand;
const SPAWN_SHADER: &str = r#"
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> life: 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;
life[slot] = 1.0;
}
"#;
const MOTION_SHADER: &str = r#"
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> life: 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 life[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 { life[i] = 0.0; }
}
"#;
fn main() {
sketch().unwrap();
exit(0).unwrap();
}
fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(900, 700, false)?;
init(Config::default())?;
let surface = glfw_ctx.create_surface(900, 700, false)?;
let graphics = graphics_create(surface, 900, 700, TextureFormat::Rgba16Float)?;
graphics_mode_3d(graphics)?;
transform_set_position(graphics, Vec3::new(0.0, 4.0, 16.0))?;
transform_look_at(graphics, Vec3::new(0.0, 2.0, 0.0))?;
let _light =
light_create_directional(graphics, bevy::color::Color::srgb(0.95, 0.9, 0.85), 800.0)?;
let particle = geometry_sphere(0.12, 8, 6)?;
let capacity: u32 = 40000;
let position_attr = geometry_attribute_position();
let color_attr = geometry_attribute_color();
let scale_attr = geometry_attribute_scale();
let life_attr = geometry_attribute_life();
let velocity_attr = geometry_attribute_create("velocity", AttributeFormat::Float3)?;
let age_attr = geometry_attribute_create("age", AttributeFormat::Float)?;
let p = particles_create(
capacity,
vec![
position_attr,
color_attr,
scale_attr,
life_attr,
velocity_attr,
age_attr,
],
)?;
let color_buf =
particles_buffer(p, color_attr)?.ok_or(error::ProcessingError::ParticlesNotFound)?;
let mat = {
let m = material_create_pbr()?;
material_set_albedo_buffer(m, color_buf)?;
m
};
let spawn_shader = shader_create(SPAWN_SHADER)?;
let spawn = compute_create(spawn_shader)?;
let motion_shader = shader_create(MOTION_SHADER)?;
let motion = compute_create(motion_shader)?;
let burst: u32 = 120;
let dt: f32 = 1.0 / 60.0;
let ttl: f32 = 2.5;
let gravity: f32 = 9.8;
let speed: f32 = 5.0;
let start = Instant::now();
while glfw_ctx.poll_events() {
graphics_begin_draw(graphics)?;
graphics_record_command(
graphics,
DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.04, 0.04, 0.07)),
)?;
graphics_record_command(graphics, DrawCommand::Material(mat))?;
graphics_record_command(
graphics,
DrawCommand::Particles {
particles: p,
geometry: Some(particle),
topology: Default::default(),
},
)?;
graphics_end_draw(graphics)?;
let t = start.elapsed().as_secs_f32();
let sx = t.cos() * 0.4;
let sz = t.sin() * 0.4;
compute_set(
spawn,
"pos",
shader_value::ShaderValue::Float4([sx, 7.0, sz, 0.0]),
)?;
compute_set(
spawn,
"speed",
shader_value::ShaderValue::Float4([speed, 0.0, 0.0, 0.0]),
)?;
particles_emit_gpu(p, burst, spawn)?;
compute_set(motion, "dt", shader_value::ShaderValue::Float(dt))?;
compute_set(motion, "ttl", shader_value::ShaderValue::Float(ttl))?;
compute_set(motion, "gravity", shader_value::ShaderValue::Float(gravity))?;
particles_apply(p, motion)?;
}
Ok(())
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.