get some more variables from storage

This commit is contained in:
TÁNCZOS Vilmos Zsombor 2025-05-31 23:35:57 +02:00
parent a405a24077
commit 6f5be26ab5
3 changed files with 58 additions and 17 deletions

View file

@ -10,6 +10,10 @@ struct VoluMaterial {
model_inverse: mat4x4<f32>,
power: f32,
iterations: i32,
max_distance: f32,
max_steps: i32,
ray_epsilon: f32,
normal_epsilon: f32,
}
@group(2) @binding(100)
@ -47,7 +51,7 @@ fn mandelbulb_sdf(p: vec3<f32>) -> f32 {
}
fn mandelbulb_normal(p: vec3<f32>) -> vec3<f32> {
let e = 0.001;
let e = volu_material.normal_epsilon;
let dx = mandelbulb_sdf(p + vec3<f32>(e, 0.0, 0.0)) - mandelbulb_sdf(p - vec3<f32>(e, 0.0, 0.0));
let dy = mandelbulb_sdf(p + vec3<f32>(0.0, e, 0.0)) - mandelbulb_sdf(p - vec3<f32>(0.0, e, 0.0));
let dz = mandelbulb_sdf(p + vec3<f32>(0.0, 0.0, e)) - mandelbulb_sdf(p - vec3<f32>(0.0, 0.0, e));
@ -56,9 +60,9 @@ fn mandelbulb_normal(p: vec3<f32>) -> vec3<f32> {
fn raymarch(ray_origin: vec3<f32>, ray_direction: vec3<f32>) -> f32 {
var t = 0.0;
let max_distance = 100.0;
let max_steps = 128;
let epsilon = 0.001;
let max_distance = volu_material.max_distance;
let max_steps = volu_material.max_steps;
let epsilon = volu_material.ray_epsilon;
for (var i = 0; i < max_steps; i++) {
let current_pos = ray_origin + t * ray_direction;

View file

@ -27,12 +27,7 @@ fn setup(
) {
let transform = Transform::from_xyz(0.0, 0.0, 0.0);
let storage = buffers.add(ShaderStorageBuffer::from(MandelbulbStorage {
mesh_translation: transform.translation,
model_inverse: transform.compute_matrix().inverse(),
power: 8.0,
iterations: 32,
}));
let storage = buffers.add(ShaderStorageBuffer::from(MandelbulbStorage::default()));
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(2.0, 2.0, 2.0))),

View file

@ -13,13 +13,38 @@ const SHADER_ASSET_PATH: &str = "shaders/mandelbulb.wgsl";
pub fn plugin(app: &mut App) {
app.add_plugins(MaterialPlugin::<MandelbulbMaterial>::default())
.init_resource::<MandelbulbSettings>()
.add_systems(Update, update_volu_material);
}
#[derive(Resource)]
struct MandelbulbSettings {
power: f32,
iterations: i32,
max_distance: f32,
max_steps: i32,
ray_epsilon: f32,
normal_epsilon: f32,
}
impl Default for MandelbulbSettings {
fn default() -> Self {
Self {
power: 8.,
iterations: 256,
max_distance: 100.,
max_steps: 128,
ray_epsilon: 0.001,
normal_epsilon: 0.001,
}
}
}
fn update_volu_material(
q: Query<(&Transform, &MeshMaterial3d<MandelbulbMaterial>)>,
mut volu_materials: ResMut<Assets<MandelbulbMaterial>>,
mut buffers: ResMut<Assets<ShaderStorageBuffer>>,
settings: Res<MandelbulbSettings>,
) {
for (transform, material) in q.iter() {
let volu_material = volu_materials.get_mut(material.0.id()).unwrap();
@ -28,11 +53,24 @@ fn update_volu_material(
.get_mut(volu_material.extension.storage.id())
.unwrap();
let MandelbulbSettings {
power,
iterations,
max_distance,
max_steps,
ray_epsilon,
normal_epsilon,
} = *settings;
let volu_storage = MandelbulbStorage {
mesh_translation: transform.translation,
model_inverse: transform.compute_matrix(),
power: 8.0,
iterations: 32,
power,
iterations,
max_distance,
max_steps,
ray_epsilon,
normal_epsilon,
};
buffer.set_data(volu_storage);
@ -42,7 +80,7 @@ fn update_volu_material(
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
pub struct MandelbulbExtension {
#[storage(100, read_only)]
pub storage: Handle<ShaderStorageBuffer>,
storage: Handle<ShaderStorageBuffer>,
}
impl MaterialExtension for MandelbulbExtension {
@ -65,8 +103,12 @@ impl MandelbulbExtension {
#[derive(ShaderType, Clone, Default, Debug)]
pub struct MandelbulbStorage {
pub mesh_translation: Vec3,
pub model_inverse: Mat4,
pub power: f32,
pub iterations: i32,
mesh_translation: Vec3,
model_inverse: Mat4,
power: f32,
iterations: i32,
max_distance: f32,
max_steps: i32,
ray_epsilon: f32,
normal_epsilon: f32,
}