Skip to content

Commit

Permalink
Applied xatlas - Meshes without UVs are also accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
daveredrum committed Feb 19, 2024
1 parent 5db4deb commit 8524249
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ For the best quality of texture synthesis, there are some necessary pre-processi

We provide `scripts/normalize_mesh.py` and `scripts/rotate_mesh.py` to make the mesh preprocessing easy for you.

If you already have a normalized mesh but haven't parameterized it yet, please use `scripts/parameterize_mesh.py` to generate the UV map.
~~If you already have a normalized mesh but haven't parameterized it yet, please use `scripts/parameterize_mesh.py` to generate the UV map.~~ Now, you don't have to parameterize your mesh by yourself thanks to [xatlas](https://github.com/jpcy/xatlas).

> NOTE: we expect the mesh to be triangulated.
Expand Down
Binary file modified lib/__pycache__/mesh_helper.cpython-39.pyc
Binary file not shown.
21 changes: 17 additions & 4 deletions lib/mesh_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import torch
import trimesh
import xatlas

import numpy as np

Expand All @@ -27,14 +28,26 @@ def compute_principle_directions(model_path, num_points=20000):
return principle_directions


def init_mesh(model_path, device):
def init_mesh(input_path, cache_path, device):
print("=> parameterizing target mesh...")

mesh = trimesh.load_mesh(input_path, force='mesh')
try:
vertices, faces = mesh.vertices, mesh.faces
except AttributeError:
print("multiple materials in {} are not supported".format(input_path))
exit()

vmapping, indices, uvs = xatlas.parametrize(vertices, faces)
xatlas.export(str(cache_path), vertices[vmapping], indices, uvs)

print("=> loading target mesh...")

# principle_directions = compute_principle_directions(model_path)
# principle_directions = compute_principle_directions(cache_path)
principle_directions = None

_, faces, aux = load_obj(model_path, device=device)
mesh = load_objs_as_meshes([model_path], device=device)
_, faces, aux = load_obj(cache_path, device=device)
mesh = load_objs_as_meshes([cache_path], device=device)

num_verts = mesh.verts_packed().shape[0]

Expand Down
6 changes: 5 additions & 1 deletion scripts/generate_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ def init_args():

# init resources
# init mesh
mesh, _, faces, aux, principle_directions, mesh_center, mesh_scale = init_mesh(os.path.join(args.input_dir, args.obj_file), DEVICE)
mesh, _, faces, aux, principle_directions, mesh_center, mesh_scale = init_mesh(
os.path.join(args.input_dir, args.obj_file),
os.path.join(output_dir, args.obj_file),
DEVICE
)

# gradient texture
init_texture = Image.open("./samples/textures/dummy.png").convert("RGB").resize((args.uv_size, args.uv_size))
Expand Down

0 comments on commit 8524249

Please sign in to comment.