Commit
·
411e8a2
1
Parent(s):
df7cc11
Enhance color assignment in generate3d function by utilizing original vertex data
Browse files- Added functionality to retrieve colors for original vertices and assign them to new vertices based on nearest neighbor search using cKDTree.
- Updated mesh creation to include vertex colors, improving the visual fidelity of the generated mesh.
- Streamlined the vertex handling process by directly using original vertex data for color assignment.
- inference.py +14 -12
inference.py
CHANGED
@@ -62,14 +62,27 @@ def generate3d(model, rgb, ccm, device):
|
|
62 |
data_config['faces'] = faces
|
63 |
|
64 |
from kiui.mesh_utils import clean_mesh
|
|
|
|
|
65 |
verts, faces = clean_mesh(
|
66 |
-
|
67 |
data_config['faces'].squeeze().cpu().numpy().astype(np.int32),
|
68 |
repair=False, remesh=True, remesh_size=0.01, remesh_iters=2
|
69 |
)
|
70 |
data_config['verts'] = torch.from_numpy(verts).to(device).contiguous()
|
71 |
data_config['faces'] = torch.from_numpy(faces).to(device).contiguous()
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
# === Export OBJ/MTL/PNG ===
|
74 |
obj_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False).name
|
75 |
base_path = obj_path[:-4] # remove .obj
|
@@ -79,15 +92,4 @@ def generate3d(model, rgb, ccm, device):
|
|
79 |
|
80 |
model.export_mesh(data_config, base_path, tri_fea_2=triplane_feature2) # writes .obj
|
81 |
|
82 |
-
# Build KDTree for nearest neighbor search
|
83 |
-
tree = cKDTree(data_config['verts'].squeeze().cpu().numpy())
|
84 |
-
|
85 |
-
# For each new vertex, find the nearest old vertex and copy its color
|
86 |
-
k = 3 # or 5, etc.
|
87 |
-
dists, idxs = tree.query(verts, k=k)
|
88 |
-
new_colors = np.mean(color_tri[idxs], axis=1)
|
89 |
-
|
90 |
-
# Create the new mesh with colors
|
91 |
-
mesh = trimesh.Trimesh(vertices=verts, faces=faces, vertex_colors=new_colors)
|
92 |
-
|
93 |
return obj_path
|
|
|
62 |
data_config['faces'] = faces
|
63 |
|
64 |
from kiui.mesh_utils import clean_mesh
|
65 |
+
orig_verts = data_config['verts'].squeeze().cpu().numpy()
|
66 |
+
orig_colors = ... # get the color for each original vertex, shape [N, 3]
|
67 |
verts, faces = clean_mesh(
|
68 |
+
orig_verts.astype(np.float32),
|
69 |
data_config['faces'].squeeze().cpu().numpy().astype(np.int32),
|
70 |
repair=False, remesh=True, remesh_size=0.01, remesh_iters=2
|
71 |
)
|
72 |
data_config['verts'] = torch.from_numpy(verts).to(device).contiguous()
|
73 |
data_config['faces'] = torch.from_numpy(faces).to(device).contiguous()
|
74 |
|
75 |
+
# Build KDTree from original verts
|
76 |
+
tree = cKDTree(orig_verts)
|
77 |
+
|
78 |
+
# For each new vertex, find the nearest old vertex and copy its color
|
79 |
+
k = 3
|
80 |
+
dists, idxs = tree.query(verts, k=k)
|
81 |
+
new_colors = np.mean(orig_colors[idxs], axis=1)
|
82 |
+
|
83 |
+
# Create the new mesh with colors
|
84 |
+
mesh = trimesh.Trimesh(vertices=verts, faces=faces, vertex_colors=new_colors)
|
85 |
+
|
86 |
# === Export OBJ/MTL/PNG ===
|
87 |
obj_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False).name
|
88 |
base_path = obj_path[:-4] # remove .obj
|
|
|
92 |
|
93 |
model.export_mesh(data_config, base_path, tri_fea_2=triplane_feature2) # writes .obj
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
return obj_path
|