Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
9ee53e8
·
1 Parent(s): d454202

Refactor generate3d function in inference.py to integrate xatlas for UV parameterization and improve texture baking logic. Updated mesh export process to create GLB files directly from the generated mesh, enhancing overall functionality and maintainability.

Browse files
Files changed (1) hide show
  1. inference.py +22 -15
inference.py CHANGED
@@ -4,6 +4,7 @@ import time
4
  import tempfile
5
  import zipfile
6
  import nvdiffrast.torch as dr
 
7
 
8
  from util.utils import get_tri
9
  from mesh import Mesh
@@ -57,21 +58,27 @@ def generate3d(model, rgb, ccm, device):
57
  data_config['faces'].squeeze().cpu().numpy().astype(np.int32),
58
  repair=False, remesh=True, remesh_size=0.005, remesh_iters=1
59
  )
60
- data_config['verts'] = torch.from_numpy(verts).cuda().contiguous()
61
- data_config['faces'] = torch.from_numpy(faces).cuda().contiguous()
62
 
63
- # Create base filename
64
- temp_path = tempfile.NamedTemporaryFile(suffix="", delete=False).name
65
- obj_base = temp_path # no extension
 
66
 
67
- # Export mesh with UV and PNG
68
- glctx = dr.RasterizeCudaContext()
69
- model.export_mesh_wt_uv(
70
- glctx, data_config, obj_base, "", device, res=(1024, 1024), tri_fea_2=triplane_feature2
71
- )
72
-
73
- # Load .obj with texture and export .glb
74
- mesh = trimesh.load(obj_base + ".obj", process=False)
75
- mesh.export(obj_base + ".glb")
76
 
77
- return obj_base + ".glb"
 
 
 
 
 
 
 
 
 
 
 
4
  import tempfile
5
  import zipfile
6
  import nvdiffrast.torch as dr
7
+ import xatlas
8
 
9
  from util.utils import get_tri
10
  from mesh import Mesh
 
58
  data_config['faces'].squeeze().cpu().numpy().astype(np.int32),
59
  repair=False, remesh=True, remesh_size=0.005, remesh_iters=1
60
  )
61
+ data_config['verts'] = torch.from_numpy(verts).contiguous()
62
+ data_config['faces'] = torch.from_numpy(faces).contiguous()
63
 
64
+ # Generate UVs using xatlas (CPU)
65
+ mesh_v = data_config['verts'].cpu().numpy()
66
+ mesh_f = data_config['faces'].cpu().numpy()
67
+ vmapping, ft, vt = xatlas.parametrize(mesh_v, mesh_f)
68
 
69
+ # Bake texture (simulate what export_mesh_wt_uv does, but CPU-only)
70
+ # Here, we just fill with white for demo; replace with your actual texture baking logic
71
+ tex_res = (1024, 1024)
72
+ albedo = np.ones((tex_res[0], tex_res[1], 3), dtype=np.float32) # TODO: bake your texture here
 
 
 
 
 
73
 
74
+ # Create Mesh and export .glb
75
+ mesh = Mesh(
76
+ v=torch.from_numpy(mesh_v).float(),
77
+ f=torch.from_numpy(mesh_f).int(),
78
+ vt=torch.from_numpy(vt).float(),
79
+ ft=torch.from_numpy(ft).int(),
80
+ albedo=torch.from_numpy(albedo).float()
81
+ )
82
+ temp_path = tempfile.NamedTemporaryFile(suffix=".glb", delete=False).name
83
+ mesh.write(temp_path)
84
+ return temp_path