kopia lustrzana https://github.com/OpenDroneMap/ODM
PoC gltf file loads without errors
rodzic
ff31f9b0dd
commit
cda8f227b1
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
import rasterio
|
||||
from rasterio.io import MemoryFile
|
||||
import warnings
|
||||
import numpy as np
|
||||
import pygltflib
|
||||
|
@ -61,6 +62,11 @@ def load_obj(obj_path, _info=print):
|
|||
cv, ct = map(int, c.split("/")[0:2])
|
||||
faces[current_material].append((av - 1, bv - 1, cv - 1, at - 1, bt - 1, ct - 1))
|
||||
|
||||
if len(vertices) > len(uvs):
|
||||
# Pad with empty UV coordinates
|
||||
add_uvs = len(vertices) - len(uvs)
|
||||
uvs += [[0,0]] * add_uvs
|
||||
|
||||
obj['vertices'] = np.array(vertices, dtype=np.float32)
|
||||
obj['uvs'] = np.array(uvs, dtype=np.float32)
|
||||
obj['normals'] = np.array(normals, dtype=np.float32)
|
||||
|
@ -89,9 +95,10 @@ def load_mtl(mtl_file, obj_base_path, _info=print):
|
|||
|
||||
_info("Loading %s" % map_kd_filename)
|
||||
|
||||
mats[current_mtl] = True
|
||||
# with rasterio.open(map_kd, 'r') as r:
|
||||
# mats[current_mtl] = r.read()
|
||||
with MemoryFile() as memfile:
|
||||
with rasterio.open(map_kd, 'r') as r:
|
||||
mats[current_mtl] = r.read()
|
||||
# TODO: copy code from export-rgb (webodm)
|
||||
return mats
|
||||
|
||||
|
||||
|
@ -100,22 +107,25 @@ def obj2glb(input_obj, output_glb, _info=print):
|
|||
|
||||
vertices = obj['vertices']
|
||||
uvs = obj['uvs']
|
||||
normals = obj['normals']
|
||||
|
||||
vertices_blob = vertices.tobytes()
|
||||
uvs_blob = uvs.tobytes()
|
||||
|
||||
faces = obj['faces']['material0000']
|
||||
material = obj['materials']['material0000']
|
||||
print(material.shape)
|
||||
print(material.)
|
||||
exit(1)
|
||||
# TODO: all faces
|
||||
|
||||
faces = np.array(faces, dtype=np.uint32)
|
||||
|
||||
#faces = faces[:2] # TODO REMOVE
|
||||
#print(faces)
|
||||
|
||||
indices = faces[:,0:3]
|
||||
uv_indices = faces[:,3:6]
|
||||
indices = faces[:,0:3].flatten()
|
||||
uv_indices = faces[:,3:6].flatten()
|
||||
|
||||
if faces.shape[1] == 9:
|
||||
normal_indices = faces[:,6:9]
|
||||
normal_indices = faces[:,6:9].flatten()
|
||||
else:
|
||||
normal_indices = None
|
||||
|
||||
|
@ -124,7 +134,7 @@ def obj2glb(input_obj, output_glb, _info=print):
|
|||
indices_blob = indices.tobytes()
|
||||
uv_indices_blob = uv_indices.tobytes()
|
||||
|
||||
bin = vertices_blob + uvs_blob + indices_blob + uv_indices_blob
|
||||
binary = vertices_blob + uvs_blob + indices_blob + uv_indices_blob
|
||||
|
||||
gltf = pygltflib.GLTF2(
|
||||
scene=0,
|
||||
|
@ -134,11 +144,20 @@ def obj2glb(input_obj, output_glb, _info=print):
|
|||
pygltflib.Mesh(
|
||||
primitives=[
|
||||
pygltflib.Primitive(
|
||||
attributes=pygltflib.Attributes(POSITION=0, TEXCOORD_0=1), indices=2
|
||||
attributes=pygltflib.Attributes(POSITION=0, TEXCOORD_0=1), indices=2, material=0
|
||||
)
|
||||
]
|
||||
)
|
||||
],
|
||||
materials=[
|
||||
|
||||
],
|
||||
textures=[
|
||||
pygltflib.Texture(source=0, sampler=0)
|
||||
],
|
||||
images=[
|
||||
pygltflib.Image(bufferView=0, mimeType="image/png") # TODO: use JPG
|
||||
],
|
||||
accessors=[
|
||||
pygltflib.Accessor(
|
||||
bufferView=0,
|
||||
|
@ -197,15 +216,21 @@ def obj2glb(input_obj, output_glb, _info=print):
|
|||
byteLength=len(uv_indices_blob),
|
||||
target=pygltflib.ELEMENT_ARRAY_BUFFER,
|
||||
),
|
||||
pygltflib.BufferView(
|
||||
buffer=0,
|
||||
byteOffset=len(vertices_blob) + len(uvs_blob) + len(indices_blob) + len(uv_indices_blob),
|
||||
byteLength=len(texture_blob),
|
||||
target=pygltflib.ARRAY_BUFFER
|
||||
)
|
||||
],
|
||||
buffers=[
|
||||
pygltflib.Buffer(
|
||||
byteLength=len(bin)
|
||||
byteLength=len(binary)
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
gltf.set_binary_blob(bin)
|
||||
gltf.set_binary_blob(binary)
|
||||
|
||||
gltf.save(output_glb)
|
||||
print("OK")
|
||||
|
|
|
@ -31,3 +31,4 @@ xmltodict==0.12.0
|
|||
fpdf2==2.4.6
|
||||
Shapely==1.7.1
|
||||
onnxruntime==1.12.1
|
||||
pygltflib==1.15.3
|
Ładowanie…
Reference in New Issue