"); //-->
GLTF文件有两种不同的主要文件类型:.gltf和.glb。
GLTF文件本质上只是一个重新命名的json文件,它们通常与包含顶点数据等内容的.bin文件相提并论,但这些内容也可以直接包含在json中。
GLB 文件类似于 GLTF 文件,但所有内容都包含在同一个文件中。它分为三个部分,一个小标头、json 字符串和二进制缓冲区。
图片来自官方gltf github
在 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/GLB 文件 (mvrlink.com)
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。