Media Library: Importing and spawning assets (#109)

This PR updates the Media Library feature to implement asset imports
from the local file system, and spawning assets from the library into
the scene. Additionally, some placeholder functionality has been
scaffolded for adding animations onto existing characters in the scene.

### Additional Changes
* Dialogs can be set to `nonBlocking` to allow interaction with
  background elements (note: this is fraught for accessibility and likely
  temporary, to facilitate testing while the UI/UX redesign is in
  progress)
* Dialogs can be moved around the screen by dragging their title-bars
* The `scene` module has been moved into a folder with a separate
  `wasm.rs` file for its submodule. (`scene/mod.rs` remains one of our
  largest single Rust files and could probably stand to be partitioned
  further.)

### To-Do
* Support for adding animations is not yet fully implemented, though
  some prerequisite functionality has been scaffolded. Namely, the "Add to
  Scene" button will be disabled if:
  - There is a mix of animations and non-animations selected in the Media
    Library
  - There is more than one animation selected in the Media Library
  - There is one animation selected in the Media Library, but no
    animatable character selected in the scene
* Adding full Storyteller-format scenes to the existing scene is not a
  use case that has been tested, and may not be a use case we wish to
  support in the long-term, as some of the data stored in Storyteller
  scenes are intended to be "singleton" resources that may conflict with
  existing data in the scene.
  
  **Attempting to spawn a Storyteller scene through the Media Library has
  not been explicitly blocked for now, but may cause unexpected
  behavior.** This needs additional UX consideration and grooming.
This commit is contained in:
Danny McGee
2024-03-27 19:20:33 -04:00
committed by GitHub
parent b905a0f635
commit 1163192d24
19 changed files with 792 additions and 140 deletions
+4 -1
View File
@@ -231,7 +231,10 @@ fn init_from_asset(app: &mut App, asset: InitAsset, skybox_id: String) {
}
InitAsset::ImportedScene(scene) => {
app.add_systems(Startup, move |mut ew: EventWriter<ImportSceneEvent>| {
ew.send(ImportSceneEvent(scene.clone()));
ew.send(ImportSceneEvent {
path: scene.clone(),
name: None,
});
});
}
}
+21 -94
View File
@@ -38,6 +38,9 @@ use crate::{
MainCamera,
};
#[cfg(feature = "wasm")]
mod wasm;
pub struct ScenePlugin;
#[derive(SystemSet, Clone, Copy, PartialEq, Eq, Hash, Debug)]
@@ -105,7 +108,7 @@ impl Plugin for ScenePlugin {
app.add_systems(
Update,
(
wasm::dispatch_object_load_request,
wasm::handle_spawn_asset_messages,
wasm::notify_state_changes
.map(bevy::utils::error)
.run_if(resource_changed::<State<SceneState>>()),
@@ -247,8 +250,11 @@ pub struct LoadSceneEvent(pub String);
/// Request the application to import and process a glTF scene from an arbitrary
/// asset source (e.g., an http path or a Storyteller.ai media token).
#[derive(Event, Deref)]
pub struct ImportSceneEvent(pub String);
#[derive(Event)]
pub struct ImportSceneEvent {
pub path: String,
pub name: Option<String>,
}
/// A tag that identifies its entity as a user-facing scene element like a prop,
/// a light, or a camera.
@@ -380,16 +386,23 @@ fn init_scene_import(
r_assets: Res<AssetServer>,
mut er: EventReader<ImportSceneEvent>,
) {
if let Some(path) = er.read().last() {
info!("Importing asset: {}", path.0);
for event in er.read() {
info!("Importing asset: {}", event.path);
let name = event
.name
.as_ref()
.map(|name| Name::new(name.clone()))
.unwrap_or_else(|| Name::new("Scene"));
cmd.spawn((
Name::new("Scene"), // TODO: Maybe use the "basename" part of the asset path?
name,
SpatialBundle::default(),
space_editor::prelude::GltfPrefab {
path: path.0.clone(),
path: event.path.clone(),
scene: "Scene0".to_string(),
},
r_assets.load::<Gltf>(&path.0),
r_assets.load::<Gltf>(&event.path),
PrefabMarker,
SceneRoot,
));
@@ -601,89 +614,3 @@ fn start_capture(mut captures: Query<&mut ScreenCapture, Added<ScreenCapture>>)
capture.start_capturing();
}
}
#[cfg(feature = "wasm")]
mod wasm {
use bevy::{prelude::*, window::PrimaryWindow};
use wasm_bindgen::prelude::*;
use super::{LoadLibraryObjectEvent, LoadingQueue, SceneState};
use crate::wasm::{self, StaticBuffer, WasmMessageBuffer};
static OBJECT_LOAD_REQUEST: WasmMessageBuffer<String> = WasmMessageBuffer::new();
#[wasm_bindgen(js_name = loadObject)]
pub fn load_object(id: &str) {
OBJECT_LOAD_REQUEST.write_message(id.into());
}
// FIXME: Is it an object or a scene? Inconsistent terminology is
// unnecessarily confusing
pub(super) fn dispatch_object_load_request(
mut ew_load_scene: EventWriter<LoadLibraryObjectEvent>,
) {
if let Some(id) = OBJECT_LOAD_REQUEST.take_message() {
ew_load_scene.send(LoadLibraryObjectEvent(id));
}
}
#[wasm_bindgen(typescript_custom_section)]
const SCENE_STATE_EVENT: &str = r#"
export interface SceneStateEvent extends CustomEvent {
type: "scene-state";
detail: SceneState;
}
declare global {
export interface GlobalEventHandlersEventMap {
"scene-state": SceneStateEvent;
}
}
"#;
pub(super) fn notify_state_changes(
r_state: Res<State<SceneState>>,
q_window: Query<&Window, With<PrimaryWindow>>,
) -> Result<(), String> {
if let Ok(window) = q_window.get_single() {
wasm::dispatch_to_js(window, "scene-state", &(**r_state as u32))?;
}
Ok(())
}
#[wasm_bindgen(typescript_custom_section)]
const LOADING_QUEUE_EVENT: &str = r#"
export interface LoadingQueueEvent extends CustomEvent {
type: "loading-queue";
detail: number;
}
declare global {
export interface GlobalEventHandlersEventMap {
"loading-queue": LoadingQueueEvent;
}
}
"#;
pub(super) fn notify_loading_queue_changes(
mut l_last_sent: Local<Option<usize>>,
r_loading: Res<LoadingQueue>,
q_window: Query<&Window, With<PrimaryWindow>>,
) -> Result<(), String> {
let queue_len = r_loading.queue.len();
if let Some(last_sent) = *l_last_sent {
if last_sent == queue_len {
return Ok(());
}
}
let Ok(window) = q_window.get_single() else {
return Ok(());
};
wasm::dispatch_to_js(window, "loading-queue", &queue_len)?;
*l_last_sent = Some(queue_len);
Ok(())
}
}
+111
View File
@@ -0,0 +1,111 @@
use bevy::{prelude::*, window::PrimaryWindow};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use super::{ImportSceneEvent, LoadingQueue, SceneState};
use crate::wasm::{self, StaticBuffer, WasmMessageBuffer};
static SPAWN_ASSETS: WasmMessageBuffer<Vec<AssetImport>> = WasmMessageBuffer::new();
#[wasm_bindgen(typescript_custom_section)]
const ASSET_IMPORT_TS: &str = r#"
export interface AssetImport {
path: string;
name?: string;
}
"#;
#[derive(Deserialize, Serialize)]
struct AssetImport {
path: String,
name: Option<String>,
}
#[wasm_bindgen(typescript_custom_section)]
const SPAWN_ASSET_TS: &str = r#"
export function spawnAsset(asset: AssetImport): void;
"#;
#[wasm_bindgen(js_name = spawnAsset, skip_typescript)]
pub fn spawn_asset(asset: JsValue) -> Result<(), JsValue> {
let asset: AssetImport = serde_wasm_bindgen::from_value(asset)?;
SPAWN_ASSETS.write_message(vec![asset]);
Ok(())
}
#[wasm_bindgen(typescript_custom_section)]
const SPAWN_ASSETS_TS: &str = r#"
export function spawnAssets(assets: AssetImport[]): void;
"#;
#[wasm_bindgen(js_name = spawnAssets, skip_typescript)]
pub fn spawn_assets(assets: JsValue) -> Result<(), JsValue> {
let assets: Vec<AssetImport> = serde_wasm_bindgen::from_value(assets)?;
SPAWN_ASSETS.write_message(assets);
Ok(())
}
pub(super) fn handle_spawn_asset_messages(mut ew_import_scene: EventWriter<ImportSceneEvent>) {
if let Some(assets) = SPAWN_ASSETS.take_message() {
for AssetImport { path, name } in assets {
ew_import_scene.send(ImportSceneEvent { path, name });
}
}
}
#[wasm_bindgen(typescript_custom_section)]
const SCENE_STATE_EVENT: &str = r#"
export interface SceneStateEvent extends CustomEvent {
type: "scene-state";
detail: SceneState;
}
declare global {
export interface GlobalEventHandlersEventMap {
"scene-state": SceneStateEvent;
}
}
"#;
pub(super) fn notify_state_changes(
r_state: Res<State<SceneState>>,
q_window: Query<&Window, With<PrimaryWindow>>,
) -> Result<(), String> {
if let Ok(window) = q_window.get_single() {
wasm::dispatch_to_js(window, "scene-state", &(**r_state as u32))?;
}
Ok(())
}
#[wasm_bindgen(typescript_custom_section)]
const LOADING_QUEUE_EVENT: &str = r#"
export interface LoadingQueueEvent extends CustomEvent {
type: "loading-queue";
detail: number;
}
declare global {
export interface GlobalEventHandlersEventMap {
"loading-queue": LoadingQueueEvent;
}
}
"#;
pub(super) fn notify_loading_queue_changes(
mut l_last_sent: Local<Option<usize>>,
r_loading: Res<LoadingQueue>,
q_window: Query<&Window, With<PrimaryWindow>>,
) -> Result<(), String> {
let queue_len = r_loading.queue.len();
if let Some(last_sent) = *l_last_sent {
if last_sent == queue_len {
return Ok(());
}
}
let Ok(window) = q_window.get_single() else {
return Ok(());
};
wasm::dispatch_to_js(window, "loading-queue", &queue_len)?;
*l_last_sent = Some(queue_len);
Ok(())
}