refactor: use bevy_sk PbrMaterial instead of StandardMaterial

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2025-07-02 22:00:27 +02:00
parent 859d38f1b8
commit 7b126557df
5 changed files with 30 additions and 29 deletions

View File

@@ -40,6 +40,7 @@ use bevy_mod_openxr::resources::{OxrFrameState, OxrFrameWaiter, OxrSessionConfig
use bevy_mod_openxr::types::AppInfo;
use bevy_mod_xr::camera::XrProjection;
use bevy_mod_xr::session::{XrFirst, XrHandleEvents, XrSessionPlugin};
use bevy_sk::vr_materials::PbrMaterial;
use clap::Parser;
use core::client::{Client, tick_internal_client};
use core::entity_handle::EntityHandlePlugin;
@@ -103,7 +104,7 @@ struct CliArgs {
restore: Option<String>,
}
pub type BevyMaterial = StandardMaterial;
pub type BevyMaterial = PbrMaterial;
static STARDUST_INSTANCE: OnceLock<String> = OnceLock::new();

View File

@@ -154,8 +154,8 @@ fn build_line_mesh(
Name::new("LinesNode"),
SpatialNode(Arc::downgrade(&lines.spatial)),
MeshMaterial3d(materials.add(BevyMaterial {
base_color: Color::WHITE,
perceptual_roughness: 1.0,
color: Color::WHITE,
roughness: 1.0,
alpha_mode: AlphaMode::Opaque,
..default()
})),

View File

@@ -237,10 +237,10 @@ impl HashedPbrMaterial {
Self(hasher.finish())
}
fn hash_pbr_mat<H: Hasher>(mat: &BevyMaterial, state: &mut H) {
hash_color(mat.base_color, state);
hash_color(mat.emissive.into(), state);
hash_color(mat.color, state);
hash_color(mat.emission_factor.into(), state);
state.write_u32(mat.metallic.to_bits());
state.write_u32(mat.perceptual_roughness.to_bits());
state.write_u32(mat.roughness.to_bits());
match mat.alpha_mode {
AlphaMode::Opaque => state.write_u8(0),
AlphaMode::Mask(v) => {
@@ -254,9 +254,9 @@ impl HashedPbrMaterial {
AlphaMode::Multiply => state.write_u8(6),
}
state.write_u8(mat.double_sided as u8);
mat.base_color_texture.hash(state);
mat.emissive_texture.hash(state);
mat.metallic_roughness_texture.hash(state);
mat.diffuse_texture.hash(state);
mat.emission_texture.hash(state);
mat.metal_texture.hash(state);
mat.occlusion_texture.hash(state);
// should always be the same, TODO: make the spherical harmonics buffer a per mesh instance thing
// mat.spherical_harmonics.hash(state);
@@ -350,7 +350,7 @@ impl MaterialParameter {
MaterialParameter::Float(val) => {
match parameter_name {
"metallic" => mat.metallic = *val,
"roughness" => mat.perceptual_roughness = *val,
"roughness" => mat.roughness = *val,
// we probably don't want to expose tex_scale
// "tex_scale" => mat.tex_scale = *val,
v => {
@@ -365,8 +365,8 @@ impl MaterialParameter {
// nothing uses a Vec3
}
MaterialParameter::Color(color) => match parameter_name {
"color" => mat.base_color = color.to_bevy(),
"emission_factor" => mat.emissive = color.to_bevy().to_linear(),
"color" => mat.color = color.to_bevy(),
"emission_factor" => mat.emission_factor = color.to_bevy(),
v => {
error!("unknown param_name ({v}) for color")
}
@@ -379,9 +379,9 @@ impl MaterialParameter {
};
let handle = asset_server.load(texture_path);
match parameter_name {
"diffuse" => mat.base_color_texture = Some(handle),
"emission" => mat.emissive_texture = Some(handle),
"metal" => mat.metallic_roughness_texture = Some(handle),
"diffuse" => mat.diffuse_texture = Some(handle),
"emission" => mat.emission_texture = Some(handle),
"metal" => mat.metal_texture = Some(handle),
"occlusion" => mat.occlusion_texture = Some(handle),
v => {
error!("unknown param_name ({v}) for texture");
@@ -410,21 +410,21 @@ pub struct Material {
impl Material {
fn to_pbr_mat(&self, asset_server: &AssetServer) -> BevyMaterial {
BevyMaterial {
base_color: self.color,
emissive: self.emission_factor.to_linear(),
color: self.color,
emission_factor: self.emission_factor,
metallic: self.metallic,
perceptual_roughness: self.roughness,
roughness: self.roughness,
alpha_mode: self.alpha_mode,
double_sided: self.double_sided,
base_color_texture: self
diffuse_texture: self
.diffuse_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
emissive_texture: self
emission_texture: self
.emission_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
metallic_roughness_texture: self
metal_texture: self
.metal_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),

View File

@@ -92,10 +92,10 @@ fn spawn_text(
text: text_string,
material: material_registry.get_handle(
BevyMaterial {
base_color: style.color.to_bevy(),
emissive: Color::WHITE.to_linear(),
color: style.color.to_bevy(),
emission_factor: Color::WHITE,
metallic: 0.0,
perceptual_roughness: 1.0,
roughness: 1.0,
// If alpha is supported on text we need to change this
alpha_mode: AlphaMode::Opaque,
double_sided: false,

View File

@@ -219,10 +219,10 @@ impl OxrHandInput {
let input = InputMethod::add_to(&node.0, hand, datamap)?;
let material = materials.add(BevyMaterial {
base_color: Srgba::new(1.0, 1.0, 1.0, 1.0).into(),
color: Srgba::new(1.0, 1.0, 1.0, 1.0).into(),
alpha_mode: AlphaMode::Blend,
base_color_texture: Some(GRADIENT_TEXTURE_HANDLE),
perceptual_roughness: 1.0,
diffuse_texture: Some(GRADIENT_TEXTURE_HANDLE),
roughness: 1.0,
..default()
});
Ok(OxrHandInput {
@@ -329,10 +329,10 @@ impl OxrHandInput {
*self.input.datamap.lock() = Datamap::from_typed(&self.datamap).unwrap();
let captured = self.capture_manager.capture.upgrade().is_some();
if captured && !self.captured {
materials.get_mut(&self.material).unwrap().base_color =
materials.get_mut(&self.material).unwrap().color =
Srgba::rgb(0., 1., 0.75).into();
} else if self.captured && !captured {
materials.get_mut(&self.material).unwrap().base_color =
materials.get_mut(&self.material).unwrap().color =
Srgba::rgb(1., 1.0, 1.0).into();
}
self.captured = captured;