Skip to content

Commit bbd3937

Browse files
committed
add glb export
1 parent 3d4282b commit bbd3937

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

app.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
get_zero123plus_input_cameras,
1818
get_circular_camera_poses,
1919
)
20-
from src.utils.mesh_util import save_obj
20+
from src.utils.mesh_util import save_obj, save_glb
2121
from src.utils.infer_util import remove_background, resize_foreground, images_to_video
2222

2323
import tempfile
@@ -139,11 +139,12 @@ def generate_mvs(input_image, sample_steps, sample_seed):
139139

140140
return z123_image, show_image
141141

142+
142143
def make_mesh(mesh_fpath, planes):
143144

144145
mesh_basename = os.path.basename(mesh_fpath).split('.')[0]
145146
mesh_dirname = os.path.dirname(mesh_fpath)
146-
mesh_vis_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.glb")
147+
mesh_glb_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.glb")
147148

148149
with torch.no_grad():
149150
# get mesh
@@ -156,14 +157,14 @@ def make_mesh(mesh_fpath, planes):
156157

157158
vertices, faces, vertex_colors = mesh_out
158159
vertices = vertices[:, [1, 2, 0]]
159-
vertices[:, -1] *= -1
160-
faces = faces[:, [2, 1, 0]]
161-
160+
161+
save_glb(vertices, faces, vertex_colors, mesh_glb_fpath)
162162
save_obj(vertices, faces, vertex_colors, mesh_fpath)
163163

164164
print(f"Mesh saved to {mesh_fpath}")
165165

166-
return mesh_fpath
166+
return mesh_fpath, mesh_glb_fpath
167+
167168

168169
def make3d(images):
169170

@@ -217,10 +218,9 @@ def make3d(images):
217218

218219
print(f"Video saved to {video_fpath}")
219220

220-
mesh_fpath = make_mesh(mesh_fpath, planes)
221-
222-
return video_fpath, mesh_fpath
221+
mesh_fpath, mesh_glb_fpath = make_mesh(mesh_fpath, planes)
223222

223+
return video_fpath, mesh_fpath, mesh_glb_fpath
224224

225225

226226
import gradio as gr
@@ -316,11 +316,20 @@ def make3d(images):
316316
)
317317

318318
with gr.Row():
319-
output_model_obj = gr.Model3D(
320-
label="Output Model (OBJ Format)",
321-
# width=768,
322-
interactive=False,
323-
)
319+
with gr.Tab("OBJ"):
320+
output_model_obj = gr.Model3D(
321+
label="Output Model (OBJ Format)",
322+
#width=768,
323+
interactive=False,
324+
)
325+
with gr.Tab("GLB"):
326+
output_model_glb = gr.Model3D(
327+
label="Output Model (GLB Format)",
328+
#width=768,
329+
interactive=False,
330+
)
331+
gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")
332+
324333
with gr.Row():
325334
gr.Markdown('''Try a different <b>seed value</b> if the result is unsatisfying (Default: 42).''')
326335

@@ -339,7 +348,7 @@ def make3d(images):
339348
).success(
340349
fn=make3d,
341350
inputs=[mv_images],
342-
outputs=[output_video, output_model_obj]
351+
outputs=[output_video, output_model_obj, output_model_glb]
343352
)
344353

345354
demo.queue(max_size=10)

src/utils/mesh_util.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,29 @@
1515
from PIL import Image
1616

1717

18-
def save_obj(pointnp_px3, facenp_fx3, colornp_px3, fname):
18+
def save_obj(pointnp_px3, facenp_fx3, colornp_px3, fpath):
19+
20+
pointnp_px3 = pointnp_px3 @ np.array([[1, 0, 0], [0, 1, 0], [0, 0, -1]])
21+
facenp_fx3 = facenp_fx3[:, [2, 1, 0]]
22+
23+
mesh = trimesh.Trimesh(
24+
vertices=pointnp_px3,
25+
faces=facenp_fx3,
26+
vertex_colors=colornp_px3,
27+
)
28+
mesh.export(fpath, 'obj')
29+
30+
31+
def save_glb(pointnp_px3, facenp_fx3, colornp_px3, fpath):
32+
33+
pointnp_px3 = pointnp_px3 @ np.array([[-1, 0, 0], [0, 1, 0], [0, 0, -1]])
34+
1935
mesh = trimesh.Trimesh(
2036
vertices=pointnp_px3,
2137
faces=facenp_fx3,
2238
vertex_colors=colornp_px3,
2339
)
24-
mesh.export(fname, 'obj')
40+
mesh.export(fpath, 'glb')
2541

2642

2743
def save_obj_with_mtl(pointnp_px3, tcoords_px2, facenp_fx3, facetex_fx3, texmap_hxwx3, fname):

0 commit comments

Comments
 (0)