Merge pull request #146 from MilanVasko/support-embedded-gltf-buffers

Add support for embedded buffers in GLTF loader
This commit is contained in:
Carter Anderson 2020-08-13 11:33:05 -07:00 committed by GitHub
commit 4beab7273e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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"
anyhow = "1.0"
base64 = "0.12.3"

View File

@ -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<PrimitiveTopology, GltfError> {
@ -111,11 +115,18 @@ fn load_node(buffer_data: &[Vec<u8>], node: &gltf::Node, depth: i32) -> Result<M
}
fn load_buffers(buffers: iter::Buffers, asset_path: &Path) -> Result<Vec<Vec<u8>>, 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)?;