File size: 1,809 Bytes
434b0b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pdb

import torch
import tqlt.mesh as tmesh
import trimesh
from core.opt import MeshOptimizer
from core.remesh import calc_vertex_normals
from einops import repeat
from tqdm import tqdm
from util.func import (
    load_obj,
    make_sphere,
    make_star_cameras,
    normalize_vertices,
    save_images,
    save_obj,
)
from util.render import NormalsRenderer
from util.snapshot import snapshot

try:
    from util.view import show
except:
    show = None


fname = "data/lucy.obj"
steps = 100
snapshot_step = 1

mv, proj = make_star_cameras(4, 4)
renderer = NormalsRenderer(mv, proj, [512, 512])

target_vertices, target_faces = load_obj(fname)
target_vertices = normalize_vertices(target_vertices)
target_normals = calc_vertex_normals(target_vertices, target_faces)
target_images = renderer.render(target_vertices, target_normals, target_faces)

alpha = repeat(target_images[..., 3:], "b h w 1 -> b h w 3")
save_images(target_images[..., :3], "./out/target_images/")
save_images(alpha, "./out/target_alpha/")

mesh = tmesh.Mesh.load("./smpl.obj")
mesh.v[..., 1] = -mesh.v[..., 1]
vertices = mesh.v
faces = mesh.f.long()

# vertices, faces = make_sphere(level=2, radius=0.5)

opt = MeshOptimizer(vertices, faces)
vertices = opt.vertices

for i in tqdm(range(steps)):
    normals = calc_vertex_normals(vertices, faces)
    images = renderer.render(vertices, normals, faces)
    loss = (images - target_images).abs().mean()
    loss.backward()
    opt.step()

    if show and i % snapshot_step == 0:
        snapshots.append(snapshot(opt))

    vertices, faces = opt.remesh()

    print(vertices.shape)

save_obj(vertices, faces, "./out/result.obj")
save_images(images[..., :3], "./out/images/")
save_images(images[..., 3:], "./out/alpha/")

if show:
    show(target_vertices, target_faces, snapshots)