generated from taichi-dev/voxel-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.py
223 lines (194 loc) · 7.16 KB
/
scene.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
211
212
213
214
215
216
217
218
219
220
221
222
223
import time
import os
from datetime import datetime
import numpy as np
import taichi as ti
from renderer import Renderer
from math_utils import np_normalize, np_rotate_matrix
REC = False
SHOT = False
LOOK_AT = (0, 0, 0)
INTERVAL_TIME = 1
VOXEL_DX = 1 / 64
SCREEN_RES = (1280, 720)
TARGET_FPS = 30
UP_DIR = (0, 1, 0)
HELP_MSG = '''
====================================================
Camera:
* Drag with your left mouse button to rotate
* Press W/A/S/D/Q/E to move or C to look at center
* Press R to rotate and record automatically
====================================================
'''
MAT_LAMBERTIAN = 1
MAT_LIGHT = 2
class Camera:
def __init__(self, window, up):
self._window = window
self._camera_pos = np.array((0.4, 0.5, 2.0))
self._lookat_pos = np.array((0.0, 0.0, 0.0))
self._up = np_normalize(np.array(up))
self._last_mouse_pos = None
@property
def mouse_exclusive_owner(self):
return True
def update_camera(self):
res = self._update_by_wasd()
res = self._update_by_mouse() or res
return res
def _update_by_mouse(self):
win = self._window
if not self.mouse_exclusive_owner or not win.is_pressed(ti.ui.LMB):
self._last_mouse_pos = None
return False
mouse_pos = np.array(win.get_cursor_pos())
if self._last_mouse_pos is None:
self._last_mouse_pos = mouse_pos
return False
# Makes camera rotation feels right
dx, dy = self._last_mouse_pos - mouse_pos
self._last_mouse_pos = mouse_pos
out_dir = self._lookat_pos - self._camera_pos
leftdir = self._compute_left_dir(np_normalize(out_dir))
scale = 3
rotx = np_rotate_matrix(self._up, dx * scale)
roty = np_rotate_matrix(leftdir, dy * scale)
out_dir_homo = np.array(list(out_dir) + [0.0])
new_out_dir = np.matmul(np.matmul(roty, rotx), out_dir_homo)[:3]
self._lookat_pos = self._camera_pos + new_out_dir
return True
def _update_by_wasd(self):
win = self._window
tgtdir = self.target_dir
leftdir = self._compute_left_dir(tgtdir)
lut = [
('w', tgtdir),
('a', leftdir),
('s', -tgtdir),
('d', -leftdir),
('e', [0, -1, 0]),
('q', [0, 1, 0]),
]
dir = np.array([0.0, 0.0, 0.0])
pressed = False
for key, d in lut:
if win.is_pressed(key):
pressed = True
dir += np.array(d)
if not pressed and not SHOT:
return False
dir *= 0.05
self._camera_pos = (self._camera_pos + dir + np.array(leftdir)*0.05) if SHOT else (self._camera_pos + dir)
self._lookat_pos = self.center_dir if SHOT else (self._lookat_pos + dir)
if win.is_pressed('c'):
self._lookat_pos = self.center_dir
return True
@property
def position(self):
return self._camera_pos
@property
def look_at(self):
return self._lookat_pos
@property
def target_dir(self):
return np_normalize(self.look_at - self.position)
@property
def center_dir(self):
return np_normalize(LOOK_AT - self.position)
def _compute_left_dir(self, tgtdir):
cos = np.dot(self._up, tgtdir)
if abs(cos) > 0.999:
return np.array([-1.0, 0.0, 0.0])
return np.cross(self._up, tgtdir)
class Scene:
def __init__(self, voxel_edges=0.06, exposure=3):
ti.init(arch=ti.vulkan)
print(HELP_MSG)
self.window = ti.ui.Window("Taichi Voxel Renderer",
SCREEN_RES,
vsync=True)
self.camera = Camera(self.window, up=UP_DIR)
self.renderer = Renderer(dx=VOXEL_DX,
image_res=SCREEN_RES,
up=UP_DIR,
voxel_edges=voxel_edges,
exposure=exposure)
self.renderer.set_camera_pos(*self.camera.position)
@staticmethod
@ti.func
def round_idx(idx_):
idx = ti.cast(idx_, ti.f32)
return ti.Vector(
[ti.round(idx[0]),
ti.round(idx[1]),
ti.round(idx[2])]).cast(ti.i32)
@ti.func
def set_voxel(self, idx, mat, color):
self.renderer.set_voxel(self.round_idx(idx), mat, color)
@ti.func
def get_voxel(self, idx):
mat, color = self.renderer.get_voxel(self.round_idx(idx))
return mat, color
def set_floor(self, height, color):
self.renderer.floor_height[None] = height
self.renderer.floor_color[None] = color
def set_directional_light(self, direction, direction_noise, color):
self.renderer.set_directional_light(direction, direction_noise, color)
def set_background_color(self, color):
self.renderer.background_color[None] = color
def finish(self):
self.renderer.recompute_bbox()
canvas = self.window.get_canvas()
spp = 1
global REC
global SHOT
count = 0
interval_time = 0
while self.window.running:
should_reset_framebuffer = False
if self.camera.update_camera():
self.renderer.set_camera_pos(*self.camera.position)
look_at = self.camera.look_at
self.renderer.set_look_at(*look_at)
should_reset_framebuffer = True
if should_reset_framebuffer:
self.renderer.reset_framebuffer()
t = time.time()
for _ in range(spp):
self.renderer.accumulate()
img = self.renderer.fetch_image()
if self.window.is_pressed('p'):
timestamp = datetime.today().strftime('%Y-%m-%d-%H%M%S')
dirpath = os.getcwd()
fname = os.path.join(dirpath, f"screenshot{timestamp}.jpg")
ti.tools.image.imwrite(img, fname)
print(f"Screenshot has been saved to {fname}")
canvas.set_image(img)
elapsed_time = time.time() - t
if elapsed_time * TARGET_FPS > 1:
spp = int(spp / (elapsed_time * TARGET_FPS) - 1)
spp = max(spp, 1)
else:
spp += 1
if self.window.is_pressed('r'):
count += 1
count %= 2
if count == 0:
REC = False if REC else True
interval_time = time.time()
print("now recording")
if not os.path.exists("record"):
os.mkdir("record")
else:
print("end recording")
SHOT = False
if REC and (time.time() - interval_time) > INTERVAL_TIME:
interval_time = time.time()
SHOT = True
timestamp = datetime.today().strftime('%Y-%m-%d-%H%M%S')
dirpath = os.getcwd()
fname = os.path.join(dirpath, f"record/{timestamp}.jpg")
ti.tools.image.imwrite(img, fname)
print(f"Screenshot has been saved to {fname}")
self.window.show()