forked from mmatl/pyrender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_360degree.py
210 lines (188 loc) · 7.43 KB
/
render_360degree.py
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
import numpy as np
import scipy
import time
import cv2
import os
import glob
import math
import trimesh
import objio
import imageio
import pyglet
from mypyrender import IntrinsicsCamera, \
DirectionalLight, SpotLight, PointLight, \
MetallicRoughnessMaterial, \
Primitive, Mesh, Node, Scene, \
Viewer, OffscreenRenderer
# skin
skin_mat = {
'color': np.array([[255, 195, 174]]) / 255.0 * 0.8,
'metallicFactor': 0.0,
'roughnessFactor': 1.0,
'ambient_light': 0.5,
'directional_light': 1.0,
'floor_gray_value': 1.0,
}
# geometry
geo_mat = {
'color': np.array([[222, 222, 222]]) / 255.0,
'metallicFactor': 0.2,
'roughnessFactor': 0.8,
'ambient_light': 0.22,
'directional_light': 1.8,
'floor_gray_value': 1.0,
}
# color
clr_mat = {
'color': None,
'metallicFactor': None,
'roughnessFactor': None,
'ambient_light': 0.8,
'directional_light': 1.0,
'floor_gray_value': 0.78,
}
def rotationx(theta):
return np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, np.cos(theta / 180 * np.pi), np.sin(theta / 180 * np.pi), 0.0],
[0.0, -np.sin(theta / 180 * np.pi), np.cos(theta / 180 * np.pi), 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def rotationy(theta):
return np.array([
[np.cos(theta / 180 * np.pi), 0.0, np.sin(theta / 180 * np.pi), 0.0],
[0.0, 1.0, 0.0, 0.0],
[-np.sin(theta / 180 * np.pi), 0.0, np.cos(theta / 180 * np.pi), 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def location(x, y, z):
return np.array([
[1.0, 0.0, 0.0, x],
[0.0, 1.0, 0.0, y],
[0.0, 0.0, 1.0, z],
[0.0, 0.0, 0.0, 1.0]
])
def main(mesh_path, outdir, view_num, color, metallicFactor=None, roughnessFactor=None,
render_shadow=False, ambient_light=0.8, directional_light=1.0, floor_gray_value=0.78,
img_res=512, cam_f=5000, cam_c=None):
os.makedirs(outdir, exist_ok=True)
scene = Scene(ambient_light=np.array([ambient_light, ambient_light, ambient_light, 1.0]))
################
# set camera
if cam_c is None:
cam_c = img_res / 2.0
cam = IntrinsicsCamera(fx=cam_f, fy=cam_f, cx=cam_c, cy=cam_c)
cam_pose = np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 10.0],
[0.0, 0.0, 0.0, 1.0]
])
################
# set material
material = None
if metallicFactor is not None and roughnessFactor is not None:
material = MetallicRoughnessMaterial(metallicFactor=metallicFactor, roughnessFactor=roughnessFactor)
################
# read mesh
mesh = objio.load_obj_data(mesh_path)
if 'vc' not in mesh:
mesh['vc'] = np.ones_like(mesh['v']) * 0.8
if color is not None:
if color.shape[0] == 1:
mesh['vc'] = np.ones_like(mesh['v']) * color
else:
assert mesh['v'].shape[0] == color.shape[0]
mesh['vc'] = color
mesh_ = trimesh.Trimesh(vertices=mesh['v'], faces=mesh['f'], vertex_colors=mesh['vc'])
points_mesh = Mesh.from_trimesh(mesh_, smooth=True, material=material)
human_node = scene.add(points_mesh)
################
# add plane
plane_size = 4
min_y = np.min(mesh_.vertices[:, 1]) + 0.005
plane_points = np.array([
[plane_size, min_y, plane_size],
[plane_size, min_y, -plane_size],
[-plane_size, min_y, plane_size],
[-plane_size, min_y, -plane_size]
])
plane_faces = np.array([
[0, 1, 2],
[2, 1, 3]
])
plane_colors = np.ones((4, 3), dtype=np.float32) * floor_gray_value
plane_mesh = trimesh.Trimesh(
vertices=plane_points, faces=plane_faces, vertex_colors=plane_colors)
plane_mesh = Mesh.from_trimesh(plane_mesh, smooth=False)
plane_node = scene.add(plane_mesh)
################
# add direct lighting
direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
light_node_1 = scene.add(direc_l, pose=np.matmul(rotationy(30), rotationx(45)))
direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
light_node_2 = scene.add(direc_l, pose=np.matmul(rotationy(-30), rotationx(45)))
direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light)
light_node_3 = scene.add(direc_l, pose=np.matmul(rotationy(-180), rotationx(45)))
direc_l = DirectionalLight(color=np.ones(3), intensity=(directional_light-0.5))
light_node_4 = scene.add(direc_l, pose=np.matmul(rotationy(0), rotationx(-10)))
################
# rendering
cam_node = scene.add(cam, pose=cam_pose)
render_flags = {
'flip_wireframe': False,
'all_wireframe': False,
'all_solid': False,
'shadows': render_shadow,
'vertex_normals': False,
'face_normals': False,
'cull_faces': True,
'point_size': 1.0,
}
r = OffscreenRenderer(viewport_width=img_res, viewport_height=img_res, render_flags=render_flags)
angle = 360 / view_num
rotmat = rotationy(angle)[:3, :3]
for view_id in range(view_num):
mesh['v'] = np.matmul(mesh['v'], rotmat.transpose())
mesh_ = trimesh.Trimesh(vertices=mesh['v'], faces=mesh['f'], vertex_colors=mesh['vc'])
points_mesh = Mesh.from_trimesh(mesh_, smooth=True, material=material)
color, depth = r.render(scene)
scene.remove_node(human_node)
human_node = scene.add(points_mesh)
cv2.imshow('test', color[:, :, ::-1])
cv2.waitKey(30)
# # [debugging code]
# import matplotlib.pyplot as plt
# plt.figure()
# plt.imshow(color)
# plt.show()
r.delete()
scene.clear()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mesh_path', default=None, type=str)
parser.add_argument('--out_dir', default=None, type=str)
parser.add_argument('--material', default=None, type=str)
parser.add_argument('--shadows', default=False, type=bool)
args = parser.parse_args()
if args.material == 'skin':
main(args.mesh_path, args.out_dir, 60, skin_mat['color'],
render_shadow=args.shadows,
metallicFactor=skin_mat['metallicFactor'], roughnessFactor=skin_mat['roughnessFactor'],
ambient_light=skin_mat['ambient_light'], directional_light=skin_mat['directional_light'],
floor_gray_value=skin_mat['floor_gray_value'])
elif args.material == 'geo':
main(args.mesh_path, args.out_dir, 60, geo_mat['color'],
render_shadow=args.shadows,
metallicFactor=geo_mat['metallicFactor'], roughnessFactor=geo_mat['roughnessFactor'],
ambient_light=geo_mat['ambient_light'], directional_light=geo_mat['directional_light'],
floor_gray_value=skin_mat['floor_gray_value'])
else:
main(args.mesh_path, args.out_dir, 60, None, None, None, render_shadow=args.shadows,
ambient_light=clr_mat['ambient_light'], directional_light=clr_mat['directional_light'],
floor_gray_value=clr_mat['floor_gray_value'])
# Example:
# python .\render_360degree.py --mesh_path .\data\WOMEN_Dresses_id_00005350_03_4_full_tex.obj --out_dir ./debug/ --material geo --shadows True
# python .\render_360degree.py --mesh_path .\data\WOMEN_Dresses_id_00005350_03_4_full_tex.obj --out_dir ./debug/ --material skin --shadows True
# python .\render_360degree.py --mesh_path .\data\WOMEN_Dresses_id_00005350_03_4_full_tex.obj --out_dir ./debug/ --material none --shadows True