MaxMilan1 commited on
Commit
3aef850
1 Parent(s): 3094469

add GLB option

Browse files
Files changed (3) hide show
  1. app.py +11 -5
  2. src/utils/mesh_util.py +19 -3
  3. util/instantmesh.py +5 -2
app.py CHANGED
@@ -88,10 +88,16 @@ with gr.Blocks(theme=theme) as GenDemo:
88
  )
89
 
90
  with gr.Row():
91
- output_model_obj = gr.Model3D(
92
- label="Output Model (OBJ Format)",
93
- interactive=False,
94
- )
 
 
 
 
 
 
95
 
96
  with gr.Row():
97
  gr.Markdown('''Try a different <b>seed value</b> if the result is unsatisfying (Default: 42).''')
@@ -111,7 +117,7 @@ with gr.Blocks(theme=theme) as GenDemo:
111
  ).success(
112
  fn=make3d,
113
  inputs=[mv_images],
114
- outputs=[output_model_obj]
115
  )
116
 
117
  GenDemo.launch()
 
88
  )
89
 
90
  with gr.Row():
91
+ with gr.Tab("glb"):
92
+ output_model_glb = gr.Model3D(
93
+ label="Output Model (GLB Format)",
94
+ interactive=False,
95
+ )
96
+ with gr.Tab("obj"):
97
+ output_model_obj = gr.Model3D(
98
+ label="Output Model (OBJ Format)",
99
+ interactive=False,
100
+ )
101
 
102
  with gr.Row():
103
  gr.Markdown('''Try a different <b>seed value</b> if the result is unsatisfying (Default: 42).''')
 
117
  ).success(
118
  fn=make3d,
119
  inputs=[mv_images],
120
+ outputs=[output_model_obj, output_model_glb]
121
  )
122
 
123
  GenDemo.launch()
src/utils/mesh_util.py CHANGED
@@ -15,13 +15,29 @@ import nvdiffrast.torch as dr
15
  from PIL import Image
16
 
17
 
18
- def save_obj(pointnp_px3, facenp_fx3, colornp_px3, fname):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  mesh = trimesh.Trimesh(
20
  vertices=pointnp_px3,
21
  faces=facenp_fx3,
22
  vertex_colors=colornp_px3,
23
  )
24
- mesh.export(fname, 'obj')
25
 
26
 
27
  def save_obj_with_mtl(pointnp_px3, tcoords_px2, facenp_fx3, facetex_fx3, texmap_hxwx3, fname):
@@ -162,4 +178,4 @@ def xatlas_uvmap(ctx, mesh_v, mesh_pos_idx, resolution):
162
  # Interpolate world space position
163
  gb_pos, _ = interpolate(mesh_v[None, ...], rast, mesh_pos_idx.int())
164
  mask = rast[..., 3:4] > 0
165
- return uvs, mesh_tex_idx, gb_pos, mask
 
15
  from PIL import Image
16
 
17
 
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
+
35
  mesh = trimesh.Trimesh(
36
  vertices=pointnp_px3,
37
  faces=facenp_fx3,
38
  vertex_colors=colornp_px3,
39
  )
40
+ mesh.export(fpath, 'glb')
41
 
42
 
43
  def save_obj_with_mtl(pointnp_px3, tcoords_px2, facenp_fx3, facetex_fx3, texmap_hxwx3, fname):
 
178
  # Interpolate world space position
179
  gb_pos, _ = interpolate(mesh_v[None, ...], rast, mesh_pos_idx.int())
180
  mask = rast[..., 3:4] > 0
181
+ return uvs, mesh_tex_idx, gb_pos, mask
util/instantmesh.py CHANGED
@@ -19,7 +19,7 @@ from src.utils.camera_util import (
19
  get_zero123plus_input_cameras,
20
  get_circular_camera_poses,
21
  )
22
- from src.utils.mesh_util import save_obj
23
  from src.utils.infer_util import remove_background, resize_foreground, images_to_video
24
 
25
  import tempfile
@@ -186,6 +186,8 @@ def make3d(images):
186
  mesh_basename = os.path.basename(mesh_fpath).split('.')[0]
187
  mesh_dirname = os.path.dirname(mesh_fpath)
188
  video_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.mp4")
 
 
189
 
190
  with torch.no_grad():
191
  # get triplane
@@ -204,7 +206,8 @@ def make3d(images):
204
  faces = faces[:, [2, 1, 0]]
205
 
206
  save_obj(vertices, faces, vertex_colors, mesh_fpath)
 
207
 
208
  print(f"Mesh saved to {mesh_fpath}")
209
 
210
- return mesh_fpath
 
19
  get_zero123plus_input_cameras,
20
  get_circular_camera_poses,
21
  )
22
+ from src.utils.mesh_util import save_obj, save_glb
23
  from src.utils.infer_util import remove_background, resize_foreground, images_to_video
24
 
25
  import tempfile
 
186
  mesh_basename = os.path.basename(mesh_fpath).split('.')[0]
187
  mesh_dirname = os.path.dirname(mesh_fpath)
188
  video_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.mp4")
189
+ mesh_glb_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.glb")
190
+
191
 
192
  with torch.no_grad():
193
  # get triplane
 
206
  faces = faces[:, [2, 1, 0]]
207
 
208
  save_obj(vertices, faces, vertex_colors, mesh_fpath)
209
+ save_glb(vertices, faces, vertex_colors, mesh_glb_fpath)
210
 
211
  print(f"Mesh saved to {mesh_fpath}")
212
 
213
+ return mesh_fpath, mesh_glb_fpath