-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path05_light.py
490 lines (441 loc) · 14.1 KB
/
05_light.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
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
# coding: utf-8
# fullscreen setup
from kivy.config import Config
Config.set('graphics', 'fullscreen', 'auto')
# python
from random import random
# kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.animation import Animation
# kivy3
from kivy3 import Renderer, Scene
from kivy3 import PerspectiveCamera
# geometry
from kivy3.extras.geometries import BoxGeometry
from kivy3 import Material, Mesh
Builder.load_string('''
<MoveButton>:
on_release: self.move(self.main_cube, self.text)
<CamRot@GridLayout>:
cols: 3
Widget:
MoveButton:
text: 'rup'
Widget:
MoveButton:
text: 'rleft'
Label:
text: 'cam\\nrot'
MoveButton:
text: 'rright'
Widget:
MoveButton:
text: 'rdown'
<CamNav@GridLayout>:
cols: 3
Widget:
MoveButton:
text: 'mup'
MoveButton:
text: 'mforw'
MoveButton:
text: 'mleft'
Label:
text: 'cam\\npos'
MoveButton:
text: 'mright'
MoveButton:
text: 'mback'
MoveButton:
text: 'mdown'
<CamStrafe@GridLayout>:
cols: 3
Widget:
MoveButton:
text: 'sup'
Widget:
MoveButton:
text: 'sleft'
Label:
text: 'cam\\nstrafe'
MoveButton:
text: 'sright'
Widget:
MoveButton:
text: 'sdown'
<ObjNav@GridLayout>:
cols: 3
MoveButton:
text: 'z-'
MoveButton:
text: 'up'
Widget:
MoveButton:
text: 'left'
Label:
text: 'cube\\npos'
MoveButton:
text: 'right'
Widget:
MoveButton:
text: 'down'
MoveButton:
text: 'z+'
<LightPos@GridLayout>:
cols: 3
Widget:
MoveButton:
text: 'lup'
MoveButton:
text: 'l+'
MoveButton:
text: 'lleft'
Label:
text: 'light\\npos'
MoveButton:
text: 'lright'
MoveButton:
text: 'l-'
MoveButton:
text: 'ldown'
<LightPanel@BoxLayout>:
Widget:
LightPos:
Widget:
''')
class MoveButton(Button):
def __init__(self, **kwargs):
super(MoveButton, self).__init__(**kwargs)
self.app = App.get_running_app()
self.main_cube = self.app.main_cube
def move(self, cube, direc, *args):
# move cube object
if direc == 'up':
cube.pos.y += .1
elif direc == 'down':
cube.pos.y -= .1
elif direc == 'right':
cube.pos.x += .1
elif direc == 'left':
cube.pos.x -= .1
elif direc == 'z+':
cube.pos.z += .1
elif direc == 'z-':
cube.pos.z -= .1
# rotate camera
elif direc == 'rup':
# rotation _around_ the axis,
# not in the _direction_ of the axis
# →
# / \
# - - - |- - - - X
# \_/
#
self.app.camera.rot.x -= 10
self.app.camera.look_at(cube.pos)
elif direc == 'rdown':
self.app.camera.rot.x += 10
self.app.camera.look_at(cube.pos)
elif direc == 'rright':
self.app.camera.rot.y += 1
self.app.camera.look_at(cube.pos)
elif direc == 'rleft':
self.app.camera.rot.y -= 1
self.app.camera.look_at(cube.pos)
# move camera, but still look at cube
elif direc == 'mforw':
self.app.camera.pos.z -= .1
elif direc == 'mback':
self.app.camera.pos.z += .1
elif direc == 'mup':
self.app.camera.pos.y += 1
self.app.camera.look_at(cube.pos)
elif direc == 'mdown':
self.app.camera.pos.y -= 1
self.app.camera.look_at(cube.pos)
elif direc == 'mright':
self.app.camera.pos.x += 1
self.app.camera.look_at(cube.pos)
elif direc == 'mleft':
self.app.camera.pos.x -= 1
self.app.camera.look_at(cube.pos)
# strafe camera
elif direc == 'sup':
self.app.camera.pos.y += 1
self.app.camera.look_at(
self.app.camera.pos.x,
self.app.camera.pos.y,
self.app.camera.pos.z -1
) # look in front of the camera
elif direc == 'sdown':
self.app.camera.pos.y -= 1
self.app.camera.look_at(
self.app.camera.pos.x,
self.app.camera.pos.y,
self.app.camera.pos.z -1
)
elif direc == 'sleft':
self.app.camera.pos.x -= 1
self.app.camera.look_at(
self.app.camera.pos.x,
self.app.camera.pos.y,
self.app.camera.pos.z -1
)
elif direc == 'sright':
self.app.camera.pos.x += 1
self.app.camera.look_at(
self.app.camera.pos.x,
self.app.camera.pos.y,
self.app.camera.pos.z -1
)
# move main light
elif direc == 'lup':
self.app.renderer.main_light.pos[1] += 10
elif direc == 'ldown':
self.app.renderer.main_light.pos[1] -= 10
elif direc == 'lleft':
self.app.renderer.main_light.pos[0] -= 10
elif direc == 'lright':
self.app.renderer.main_light.pos[0] += 10
elif direc == 'lforw':
self.app.renderer.main_light.pos[2] -= 10
elif direc == 'lback':
self.app.renderer.main_light.pos[2] += 10
elif direc == 'l+':
self.app.renderer.main_light.intensity += 10
elif direc == 'l-':
self.app.renderer.main_light.intensity -= 10
class Listener(Widget):
def __init__(self, **kwargs):
super(Listener, self).__init__(**kwargs)
app = App.get_running_app()
self.cube = app.main_cube
self.movebutton = MoveButton()
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_key_down)
def _keyboard_closed(self):
self._keyboard.unbind(on_key_down=self._on_key_down)
self._keyboard = None
def _on_key_down(self, keyboard, keycode, text, modifiers):
# camera rotation
if keycode[1] == 'w':
self.movebutton.move(self.cube, 'rup')
elif keycode[1] == 'a':
self.movebutton.move(self.cube, 'rleft')
elif keycode[1] == 's':
self.movebutton.move(self.cube, 'rdown')
elif keycode[1] == 'd':
self.movebutton.move(self.cube, 'rright')
# camera strafe
elif keycode[1] == 't':
self.movebutton.move(self.cube, 'sup')
elif keycode[1] == 'f':
self.movebutton.move(self.cube, 'sleft')
elif keycode[1] == 'g':
self.movebutton.move(self.cube, 'sdown')
elif keycode[1] == 'h':
self.movebutton.move(self.cube, 'sright')
# camera move
elif keycode[1] == 'i':
self.movebutton.move(self.cube, 'mup')
elif keycode[1] == 'j':
self.movebutton.move(self.cube, 'mleft')
elif keycode[1] == 'k':
self.movebutton.move(self.cube, 'mdown')
elif keycode[1] == 'l':
self.movebutton.move(self.cube, 'mright')
elif keycode[1] == 'u':
self.movebutton.move(self.cube, 'mforw')
elif keycode[1] == 'o':
self.movebutton.move(self.cube, 'mback')
# object move
elif keycode[1] == 'up':
self.movebutton.move(self.cube, 'up')
elif keycode[1] == 'left':
self.movebutton.move(self.cube, 'left')
elif keycode[1] == 'down':
self.movebutton.move(self.cube, 'down')
elif keycode[1] == 'right':
self.movebutton.move(self.cube, 'right')
elif keycode[1] == '.':
self.movebutton.move(self.cube, 'z-')
elif keycode[1] == '-':
self.movebutton.move(self.cube, 'z+')
# main light move
elif keycode[1] == 'numpad8':
self.movebutton.move(self.cube, 'lup')
elif keycode[1] == 'numpad4':
self.movebutton.move(self.cube, 'lleft')
elif keycode[1] == 'numpad2':
self.movebutton.move(self.cube, 'ldown')
elif keycode[1] == 'numpad6':
self.movebutton.move(self.cube, 'lright')
elif keycode[1] == 'numpadsubstract':
self.movebutton.move(self.cube, 'l-')
elif keycode[1] == 'numpadadd':
self.movebutton.move(self.cube, 'l+')
elif keycode[1] == 'numpad9':
self.movebutton.move(self.cube, 'lforw')
elif keycode[1] == 'numpad1':
self.movebutton.move(self.cube, 'lback')
class My3D(App):
def _adjust_aspect(self, *args):
rsize = self.renderer.size
aspect = rsize[0] / float(rsize[1])
self.renderer.camera.aspect = aspect
def rotate_cube(self, *dt):
for cube in self.cubes:
cube.rotation.y += 1
def scale_cube(self, *dt):
for cube in self.cubes:
factor = random()
anim = Animation(x=factor)
anim &= Animation(y=factor)
anim &= Animation(z=factor)
anim.start(cube.scale)
def build(self):
layout = GridLayout(cols=3)
# create renderer
self.renderer = Renderer(size_hint=(5, 5))
self.renderer.set_clear_color(
(0.1, 0.1, 0.1, 1)
) # rgba
# create scene
scene = Scene()
self.cubes = []
# create cubes for scene
#
# default pure green cube
cube_geo = BoxGeometry(.3, .3, .3)
cube_mat = Material(
color=(0, 0.5, 0) # base color
)
self.cubes.append(Mesh(
geometry=cube_geo,
material=cube_mat
)) # default pos == (0, 0, 0)
self.cubes[0].pos.z = -5
self.cubes[0].pos.x = 1
self.cubes[0].pos.y = 0.8
self.cubes[0].rotation.x = 45
# black cube, red shadow, half-transparent
cube_geo = BoxGeometry(.3, .3, .3)
cube_mat = Material(
transparency=0.5,
color=(0, 0, 0), # base color
diffuse=(10, 0, 0), # color of "shadows"
specular=(0, 0, 0) # mirror-like reflections
)
self.cubes.append(Mesh(
geometry=cube_geo,
material=cube_mat
)) # default pos == (0, 0, 0)
self.cubes[1].pos.z = -5
self.cubes[1].pos.x = -1
self.cubes[1].pos.y = 0.8
self.cubes[1].rotation.y = 45
# default pure green cube with red reflections
cube_geo = BoxGeometry(.3, .3, .3)
cube_mat = Material(
transparency=1,
color=(0, 0.5, 0), # base color
diffuse=(0, 0, 0), # color of "shadows"
specular=(10, 0, 0) # mirror-like reflections
)
self.cubes.append(Mesh(
geometry=cube_geo,
material=cube_mat
)) # default pos == (0, 0, 0)
self.cubes[2].pos.z = -5
self.cubes[2].pos.x = 1
self.cubes[2].pos.y = -0.8
self.cubes[2].rotation.z = 45
# black cube with red reflections
# and half-transparent
cube_geo = BoxGeometry(.3, .3, .3)
cube_mat = Material(
transparency=0.5,
color=(0, 0, 0), # base color
specular=(10, 0, 0) # mirror-like reflections
)
self.cubes.append(Mesh(
geometry=cube_geo,
material=cube_mat
)) # default pos == (0, 0, 0)
self.cubes[3].pos.z = -5
self.cubes[3].pos.x = -1
self.cubes[3].pos.y = -0.8
self.cubes[3].rotation.x = 45
cube_geo = BoxGeometry(.3, .3, .3)
cube_mat = Material(
transparency=0.5,
color=(0, 0, 0), # base color
specular=(10, 0, 0)
)
self.main_cube = Mesh(
geometry=cube_geo,
material=cube_mat
) # default pos == (0, 0, 0)
self.main_cube.rotation.x = 45
self.main_cube.rotation.y = 45
self.main_cube.pos.z = -5
scene.add(self.main_cube)
planes = [
((0, 0, -10), (0, 0, 0)),
((-10, 0, 0), (0, -90, 0)),
((10, 0, 0), (0, 90, 0)),
((0, 0, 10), (0, 180, 0))
] # position and rotation changes
for plane in planes:
geo = BoxGeometry(5, 5, .1)
mat = Material(
color=(1, 1, 1)
)
mesh = Mesh(
geometry=geo,
material=mat
)
mesh.pos.x += plane[0][0]
mesh.pos.y += plane[0][1]
mesh.pos.z += plane[0][2]
mesh.rot.x += plane[1][0]
mesh.rot.y += plane[1][1]
mesh.rot.z += plane[1][2]
scene.add(mesh)
# create camera for scene
self.camera = PerspectiveCamera(
fov=75, # distance from the screen
aspect=0, # "screen" ratio
near=.1, # nearest rendered point
far=1000 # farthest rendered point
)
# start rendering the scene and camera
for cube in self.cubes:
scene.add(cube)
self.renderer.render(scene, self.camera)
# set renderer ratio is its size changes
# e.g. when added to parent
self.renderer.bind(size=self._adjust_aspect)
layout.add_widget(Factory.CamRot())
layout.add_widget(Factory.LightPanel())
layout.add_widget(Factory.CamStrafe())
layout.add_widget(Widget())
layout.add_widget(self.renderer)
layout.add_widget(Label(text='+\n\nY\n\n-'))
layout.add_widget(Factory.CamNav())
layout.add_widget(Label(text='- X +'))
layout.add_widget(Factory.ObjNav())
Clock.schedule_interval(self.rotate_cube, .01)
Clock.schedule_interval(self.scale_cube, 1)
# keyboard listener
Listener()
return layout
My3D().run()