Commit
·
37c1f6f
1
Parent(s):
5f62420
Refactor texture baking logic in generate3d function of inference.py to utilize per-vertex colors for improved texture quality. Updated the method for filling the texture image to enhance the visual output in mesh exports.
Browse files- inference.py +12 -22
inference.py
CHANGED
@@ -68,29 +68,19 @@ def generate3d(model, rgb, ccm, device):
|
|
68 |
mesh_f = data_config['faces'].cpu().numpy()
|
69 |
vmapping, ft, vt = xatlas.parametrize(mesh_v, mesh_f)
|
70 |
|
71 |
-
#
|
|
|
|
|
|
|
|
|
|
|
72 |
tex_res = (1024, 1024)
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
# For simplicity, we'll sample random points on the mesh surface and use their UVs
|
80 |
-
# (A more advanced approach would rasterize each face, but this is a CPU-friendly approximation)
|
81 |
-
mesh_trimesh = trimesh.Trimesh(vertices=mesh_v, faces=mesh_f, process=False)
|
82 |
-
points, face_indices = trimesh.sample.sample_surface(mesh_trimesh, tex_res[0]*tex_res[1])
|
83 |
-
# Use the model's decoder and rgbMlp to get color for each sampled point
|
84 |
-
points_tensor = torch.from_numpy(points).float().unsqueeze(0).cpu() # (1, N, 3) on CPU
|
85 |
-
triplane_feature2 = triplane_feature2.cpu() # Ensure on CPU
|
86 |
-
with torch.no_grad():
|
87 |
-
dec_verts = model.decoder(triplane_feature2, points_tensor)
|
88 |
-
colors = model.rgbMlp(dec_verts).squeeze().cpu().numpy() # (N, 3)
|
89 |
-
colors = (colors * 0.5 + 0.5).clip(0, 1)
|
90 |
-
# Fill the texture image
|
91 |
-
texture = np.zeros((tex_res[1]*tex_res[0], 3), dtype=np.float32)
|
92 |
-
texture[:colors.shape[0]] = colors
|
93 |
-
texture = texture.reshape(tex_res[1], tex_res[0], 3)
|
94 |
texture = np.clip(texture, 0, 1)
|
95 |
|
96 |
# Create Mesh and export .glb
|
|
|
68 |
mesh_f = data_config['faces'].cpu().numpy()
|
69 |
vmapping, ft, vt = xatlas.parametrize(mesh_v, mesh_f)
|
70 |
|
71 |
+
# Use per-vertex colors if available, else fallback to white
|
72 |
+
vertex_colors = np.ones((mesh_v.shape[0], 3), dtype=np.float32) # fallback: white
|
73 |
+
# If you have per-vertex color, you can assign here, e.g.:
|
74 |
+
# vertex_colors = ...
|
75 |
+
|
76 |
+
# Bake vertex colors to texture in UV space
|
77 |
tex_res = (1024, 1024)
|
78 |
+
texture = np.zeros((tex_res[1], tex_res[0], 3), dtype=np.float32)
|
79 |
+
vt_img = (vt * np.array(tex_res)).astype(np.int32)
|
80 |
+
for face, uv_idx in zip(mesh_f, ft):
|
81 |
+
pts = vt_img[uv_idx]
|
82 |
+
color = vertex_colors[face].mean(axis=0)
|
83 |
+
cv2.fillPoly(texture, [pts], color.tolist())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
texture = np.clip(texture, 0, 1)
|
85 |
|
86 |
# Create Mesh and export .glb
|