-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path15.camera_motion_car_blur.py
221 lines (183 loc) · 6.91 KB
/
15.camera_motion_car_blur.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
import nvisii
import noise
import random
import numpy as np
opt = lambda: None
opt.spp = 400
opt.width = 1920
opt.height = 1080
opt.noise = False
opt.out = '15_camera_motion_car_blur.png'
opt.control = True
# # # # # # # # # # # # # # # # # # # # # # # # #
nvisii.initialize()
nvisii.set_dome_light_intensity(.8)
nvisii.resize_window(int(opt.width), int(opt.height))
# # # # # # # # # # # # # # # # # # # # # # # # #
# load the textures
dome = nvisii.texture.create_from_file("dome", "content/teatro_massimo_2k.hdr")
# we can add HDR images to act as dome
nvisii.set_dome_light_texture(dome)
nvisii.set_dome_light_rotation(nvisii.angleAxis(nvisii.pi() * .5, nvisii.vec3(0, 0, 1)))
car_speed = 0
car_speed_x = car_speed
car_speed_y = -2 * car_speed
camera_height = 80
# # # # # # # # # # # # # # # # # # # # # # # # #
if not opt.noise is True:
nvisii.enable_denoiser()
camera = nvisii.entity.create(
name = "camera",
transform = nvisii.transform.create("camera"),
camera = nvisii.camera.create(
name = "camera",
aspect = float(opt.width)/float(opt.height)
)
)
camera.get_transform().look_at(
at = nvisii.vec3(-50,0,camera_height) , # look at (world coordinate)
up = nvisii.vec3(0,0,1), # up vector
eye = nvisii.vec3(-500,500,100 + camera_height),
previous = False
)
camera.get_transform().look_at(
at = nvisii.vec3(-50,0,camera_height) + nvisii.vec3(car_speed_x, car_speed_y, .0) , # look at (world coordinate)
up = nvisii.vec3(0,0,1), # up vector
eye = nvisii.vec3(-500,500,100 + camera_height),
previous = True
)
camera.get_camera().set_aperture_diameter(5000)
camera.get_camera().set_focal_distance(500)
nvisii.set_camera_entity(camera)
floor = nvisii.entity.create(
name = "floor",
mesh = nvisii.mesh.create_plane("plane"),
material = nvisii.material.create("plane"),
transform = nvisii.transform.create("plane")
)
floor.get_transform().set_scale(nvisii.vec3(10000))
floor.get_transform().set_position(nvisii.vec3(0, 0, -5))
floor.get_material().set_base_color(nvisii.vec3(1.0))
floor.get_material().set_roughness(1)
floor.get_material().set_specular(0)
# # # # # # # # # # # # # # # # # # # # # # # # #
# This function loads the
sdb = nvisii.import_scene(
'content/bmw/bmw.obj', #obj path
nvisii.vec3(0,0,0), # translation
nvisii.vec3(1), # scale here
nvisii.angleAxis(3.14 * .5, nvisii.vec3(1,0,0)), #rotation here
args=["verbose"]
)
# mirror = nvisii.material.get('sdbMirror')
# mirror.set_roughness(0)
# mirror.set_metallic(1)
# mirror.set_base_color(nvisii.vec3(1))
hl = nvisii.light.create("headlights")
tl = nvisii.light.create("taillights")
tl.set_color(nvisii.vec3(1,0,0))
hl.set_intensity(1000)
tl.set_intensity(1000)
#%%
for i_s, s in enumerate(sdb.entities):
# print(s.get_name())
# if 'car' in s.get_name():
# print(s.get_name())
s.get_transform().set_linear_velocity(nvisii.vec3(car_speed_x, car_speed_y, .0))
print(s.get_name())
if "carshell" in s.get_name().lower():
s.get_material().set_clearcoat(1)
s.get_material().set_clearcoat_roughness(0)
s.get_material().set_roughness(.1)
if "angeleye" in s.get_name().lower():
s.set_light(hl)
if "lightsbulb" in s.get_name().lower():
s.set_light(hl)
# if "taillight" in s.get_name().lower():
# s.set_light(tl)
if "tirerim" in s.get_name().lower():
s.clear_material()
s.set_material(nvisii.material.create(s.get_name().lower()))
# s.get_material().set_ior(1.0)
s.get_material().set_base_color(nvisii.vec3(0.6,0.6,0.6))
s.get_material().set_transmission(0)
s.get_material().set_roughness(0.1)
s.get_material().set_metallic(1)
if "lightsglass" in s.get_name().lower() or "window" in s.get_name().lower():
print(s.get_name())
s.clear_material()
s.set_material(nvisii.material.create(s.get_name().lower()))
s.get_material().set_ior(1.0)
# s.get_material().set_base_color(nvisii.vec3(1,1,1))
s.get_material().set_transmission(1)
s.get_material().set_roughness(0)
s.get_material().set_metallic(0)
# elif 'light' in s.get_name().lower():
# print(s.get_name())
# s.set_light(nvisii.light.create('light' + str(i_s)))
# s.get_light().set_intensity(20)
# s.get_light().set_temperature(5000)
# if 'tire' in s.get_name().lower():
# s.get_transform().set_angular_velocity(nvisii.angleAxis(3.14 * .05, nvisii.vec3(1,0,0)))
# nvisii.entity.get("sdbcarShell_1").get_material().set_base_color(nvisii.vec3(1,0,0))
# nvisii.render_to_png(1024, 1024, 1000, "motion_blur_3")
if opt.control:
camera.get_transform().clear_motion()
cursor = nvisii.vec4()
speed_camera = 4.0
rot = nvisii.vec2(nvisii.pi() * 1.25, 0)
def interact():
global speed_camera
global cursor
global rot
# nvisii camera matrix
cam_matrix = camera.get_transform().get_local_to_world_matrix()
dt = nvisii.vec4(0,0,0,0)
# translation
if nvisii.is_button_held("W"): dt[2] = -speed_camera
if nvisii.is_button_held("S"): dt[2] = speed_camera
if nvisii.is_button_held("A"): dt[0] = -speed_camera
if nvisii.is_button_held("D"): dt[0] = speed_camera
if nvisii.is_button_held("Q"): dt[1] = -speed_camera
if nvisii.is_button_held("E"): dt[1] = speed_camera
# control the camera
if nvisii.length(dt) > 0.0:
w_dt = cam_matrix * dt
camera.get_transform().add_position(nvisii.vec3(w_dt))
# camera rotation
cursor[2] = cursor[0]
cursor[3] = cursor[1]
cursor[0] = nvisii.get_cursor_pos().x
cursor[1] = nvisii.get_cursor_pos().y
if nvisii.is_button_held("MOUSE_LEFT"):
nvisii.set_cursor_mode("DISABLED")
rotate_camera = True
else:
nvisii.set_cursor_mode("NORMAL")
rotate_camera = False
if rotate_camera:
rot.x -= (cursor[0] - cursor[2]) * 0.001
rot.y -= (cursor[1] - cursor[3]) * 0.001
init_rot = nvisii.angleAxis(nvisii.pi() * .5, (1,0,0))
yaw = nvisii.angleAxis(rot.x, (0,1,0))
pitch = nvisii.angleAxis(rot.y, (1,0,0))
camera.get_transform().set_rotation(init_rot * yaw * pitch)
# change speed movement
if nvisii.is_button_pressed("UP"):
speed_camera *= 0.5
print('decrease speed camera', speed_camera)
if nvisii.is_button_pressed("DOWN"):
speed_camera /= 0.5
print('increase speed camera', speed_camera)
nvisii.register_pre_render_callback(interact)
import time
while not nvisii.should_window_close():
time.sleep(.1)
nvisii.render_to_file(
width=int(opt.width),
height=int(opt.height),
samples_per_pixel=int(opt.spp),
file_path=f"{opt.out}"
)
# let's clean up the GPU
nvisii.deinitialize()