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- 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).
|
61 |
-
data_config['faces'] = torch.from_numpy(faces).
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|