refactor: switch to bevys StandardMaterial for now

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2025-06-29 22:37:52 +02:00
committed by Nova King
parent 806857b738
commit c22bf9b511
8 changed files with 69 additions and 75 deletions

1
Cargo.lock generated
View File

@@ -1432,7 +1432,6 @@ dependencies = [
[[package]]
name = "bevy_sk"
version = "0.1.0"
source = "git+https://github.com/Schmarni-Dev/bevy_sk?branch=fix_mat_stuff#744e9bb442a690b1abc15f50ba2655228bd84eea"
dependencies = [
"bevy",
"bevy_mod_xr",

View File

@@ -83,7 +83,8 @@ bevy = { version = "0.16", features = ["wayland", "bevy_remote", "mp3", "wav"] }
bevy_mod_xr = "0.3"
bevy_mod_openxr = "0.3"
# bevy_sk.git = "https://github.com/MalekiRe/bevy_sk"
bevy_sk = { git = "https://github.com/Schmarni-Dev/bevy_sk", branch = "fix_mat_stuff" }
# bevy_sk = { git = "https://github.com/Schmarni-Dev/bevy_sk", branch = "fix_mat_stuff" }
bevy_sk.path = "../bevy_sk"
bevy-mesh-text-3d.git = "https://github.com/terhechte/bevy-mesh-text-3d"
openxr = "0.19"

View File

@@ -39,7 +39,6 @@ use bevy_mod_openxr::render::{OxrRenderPlugin, OxrWaitFrameSystem};
use bevy_mod_openxr::resources::{OxrFrameState, OxrFrameWaiter, OxrSessionConfig};
use bevy_mod_openxr::types::AppInfo;
use bevy_mod_xr::camera::XrProjection;
use bevy_mod_xr::hand_debug_gizmos::HandGizmosPlugin;
use bevy_mod_xr::session::{XrFirst, XrHandleEvents};
use clap::Parser;
use core::client::{Client, tick_internal_client};
@@ -108,6 +107,8 @@ struct CliArgs {
nvidia: bool,
}
pub type BevyMaterial = StandardMaterial;
static STARDUST_INSTANCE: OnceLock<String> = OnceLock::new();
// #[tokio::main(flavor = "current_thread")]
@@ -350,13 +351,14 @@ fn bevy_loop(
app.add_plugins((
bevy_sk::hand::HandPlugin,
bevy_sk::vr_materials::SkMaterialPlugin {
replace_standard_material: true,
},
bevy_sk::skytext::SphericalHarmonicsPlugin,
// bevy_sk::vr_materials::SkMaterialPlugin {
// replace_standard_material: false,
// },
// bevy_sk::skytext::SphericalHarmonicsPlugin,
));
// app.add_plugins(HandGizmosPlugin);
app.init_asset::<Font>().init_asset_loader::<FontLoader>();
app.world_mut().resource_mut::<AmbientLight>().brightness = 2000.0;
if let Some(priority) = args.overlay_priority {
app.insert_resource(OxrOverlaySettings {
session_layer_placement: priority,

View File

@@ -5,9 +5,8 @@ use crate::{
registry::Registry,
},
nodes::{
Node,
spatial::{Spatial, SpatialNode},
},
spatial::{Spatial, SpatialNode}, Node
}, BevyMaterial,
};
use bevy::{
asset::RenderAssetUsages,
@@ -17,7 +16,6 @@ use bevy::{
primitives::Aabb,
},
};
use bevy_sk::vr_materials::PbrMaterial;
use glam::Vec3;
use parking_lot::Mutex;
use std::sync::{
@@ -54,7 +52,7 @@ fn update_visibillity(mut cmds: Commands) {
fn build_line_mesh(
mut meshes: ResMut<Assets<Mesh>>,
mut cmds: Commands,
mut materials: ResMut<Assets<PbrMaterial>>,
mut materials: ResMut<Assets<BevyMaterial>>,
) {
for lines in LINES_REGISTRY
.get_valid_contents()
@@ -171,9 +169,9 @@ fn build_line_mesh(
let e = cmds.spawn((
Name::new("LinesNode"),
SpatialNode(Arc::downgrade(&lines.spatial)),
MeshMaterial3d(materials.add(PbrMaterial {
color: Color::WHITE,
roughness: 1.0,
MeshMaterial3d(materials.add(BevyMaterial {
base_color: Color::WHITE,
perceptual_roughness: 1.0,
alpha_mode: AlphaMode::Opaque,
..default()
})),

View File

@@ -1,5 +1,4 @@
use super::{MODEL_PART_ASPECT_ALIAS_INFO, MaterialParameter, ModelAspect, ModelPartAspect};
use crate::bail;
use crate::core::bevy_channel::{BevyChannel, BevyChannelReader};
use crate::core::client::Client;
use crate::core::color::ColorConvert as _;
@@ -10,9 +9,9 @@ use crate::core::resource::get_resource_file;
use crate::nodes::Node;
use crate::nodes::alias::{Alias, AliasList};
use crate::nodes::spatial::{Spatial, SpatialNode};
use crate::{BevyMaterial, bail};
use bevy::prelude::*;
use bevy::render::primitives::Aabb;
use bevy_sk::vr_materials::PbrMaterial;
use color_eyre::eyre::eyre;
use parking_lot::Mutex;
use rustc_hash::{FxHashMap, FxHasher};
@@ -79,10 +78,10 @@ fn load_models(
}
fn apply_materials(
mut query: Query<&mut MeshMaterial3d<PbrMaterial>>,
mut query: Query<&mut MeshMaterial3d<BevyMaterial>>,
mut material_registry: ResMut<MaterialRegistry>,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<PbrMaterial>>,
mut materials: ResMut<Assets<BevyMaterial>>,
) -> bevy::prelude::Result {
for model_part in MODEL_REGISTRY
.get_valid_contents()
@@ -248,16 +247,16 @@ fn gen_path(
#[derive(PartialEq, Deref, DerefMut, Clone, Copy, Eq, PartialOrd, Ord, Hash)]
struct HashedPbrMaterial(u64);
impl HashedPbrMaterial {
fn new(material: &PbrMaterial) -> Self {
fn new(material: &BevyMaterial) -> Self {
let mut hasher = FxHasher::default();
Self::hash_pbr_mat(material, &mut hasher);
Self(hasher.finish())
}
fn hash_pbr_mat<H: Hasher>(mat: &PbrMaterial, state: &mut H) {
hash_color(mat.color, state);
hash_color(mat.emission_factor, state);
fn hash_pbr_mat<H: Hasher>(mat: &BevyMaterial, state: &mut H) {
hash_color(mat.base_color, state);
hash_color(mat.emissive.into(), state);
state.write_u32(mat.metallic.to_bits());
state.write_u32(mat.roughness.to_bits());
state.write_u32(mat.perceptual_roughness.to_bits());
match mat.alpha_mode {
AlphaMode::Opaque => state.write_u8(0),
AlphaMode::Mask(v) => {
@@ -271,12 +270,12 @@ impl HashedPbrMaterial {
AlphaMode::Multiply => state.write_u8(6),
}
state.write_u8(mat.double_sided as u8);
mat.diffuse_texture.hash(state);
mat.emission_texture.hash(state);
mat.metal_texture.hash(state);
mat.base_color_texture.hash(state);
mat.emissive_texture.hash(state);
mat.metallic_roughness_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);
// mat.spherical_harmonics.hash(state);
}
}
fn hash_color<H: Hasher>(color: Color, state: &mut H) {
@@ -347,7 +346,7 @@ impl MaterialParameter {
fn apply_to_material(
&self,
client: &Client,
mat: &mut PbrMaterial,
mat: &mut BevyMaterial,
parameter_name: &str,
asset_server: &AssetServer,
) {
@@ -367,7 +366,7 @@ impl MaterialParameter {
MaterialParameter::Float(val) => {
match parameter_name {
"metallic" => mat.metallic = *val,
"roughness" => mat.roughness = *val,
"roughness" => mat.perceptual_roughness = *val,
// we probably don't want to expose tex_scale
// "tex_scale" => mat.tex_scale = *val,
v => {
@@ -382,8 +381,8 @@ impl MaterialParameter {
// nothing uses a Vec3
}
MaterialParameter::Color(color) => match parameter_name {
"color" => mat.color = color.to_bevy(),
"emission_factor" => mat.emission_factor = color.to_bevy(),
"color" => mat.base_color = color.to_bevy(),
"emission_factor" => mat.emissive = color.to_bevy().to_linear(),
v => {
error!("unknown param_name ({v}) for color")
}
@@ -396,16 +395,15 @@ impl MaterialParameter {
};
let handle = asset_server.load(texture_path);
match parameter_name {
"diffuse" => mat.diffuse_texture = Some(handle),
"emission" => mat.emission_texture = Some(handle),
"metal" => mat.metal_texture = Some(handle),
"diffuse" => mat.base_color_texture = Some(handle),
"emission" => mat.emissive_texture = Some(handle),
"metal" => mat.metallic_roughness_texture = Some(handle),
"occlusion" => mat.occlusion_texture = Some(handle),
v => {
error!("unknown param_name ({v}) for texture");
return;
}
}
mat.alpha_mode = AlphaMode::AlphaToCoverage;
// mat.alpha_mode = AlphaMode::Blend;
}
}
}
@@ -426,23 +424,23 @@ pub struct Material {
}
impl Material {
fn to_pbr_mat(&self, asset_server: &AssetServer) -> PbrMaterial {
PbrMaterial {
color: self.color,
emission_factor: self.emission_factor,
fn to_pbr_mat(&self, asset_server: &AssetServer) -> BevyMaterial {
BevyMaterial {
base_color: self.color,
emissive: self.emission_factor.to_linear(),
metallic: self.metallic,
roughness: self.roughness,
perceptual_roughness: self.roughness,
alpha_mode: self.alpha_mode,
double_sided: self.double_sided,
diffuse_texture: self
base_color_texture: self
.diffuse_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
emission_texture: self
emissive_texture: self
.emission_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
metal_texture: self
metallic_roughness_texture: self
.metal_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
@@ -450,7 +448,8 @@ impl Material {
.occlusion_texture
.as_ref()
.map(|p| asset_server.load(p.as_path())),
spherical_harmonics: bevy_sk::skytext::SPHERICAL_HARMONICS_HANDLE,
..Default::default()
// spherical_harmonics: bevy_sk::skytext::SPHERICAL_HARMONICS_HANDLE,
}
}
}
@@ -510,14 +509,14 @@ impl ModelPartAspect for ModelPart {
}
}
#[derive(Default, Resource)]
pub struct MaterialRegistry(FxHashMap<HashedPbrMaterial, Handle<PbrMaterial>>);
pub struct MaterialRegistry(FxHashMap<HashedPbrMaterial, Handle<BevyMaterial>>);
impl MaterialRegistry {
/// returns strong handle for PbrMaterial elminitating duplications
pub fn get_handle(
&mut self,
material: PbrMaterial,
materials: &mut ResMut<Assets<PbrMaterial>>,
) -> Handle<PbrMaterial> {
material: BevyMaterial,
materials: &mut ResMut<Assets<BevyMaterial>>,
) -> Handle<BevyMaterial> {
let hash = HashedPbrMaterial::new(&material);
match self
.0

View File

@@ -9,17 +9,14 @@ use crate::{
resource::get_resource_file,
},
nodes::{
Node,
drawable::XAlign,
spatial::{Spatial, SpatialNode},
},
drawable::XAlign, spatial::{Spatial, SpatialNode}, Node
}, BevyMaterial,
};
use bevy::{platform::collections::HashMap, prelude::*};
use bevy_mesh_text_3d::{
Align, Attrs, MeshTextPlugin, Settings as FontSettings, generate_meshes,
text_glyphs::TextGlyphs,
};
use bevy_sk::vr_materials::PbrMaterial;
use color_eyre::eyre::eyre;
use core::f32;
use cosmic_text::Metrics;
@@ -70,7 +67,7 @@ fn spawn_text(
mut cmds: Commands,
mut font_settings: ResMut<FontSettings>,
mut material_registry: ResMut<MaterialRegistry>,
mut materials: ResMut<Assets<PbrMaterial>>,
mut materials: ResMut<Assets<BevyMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut font_registry: Local<FontDatabaseRegistry>,
) {
@@ -109,11 +106,11 @@ fn spawn_text(
bevy_mesh_text_3d::InputText::Simple {
text: text_string,
material: material_registry.get_handle(
PbrMaterial {
color: style.color.to_bevy(),
emission_factor: Color::WHITE,
BevyMaterial {
base_color: style.color.to_bevy(),
emissive: Color::WHITE.to_linear(),
metallic: 0.0,
roughness: 1.0,
perceptual_roughness: 1.0,
// If alpha is supported on text we need to change this
alpha_mode: AlphaMode::Opaque,
double_sided: false,

View File

@@ -27,7 +27,6 @@ use bevy_mod_xr::{
session::{XrPreDestroySession, XrSessionCreated, XrSessionCreatedEvent},
spaces::{XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace},
};
use bevy_sk::vr_materials::PbrMaterial;
use color_eyre::eyre::Result;
use glam::{Affine3A, Mat4, Vec2, Vec3};
use openxr::{Action, ActiveActionSet, SpaceLocationFlags};

View File

@@ -8,7 +8,7 @@ use crate::nodes::{
spatial::Spatial,
};
use crate::objects::{ObjectHandle, SpatialRef, Tracked};
use crate::{DbusConnection, ObjectRegistryRes, PreFrameWait};
use crate::{BevyMaterial, DbusConnection, ObjectRegistryRes, PreFrameWait};
use bevy::prelude::Transform as BevyTransform;
use bevy::prelude::*;
use bevy_mod_openxr::helper_traits::{ToQuat, ToVec3};
@@ -18,7 +18,6 @@ use bevy_mod_xr::hands::{HandBone, HandSide, XrHandBoneEntities, XrHandBoneRadiu
use bevy_mod_xr::session::{XrPreDestroySession, XrSessionCreated};
use bevy_mod_xr::spaces::{XrPrimaryReferenceSpace, XrSpaceLocationFlags};
use bevy_sk::hand::GRADIENT_TEXTURE_HANDLE;
use bevy_sk::vr_materials::PbrMaterial;
use color_eyre::eyre::Result;
use glam::{Mat4, Quat, Vec3};
use openxr::{HandJointLocation, SpaceLocationFlags};
@@ -44,7 +43,7 @@ fn update_hands(
session: Option<Res<OxrSession>>,
state: Option<Res<OxrFrameState>>,
ref_space: Option<Res<XrPrimaryReferenceSpace>>,
mut materials: ResMut<Assets<PbrMaterial>>,
mut materials: ResMut<Assets<BevyMaterial>>,
mut joint_query: Query<(
&mut BevyTransform,
&mut XrSpaceLocationFlags,
@@ -116,7 +115,7 @@ fn update_hand_material(
(Entity, &HandSide),
(
With<XrHandBoneEntities>,
With<MeshMaterial3d<PbrMaterial>>,
With<MeshMaterial3d<BevyMaterial>>,
Without<CorrectHandMaterial>,
),
>,
@@ -137,7 +136,7 @@ fn update_hand_material(
fn setup(
connection: Res<DbusConnection>,
mut cmds: Commands,
mut materials: ResMut<Assets<PbrMaterial>>,
mut materials: ResMut<Assets<BevyMaterial>>,
) {
tokio::task::spawn({
let connection = connection.clone();
@@ -186,13 +185,13 @@ pub struct SkHand {
tracked: ObjectHandle<Tracked>,
tracker: Option<openxr::HandTracker>,
captured: bool,
material: Handle<PbrMaterial>,
material: Handle<BevyMaterial>,
}
impl SkHand {
pub fn new(
connection: &Connection,
side: HandSide,
materials: &mut Assets<PbrMaterial>,
materials: &mut Assets<BevyMaterial>,
) -> Result<Self> {
let (palm_spatial, palm_object) = SpatialRef::create(
connection,
@@ -219,11 +218,11 @@ impl SkHand {
let datamap = Datamap::from_typed(HandDatamap::default())?;
let input = InputMethod::add_to(&node.0, hand, datamap)?;
let material = materials.add(PbrMaterial {
color: Srgba::new(1.0, 1.0, 1.0, 1.0).into(),
let material = materials.add(BevyMaterial {
base_color: Srgba::new(1.0, 1.0, 1.0, 1.0).into(),
alpha_mode: AlphaMode::Blend,
diffuse_texture: Some(GRADIENT_TEXTURE_HANDLE),
roughness: 1.0,
base_color_texture: Some(GRADIENT_TEXTURE_HANDLE),
perceptual_roughness: 1.0,
..default()
});
Ok(SkHand {
@@ -255,7 +254,7 @@ impl SkHand {
fn update(
&mut self,
joints: Option<&openxr::HandJointLocations>,
materials: &mut ResMut<Assets<PbrMaterial>>,
materials: &mut ResMut<Assets<BevyMaterial>>,
) {
// TODO: use the hand data source ext
let real_hand = true;
@@ -330,9 +329,9 @@ impl SkHand {
*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().color = Srgba::rgb(0., 1., 0.75).into();
materials.get_mut(&self.material).unwrap().base_color = Srgba::rgb(0., 1., 0.75).into();
} else if self.captured && !captured {
materials.get_mut(&self.material).unwrap().color = Srgba::rgb(1., 1.0, 1.0).into();
materials.get_mut(&self.material).unwrap().base_color = Srgba::rgb(1., 1.0, 1.0).into();
}
self.captured = captured;
}