十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決

(1)b3dm模型文件時(shí)二進(jìn)制文件,其中包含glTF文件:


當(dāng)使用tiny_gltf庫(kù)解析glTF時(shí),需要減去(28byte + featuretable的byte + batchTable的byte ):
bool TinyGLTF::ExtractGltfFromMemory(Model *model,
std::string *err,
std::string *warn,
const unsigned char *bytes,
unsigned int size,
const std::string &base_dir,
unsigned int check_sections) {
if (size< 28) {
if (err) {
(*err) = "Too short data size for b3dm Binary.";
}
return false;
}
if (bytes[0] == 'b' && bytes[1] == '3' && bytes[2] == 'd' &&
bytes[3] == 'm') {
// ok
} else {
if (err) {
(*err) = "Invalid magic.";
}
return false;
}
unsigned int version; // 4 bytes
unsigned int byteLength; // 4 bytes
unsigned int featureTableJSONByteLength; // 4 bytes
unsigned int featureTableBinaryByteLength;// 4 bytes;
unsigned int batchTableJSONByteLength; // 4 bytes
unsigned int batchTableBinaryByteLength; // 4 bytes;
// @todo { Endian swap for big endian machine. }
memcpy(&version, bytes + 4, 4);
swap4(&version);
memcpy(&byteLength, bytes + 8, 4);
swap4(&byteLength);
memcpy(&featureTableJSONByteLength, bytes + 12, 4);
swap4(&featureTableJSONByteLength);
memcpy(&featureTableBinaryByteLength, bytes + 16, 4);
swap4(&featureTableBinaryByteLength);
memcpy(&batchTableJSONByteLength, bytes + 20, 4);
swap4(&batchTableJSONByteLength);
memcpy(&batchTableBinaryByteLength, bytes + 24, 4);
swap4(&batchTableBinaryByteLength);
if ((byteLength != size) || (byteLength< 1) ) {
if (err) {
(*err) = "Invalid b3dm binary.";
}
return false;
}
const int byteOffset = 28 + featureTableJSONByteLength + batchTableJSONByteLength;
// 解析glTF二進(jìn)制
bool ret = LoadBinaryFromMemory(
model, err, warn, &bytes[byteOffset],
byteLength - byteOffset, base_dir, check_sections);
if (!ret) {
return ret;
}
return true;
}(2)使用tiny_gltf庫(kù)只需要3個(gè)文件stb_image.h,stb_image_write.h,json.hpp和tiny_gltf.h。
使用時(shí)需要注意,在調(diào)用#include "tiny_gltf.h"文件中的函數(shù)時(shí),需要添加三個(gè)宏,如:
// Define these only in *one* .cc file.
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
// #define TINYGLTF_NOEXCEPTION // optional. disable exception handling.
#include "tiny_gltf.h"
using namespace tinygltf;
Model model;
TinyGLTF loader;
std::string err;
std::string warn;
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, argv[1]);
//bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, argv[1]); // for binary glTF(.glb)
if (!warn.empty()) {
printf("Warn: %s\n", warn.c_str());
}
if (!err.empty()) {
printf("Err: %s\n", err.c_str());
}
if (!ret) {
printf("Failed to parse glTF\n");
return -1;
}3.關(guān)于glTF格式詳解{
"scenes" : [ // 只包含了一個(gè)場(chǎng)景
{
"nodes" : [ 0, 1]
}
],
"nodes" : [ // 將mesh對(duì)象附著到兩個(gè)不同的node對(duì)象,將其渲染了兩次
{
"mesh" : 0
},
{
"mesh" : 0,
"translation" : [ 1.0, 0.0, 0.0 ] // 使用translation屬性來(lái)將mesh對(duì)象的渲染位置移動(dòng)到其它地方
"rotation": [ 0.259, 0.0, 0.0, 0.966 ], // 四元數(shù)
"scale": [ 2.0, 1.0, 0.5 ] // x,y和z軸的縮放系數(shù)
// 或者只給出一個(gè)matrix進(jìn)行變換
"matrix": [ // 描述了一個(gè)縮放(2,1,0.5),繞x軸旋轉(zhuǎn)30度,平移(10,20,30)的matrix屬性
2.0, 0.0, 0.0, 0.0,
0.0, 0.866, 0.5, 0.0,
0.0, -0.25, 0.433, 0.0,
10.0, 20.0, 30.0, 1.0
]
}
],
"meshes" : [
{
"primitives" : [ {
"attributes" : {
"POSITION" : 1, // 引用了索引為1的accessor對(duì)象
"NORMAL" : 2 // "NORMAL"屬性引用了索引為2的accessor對(duì)象
},
"indices" : 0
} ]
}
],
"buffers" : [ // 表示了一個(gè)沒(méi)有任何層次結(jié)構(gòu)和意義的二進(jìn)制數(shù)據(jù)塊
{
"uri" : "data:application/octet-stream;base64,AAA為了排版而刪除,可以使用英文原文中的代碼=",
"byteLength" : 80 // 使用了大小為80字節(jié)的經(jīng)過(guò)編碼的數(shù)據(jù)URI作為緩沖的數(shù)據(jù)
}
],
"bufferViews" : [ // 一個(gè)bufferView對(duì)象代表了一個(gè)buffer對(duì)象的部分?jǐn)?shù)據(jù)
{
"buffer" : 0,
"byteOffset" : 0,
"byteLength" : 6, // 引用了索引為0的buffer對(duì)象的前6個(gè)字節(jié)的數(shù)據(jù)
"target" : 34963 // 表示數(shù)據(jù)使用方式的常量,ELEMENT_ARRAY_BUFFER
},
{
"buffer" : 0,
"byteOffset" : 8,
"byteLength" : 72, // 從偏移值8開(kāi)始的36個(gè)字節(jié)的buffer對(duì)象的數(shù)據(jù)
"target" : 34962 // ARRAY_BUFFER
}
],
"accessors" : [
{ // 表示頂點(diǎn)索引數(shù)據(jù)是unsigned short類(lèi)型的標(biāo)量
"bufferView" : 0, // 引用了索引為0的bufferView,這一bufferView對(duì)象描述了buffer數(shù)據(jù)中的頂點(diǎn)索引數(shù)據(jù)
"byteOffset" : 0, // 指定了accessor所訪問(wèn)數(shù)據(jù)的開(kāi)始位置
"componentType" : 5123, // 定義了數(shù)據(jù)分量的基礎(chǔ)類(lèi)型,5123代表UNSIGNED_SHORT。同short占用2個(gè)字節(jié)
"count" : 3, // count屬性指定了數(shù)據(jù)元素的數(shù)量,對(duì)應(yīng)三角形的3個(gè)頂點(diǎn)的索引值
"type" : "SCALAR",
"max" : [ 2 ],
"min" : [ 0 ]
},
{ // 這一accessor對(duì)象描述了分量類(lèi)型為float的3D向量數(shù)據(jù)
"bufferView" : 1,
"byteOffset" : 0, // 指定了accessor所訪問(wèn)數(shù)據(jù)的開(kāi)始位置
"componentType" : 5126, // 5126代表FLOAT,一個(gè)float類(lèi)型值占4個(gè)字節(jié)
"count" : 3, // count屬性指定了數(shù)據(jù)元素的數(shù)量
"type" : "VEC3",
"max" : [ 1.0, 1.0, 0.0 ], // min和max屬性定義了3D對(duì)象的包圍盒
"min" : [ 0.0, 0.0, 0.0 ]
},
{
"bufferView" : 1,
"byteOffset" : 36,
"componentType" : 5126, // 5126代表FLOAT
"count" : 3, // count屬性指定了數(shù)據(jù)元素的數(shù)量
"type" : "VEC3",
"max" : [ 0.0, 0.0, 1.0 ], // min和max屬性定義了3D對(duì)象的包圍盒
"min" : [ 0.0, 0.0, 1.0 ]
}
],
"asset" : {
"version" : "2.0"
}
}參考資料
1.解析json文件Reference:
RapidJSON的簡(jiǎn)單使用示例:
RapidJSON簡(jiǎn)介及使用
2.解析3dtile-b3dm格式Reference:
(1)對(duì)相關(guān)資料做了總結(jié)和推薦:
Cesium之b3dm格式_全??臻g-程序員秘密

