-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpreprocess_mesh.py
More file actions
1548 lines (1310 loc) · 61.6 KB
/
preprocess_mesh.py
File metadata and controls
1548 lines (1310 loc) · 61.6 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import argparse
import numpy as np
import torch, os, sys, io
import torch.nn.functional as F
import trimesh, cv2, os, xatlas
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Literal,
NamedTuple,
NewType,
Optional,
Sized,
Tuple,
Type,
TypeVar,
Union,
)
from jaxtyping import Bool, Complex, Float, Inexact, Int, Integer, Num, Shaped, UInt
from omegaconf import DictConfig
from torch import Tensor
from typeguard import typechecked as typechecker
from numpy import ndarray
from tqdm.contrib.concurrent import process_map
# import multiprocessing as mp
from tqdm import tqdm, trange
import imageio
from collections import namedtuple
from scipy.spatial.transform import Rotation
from megfile import smart_open, smart_glob, smart_copy
import shutil
import nvdiffrast.torch as dr
import logging
logging.getLogger('nvdiffrast').setLevel(logging.ERROR)
glctx = dr.RasterizeCudaContext()
# torch / numpy math utils
def dot(x: Union[Tensor, ndarray], y: Union[Tensor, ndarray]) -> Union[Tensor, ndarray]:
"""dot product (along the last dim).
Args:
x (Union[Tensor, ndarray]): x, [..., C]
y (Union[Tensor, ndarray]): y, [..., C]
Returns:
Union[Tensor, ndarray]: x dot y, [..., 1]
"""
if isinstance(x, np.ndarray):
return np.sum(x * y, -1, keepdims=True)
else:
return torch.sum(x * y, -1, keepdim=True)
def length(x: Union[Tensor, ndarray], eps=1e-20) -> Union[Tensor, ndarray]:
"""length of an array (along the last dim).
Args:
x (Union[Tensor, ndarray]): x, [..., C]
eps (float, optional): eps. Defaults to 1e-20.
Returns:
Union[Tensor, ndarray]: length, [..., 1]
"""
if isinstance(x, np.ndarray):
return np.sqrt(np.maximum(np.sum(x * x, axis=-1, keepdims=True), eps))
else:
return torch.sqrt(torch.clamp(dot(x, x), min=eps))
def safe_normalize(x: Union[Tensor, ndarray], eps=1e-20) -> Union[Tensor, ndarray]:
"""normalize an array (along the last dim).
Args:
x (Union[Tensor, ndarray]): x, [..., C]
eps (float, optional): eps. Defaults to 1e-20.
Returns:
Union[Tensor, ndarray]: normalized x, [..., C]
"""
return x / length(x, eps)
def make_divisible(x: int, m: int = 8):
"""make an int x divisible by m.
Args:
x (int): x
m (int, optional): m. Defaults to 8.
Returns:
int: x + (m - x % m)
"""
return int(x + (m - x % m))
def look_at(campos, target, opengl=True):
"""construct pose rotation matrix by look-at.
Args:
campos (np.ndarray): camera position, float [3]
target (np.ndarray): look at target, float [3]
opengl (bool, optional): whether use opengl camera convention (forward direction is target --> camera). Defaults to True.
Returns:
np.ndarray: the camera pose rotation matrix, float [3, 3], normalized.
"""
if not opengl:
# forward is camera --> target
forward_vector = safe_normalize(target - campos)
up_vector = np.array([0, 1, 0], dtype=np.float32)
right_vector = safe_normalize(np.cross(forward_vector, up_vector))
up_vector = safe_normalize(np.cross(right_vector, forward_vector))
else:
# forward is target --> camera
forward_vector = safe_normalize(campos - target)
up_vector = np.array([0, 1, 0], dtype=np.float32)
right_vector = safe_normalize(np.cross(up_vector, forward_vector))
up_vector = safe_normalize(np.cross(forward_vector, right_vector))
R = np.stack([right_vector, up_vector, forward_vector], axis=1)
return R
def orbit_camera(elevation, azimuth, radius=1, is_degree=True, target=None, opengl=True):
"""construct a camera pose matrix orbiting a target with elevation & azimuth angle.
Args:
elevation (float): elevation in (-90, 90), from +y to -y is (-90, 90)
azimuth (float): azimuth in (-180, 180), from +z to +x is (0, 90)
radius (int, optional): camera radius. Defaults to 1.
is_degree (bool, optional): if the angles are in degree. Defaults to True.
target (np.ndarray, optional): look at target position. Defaults to None.
opengl (bool, optional): whether to use OpenGL camera convention. Defaults to True.
Returns:
np.ndarray: the camera pose matrix, float [4, 4]
"""
if is_degree:
elevation = np.deg2rad(elevation)
azimuth = np.deg2rad(azimuth)
x = radius * np.cos(elevation) * np.sin(azimuth)
y = - radius * np.sin(elevation)
z = radius * np.cos(elevation) * np.cos(azimuth)
if target is None:
target = np.zeros([3], dtype=np.float32)
campos = np.array([x, y, z]) + target # [3]
T = np.eye(4, dtype=np.float32)
T[:3, :3] = look_at(campos, target, opengl)
T[:3, 3] = campos
return T
class OrbitCamera:
""" An orbital camera class.
"""
def __init__(self, W, H, r=2, fovy=60, near=0.01, far=100):
"""init function
Args:
W (int): image width
H (int): image height
r (int, optional): camera radius. Defaults to 2.
fovy (int, optional): camera field of view in degree along y-axis. Defaults to 60.
near (float, optional): near clip plane. Defaults to 0.01.
far (int, optional): far clip plane. Defaults to 100.
"""
self.W = W
self.H = H
self.radius = r # camera distance from center
self.fovy = np.deg2rad(fovy) # deg 2 rad
self.near = near
self.far = far
self.center = np.array([0, 0, 0], dtype=np.float32) # look at this point
self.rot = Rotation.from_matrix(np.eye(3))
self.up = np.array([0, 1, 0], dtype=np.float32) # need to be normalized!
@property
def fovx(self):
"""get the field of view in radians along x-axis
Returns:
float: field of view in radians along x-axis
"""
return 2 * np.arctan(np.tan(self.fovy / 2) * self.W / self.H)
@property
def campos(self):
"""get the camera position
Returns:
np.ndarray: camera position, float [3]
"""
return self.pose[:3, 3]
@property
def pose(self):
"""get the camera pose matrix (cam2world)
Returns:
np.ndarray: camera pose, float [4, 4]
"""
# first move camera to radius
res = np.eye(4, dtype=np.float32)
res[2, 3] = self.radius # opengl convention...
# rotate
rot = np.eye(4, dtype=np.float32)
rot[:3, :3] = self.rot.as_matrix()
res = rot @ res
# translate
res[:3, 3] -= self.center
return res
@property
def view(self):
"""get the camera view matrix (world2cam, inverse of cam2world)
Returns:
np.ndarray: camera view, float [4, 4]
"""
return np.linalg.inv(self.pose)
@property
def perspective(self):
"""get the perspective matrix
Returns:
np.ndarray: camera perspective, float [4, 4]
"""
y = np.tan(self.fovy / 2)
aspect = self.W / self.H
# return np.array(
# [
# [1 / (y * aspect), 0, 0, 0],
# [0, -1 / y, 0, 0],
# [
# 0,
# 0,
# -(self.far + self.near) / (self.far - self.near),
# -(2 * self.far * self.near) / (self.far - self.near),
# ],
# [0, 0, -1, 0],
# ],
# dtype=np.float32,
# )
return np.array(
[
[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -2/(self.far - self.near), -(self.far + self.near)/(self.far - self.near)],
[0, 0, 0, 1],
],
dtype=np.float32,
)
# intrinsics
@property
def intrinsics(self):
"""get the camera intrinsics
Returns:
np.ndarray: intrinsics (fx, fy, cx, cy), float [4]
"""
focal = self.H / (2 * np.tan(self.fovy / 2))
return np.array([focal, focal, self.W // 2, self.H // 2], dtype=np.float32)
@property
def mvp(self):
"""get the MVP (model-view-perspective) matrix.
Returns:
np.ndarray: camera MVP, float [4, 4]
"""
return self.perspective @ np.linalg.inv(self.pose) # [4, 4]
def orbit(self, dx, dy):
""" rotate along camera up/side axis!
Args:
dx (float): delta step along x (up).
dy (float): delta step along y (side).
"""
side = self.rot.as_matrix()[:3, 0]
rotvec_x = self.up * np.radians(-0.05 * dx)
rotvec_y = side * np.radians(-0.05 * dy)
self.rot = Rotation.from_rotvec(rotvec_x) * Rotation.from_rotvec(rotvec_y) * self.rot
def scale(self, delta):
"""scale the camera.
Args:
delta (float): delta step.
"""
self.radius *= 1.1 ** (-delta)
def pan(self, dx, dy, dz=0):
"""pan the camera.
Args:
dx (float): delta step along x.
dy (float): delta step along y.
dz (float, optional): delta step along x. Defaults to 0.
"""
# pan in camera coordinate system (careful on the sensitivity!)
self.center += 0.0005 * self.rot.as_matrix()[:3, :3] @ np.array([dx, -dy, dz])
def from_angle(self, elevation, azimuth, is_degree=True):
"""set the camera pose from elevation & azimuth angle.
Args:
elevation (float): elevation in (-90, 90), from +y to -y is (-90, 90)
azimuth (float): azimuth in (-180, 180), from +z to +x is (0, 90)
is_degree (bool, optional): whether the angles are in degree. Defaults to True.
"""
if is_degree:
elevation = np.deg2rad(elevation)
azimuth = np.deg2rad(azimuth)
x = self.radius * np.cos(elevation) * np.sin(azimuth)
y = - self.radius * np.sin(elevation)
z = self.radius * np.cos(elevation) * np.cos(azimuth)
campos = np.array([x, y, z]) # [N, 3]
rot_mat = look_at(campos, np.zeros([3], dtype=np.float32))
self.rot = Rotation.from_matrix(rot_mat)
class Renderer:
def __init__(self, opt):
self.opt = opt
self.W = opt.W
self.H = opt.H
self.cam = OrbitCamera(opt.W, opt.H, r=opt.radius, fovy=opt.fovy)
self.bg_color = torch.ones(3, dtype=torch.float32).cuda() # default white bg
# self.bg_color = torch.zeros(3, dtype=torch.float32).cuda() # black bg
self.render_buffer = np.zeros((self.W, self.H, 3), dtype=np.float32)
self.alpha = np.zeros((self.W, self.H, 1), dtype=np.float32)
self.need_update = True # camera moved, should reset accumulation
self.light_dir = np.array([0, 0])
self.ambient_ratio = 0.5
# auto-rotate
self.auto_rotate_cam = False
self.auto_rotate_light = False
self.mode = opt.mode
self.render_modes = ['albedo', 'depth', 'normal', 'lambertian']
# load pbr if enabled
if self.opt.pbr:
import envlight
if self.opt.envmap is None:
hdr_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '/mnt/share/cq8/wadecheng/thirdparties/kiuikit/kiui/assets/lights/mud_road_puresky_1k.hdr')
else:
hdr_path = self.opt.envmap
self.light = envlight.EnvLight(hdr_path, scale=2, device='cuda')
self.FG_LUT = torch.from_numpy(np.fromfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "/mnt/share/cq8/wadecheng/thirdparties/kiuikit/kiui/assets/lights/bsdf_256_256.bin"), dtype=np.float32).reshape(1, 256, 256, 2)).cuda()
self.metallic_factor = 1
self.roughness_factor = 1
self.render_modes.append('pbr')
def __del__(self):
pass
def step(self, mesh, glctx):
if not self.need_update:
return
starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
starter.record()
# do MVP for vertices
pose = torch.from_numpy(self.cam.pose.astype(np.float32)).cuda()
proj = torch.from_numpy(self.cam.perspective.astype(np.float32)).cuda()
v_cam = torch.matmul(F.pad(mesh.v, pad=(0, 1), mode='constant', value=1.0), torch.inverse(pose).T).float().unsqueeze(0)
v_clip = v_cam @ proj.T
# print(v_clip[...,:3].min(), v_clip[...,:3].max(), v_clip.shape)
rast, rast_db = dr.rasterize(glctx, v_clip, mesh.f, (self.H, self.W))
# print(rast.min(), rast.max())
alpha = (rast[..., 3:] > 0).float()
alpha = dr.antialias(alpha, rast, v_clip, mesh.f).squeeze(0).clamp(0, 1) # [H, W, 3]
if self.mode == 'depth':
depth, _ = dr.interpolate(-v_cam[..., [2]], rast, mesh.f) # [1, H, W, 1]
depth = (depth - depth.min()) / (depth.max() - depth.min() + 1e-20)
buffer = depth.squeeze(0).detach().cpu().numpy().repeat(3, -1) # [H, W, 3]
elif self.mode == 'uv':
texc, _ = dr.interpolate(mesh.vt.unsqueeze(0).contiguous(), rast, mesh.ft)
texc = torch.where(rast[..., 3:] > 0, texc, torch.tensor(1).to(texc.device)) # remove background
buffer = texc.squeeze(0).detach().cpu().numpy()
buffer = np.concatenate([buffer, np.ones_like(buffer[...,:1])], axis=-1)
elif self.mode == 'inv_uv':
# texc, _ = dr.interpolate(mesh.vt.unsqueeze(0).contiguous(), rast, mesh.ft)
# albedo = dr.texture(mesh.albedo.unsqueeze(0), texc, filter_mode='linear')
# valid_face_idx = torch.unique(rast[...,3]).int()[1:] - 1
# # vt_clip = v_clip.clone()
# # vt_clip[..., :2] = mesh.vt.unsqueeze(0) * 2 - 1
# # # v_clip[...,1] = -v_clip[...,1]
# vt_clip = mesh.vt.unsqueeze(0) * 2 - 1
# vt_clip = torch.cat(
# (
# vt_clip,
# torch.zeros_like(vt_clip[..., 0:1]),
# torch.ones_like(vt_clip[..., 0:1]),
# ),
# dim=-1,
# )
# v_clip[...,:2] = (v_clip[...,:2] + 1) / 2
# faces = mesh.f.clone()[valid_face_idx]
# rast_tex, rast_tex_db = dr.rasterize(glctx, vt_clip, mesh.ft[valid_face_idx], (self.H, self.W))
# rast_texc, _ = dr.interpolate(v_clip[...,:2].contiguous(), rast_tex, mesh.ft[valid_face_idx])
# # recon_albedo = dr.texture(albedo, rast_texc, filter_mode='linear')
# rast_texc = torch.where(rast_tex[..., 3:] > 0, rast_texc, torch.tensor(0).to(texc.device)) # remove background
# buffer = rast_texc.squeeze(0).detach().cpu().numpy()
# if buffer.shape[-1] == 2:
# buffer = np.concatenate([buffer, np.zeros_like(buffer[...,:1])], axis=-1)
valid_face_idx = torch.unique(rast[...,3]).int()[1:] - 1
vt_clip = mesh.vt.unsqueeze(0) * 2 - 1
vt_clip = torch.cat(
(
vt_clip,
torch.zeros_like(vt_clip[..., 0:1]),
torch.ones_like(vt_clip[..., 0:1]),
),
dim=-1,
)
v_clip[...,:2] = (v_clip[...,:2] + 1) / 2
faces = mesh.f.clone()[valid_face_idx]
rast_tex, rast_tex_db = dr.rasterize(glctx, vt_clip, mesh.ft[valid_face_idx], (self.H, self.W))
rast_texc, _ = dr.interpolate(v_clip[...,:2].contiguous(), rast_tex, mesh.f[valid_face_idx])
rast_texc = torch.where(rast_tex[..., 3:] > 0, rast_texc, torch.tensor(1).to(rast_texc.device)) # remove background
buffer = rast_texc.squeeze(0).detach().cpu().numpy()
if buffer.shape[-1] == 2:
buffer = np.concatenate([buffer, np.ones_like(buffer[...,:1])], axis=-1)
else:
# use vertex color if exists
if mesh.vc is not None:
albedo, _ = dr.interpolate(mesh.vc.unsqueeze(0).contiguous(), rast, mesh.f)
# use texture image
elif mesh.albedo is not None:
texc, _ = dr.interpolate(mesh.vt.unsqueeze(0).contiguous(), rast, mesh.ft)
albedo = dr.texture(mesh.albedo.unsqueeze(0), texc, filter_mode='linear') # [1, H, W, 3]
else:
mesh.vc = torch.ones_like(mesh.v) * 0.75
albedo, _ = dr.interpolate(mesh.vc.unsqueeze(0).contiguous(), rast, mesh.f)
# print(mesh.v.dtype, mesh.vc.dtype, albedo.dtype, rast.dtype)
albedo = torch.where(rast[..., 3:] > 0, albedo, torch.tensor(0.).to(albedo.device)) # remove background
albedo = dr.antialias(albedo, rast, v_clip, mesh.f).clamp(0, 1) # [1, H, W, 3]
if self.mode == 'albedo':
albedo = albedo * alpha + self.bg_color * (1 - alpha)
buffer = albedo[0].detach().cpu().numpy()
else:
normal, _ = dr.interpolate(mesh.vn.unsqueeze(0).contiguous(), rast, mesh.fn)
normal = safe_normalize(normal)
if self.mode == 'normal':
normal_image = (normal[0] + 1) / 2
normal_image = torch.where(rast[..., 3:] > 0, normal_image, torch.tensor(1).to(normal_image.device)) # remove background
buffer = normal_image[0].detach().cpu().numpy()
# normal_image = (normal[0] + 1) / 2
# viewdir = safe_normalize(pose[:3, 3])
# # print(viewdir)
# nn = torch.where(rast[..., 3:] > 0, normal[0], torch.tensor(0).to(normal.device))
# cos_angle = torch.sum(viewdir[None,:] * nn.reshape(-1,3), dim=-1, keepdim=True).reshape([1, normal.shape[1], normal.shape[2], 1])
# # print(cos_angle.min(), cos_angle.max())
# normal_image = torch.cat([cos_angle]*3, dim=-1).clamp(0, 1)
# normal_image[...,0][cos_angle[...,0]<0] = 1
# normal_image[...,1:][cos_angle[...,0]<0] = 0
# # normal_image = torch.where(rast[..., 3:] > 0, normal_image, torch.tensor(1).to(normal_image.device)) # remove background
# buffer = normal_image[0].detach().cpu().numpy()
elif self.mode == 'lambertian':
light_d = np.deg2rad(self.light_dir)
light_d = np.array([
np.cos(light_d[0]) * np.sin(light_d[1]),
-np.sin(light_d[0]),
np.cos(light_d[0]) * np.cos(light_d[1]),
], dtype=np.float32)
light_d = torch.from_numpy(light_d).to(albedo.device)
lambertian = self.ambient_ratio + (1 - self.ambient_ratio) * (normal @ light_d).float().clamp(min=0)
albedo = (albedo * lambertian.unsqueeze(-1)) * alpha + self.bg_color * (1 - alpha)
buffer = albedo[0].detach().cpu().numpy()
elif self.mode == 'pbr':
if mesh.metallicRoughness is not None:
metallicRoughness = dr.texture(mesh.metallicRoughness.unsqueeze(0), texc, filter_mode='linear') # [1, H, W, 3]
metallic = metallicRoughness[..., 2:3] * self.metallic_factor
roughness = metallicRoughness[..., 1:2] * self.roughness_factor
else:
metallic = torch.ones_like(albedo[..., :1]) * self.metallic_factor
roughness = torch.ones_like(albedo[..., :1]) * self.roughness_factor
xyzs, _ = dr.interpolate(mesh.v.unsqueeze(0), rast, mesh.f) # [1, H, W, 3]
viewdir = safe_normalize(xyzs - pose[:3, 3])
n_dot_v = (normal * viewdir).sum(-1, keepdim=True) # [1, H, W, 1]
reflective = n_dot_v * normal * 2 - viewdir
diffuse_albedo = (1 - metallic) * albedo
fg_uv = torch.cat([n_dot_v, roughness], -1).clamp(0, 1) # [H, W, 2]
fg = dr.texture(
self.FG_LUT,
fg_uv.reshape(1, -1, 1, 2).contiguous(),
filter_mode="linear",
boundary_mode="clamp",
).reshape(1, self.H, self.W, 2)
F0 = (1 - metallic) * 0.04 + metallic * albedo
specular_albedo = F0 * fg[..., 0:1] + fg[..., 1:2]
diffuse_light = self.light(normal)
specular_light = self.light(reflective, roughness)
color = diffuse_albedo * diffuse_light + specular_albedo * specular_light # [H, W, 3]
color = color * alpha + self.bg_color * (1 - alpha)
buffer = color[0].detach().cpu().numpy()
ender.record()
torch.cuda.synchronize()
t = starter.elapsed_time(ender)
self.need_update = False
if self.auto_rotate_cam:
self.cam.orbit(5, 0)
self.need_update = True
if self.auto_rotate_light:
self.light_dir[1] += 3
self.need_update = True
return buffer
class MeshLoader:
"""
A torch-native trimesh class, with support for ``ply/obj/glb`` formats.
Note:
This class only supports one mesh with a single texture image (an albedo texture and a metallic-roughness texture).
"""
def __init__(
self,
v: Optional[Tensor] = None,
f: Optional[Tensor] = None,
vn: Optional[Tensor] = None,
fn: Optional[Tensor] = None,
vt: Optional[Tensor] = None,
ft: Optional[Tensor] = None,
vc: Optional[Tensor] = None, # vertex color
albedo: Optional[Tensor] = None,
metallicRoughness: Optional[Tensor] = None,
device: Optional[torch.device] = None,
):
"""Init a mesh directly using all attributes.
Args:
v (Optional[Tensor]): vertices, float [N, 3]. Defaults to None.
f (Optional[Tensor]): faces, int [M, 3]. Defaults to None.
vn (Optional[Tensor]): vertex normals, float [N, 3]. Defaults to None.
fn (Optional[Tensor]): faces for normals, int [M, 3]. Defaults to None.
vt (Optional[Tensor]): vertex uv coordinates, float [N, 2]. Defaults to None.
ft (Optional[Tensor]): faces for uvs, int [M, 3]. Defaults to None.
vc (Optional[Tensor]): vertex colors, float [N, 3]. Defaults to None.
albedo (Optional[Tensor]): albedo texture, float [H, W, 3], RGB format. Defaults to None.
metallicRoughness (Optional[Tensor]): metallic-roughness texture, float [H, W, 3], metallic(Blue) = metallicRoughness[..., 2], roughness(Green) = metallicRoughness[..., 1]. Defaults to None.
device (Optional[torch.device]): torch device. Defaults to None.
"""
self.device = device
self.v = v
self.vn = vn
self.vt = vt
self.f = f
self.fn = fn
self.ft = ft
# will first see if there is vertex color to use
self.vc = vc
# only support a single albedo image
self.albedo = albedo
# pbr extension, metallic(Blue) = metallicRoughness[..., 2], roughness(Green) = metallicRoughness[..., 1]
# ref: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
self.metallicRoughness = metallicRoughness
self.ori_center = 0
self.ori_scale = 1
@classmethod
def load(cls, path, glctx, resize=True, clean=False, renormal=True, retex=False, bound=0.9, front_dir='+z', retex_options=None, **kwargs):
"""load mesh from path.
Args:
path (str): path to mesh file, supports ply, obj, glb.
clean (bool, optional): perform mesh cleaning at load (e.g., merge close vertices). Defaults to False.
resize (bool, optional): auto resize the mesh using ``bound`` into [-bound, bound]^3. Defaults to True.
renormal (bool, optional): re-calc the vertex normals. Defaults to True.
retex (bool, optional): re-calc the uv coordinates, will overwrite the existing uv coordinates. Defaults to False.
bound (float, optional): bound to resize. Defaults to 0.9.
front_dir (str, optional): front-view direction of the mesh, should be [+-][xyz][ 123]. Defaults to '+z'.
device (torch.device, optional): torch device. Defaults to None.
Note:
a ``device`` keyword argument can be provided to specify the torch device.
If it's not provided, we will try to use ``'cuda'`` as the device if it's available.
Returns:
Mesh: the loaded Mesh object.
"""
# obj supports face uv
if path.endswith(".obj"):
mesh = cls.load_obj(path, **kwargs)
# trimesh only supports vertex uv, but can load more formats
else:
mesh = cls.load_trimesh(path, **kwargs)
# clean
if clean:
from kiui.mesh_utils import clean_mesh
vertices = mesh.v.detach().cpu().numpy()
triangles = mesh.f.detach().cpu().numpy()
vertices, triangles = clean_mesh(vertices, triangles, remesh=False)
mesh.v = torch.from_numpy(vertices).contiguous().float().to(mesh.device)
mesh.f = torch.from_numpy(triangles).contiguous().int().to(mesh.device)
# print(f"[INFO] load mesh, v: {mesh.v.shape}, f: {mesh.f.shape}")
# auto-normalize
if resize:
mesh.auto_size(bound=bound)
# auto-fix normal
if renormal or mesh.vn is None:
mesh.auto_normal()
# print(f"[INFO] load mesh, vn: {mesh.vn.shape}, fn: {mesh.fn.shape}")
# auto-fix texcoords
if retex or (mesh.albedo is not None and mesh.vt is None):
mesh.auto_uv(glctx, cache_path=path, options=retex_options)
# print(f"[INFO] load mesh, vt: {mesh.vt.shape}, ft: {mesh.ft.shape}")
# rotate front dir to +z
if front_dir != "+z":
# axis switch
if "-z" in front_dir:
T = torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, -1]], device=mesh.device, dtype=torch.float32)
elif "+x" in front_dir:
T = torch.tensor([[0, 0, 1], [0, 1, 0], [1, 0, 0]], device=mesh.device, dtype=torch.float32)
elif "-x" in front_dir:
T = torch.tensor([[0, 0, -1], [0, 1, 0], [1, 0, 0]], device=mesh.device, dtype=torch.float32)
elif "+y" in front_dir:
T = torch.tensor([[1, 0, 0], [0, 0, 1], [0, 1, 0]], device=mesh.device, dtype=torch.float32)
elif "-y" in front_dir:
T = torch.tensor([[1, 0, 0], [0, 0, -1], [0, 1, 0]], device=mesh.device, dtype=torch.float32)
else:
T = torch.tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]], device=mesh.device, dtype=torch.float32)
# rotation (how many 90 degrees)
if '1' in front_dir:
T @= torch.tensor([[0, -1, 0], [1, 0, 0], [0, 0, 1]], device=mesh.device, dtype=torch.float32)
elif '2' in front_dir:
T @= torch.tensor([[1, 0, 0], [0, -1, 0], [0, 0, 1]], device=mesh.device, dtype=torch.float32)
elif '3' in front_dir:
T @= torch.tensor([[0, 1, 0], [-1, 0, 0], [0, 0, 1]], device=mesh.device, dtype=torch.float32)
mesh.v @= T
mesh.vn @= T
return mesh
# load from obj file
@classmethod
def load_obj(cls, path, albedo_path=None, device=None):
"""load an ``obj`` mesh.
Args:
path (str): path to mesh.
albedo_path (str, optional): path to the albedo texture image, will overwrite the existing texture path if specified in mtl. Defaults to None.
device (torch.device, optional): torch device. Defaults to None.
Note:
We will try to read `mtl` path from `obj`, else we assume the file name is the same as `obj` but with `mtl` extension.
The `usemtl` statement is ignored, and we only use the last material path in `mtl` file.
Returns:
Mesh: the loaded Mesh object.
"""
assert os.path.splitext(path)[-1] == ".obj"
mesh = cls()
# device
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mesh.device = device
# load obj
with open(path, "r") as f:
lines = f.readlines()
def parse_f_v(fv):
# pass in a vertex term of a face, return {v, vt, vn} (-1 if not provided)
# supported forms:
# f v1 v2 v3
# f v1/vt1 v2/vt2 v3/vt3
# f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3
# f v1//vn1 v2//vn2 v3//vn3
xs = [int(x) - 1 if x != "" else -1 for x in fv.split("/")]
xs.extend([-1] * (3 - len(xs)))
return xs[0], xs[1], xs[2]
vertices, texcoords, normals = [], [], []
faces, tfaces, nfaces = [], [], []
mtl_path = None
for line in lines:
split_line = line.split()
# empty line
if len(split_line) == 0:
continue
prefix = split_line[0].lower()
# mtllib
if prefix == "mtllib":
mtl_path = split_line[1]
# usemtl
elif prefix == "usemtl":
pass # ignored
# v/vn/vt
elif prefix == "v":
vertices.append([float(v) for v in split_line[1:]])
elif prefix == "vn":
normals.append([float(v) for v in split_line[1:]])
elif prefix == "vt":
val = [float(v) for v in split_line[1:]]
texcoords.append([val[0], 1.0 - val[1]])
elif prefix == "f":
vs = split_line[1:]
nv = len(vs)
v0, t0, n0 = parse_f_v(vs[0])
for i in range(nv - 2): # triangulate (assume vertices are ordered)
v1, t1, n1 = parse_f_v(vs[i + 1])
v2, t2, n2 = parse_f_v(vs[i + 2])
faces.append([v0, v1, v2])
tfaces.append([t0, t1, t2])
nfaces.append([n0, n1, n2])
mesh.v = torch.tensor(vertices, dtype=torch.float32, device=device)
mesh.vt = (
torch.tensor(texcoords, dtype=torch.float32, device=device)
if len(texcoords) > 0
else None
)
if mesh.vt is not None:
mesh.vt[...,0] = mesh.vt[...,0] - torch.floor(mesh.vt[...,0].mean())
mesh.vt[...,1] = mesh.vt[...,1] - torch.floor(mesh.vt[...,1].mean())
mesh.vn = (
torch.tensor(normals, dtype=torch.float32, device=device)
if len(normals) > 0
else None
)
mesh.f = torch.tensor(faces, dtype=torch.int32, device=device)
mesh.ft = (
torch.tensor(tfaces, dtype=torch.int32, device=device)
if len(texcoords) > 0
else None
)
mesh.fn = (
torch.tensor(nfaces, dtype=torch.int32, device=device)
if len(normals) > 0
else None
)
# see if there is vertex color
use_vertex_color = False
if mesh.v.shape[1] == 6:
use_vertex_color = True
mesh.vc = mesh.v[:, 3:]
mesh.v = mesh.v[:, :3]
# print(f"[INFO] load obj mesh: use vertex color: {mesh.vc.shape}")
# try to load texture image
if not use_vertex_color:
# try to retrieve mtl file
mtl_path_candidates = []
if mtl_path is not None:
mtl_path_candidates.append(mtl_path)
mtl_path_candidates.append(os.path.join(os.path.dirname(path), mtl_path))
mtl_path_candidates.append(path.replace(".obj", ".mtl"))
mtl_path = None
for candidate in mtl_path_candidates:
if os.path.exists(candidate):
mtl_path = candidate
break
# if albedo_path is not provided, try retrieve it from mtl
metallic_path = None
roughness_path = None
if mtl_path is not None and albedo_path is None:
with open(mtl_path, "r") as f:
lines = f.readlines()
for line in lines:
split_line = line.split()
# empty line
if len(split_line) == 0:
continue
prefix = split_line[0]
if "map_Kd" in prefix:
# assume relative path!
albedo_path = os.path.join(os.path.dirname(path), split_line[1])
# print(f"[INFO] load obj mesh: use texture from: {albedo_path}")
elif "map_Pm" in prefix:
metallic_path = os.path.join(os.path.dirname(path), split_line[1])
elif "map_Pr" in prefix:
roughness_path = os.path.join(os.path.dirname(path), split_line[1])
# still not found albedo_path, or the path doesn't exist
## GSO
if albedo_path is not None and not os.path.exists(albedo_path):
albedo_path = os.path.join(os.path.dirname(albedo_path), '../materials/textures/texture.png')
albedo = cv2.imread(albedo_path, cv2.IMREAD_UNCHANGED)
albedo = cv2.cvtColor(albedo, cv2.COLOR_BGR2RGB)
albedo = albedo.astype(np.float32) / 255
# print(f"[INFO] load obj mesh: load texture: {albedo.shape}")
mesh.albedo = torch.tensor(albedo, dtype=torch.float32, device=device)
elif albedo_path is None or not os.path.exists(albedo_path):
# print(f"[INFO] load obj mesh: failed to load texture!")
mesh.albedo = None
else:
albedo = cv2.imread(albedo_path, cv2.IMREAD_UNCHANGED)
albedo = cv2.cvtColor(albedo, cv2.COLOR_BGR2RGB)
albedo = albedo.astype(np.float32) / 255
# print(f"[INFO] load obj mesh: load texture: {albedo.shape}")
mesh.albedo = torch.tensor(albedo, dtype=torch.float32, device=device)
# try to load metallic and roughness
if metallic_path is not None and roughness_path is not None:
# print(f"[INFO] load obj mesh: load metallicRoughness from: {metallic_path}, {roughness_path}")
metallic = cv2.imread(metallic_path, cv2.IMREAD_UNCHANGED)
metallic = metallic.astype(np.float32) / 255
roughness = cv2.imread(roughness_path, cv2.IMREAD_UNCHANGED)
roughness = roughness.astype(np.float32) / 255
metallicRoughness = np.stack([np.zeros_like(metallic), roughness, metallic], axis=-1)
mesh.metallicRoughness = torch.tensor(metallicRoughness, dtype=torch.float32, device=device).contiguous()
return mesh
@classmethod
def load_trimesh(cls, path, device=None):
"""load a mesh using ``trimesh.load()``.
Can load various formats like ``glb`` and serves as a fallback.
Note:
We will try to merge all meshes if the glb contains more than one,
but **this may cause the texture to lose**, since we only support one texture image!
Args:
path (str): path to the mesh file.
device (torch.device, optional): torch device. Defaults to None.
Returns:
Mesh: the loaded Mesh object.
"""
mesh = cls()
# device
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mesh.device = device
# use trimesh to load ply/glb
_data = trimesh.load(path)
# always convert scene to mesh, and apply all transforms...
if isinstance(_data, trimesh.Scene):
# print(f"[INFO] load trimesh: concatenating {len(_data.geometry)} meshes.")
_concat = []
# loop the scene graph and apply transform to each mesh
scene_graph = _data.graph.to_flattened() # dict {name: {transform: 4x4 mat, geometry: str}}
for k, v in scene_graph.items():
name = v['geometry']
if name in _data.geometry and isinstance(_data.geometry[name], trimesh.Trimesh):
transform = v['transform']
_concat.append(_data.geometry[name].apply_transform(transform))
_mesh = trimesh.util.concatenate(_concat)
else:
_mesh = _data
if _mesh.visual.kind == 'vertex':
vertex_colors = _mesh.visual.vertex_colors
vertex_colors = np.array(vertex_colors[..., :3]).astype(np.float32) / 255
mesh.vc = torch.tensor(vertex_colors, dtype=torch.float32, device=device)
# print(f"[INFO] load trimesh: use vertex color: {mesh.vc.shape}")
elif _mesh.visual.kind == 'texture':
_material = _mesh.visual.material
if isinstance(_material, trimesh.visual.material.PBRMaterial):
texture = np.array(_material.baseColorTexture).astype(np.float32) / 255
# load metallicRoughness if present
if _material.metallicRoughnessTexture is not None:
metallicRoughness = np.array(_material.metallicRoughnessTexture).astype(np.float32) / 255
mesh.metallicRoughness = torch.tensor(metallicRoughness, dtype=torch.float32, device=device).contiguous()
elif isinstance(_material, trimesh.visual.material.SimpleMaterial):
texture = np.array(_material.to_pbr().baseColorTexture).astype(np.float32) / 255
else:
raise NotImplementedError(f"material type {type(_material)} not supported!")
mesh.albedo = torch.tensor(texture[..., :3], dtype=torch.float32, device=device).contiguous()
# print(f"[INFO] load trimesh: load texture: {texture.shape}")
else:
mesh.albedo = None
# print(f"[INFO] load trimesh: failed to load texture.")
vertices = _mesh.vertices
try:
texcoords = _mesh.visual.uv
texcoords[:, 1] = 1 - texcoords[:, 1]
except Exception as e:
texcoords = None
try:
normals = _mesh.vertex_normals
except Exception as e:
normals = None
# trimesh only support vertex uv...
faces = tfaces = nfaces = _mesh.faces
mesh.v = torch.tensor(vertices, dtype=torch.float32, device=device)
mesh.vt = (
torch.tensor(texcoords, dtype=torch.float32, device=device)
if texcoords is not None
else None
)
mesh.vn = (
torch.tensor(normals, dtype=torch.float32, device=device)
if normals is not None
else None
)
mesh.f = torch.tensor(faces, dtype=torch.int32, device=device)
mesh.ft = (
torch.tensor(tfaces, dtype=torch.int32, device=device)
if texcoords is not None
else None
)
mesh.fn = (
torch.tensor(nfaces, dtype=torch.int32, device=device)
if normals is not None
else None
)
return mesh
# sample surface (using trimesh)
def sample_surface(self, count: int):
"""sample points on the surface of the mesh.
Args:
count (int): number of points to sample.
Returns:
torch.Tensor: the sampled points, float [count, 3].
"""
_mesh = trimesh.Trimesh(vertices=self.v.detach().cpu().numpy(), faces=self.f.detach().cpu().numpy())
points, face_idx = trimesh.sample.sample_surface(_mesh, count)
points = torch.from_numpy(points).float().to(self.device)
return points
# aabb
def aabb(self):
"""get the axis-aligned bounding box of the mesh.
Returns:
Tuple[torch.Tensor]: the min xyz and max xyz of the mesh.
"""
return torch.min(self.v, dim=0).values, torch.max(self.v, dim=0).values
# unit size
@torch.no_grad()
def auto_size(self, bound=0.9):
"""auto resize the mesh.