Skip to content

Commit

Permalink
mesh: impl uv remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ashawkey committed May 16, 2024
1 parent 6fb1f05 commit 1765b18
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/source/blender.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ python -m kiui.cli.blender_render --mesh chest.glb --gpu 0 --depth --normal --al
Features include:
* Set which GPU to use with `--gpu 0` for `CYCLES` rendering engine.
* Render with random built-in HDRI environment texture shading (check `assets/blender_lights`).
* Optionally render depth (`exr`), normal, and albedo.
* Empirical cleaning of the scene (remove the annoying plane under the object).
* Empirical cleaning of the scene (remove the annoying plane under the object).
* Optionally render depth (`exr`), normal.
* Optionally render PBR (albedo, metallic, roughness).
29 changes: 29 additions & 0 deletions kiui/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,35 @@ def auto_uv(self, cache_path=None, vmap=True):
vmapping = torch.from_numpy(vmapping.astype(np.int64)).long().to(self.device)
self.align_v_to_vt(vmapping)

def remap_uv(self, v):
""" remap uv texture (vt) to other surface.
Args:
v (torch.Tensor): the target mesh vertices, float [N, 3].
"""

assert self.vt is not None

if self.v.shape[0] != self.vt.shape[0]:
self.align_v_to_vt()

# find the closest face for each vertex
import cubvh
BVH = cubvh.cuBVH(self.v, self.f)
dist, face_id, uvw = BVH.unsigned_distance(v, return_uvw=True)

# get original uv
faces = self.f[face_id].long()
vt0 = self.vt[faces[:, 0]]
vt1 = self.vt[faces[:, 1]]
vt2 = self.vt[faces[:, 2]]

# calc new uv
vt = vt0 * uvw[:, 0:1] + vt1 * uvw[:, 1:2] + vt2 * uvw[:, 2:3]

return vt


def align_v_to_vt(self, vmapping=None):
""" remap v/f and vn/fn to vt/ft.
Expand Down
31 changes: 31 additions & 0 deletions tests/test_remap_uv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import kiui
from kiui.mesh import Mesh
from kiui.mesh_utils import clean_mesh

import torch
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--mesh", type=str)
parser.add_argument("--mesh2", type=str)
parser.add_argument("--out", type=str, default='out.glb')
opt = parser.parse_args()

# load input mesh
mesh = Mesh.load(opt.mesh)

# must have texture to remap uv
assert mesh.albedo is not None

# load target mesh
mesh2 = Mesh.load(opt.mesh2)

# no texture for this mesh
assert mesh2.albedo is None

# remap uv
mesh2.albedo = mesh.albedo
mesh2.vt = mesh.remap_uv(mesh2.v)

# write
mesh2.write(opt.out)

0 comments on commit 1765b18

Please sign in to comment.