From e96afc33a7446389dd1007d2533b6657a6e08939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Va=C5=A1ko?= Date: Wed, 12 Aug 2020 19:23:54 +0200 Subject: [PATCH] Add support for embedded buffers in GLTF loader --- crates/bevy_gltf/Cargo.toml | 3 ++- crates/bevy_gltf/src/loader.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/bevy_gltf/Cargo.toml b/crates/bevy_gltf/Cargo.toml index 8b58e85889..ceb11e7421 100644 --- a/crates/bevy_gltf/Cargo.toml +++ b/crates/bevy_gltf/Cargo.toml @@ -18,4 +18,5 @@ bevy_render = { path = "../bevy_render", version = "0.1" } # other gltf = { version = "0.15.2", default-features = false, features = ["utils"] } thiserror = "1.0" -anyhow = "1.0" \ No newline at end of file +anyhow = "1.0" +base64 = "0.12.3" diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index d11120af90..f2827b5b79 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -38,6 +38,10 @@ pub enum GltfError { Io(#[from] io::Error), #[error("Binary buffers not supported yet.")] BinaryBuffersUnsupported, + #[error("Failed to decode base64 mesh data.")] + Base64Decode(#[from] base64::DecodeError), + #[error("Unsupported buffer format.")] + BufferFormatUnsupported, } fn get_primitive_topology(mode: Mode) -> Result { @@ -111,11 +115,18 @@ fn load_node(buffer_data: &[Vec], node: &gltf::Node, depth: i32) -> Result Result>, GltfError> { + const OCTET_STREAM_URI: &str = "data:application/octet-stream;base64,"; + let mut buffer_data = Vec::new(); for buffer in buffers { match buffer.source() { Source::Uri(uri) => { if uri.starts_with("data:") { + if uri.starts_with(OCTET_STREAM_URI) { + buffer_data.push(base64::decode(&uri[OCTET_STREAM_URI.len()..])?); + } else { + return Err(GltfError::BufferFormatUnsupported); + } } else { let buffer_path = asset_path.parent().unwrap().join(uri); let buffer_bytes = fs::read(buffer_path)?;