-
Notifications
You must be signed in to change notification settings - Fork 23
/
lego_3d.py
214 lines (142 loc) · 5.51 KB
/
lego_3d.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
"""
examples/lego-3d.py
Copyright (c) 2022, Nir Aides - [email protected]
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import logging
import random
import struct
import time
import glm
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from jupylet.label import Label
from jupylet.app import App
from jupylet.state import State
from jupylet.loader import load_blender_gltf
logger = logging.getLogger()
app = App(768, 512)
scene = load_blender_gltf('./scenes/lego/lego.gltf')
scene.shadows = True
camera = scene.cameras['Camera']
brick = scene.meshes['brick.green']
state = State(
capslock = False,
shift = False,
alt = False,
up = False,
down = False,
right = False,
left = False,
key_w = False,
key_s = False,
key_a = False,
key_d = False,
lv = glm.vec3(0),
av = glm.vec3(0),
)
@app.event
def key_event(key, action, modifiers):
logger.info('Enter key_event(key=%r, action=%r, modifiers=%r).', key, action, modifiers)
keys = app.window.keys
value = action != keys.ACTION_RELEASE
if key == keys.CAPS_LOCK and value:
state.capslock = not state.capslock
state.alt = modifiers.alt
state.shift = modifiers.shift
if key == keys.SPACE:
state.lv *= 0.
state.av *= 0.
if key == keys.UP:
state.up = value
if key == keys.DOWN:
state.down = value
if key == keys.LEFT:
state.left = value
if key == keys.RIGHT:
state.right = value
if key == keys.W:
state.key_w = value
if key == keys.S:
state.key_s = value
if key == keys.A:
state.key_a = value
if key == keys.D:
state.key_d = value
obj = brick if state.capslock else camera
linear_acceleration = 1 / 2
angular_acceleration = 1 / 24
@app.run_me_every(1/48)
def move_object(ct, dt):
global obj
obj = brick if state.capslock else camera
sign = -1 if obj is camera else 1
if state.right and state.shift:
state.av.z += angular_acceleration * sign
if state.right and not state.shift:
state.av.y -= angular_acceleration
if state.left and state.shift:
state.av.z -= angular_acceleration * sign
if state.left and not state.shift:
state.av.y += angular_acceleration
if state.up:
state.av.x -= angular_acceleration
if state.down:
state.av.x += angular_acceleration
if state.key_w and state.alt:
state.lv.y += linear_acceleration
if state.key_w and not state.alt:
state.lv.z += linear_acceleration * sign
if state.key_s and state.alt:
state.lv.y -= linear_acceleration
if state.key_s and not state.alt:
state.lv.z -= linear_acceleration * sign
if state.key_a:
state.lv.x += linear_acceleration * sign
if state.key_d:
state.lv.x -= linear_acceleration * sign
state.lv = glm.clamp(state.lv, -64, 64)
state.av = glm.clamp(state.av, -64, 64)
obj.move_local(dt * state.lv)
obj.rotate_local(dt * state.av.x, (1, 0, 0))
obj.rotate_local(dt * state.av.y, (0, 1, 0))
obj.rotate_local(dt * state.av.z, (0, 0, 1))
state.lv *= 0.67 ** dt
state.av *= 0.67 ** dt
label0 = Label('Hello World!', color='white', font_size=12, x=10, y=74)
label1 = Label('Hello World!', color='white', font_size=12, x=10, y=52)
label2 = Label('Hello World!', color='white', font_size=12, x=10, y=30)
label3 = Label('Hello World!', color='white', font_size=12, x=10, y=8)
hello_world = Label('hello, world 3D!', color='cyan', font_size=24, x=575, y=10)
@app.event
def render(ct, dt):
app.window.clear()
scene.draw()
label0.text = 'time to draw - %.2f ms' % (1000 * app._time2draw_rm)
label1.text = 'up - %r' % obj.up
label2.text = 'front - %r' % obj.front
label3.text = 'position - %r' % obj.position
label0.draw()
label1.draw()
label2.draw()
label3.draw()
hello_world.draw()
if __name__ == '__main__':
app.run()