volumetric changing unlighted sphere

This commit is contained in:
TÁNCZOS Vilmos Zsombor 2025-05-31 00:38:33 +02:00
parent a867364760
commit aaf26320cb
2 changed files with 93 additions and 42 deletions

View file

@ -1,53 +1,54 @@
//! Demonstrates using a custom extension to the `StandardMaterial` to modify the results of the builtin pbr shader.
use bevy::{
color::palettes::basic::RED,
pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod},
pbr::{ExtendedMaterial, MaterialExtension},
prelude::*,
render::render_resource::*,
render::{render_resource::*, storage::ShaderStorageBuffer},
};
/// This example uses a shader source file from the assets subdirectory
const SHADER_ASSET_PATH: &str = "shaders/material.wgsl";
type VoluMaterial = ExtendedMaterial<StandardMaterial, VoluExtension>;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MaterialPlugin::<
ExtendedMaterial<StandardMaterial, MyExtension>,
>::default())
.add_plugins(MaterialPlugin::<VoluMaterial>::default())
.add_systems(Startup, setup)
.add_systems(Update, rotate_things)
.add_systems(Update, (rotate_things, update_volu_material))
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, MyExtension>>>,
mut materials: ResMut<Assets<VoluMaterial>>,
mut buffers: ResMut<Assets<ShaderStorageBuffer>>,
) {
// sphere
let transform = Transform::from_xyz(0.0, 0.0, 0.0);
let storage = buffers.add(ShaderStorageBuffer::from(VoluStorage {
mesh_translation: transform.translation,
sphere_radius: 1.0,
color: Vec4::default(),
}));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(1.0))),
Mesh3d(meshes.add(Cuboid::new(2.0, 2.0, 2.0))),
MeshMaterial3d(materials.add(ExtendedMaterial {
base: StandardMaterial {
base_color: RED.into(),
opaque_render_method: OpaqueRendererMethod::Auto,
..Default::default()
alpha_mode: AlphaMode::Blend,
..default()
},
extension: MyExtension { quantize_steps: 3 },
extension: VoluExtension { storage },
})),
Transform::from_xyz(0.0, 0.5, 0.0),
));
// light
commands.spawn((
DirectionalLight::default(),
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
transform,
Rotate,
));
// camera
commands.spawn((
DirectionalLight::default(),
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
@ -63,14 +64,48 @@ fn rotate_things(mut q: Query<&mut Transform, With<Rotate>>, time: Res<Time>) {
}
}
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
struct MyExtension {
#[uniform(100)]
quantize_steps: u32,
fn update_volu_material(
mut q: Query<(&mut Transform, &MeshMaterial3d<VoluMaterial>), Changed<Transform>>,
mut volu_materials: ResMut<Assets<VoluMaterial>>,
mut buffers: ResMut<Assets<ShaderStorageBuffer>>,
time: Res<Time>,
) {
for (mut transform, material) in q.iter_mut() {
transform.translation.y = time.elapsed_secs().sin();
let volu_material = volu_materials.get_mut(material.0.id()).unwrap();
let buffer = buffers
.get_mut(volu_material.extension.storage.id())
.unwrap();
let elapsed = time.elapsed_secs_wrapped();
let volu_storage = VoluStorage {
mesh_translation: transform.translation,
sphere_radius: (elapsed.sin() + 3.) / 4.,
color: vec4((elapsed.sin() + 1.) / 2., 0.0, 0.0, 1.0),
};
buffer.set_data(volu_storage);
}
}
impl MaterialExtension for MyExtension {
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
struct VoluExtension {
#[storage(100, read_only)]
storage: Handle<ShaderStorageBuffer>,
}
impl MaterialExtension for VoluExtension {
fn fragment_shader() -> ShaderRef {
SHADER_ASSET_PATH.into()
}
}
#[derive(ShaderType, Clone, Default, Debug)]
pub struct VoluStorage {
mesh_translation: Vec3,
sphere_radius: f32,
color: Vec4,
}