mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-26 08:38:10 +00:00
Limit saved scene to Inspectable trees
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use bevy::{prelude::*, scene::SceneInstance, utils::HashSet, window::PrimaryWindow};
|
use bevy::{prelude::*, utils::hashbrown::HashSet, window::PrimaryWindow};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use crate::wasm::{self, AsJson, WasmMessageBuffer};
|
use crate::{
|
||||||
|
wasm::{self, AsJson, WasmMessageBuffer},
|
||||||
|
MainCamera,
|
||||||
|
};
|
||||||
|
|
||||||
use super::Inspectable;
|
use super::Inspectable;
|
||||||
|
|
||||||
@@ -20,25 +23,42 @@ pub(super) fn scene_download_requested() -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn save_scene_system(world: &mut World) -> Result<(), String> {
|
pub(super) fn save_scene_system(world: &mut World) -> Result<(), String> {
|
||||||
let scene_inst = world
|
// Extract all entities with the `Inspectable` marker...
|
||||||
.query::<&SceneInstance>()
|
let mut scene_entities = world
|
||||||
.get_single(world)
|
.query_filtered::<Entity, With<Inspectable>>()
|
||||||
.map_err(|err| format!("{err}"))?;
|
.iter(world)
|
||||||
|
.collect::<HashSet<_>>();
|
||||||
|
|
||||||
let scene_mgr = world.resource::<SceneSpawner>();
|
// ...and their parents...
|
||||||
|
scene_entities.extend(
|
||||||
|
world
|
||||||
|
.query_filtered::<&Parent, With<Inspectable>>()
|
||||||
|
.iter(world)
|
||||||
|
.map(|parent| **parent),
|
||||||
|
);
|
||||||
|
|
||||||
|
// ...and their children.
|
||||||
|
scene_entities.extend(
|
||||||
|
world
|
||||||
|
// TODO: This is a bit of a hack to exclude the GizmoCamera entity,
|
||||||
|
// which is a child of the MainCamera.
|
||||||
|
.query_filtered::<&Children, (With<Inspectable>, Without<MainCamera>)>()
|
||||||
|
.iter(world)
|
||||||
|
.flat_map(|children| children.iter().copied()),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Build and serialize the scene
|
||||||
let mut builder = DynamicSceneBuilder::from_world(world);
|
let mut builder = DynamicSceneBuilder::from_world(world);
|
||||||
builder
|
builder
|
||||||
.allow_all()
|
.allow_all()
|
||||||
.allow_all_resources()
|
.allow_all_resources()
|
||||||
.deny::<ComputedVisibility>()
|
.deny::<ComputedVisibility>()
|
||||||
.deny_resource::<Time>()
|
.deny_resource::<Time>()
|
||||||
.extract_entities(scene_mgr.iter_instance_entities(**scene_inst))
|
.extract_entities(scene_entities.into_iter())
|
||||||
.remove_empty_entities()
|
.remove_empty_entities()
|
||||||
.extract_resources();
|
.extract_resources();
|
||||||
|
|
||||||
let scene = builder.build();
|
let scene = builder.build();
|
||||||
|
|
||||||
let type_registry = world.resource::<AppTypeRegistry>();
|
let type_registry = world.resource::<AppTypeRegistry>();
|
||||||
let serialized = scene
|
let serialized = scene
|
||||||
.serialize_ron(type_registry)
|
.serialize_ron(type_registry)
|
||||||
|
|||||||
Reference in New Issue
Block a user