Skip to content

Commit

Permalink
Add gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenhuyn committed Apr 30, 2023
1 parent d96cd23 commit 7879f78
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
// "rust-analyzer.cargo.target": "wasm32-unknown-unknown",
}
2 changes: 1 addition & 1 deletion cubeway/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 3D Conway Implementation - WASM Compatible

```bash
wasm-pack build --target web --out-dir app/pkg
wasm-pack build --target web
```

Add to vscode workspace json settings
Expand Down
14 changes: 12 additions & 2 deletions cubeway/src/compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ struct SimParams {
struct Instance {
position: vec3<f32>,
unused: f32,
velocity: vec3<f32>,
unused2: f32,
rotation: vec4<f32>,
}

Expand All @@ -18,8 +20,16 @@ struct Instances {
fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
var index = GlobalInvocationID.x;

var force: vec3<f32> = vec3<f32>(0.0, 0.0, 0.0);
for (var i = 0u; i < arrayLength(&instanceBuffer); i++) {
if i == index {
continue;
}

if index < 100u {
instanceBuffer[index].position.y -= 0.001;
var diff = instanceBuffer[i].position - instanceBuffer[index].position;
force += normalize(diff) * params.gravity / length(diff);
}

instanceBuffer[index].velocity += force;
instanceBuffer[index].position += instanceBuffer[index].velocity;
}
43 changes: 30 additions & 13 deletions cubeway/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{iter, mem};
use std::{f32::consts::E, iter, mem};

use camera::{Camera, CameraController, CameraUniform};
use cgmath::prelude::*;
Expand Down Expand Up @@ -58,6 +58,8 @@ impl Vertex {
struct Instance {
position: [f32; 3],
_pad: f32,
velocity: [f32; 3],
_pad2: f32,
rotation: [f32; 4],
}

Expand All @@ -76,8 +78,13 @@ impl Instance {
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: 16 as wgpu::BufferAddress,
offset: 16,
shader_location: 3,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: 32 as wgpu::BufferAddress,
shader_location: 4,
format: wgpu::VertexFormat::Float32x4,
},
],
Expand Down Expand Up @@ -130,29 +137,34 @@ impl State {

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();

println!("{:?}", adapter.get_info());
log::warn!("{:?}", adapter.get_info());
log::warn!("{:?}", adapter.get_downlevel_capabilities());

log::warn!("Getting device");

let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
// WebGL doesn't support all of wgpu's features, so if
// we're building for the web we'll have to disable some.
limits: if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_webgl2_defaults()
} else {
wgpu::Limits::default()
limits: wgpu::Limits {
..wgpu::Limits::downlevel_defaults()
},
},
None, // Trace path
)
.await
.unwrap();
.expect("Device unable to get requested features");

log::warn!("Device Gotten");

let surface_caps = surface.get_capabilities(&adapter);
// Shader code in this tutorial assumes an Srgb surface texture. Using a different
Expand All @@ -164,6 +176,7 @@ impl State {
.copied()
.find(|f| f.is_srgb())
.unwrap_or(surface_caps.formats[0]);

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
Expand All @@ -173,6 +186,7 @@ impl State {
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![],
};

surface.configure(&device, &config);

let camera = Camera::new(config.width as f32, config.height as f32);
Expand Down Expand Up @@ -201,6 +215,8 @@ impl State {
Instance {
position: position.into(),
_pad: 0.0,
velocity: [0.0, 0.0, 0.0],
_pad2: 0.0,
rotation: [1., 0., 0., 0.],
}
})
Expand Down Expand Up @@ -474,11 +490,12 @@ impl State {
// Run Compute Pass
encoder.push_debug_group("Particle System");
{
let mut cpass =
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { label: None });
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Compute Pass"),
});
cpass.set_pipeline(&self.compute_pipeline);
cpass.set_bind_group(0, &self.particle_bind_groups[0], &[]);
cpass.dispatch_workgroups(2, 1, 1);
cpass.dispatch_workgroups(65535, 1, 1);
}
encoder.pop_debug_group();

Expand Down
3 changes: 2 additions & 1 deletion cubeway/src/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ struct VertexInput {
}
struct InstanceInput {
@location(2) model_position: vec3<f32>,
@location(3) model_rotation: vec4<f32>
@location(3) model_velocity: vec3<f32>,
@location(4) model_rotation: vec4<f32>
}

struct VertexOutput {
Expand Down

0 comments on commit 7879f78

Please sign in to comment.