利用Python和C++實(shí)現(xiàn)解析gltf文件
gltf是類似于stl、obj、ply等常見的3D對象存儲格式,它被設(shè)計出來是為了便于渲染的數(shù)據(jù)轉(zhuǎn)換和傳輸。如果你的瀏覽器可以連接外網(wǎng),可以通過 glTF Viewer 網(wǎng)址打開瀏覽gltf的3D對象。這里介紹兩種語言下從gltf拿到網(wǎng)格的頂點(diǎn)和面片數(shù)據(jù)。
一、Python
第一步安裝pygltflib:
pip install pygltflib
第二步,python解析:
import pygltflib import numpy as np pathGltf="test.gltf" gltf=pygltflib.GLTF2().load(pathGltf) scene=gltf.scenes[gltf.scene] nodes=[gltf.nodes[node] for node in scenes.nodes] vertices=np.arry([node.mesh.primitives[0].attributes["POSITION"] for node in nodes]) print(vertices)
不知道為什么,通過上面這種方式解析,node.mesh我這里是一個int類型的值,運(yùn)行代碼提示node.mesh沒有primitives屬性,然后在網(wǎng)上找了下面的代碼是ok的:
import pygltflib
import pathlib
import struct
# load a gltf file
fname = pathlib.Path("C:/Users/User/Desktop/cube.gltf")
gltf = GLTF2().load(fname)
# get the first mesh in the current scene
mesh = gltf.meshes[gltf.scenes[gltf.scene].nodes[0]-1]
# get the vertices for each primitive in the mesh
for primitive in mesh.primitives:
# get the binary data for this mesh primitive from the buffer
accessor = gltf.accessors[primitive.attributes.POSITION]
bufferView = gltf.bufferViews[accessor.bufferView]
buffer = gltf.buffers[bufferView.buffer]
data = gltf.get_data_from_buffer_uri(buffer.uri)
# pull each vertex from the binary buffer and convert it into a tuple of python floats
vertices = []
for i in range(accessor.count):
index = bufferView.byteOffset + accessor.byteOffset + i*12 # the location in the buffer of this vertex
d = data[index:index+12] # the vertex data
v = struct.unpack("<fff", d) # convert from base64 to three floats
vertices.append(v)
# unpack floats
vertices2 = []
for a,b,c in vertices:
vertices2 += [a,b,c]
# create triangles
vertices = vertices2
triangles = []
for i in range(0,len(vertices),9):
triangles.append(vertices[i:i+9])
# print data
print(triangles)二、C++解析
c++依賴的庫主要是draco,這個庫是開源的,網(wǎng)上可以下載,有了draco之后代碼如下:
#include<draco/io/gltf_decoder.h>
#include<draco/tools/draco_transcoder_lib.h>
// 讀取gltf文件
bool parse_gltf_from_file(const std::string& filename,std::unique_ptr<draco::Mesh>& mesh){
draco::GltfDecoder gltfDec;
draco::StatusOr<std::unique_ptr<draco::Mesh>> stormesh=gltfDec.DecodeFromFile(filename);
if(!stormesh.ok()){
return false;
}
std::unique_ptr<draco::Mesh> pDracomesh=std::move(stormesh).value();
std::cout<<"faces num:"<<pDracomesh->num_faces()<<std::endl;
pDracomesh.swap(mesh);
return true;
}
//解析出頂點(diǎn)和面片數(shù)據(jù)
bool get_faces_vertexes(const std::unique_ptr<draco::Mesh>& dracomesh,
std::vector<Eigen::Vector3>& vertexes,
std::vector<Eigen::Vector3i>& faces){
auto dump_attribute_to_vec3=[](const draco::PointAttribute& att,std::vector<Eigen::Vector3>& attD){
if(att.size()==0) return;
std::vector<Eigen::Vector3> tmp(att.size());
for(int i=0;i<att.size();++i){
if(!att.ConvertValue<float,3>(draco::AttributeValueIndex(i),&tmp[i][0])) return;
}
attD=std::move(tmp);
}
// 解析頂點(diǎn)
const draco::PointAttribute* posAtt=nullptr;
std::vector<Eigen::Vector3> points;
for(int i=0;i<dracomesh->num_attributes();++i){
const draco::PointAttribute* pAtt=dracomesh->attribute(i);
switch(pAtt->attribute_type()){
case draco::PointAttribute::POSITION:
posAtt=pAtt;
dump_attribute_to_vec3(*pAtt,points);
break;
}
}
vertexes=points;
// 解析面片
faces.resize(dracomesh->num_faces());
for(int i=0;i<dracomesh->num_faces();++i){
for(int j=0;j<3;++j){
const draco::PointIndex idx=dracomesh->face(draco::FaceIndex(i))[j];
faces[i][j]=posAtt->mapped_index(idx).value();
}
}
return true;
}以上就是利用Python和C++實(shí)現(xiàn)解析gltf文件的詳細(xì)內(nèi)容,更多關(guān)于Python解析gltf文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Pytorch實(shí)現(xiàn)網(wǎng)絡(luò)部分層的固定不進(jìn)行回傳更新問題及思路詳解
這篇文章主要介紹了Pytorch實(shí)現(xiàn)網(wǎng)絡(luò)部分層的固定不進(jìn)行回傳更新,實(shí)現(xiàn)思路就是利用tensor的requires_grad,每一個tensor都有自己的requires_grad成員,值只能為True和False,具體內(nèi)容詳情跟隨小編一起看看吧2021-08-08
Django框架model模型對象驗(yàn)證實(shí)現(xiàn)方法分析
這篇文章主要介紹了Django框架model模型對象驗(yàn)證實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Django框架model模型對象驗(yàn)證相關(guān)原理、實(shí)現(xiàn)步驟及操作注意事項(xiàng),需要的朋友可以參考下2019-10-10
Pygame框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Pygame框架實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
Flask應(yīng)用中執(zhí)行指定JavaScript腳本的6種方法
在 Flask 應(yīng)用中執(zhí)行指定 JavaScript 腳本,需結(jié)合后端路由,模板渲染及前后端交互技術(shù),本文主要介紹了六種核心方法及其實(shí)現(xiàn)細(xì)節(jié),大家可以根據(jù)需要進(jìn)行選擇2025-06-06

