如何手动读取 GLTF/GLB 文件

推荐:使用NSDT场景编辑器快速搭建3D应用场景

文件类型

GLTF文件有两种不同的主要文件类型:.gltf和.glb。

GLTF文件本质上只是一个重新命名的json文件,它们通常与包含顶点数据等内容的.bin文件相提并论,但这些内容也可以直接包含在json中。

GLB 文件类似于 GLTF 文件,但所有内容都包含在同一个文件中。它分为三个部分,一个小标头、json 字符串和二进制缓冲区。

图片来自官方gltf github

GLTF 格式

在 GLTF 中,与网格、动画和蒙皮相关的一切都存储在缓冲区中,虽然一开始,在没有库的情况下从原始二进制文件中读取似乎令人生畏,但实际上并不太难。我们将逐步进行。

在本文中,我们将介绍如何从 .gltf 和 .glb 文件中从单个网格读取顶点位置数据。

在我们获得实际代码之前,我们需要了解如何使用文件的 json 部分来查找我们想要的内容,因为我们必须跳来跳去才能找到任何东西。你可以从场景级别开始,如果需要,也可以逐步向下工作,但由于我计划只对单个网格使用格式,所以我将从图形的网格节点开始。

假设我们的 GLTF 文件看起来像这样(请注意,实际文件将包含更多数据):

{
    "accessors" : [
        {
         "bufferView": 0,
         "byteOffset": 0,
         "componentType": 5126,
         "count": 197,
         "max": [ -0.004780198, 0.0003038254, 0.007360002 ],
         "min": [ -0.008092392, -0.008303153, -0.007400591 ],
         "type": "VEC3"
      }
    ],
   "buffers": [
      {
         "byteLength": 2460034,
         "uri": "example.bin"
      }
   ],
    "bufferViews": [
      {
         "buffer": 0,
         "byteLength": 306642,
         "target": 34963,
         "byteOffset": 2153392
      },
    ],
    "meshes": [
      {
         "name": "example mesh",
         "primitives": [
            {
               "attributes": {
                  "POSITION": 0,
                  "NORMAL": 1,
                  "TEXCOORD_0": 2,
                  "TANGENT": 3
               },
               "indices": 4,
               "material": 0,
               "mode": 4
            }
         ]
      }
    ]
}

要查找网格的位置数据,我们首先需要访问索引 0 处的 “meshes” 键,然后访问第一个基元。(据我所知,基元本质上只是子网格。然后我们将检索“属性”->“位置”。这将为我们提供访问器的索引。插入它,我们可以从第一个访问器获取“bufferView”值。然后,这为我们提供了缓冲区视图的索引,我们最终可以使用它来获取缓冲区以从中检索数据。在这种情况下,缓冲区存储在外部文件“example.bin”中。打开该文件后,我们将转到访问器中“byteOffset”提供给我们的位置,最后读取缓冲区数据。

下面开始分别介绍如何从gltf/glb文件中读取数据 ,在此过程中你可以用GLTF编辑器对3D模型文件进行编辑和验证模型数据。

从 GLTF 文件读取

我将在我的示例代码中使用 c++ ,但对于任何其他语言,步骤应该大致相同。

// First define our filname, would probbably be better to prompt the user for one
const std::string& gltfFilename = "example.gltf"

// open the gltf file
std::ifstream jsonFile(gltfFilename, std::ios::binary);

// parse the json so we can use it later
Json::Value json;

try{
    jsonFile >> json;
}catch(const std::exception& e){
    std::cerr << "Json parsing error: " << e.what() << std::endl;
}
jsonFile.close();

// Extract the name of the bin file, for the sake of simplicity I'm assuming there's only one
std::string binFilename = json["buffers"][0]["uri"].asString();

// Open it with the cursor at the end of the file so we can determine it's size,
// We could techincally read the filesize from the gltf file, but I trust the file itself more
std::ifstream binFile = std::ifstream(binFilename, std::ios::binary | std::ios::ate);

// Read file length and then reset cursor
size_t binLength = binFile.tellg();
binFile.seekg(0);


std::vector<char> bin(binLength);
binFile.read(bin.data(), binLength);
binFile.close();



// Now that we have the files read out, let's actually do something with them
// This code prints out all the vertex positions for the first primitive

// Get the primitve we want to print out: 
Json::Value& primitive = json["meshes"][0]["primitives"][0];


// Get the accessor for position: 
Json::Value& positionAccessor = json["accessors"][primitive["attributes"]["POSITION"].asInt()];


// Get the bufferView 
Json::Value& bufferView = json["bufferViews"][positionAccessor["bufferView"].asInt()];


// Now get the start of the float3 array by adding the bufferView byte offset to the bin pointer
// It's a little sketchy to cast to a raw float array, but hey, it works.
float* buffer = (float*)(bin.data() + bufferView["byteOffset"].asInt());

// Print out all the vertex positions 
for (int i = 0; i < positionAccessor["count"].asInt(); ++i)
{
    std::cout << "(" << buffer[i*3] << ", " << buffer[i*3 + 1] << ", " << buffer[i*3 + 2] << ")" << std::endl;
}

// And as a cherry on top, let's print out the total number of verticies
std::cout << "vertices: " << positionAccessor["count"].asInt() << std::endl;

从 GLB 文件读取:

从 .glb 文件中读取有点困难,因为我们不能只是将其放入 JSON 解析器中,但它是可行的。在文件类型部分中参考上图,我们可以找到有关所需文件格式的所有信息:

std::ifstream binFile = std::ifstream(glbFilename, std::ios::binary); 

binFile.seekg(12); //Skip past the 12 byte header, to the json header
uint32_t jsonLength;
binFile.read((char*)&jsonLength, sizeof(uint32_t)); //Read the length of the json file from it's header

std::string jsonStr;
jsonStr.resize(jsonLength);
binFile.seekg(20); // Skip the rest of the JSON header to the start of the string
binFile.read(jsonStr.data(), jsonLength); // Read out the json string

// Parse the json
Json::Reader reader;
if(!reader.parse(jsonStr, _json))
	std::cerr << "Problem parsing assetData: " << jsonStr << std::endl;

// After reading from the json, the file cusor will automatically be at the start of the binary header

uint32_t binLength;
binFile.read((char*)&binLength, sizeof(binLength)); // Read out the bin length from it's header
binFile.seekg(sizeof(uint32_t), std::ios_base::cur); // skip chunk type

std::vector<char> bin(binLength);
binFile.read(bin.data(), binLength);


//Now you're free to use the data the same way we did above

总结

希望这对您有所帮助。我知道它在某些方面有点缺乏细节,所以一旦我了解更多,我可能会回来更新它,提供更多关于动画和皮肤的信息。但在那之前,再见。

原文链接:了解 glTF 2.0 格式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值