-
Notifications
You must be signed in to change notification settings - Fork 29
/
editor3d.py
445 lines (363 loc) · 17.5 KB
/
editor3d.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
import math
from textwrap import dedent
from kivy.app import App
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.graphics.transformation import Matrix
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from editor.space_editor import SpaceEditor
from editor.editor_manager import EditorManager
from kivy.core.window import Window, Keyboard
class Minimal3dApp(App):
editor_manager = ObjectProperty(None, allownone=True)
def build(self):
self.editor_manager = EditorManager(self)
self.move_camera = True
self.cam_distance = 10
self.super = []
init_dist = []
rad = 70.0
azimuth = 0 #0 to 2PI
polar = 90 #0 to PI
self.m_sx = 0
x = rad * math.cos(azimuth) * math.sin(polar)
y = rad * math.sin(azimuth) * math.sin(polar)
z = rad * math.cos(polar)
self.rad = rad
self.azimuth = azimuth
self.polar = polar
layout3d = Builder.load_string(dedent('''
#:kivy 1.0
#: import Layout3D kivy3dgui.layout3d
#: import Animation kivy.animation.Animation
Layout3D:
id: board3d
look_at: [0, 0, 10, 0, 0, -20, 0, 1, 0]
canvas_size: (1920, 1080)
shadow_offset: 2
light_position: [-24.5, 150, 100]
shadow_origin: [-4, 1., -20.]
shadow_target: [-4.01, 0., -23.0]
shadow_threshold: 0.3
post_processing: True
Node:
id: bottom
rotate: (-90, 1, 0, 0)
scale: (1.0, 0.8, 1.0)
translate: (0, -10, -15)
min_light_intensity: 0.5
receive_shadows: True
meshes: ("./meshes/2dbox.obj",)
Button:
id: bottom_floor
text: "Create a Box"
Node:
id: left
rotate: (90, 0, 1, 0)
scale: (1.0, 0.8, 1.0)
translate: (-80, 0, 0)
min_light_intensity: 1.0
receive_shadows: False
meshes: ("./meshes/2dbox.obj",)
Button:
text: "Hello"
canvas:
Color:
rgb: 1.0, 0.0, 1.0, 1.0
Rectangle:
size: self.size
pos: self.pos
Node:
id: right
rotate: (-90, 0, 1, 0)
scale: (0.8, 1.0, 1.0)
translate: (80, 0, 0)
min_light_intensity: 1.0
receive_shadows: False
meshes: ("./meshes/2dbox.obj",)
Button:
text: "Hello"
canvas:
Color:
rgb: 0.0, 0.0, 1.0, 1.0
Rectangle:
size: self.size
pos: self.pos
Node:
id: upper
rotate: (90, 1, 0, 0)
scale: (0.8, 1.0, 1.0)
translate: (0, 80, 0)
min_light_intensity: 1.0
receive_shadows: False
meshes: ("./meshes/2dbox.obj",)
Button:
text: "Hello"
canvas:
Color:
rgb: 0.0, 1.0, 0.0, 1.0
Rectangle:
size: self.size
pos: self.pos
Node:
id: front
rotate: (0, 0, 1, 0)
scale: (1.0, 1.2, 0.8)
translate: (0, 0, -80)
min_light_intensity: 1.0
receive_shadows: True
meshes: ("./meshes/2dbox.obj",)
FloatLayout:
GridLayout:
cols: 3
size_hint: 1.0, 1.0
#pos_hint: {"x": 1.0, "y": 1.0}
CheckBox:
text: "3D Editor?"
Spinner:
id: spinner_c
text: "Cube"
values: ('Cube', 'Sphere')
on_text:
bottom_floor.text = "Create a Box" if args[1] == "Cube" else "Create a Sphere";
on_text:
root.f_type= 0 if args[1] == "Cube" else 1
Button:
text: "Editor"
size_hint: 1.0, 1.0
font_size: 64
Button:
text: "Editor"
size_hint: 1.0, 1.0
font_size: 64
Button:
text: "Editor"
size_hint: 1.0, 1.0
font_size: 64
TextInput:
text: "Editor"
size_hint: 1.0, 1.0
font_size: 32
Button:
text: "Editor"
size_hint: 1.0, 1.0
font_size: 64
Node:
id: back
rotate: (-180, 0, 1, 0)
scale: (1.0, 1.0, 0.8)
translate: (0, 0, 80)
min_light_intensity: 1.0
meshes: ("./meshes/2dbox.obj",)
Button:
text: "Hello"
canvas:
Color:
rgb: 0.5, 0.1, 0.8, 1.0
Rectangle:
size: self.size
pos: self.pos
Button:
id: undo
size_hint: (0.1, 0.1)
text: "Undo"
Button:
id: delete
pos_hint: {"x": 0.1, "y": 0}
size_hint: (0.1, 0.1)
text: "Remove"
'''))
self.box_str = '''
Node:
rotate: ({3}, 0, 1, 0)
yaw: 45
pitch: 0
scale: (2.8, 2.8, 2.8)
translate: ({0}, {1}, {2})
shadows_bias: 0.02
min_light_intensity: 0.4
specular_intensity: 1
specular_power: 320
receive_shadows: True
#normal_map: "./editor/images/normal.png"
meshes: ("./meshes/{4}.obj",)
FloatLayout:
Button:
size_hint: (1., 1.)
id: a_button
'''
str_prop = '''
GridLayout
cols: 1
size_hint: 0.2, 1.0
Button:
'''
properties = Builder.load_string(dedent(str_prop))
layout3d.bind(on_touch_move = self.on_touch_move)
layout3d.ids.bottom_floor.bind(on_touch_up = self.bottom_touch)
layout3d.ids.undo.bind(on_touch_up = self.undo)
layout3d.ids.delete.bind(on_touch_up = self.remove)
self.layout3d = layout3d
self.layout3d.f_type = 0
self.space_editor = SpaceEditor(layout3d, self, self.editor_manager)
layout3d.bind(on_motion=self.on_motion)
#keyboard = Window.request_keyboard(self._keyboard_released, self)
#keyboard.bind(on_key_down=self._keyboard_on_key_down, on_key_up=self._keyboard_released)
grid = GridLayout(cols=2)
grid.add_widget(layout3d)
self.properties = properties
grid.add_widget(self.properties)
return grid
#return layout3d
def _keyboard_released(self, window, keycode):
self.super = []
def _keyboard_on_key_down(self, window, keycode, text, super):
print("Values ", self.super)
if 'lctrl' in self.super and keycode[1] == 'z':
#self.super = []
self.undo()
return False
elif 'lctrl' not in self.super:
self.super.append('lctrl')
return False
else:
self.super = []
return False
def undo(self, *args):
self.editor_manager.restore()
def remove(self, *args):
if self.space_editor.node_helper.current_mesh:
self.space_editor.node_helper.save_command(4, self.layout3d, self.set_mesh)
self.layout3d.remove_widget(self.space_editor.node_helper.current_mesh)
self.space_editor.node_helper.move_to([10000, 10000, 100000])
def on_motion(self, etype, motionevent):
print(etype)
pass
def set_mesh(self, box, g_pos, g_scale, selector=None, c_rot=None):
self.layout3d.add_widget(box)
self.space_editor.free()
self.space_editor = SpaceEditor(self.layout3d, self, self.editor_manager)
self.space_editor.node_helper.current_mesh = box
box.translate = g_pos[:]
#self.space_editor.node_helper.move_to(g_pos[:])
sel = None
if not selector:
sel = box.ids.a_button
else:
sel = selector
sel.bind(on_touch_up = self.on_select_button)
#box.fbo_widget.bind(on_touch_up = self.on_select_button)
sel.s_mesh = box
self.space_editor.node_helper.scale = box.scale[:]
#self.space_editor.node_helper.set_scale(g_scale[:])
if not c_rot:
self.space_editor.node_helper.rotate(self.space_editor.node_helper.rot[:])
else:
self.space_editor.node_helper.rotate(c_rot)
self.space_editor.node_helper.move_to(g_pos[:])
def bottom_touch(self, widget, touch):
size = widget.size
touch_pos = touch.pos
center = [size[0] / 2, size[1] / 2]
#g_pos = self.space_editor.node_helper.pos[:]
g_scale = self.space_editor.node_helper.pos[:]
box = Builder.load_string(dedent(self.box_str.format(0, 0, 0, 0, "box" if self.layout3d.f_type == 0 else "sphere")))
self.set_mesh(box, self.layout3d.look_at[3:6][:], g_scale)
self.space_editor.node_helper.save_command(3, self.layout3d)
def on_select_button(self, *args):
self.space_editor.node_helper.current_mesh = args[0].s_mesh
g_pos = args[0].s_mesh.translate
rot = [args[0].s_mesh.pitch, args[0].s_mesh.yaw, args[0].s_mesh.roll]
g_scale = args[0].s_mesh.scale
self.space_editor.node_helper.move_to(g_pos)
self.space_editor.node_helper.set_scale(g_scale)
self.space_editor.node_helper.rotate(rot)
def get_camera_pos(self):
rad = self.rad
azimuth = math.radians(self.azimuth)
polar = math.radians(self.polar)
x = rad * math.sin(azimuth) * math.sin(polar)
y = rad * math.cos(polar)
z = rad * math.cos(azimuth) * math.sin(polar)
return [x, y, z]
def on_button_touch_down(self, *args):
self.c_sx = self.m_sx = args[1].sx
self.c_sy = self.m_sy = args[1].sy
self.move_camera = False
self.space_editor.node_helper.save_current_state()
c_id = args[0].c_id
if c_id in [3,4, 5]:
self.space_editor.node_helper.save_command(1)
elif c_id in [6, 7, 8]:
self.space_editor.node_helper.save_command(0)
elif c_id in [0, 1, 2]:
self.space_editor.node_helper.save_command(2)
return False
def on_button_touch_up(self, *args):
self.move_camera = True
def on_button_touch_move(self, *args):
c_dis = args[1].sx - self.m_sx
c_dis_y = args[1].sy - self.m_sy
self.m_sx = args[1].sx
self.m_sy = args[1].sy
g_pos = self.space_editor.node_helper.pos[:]
g_scale = self.space_editor.node_helper.scale[:]
if args[0].c_id == 3:
if self.azimuth < 90 and self.azimuth > -90 or (self.azimuth >= 270 and self.azimuth <= 360):
g_scale[0] += c_dis*self.rad
else:
g_scale[0] -= c_dis*self.rad
self.space_editor.node_helper.set_scale(g_scale)
elif args[0].c_id == 5:
g_scale[1] += c_dis_y*self.rad
self.space_editor.node_helper.set_scale(g_scale)
elif (args[0].c_id == 4):
if self.azimuth >= 0 and self.azimuth <= 180:
g_scale[2] -= c_dis*self.rad
else:
g_scale[2] += c_dis*self.rad
self.space_editor.node_helper.set_scale(g_scale)
elif args[0].c_id == 6:
if self.azimuth < 90 and self.azimuth > -90 or (self.azimuth >= 270 and self.azimuth <= 360):
g_pos[0] += c_dis*self.rad
else:
g_pos[0] -= c_dis*self.rad
self.space_editor.node_helper.move_to(g_pos)
elif args[0].c_id == 8:
g_pos[1] += c_dis_y*self.rad
self.space_editor.node_helper.move_to(g_pos)
elif args[0].c_id == 7:
if self.azimuth >= 0 and self.azimuth <= 180:
g_pos[2] -= c_dis*40.0
else:
g_pos[2] += c_dis*40.0
self.space_editor.node_helper.move_to(g_pos)
elif args[0].c_id == 1:
if self.azimuth >= 0 and self.azimuth <= 180:
self.space_editor.node_helper.yaw(c_dis*180.0)
else:
self.space_editor.node_helper.yaw(-c_dis*180.0)
elif args[0].c_id == 2:
self.space_editor.node_helper.roll(c_dis_y*180.0)
elif args[0].c_id == 0:
self.space_editor.node_helper.pitch(-c_dis_y*180.0)
return False
def on_touch_move(self, widget, touch):
if not self.move_camera:
return True
polar_angle = (touch.dy / self.layout3d.height) * 360
azimuth_angle = (touch.dx / self.layout3d.width) * -360
self.azimuth += azimuth_angle
self.polar += polar_angle
if self.polar >= 180:
self.polar = 180
if self.polar <= 0:
self.polar = 0.01
if self.azimuth >= 360:
self.azimuth = 0
x,y,z = self.get_camera_pos()
self.layout3d.look_at = [x, y, z-10, 0, 0, -10, 0, 1, 0]
if __name__ == '__main__':
Minimal3dApp().run()