10
10
11
11
from multipyjs import MICROPYTHON , new , call , to_js , create_proxy
12
12
13
+
13
14
@dataclass
14
15
class SoundPlayer :
15
16
sound : THREE .Audio = field ()
16
17
on_start : Callable [[], None ] = field ()
17
18
on_stop : Callable [[], None ] = field (default = lambda : None )
18
19
19
20
_start_time : float = - 1.0
20
-
21
+
21
22
def play (self ):
22
23
self .sound .stop ()
23
24
self .on_start ()
@@ -34,7 +35,7 @@ def toggle(self):
34
35
self .stop ()
35
36
else :
36
37
self .play ()
37
-
38
+
38
39
@property
39
40
def running_time (self ):
40
41
if self .sound .isPlaying :
@@ -43,6 +44,7 @@ def running_time(self):
43
44
self .stop ()
44
45
return 0.0
45
46
47
+
46
48
def get_renderer ():
47
49
renderer = new (THREE .WebGLRenderer , antialias = True )
48
50
renderer .setSize (window .innerWidth , window .innerHeight )
@@ -57,6 +59,7 @@ def get_renderer():
57
59
document .getElementById ("threejs" ).appendChild (renderer .domElement )
58
60
59
61
initial = {0 : "115px" , 1 : "calc(100vh - 120px)" }
62
+
60
63
@create_proxy
61
64
def split_element_style (dimension , size , gutter_size , index ):
62
65
if index in initial :
@@ -72,64 +75,71 @@ def split_element_style(dimension, size, gutter_size, index):
72
75
elementStyle = split_element_style ,
73
76
minSize = 0 ,
74
77
maxSize = to_js ([120 , 10000 ]),
75
- )
78
+ )
76
79
return renderer
77
80
81
+
78
82
def get_ortho_camera (view_size ):
79
83
aspect_ratio = window .innerWidth / window .innerHeight
80
84
camera = new (
81
85
THREE .OrthographicCamera ,
82
86
- view_size * aspect_ratio , # Left
83
- view_size * aspect_ratio , # Right
84
- view_size , # Top
85
- - view_size , # Bottom
86
- - view_size , # Near plane
87
- view_size , # Far plane
87
+ view_size * aspect_ratio , # Right
88
+ view_size , # Top
89
+ - view_size , # Bottom
90
+ - view_size , # Near plane
91
+ view_size , # Far plane
88
92
)
89
93
camera .updateProjectionMatrix ()
90
94
camera .position .set (0 , 0 , 0 )
91
95
return camera
92
96
97
+
93
98
def get_loading_manager ():
94
99
loading_mgr = new (THREE .LoadingManager )
95
100
ev = asyncio .Event ()
96
-
101
+
97
102
@create_proxy
98
103
def on_start (url , itemsLoaded , itemsTotal ):
99
104
print (f'[{ itemsLoaded } /{ itemsTotal } ] Started loading file: { url } ' )
105
+
100
106
loading_mgr .onStart = on_start
101
-
107
+
102
108
@create_proxy
103
109
def on_progress (url , itemsLoaded , itemsTotal ):
104
110
print (f'[{ itemsLoaded } /{ itemsTotal } ] Loading file: { url } ' )
111
+
105
112
loading_mgr .onProgress = on_progress
106
-
113
+
107
114
@create_proxy
108
115
def on_error (url ):
109
116
print (f'There was a problem loading { url } ' )
117
+
110
118
loading_mgr .onError = on_error
111
-
119
+
112
120
@create_proxy
113
121
def on_load ():
114
122
print ('Loading assets complete!' )
115
123
ev .set ()
124
+
116
125
loading_mgr .onLoad = on_load
117
-
126
+
118
127
return loading_mgr , ev
119
128
120
129
121
130
def get_perspective_camera ():
122
131
aspect_ratio = window .innerWidth / window .innerHeight
123
132
camera = new (
124
133
THREE .PerspectiveCamera ,
125
- 45 , # fov
134
+ 45 , # fov
126
135
aspect_ratio ,
127
- 0.25 , # near plane
128
- 300 , # far plane
136
+ 0.25 , # near plane
137
+ 300 , # far plane
129
138
)
130
139
camera .position .set (0 , 0 , 30 )
131
140
return camera
132
141
142
+
133
143
def get_stats_gl (renderer ):
134
144
stats = new (StatsGL , trackGPU = True , horizontal = False )
135
145
stats .init (renderer )
@@ -138,19 +148,20 @@ def get_stats_gl(renderer):
138
148
document .getElementById ("stats" ).appendChild (stats .dom )
139
149
return stats
140
150
151
+
141
152
def bg_from_v (* vertices ):
142
153
geometry = new (THREE .BufferGeometry )
143
154
vertices_f32a = new (Float32Array , vertices )
144
155
attr = new (THREE .Float32BufferAttribute , vertices_f32a , 3 )
145
156
return geometry .setAttribute ('position' , attr )
146
157
158
+
147
159
def bg_from_p (* points ):
148
160
buf = new (THREE .BufferGeometry )
149
- buf .setFromPoints (
150
- [new (THREE .Vector3 , p [0 ], p [1 ], p [2 ]) for p in points ]
151
- )
161
+ buf .setFromPoints ([new (THREE .Vector3 , p [0 ], p [1 ], p [2 ]) for p in points ])
152
162
return buf
153
163
164
+
154
165
def clear ():
155
166
# toggle stats and terminal?
156
167
stats_style = document .getElementById ("stats-off" ).style
@@ -160,7 +171,7 @@ def clear():
160
171
document .getElementById ("pyterm" ).style .height = "115px"
161
172
document .getElementById ("threejs" ).style .height = "calc(100vh - 120px)"
162
173
for e in document .getElementsByClassName ("gutter" ):
163
- e .style .removeProperty ("display" )
174
+ e .style .removeProperty ("display" )
164
175
for e in document .getElementsByClassName ("xterm-helper-textarea" ):
165
176
e .focus ()
166
177
break
@@ -186,4 +197,4 @@ def clear():
186
197
for e in document .getElementsByTagName ("svg" ):
187
198
style = e .getAttribute ("style" )
188
199
if style and style .startswith ("z-index:999" ):
189
- e .style .display = "none"
200
+ e .style .display = "none"
0 commit comments