Skip to content

Commit

Permalink
Use GLTF component loading crate
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Feb 19, 2024
1 parent 528ba08 commit 74eedc2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ bevy_asset_loader = { version = "0.19", features = ["progress_tracking"] }
bevy_common_assets = { version = "0.9", features = ["ron", "toml"] }
bevy_egui = "0.24"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
oxidized_navigation = { version = "0.8", features = ["xpbd", "debug_draw"] }
iyes_progress = "0.10"
unicode-segmentation = "1"
Expand All @@ -50,9 +49,9 @@ bevy_yarnspinner_example_dialogue_view = "0.1"
bevy-tnua-xpbd3d = "0.1"
bevy_xpbd_3d = { version = "0.3", features = ["simd"] }
bevy-tnua = "0.14.1"
ron = "0.8.1"
bevy_atmosphere = "0.8.1"
warbler_grass = "0.5.0"
bevy_gltf_components = {git = "https://github.com/kaosat-dev/Blender_bevy_components_workflow", rev = "de543727ed64e0d493b8eeb032bdd2e79c7ac6a1" }

[dependencies.bevy]
version = "0.12.1"
Expand Down
58 changes: 5 additions & 53 deletions src/level_instantiation/spawning.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use crate::{level_instantiation::spawning::objects::*, GameState};
use anyhow::{Context, Result};
use bevy::{
gltf::GltfExtras, prelude::*, reflect::serde::TypedReflectDeserializer, utils::HashMap,
prelude::*,
};

use bevy_xpbd_3d::PhysicsSet;
use serde::{de::DeserializeSeed, Deserialize, Serialize};
use serde_json::Value;
use std::ops::Deref;
use serde::{Deserialize, Serialize};
use bevy_gltf_components::ComponentsFromGltfPlugin;

pub(crate) mod objects;

pub(crate) fn spawning_plugin(app: &mut App) {
app.register_type::<camera::IngameCameraMarker>()
app.add_plugins(ComponentsFromGltfPlugin::default())
.register_type::<camera::IngameCameraMarker>()
.register_type::<orb::Orb>()
.register_type::<sunlight::Sun>()
.register_type::<Hidden>()
.register_type::<ground::Grass>()
.add_systems(Update, add_components_from_gltf_extras.map(Result::unwrap))
.add_systems(
Update,
(
Expand All @@ -33,51 +30,6 @@ pub(crate) fn spawning_plugin(app: &mut App) {
);
}

// Reads the extras filed from the GLTF. In Blender, this is the "Custom Attributes" you can set on an object.
// We treat each extra as a indication that we want to inject a marker struct for populating the object later.
// See this as a simplified version of https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/crates/bevy_gltf_components

fn add_components_from_gltf_extras(world: &mut World) -> Result<()> {
let mut extras = world.query::<(Entity, &GltfExtras)>();
let mut components = HashMap::new();
for (entity, extra) in extras.iter(world) {
let Ok(json) = serde_json::from_str::<Value>(&extra.value) else {
continue;
};
let Some(object) = json.as_object() else {
continue;
};
let component_names: Vec<_> = object.keys().map(|k| k.to_string()).collect();
components.insert(entity, component_names);
}
for (entity, component_names) in components {
for component_name in component_names {
let (type_registration, component, component_name) = {
let type_registry: &AppTypeRegistry = world.resource();
let type_registry = type_registry.read();
let Some(type_registration) =
type_registry.get_with_short_type_path(&component_name)
else {
warn!("No type or ambiguous registration found for component {component_name}");
continue;
};
let reflection_deserializer =
TypedReflectDeserializer::new(type_registration, type_registry.deref());
let mut ron_deserializer = ron::Deserializer::from_str(&component_name)?;
let component = reflection_deserializer.deserialize(&mut ron_deserializer)?;
(type_registration.clone(), component, component_name)
};

let mut entity_mut = world.entity_mut(entity);
type_registration
.data::<ReflectComponent>()
.with_context(|| format!("{component_name} does not reflect Component"))?
.insert(&mut entity_mut, &*component);
}
}
Ok(())
}

#[derive(Debug, Clone, Eq, PartialEq, Component, Reflect, Serialize, Deserialize, Default)]
#[reflect(Component, Serialize, Deserialize)]
pub(crate) struct Hidden;
Expand Down

0 comments on commit 74eedc2

Please sign in to comment.