(2)對(duì)b3dm二進(jìn)制結(jié)構(gòu)做了詳細(xì)的解析:
[3dTiles 數(shù)據(jù)規(guī)范詳解4.1] b3dm瓦片二進(jìn)制數(shù)據(jù)文件結(jié)構(gòu) _

(3)官網(wǎng)對(duì)Batched3DModel的介紹
3d-tiles/specification/TileFormats/Batched3DModel/

(4)3dtiles官方規(guī)范
3d-tiles/specification
(5)
3dTile 數(shù)據(jù)文件格式說(shuō)明


(6)3dtiles規(guī)范中文版pdf
鏈接: https://pan.baidu.com/s/1hhbvD_2DXrPCTOs7slsNHA 提取碼: ejm7
3.glTF(1)glTF官方github:
KhronosGroup/glTF
(2)glTF規(guī)范:
Specification.adoc
(3)對(duì)glTF格式的詳細(xì)介紹,教程(glTF官方文檔翻譯)
glTF格式詳解(glTF格式基本結(jié)構(gòu))
(4)博主的github寫(xiě)了一個(gè)解析glTF的工具:
模型導(dǎo)入之優(yōu)雅姿勢(shì)——glTF
songchaow/toy-renderer
(5)解析b3dm模型的庫(kù)tiny_gltf(項(xiàng)目中用這個(gè)庫(kù)解析)
https://github.com/fanvanzh/3dtiles
(6)tiny_gltf原始項(xiàng)目出處:
chainblocks/tinygltf
(7)glTF模型文件講解(B站)
【日常 | 學(xué)習(xí)Vlog | 程序媛樂(lè)樂(lè)】數(shù)字孿生基礎(chǔ) | 三維模型 | What is glTF?

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