首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编辑gltf锈蚀箱中的gltf数据?

如何编辑gltf锈蚀箱中的gltf数据?
EN

Stack Overflow用户
提问于 2022-07-17 04:59:38
回答 1查看 134关注 0票数 2

我正在通过编写一个我需要的终端实用程序来学习锈病。

该工具需要接受一个gltf文件,并将线性转换应用到节点和缓冲区。

现在,我正试图缩小逆绑定矩阵数据。

为此,我使用了这个箱子:

https://docs.rs/gltf/latest/gltf/

在加载了数据之后,我尝试达到这样的二进制数据:

代码语言:javascript
复制
use gltf::Gltf;

fn main() -> Result<(), gltf::Error>
{
    let gltf = Gltf::open("Assets/werewolf_animated.gltf")?;

    for skin in gltf.skins() {
        for joint in skin.joints()
        {
            println!(
                "Skin #{} has {} joint",
                skin.index(),
                joint.index(),
            );
        }

        for ibm_accessor in skin.inverse_bind_matrices()
        {
            println!("Accessor #{}", ibm_accessor.index());
            println!("offset: {}", ibm_accessor.offset());
            println!("count: {}", ibm_accessor.count());
            println!("data_type: {:?}", ibm_accessor.data_type());
            println!("dimensions: {:?}", ibm_accessor.dimensions());

            println!("buffer: {:?}", ibm_accessor.view().unwrap().buffer().source());
      
        }
    }

    Ok(())
}

如果这是C或C++,在这个阶段,我将有一个浮点数*,我将能够开始读取和写入二进制数据,把它当作一个浮点数数组。但这是生锈。

关于如何开始篡改ibm_accessor.view().unwrap().buffer().source()下的数据的任何建议

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-17 05:40:54

免责声明:我以前从未使用过glTF数据结构或glft机箱。但是,我发现了这篇文章,以及这份文件,这表明源数据可能被编码为Base64。

注:url辅助库

根据文档的说法,Source是一个枚举,有两个变体:BinUri(&'a str)

处理枚举的惯用方法是将其与之匹配:

代码语言:javascript
复制
// In the import section
use data_url::DataUrl;

// In your code
let source = ibm_accessor.view().unwrap().buffer().source();

match source {
    Bin => {
        // This case seems to only apply to .glb (binary glTF) files.
        // If it doesn't apply, maybe skip it?
        continue;
    },
    Uri(data) => {
        // Here we can handle the data which is of type `&str`
        // As mentioned in the linked documentation, this is
        // probably (?) a `data:` Base64 encoded URI, something like
        // data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=
        // Mozilla/SimonSapin (well-known Rust dev) wrote this library
        // to handle `data:` URIs: https://docs.rs/data-url/latest/data_url/
        // let's use that.
        let url = match DataUrl::process(data) {
            Ok(url) => url,
            Err(_) => {
                // This is probably not a `data` URI? Maybe it's a https:// link or something? 
                continue;
            }
        };
        
        let (body, _fragment) = match url.decode_to_vec() {
            Ok(res) => res,
            Err(_) => {
                // The base64 was malformed!
                panic!("Or handle this error somehow?");
            }
        };
        // `body` is now a `Vec<u8>` (vector of bytes) which you can loop over.
    }
}

data_url概念证明

为了向我自己证明data_url机箱是干什么的,我做了这个小POC

Carto.toml

代码语言:javascript
复制
[package]
name = "data-url-test"
version = "0.1.0"
edition = "2021"

[dependencies]
data-url = "0.1.1"

main.rs

代码语言:javascript
复制
use data_url::{DataUrl, mime};

fn main() {
    // URL is from https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_005_BuffersBufferViewsAccessors.md
    let data = "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=";
    let url = match DataUrl::process(data) {
        Ok(url) => url,
        Err(e) => {
            eprintln!("Error {:#?}", e);
            return;
        }
    };
    let (body, _fragment) = match url.decode_to_vec() {
        Ok(res) => res,
        Err(_) => {
            // The base64 was malformed!
            panic!("Or handle this error somehow?");
        }
    };
    println!("body {:?}", body);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73009356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档