Repository navigation
Expand file tree
/
Copy pathparticles_scatter_volume.rs
More file actions
118 lines (96 loc) · 3.54 KB
/
Copy pathparticles_scatter_volume.rs
File metadata and controls
118 lines (96 loc) · 3.54 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
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 AGE_SHADER: &str = r#"
struct Params { dt: f32, ttl: f32, _pad0: f32, _pad1: f32 }
@group(0) @binding(0) var<storage, read_write> scale: array<f32>;
@group(0) @binding(1) var<storage, read_write> age: array<f32>;
@group(0) @binding(2) var<storage, read_write> life: array<f32>;
@group(0) @binding(3) 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;
let t = age[i] / params.ttl;
let rise = clamp(t / 0.05, 0.0, 1.0);
let fall = clamp((1.0 - t) / 0.6, 0.0, 1.0);
let s = rise * fall;
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(600, 400, 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, 100.0, 400.0))?;
transform_look_at(graphics, Vec3::new(0.0, 80.0, 0.0))?;
graphics_orbit_camera(graphics)?;
let gltf = gltf_load(graphics, "gltf/Duck.glb")?;
let duck = gltf_geometry(gltf, "LOD3spShape")?;
let scatter = particles_scatter_volume_create(duck)?;
let particle = geometry_sphere(0.15, 4, 3)?;
let capacity: u32 = 30_000;
let position_attr = geometry_attribute_position();
let scale_attr = geometry_attribute_scale();
let life_attr = geometry_attribute_life();
let age_attr = geometry_attribute_create("age", AttributeFormat::Float)?;
let p = particles_create(
capacity,
vec![position_attr, scale_attr, life_attr, age_attr],
)?;
let age_shader = shader_create(AGE_SHADER)?;
let aging = compute_create(age_shader)?;
let mat = material_create_unlit()?;
material_set(
mat,
"color",
shader_value::ShaderValue::Float4([1.0, 1.0, 1.0, 1.0]),
)?;
let burst: u32 = 250;
let dt: f32 = 1.0 / 60.0;
let ttl: 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.03, 0.03, 0.05)),
)?;
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();
compute_set(
scatter,
"seed",
shader_value::ShaderValue::UInt((t * 1000.0) as u32 ^ 0xc0ffeeu32),
)?;
particles_emit_gpu(p, burst, scatter)?;
compute_set(aging, "dt", shader_value::ShaderValue::Float(dt))?;
compute_set(aging, "ttl", shader_value::ShaderValue::Float(ttl))?;
particles_apply(p, aging)?;
}
Ok(())
}