Repository navigation
Expand file tree
/
Copy pathflock.js
More file actions
290 lines (273 loc) · 8.27 KB
/
Copy pathflock.js
File metadata and controls
290 lines (273 loc) · 8.27 KB
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
// Flock - Creative coding in 3D
// Dr Tracy Gardner - https://github.com/tracygardner
// Flip Computing Limited - flipcomputing.com
import * as BABYLON from '@babylonjs/core';
import { FlowGraphLog10Block } from '@babylonjs/core';
import * as BABYLON_LOADER from '@babylonjs/loaders';
import { GradientMaterial, ShadowOnlyMaterial } from '@babylonjs/materials';
import earcut from 'earcut';
import '@fontsource/atkinson-hyperlegible-next';
import '@fontsource/atkinson-hyperlegible-next/500.css';
import '@fontsource/atkinson-hyperlegible-next/600.css';
import '@fontsource/asap';
import '@fontsource/asap/500.css';
import '@fontsource/asap/600.css';
import { characterNames, getModelDisplayName } from './config';
import { flockRuntimeUrl } from './runtime/urls.js';
import { startStandaloneWhenReady } from './standalone.js';
import { getShowInternalNodes, setShowInternalNodes } from './ui/inspectorVisibility.js';
import * as csg from './api/csg';
import * as animate from './api/animate';
import * as sound from './api/sound';
import * as ui from './api/ui';
import * as movement from './api/movement';
import * as models from './api/models';
import * as shapes from './api/shapes';
import * as transform from './api/transform';
import * as material from './api/material';
import * as effects from './api/effects';
import * as physics from './api/physics';
import * as xr from './api/xr';
import * as control from './api/control';
import * as scene from './api/scene';
import * as mesh from './api/mesh';
import * as camera from './api/camera';
import * as events from './api/events';
import * as sections from './api/sections';
import * as microbit from './api/microbit';
import * as microbitManager from './microbit/manager.js';
import * as math from './api/math';
import * as sensing from './api/sensing';
import * as sandbox from './runtime/sandbox.js';
import * as readiness from './runtime/readiness.js';
import * as lifecycle from './runtime/lifecycle.js';
import * as diagnostics from './runtime/diagnostics.js';
import * as freeze from './runtime/freeze.js';
// Point Babylon’s Draco loader at local folder for offline use
BABYLON.DracoCompression.Configuration = {
decoder: {
wasmUrl: './draco/draco_wasm_wrapper_gltf.js',
wasmBinaryUrl: './draco/draco_decoder_gltf.wasm',
fallbackUrl: './draco/draco_decoder_gltf.js',
},
};
const optionalBabylonDeps = { earcut, FlowGraphLog10Block };
const globalEarcutTarget = typeof globalThis !== 'undefined' ? globalThis : undefined;
if (globalEarcutTarget) {
Object.assign(globalEarcutTarget, optionalBabylonDeps);
}
const flockModules = [
csg,
animate,
sound,
ui,
movement,
models,
shapes,
transform,
material,
effects,
physics,
scene,
mesh,
camera,
xr,
math,
control,
events,
sections,
microbit,
sensing,
sandbox,
readiness,
lifecycle,
diagnostics,
freeze,
microbitManager,
];
export const flock = {
blockDebug: false,
separateAnimations: true,
memoryDebug: false,
memoryMonitorInterval: 5000,
materialsDebug: false,
meshDebug: false,
performanceOverlay: false,
get showInternalNodes() {
return getShowInternalNodes();
},
set showInternalNodes(show) {
setShowInternalNodes(show, this.scene);
},
hideProjectName: true, // input stays in DOM; save/export still reads its value
maxMeshes: 5000,
maxClonesPerSource: 500,
meshLimitEnabled: false,
meshRecyclingEnabled: false,
console: console,
havokAbortHandled: false,
triggerHandlingDebug: false,
soundDebug: false,
modelPath: './models/',
soundPath: './sounds/',
imagePath: './images/',
texturePath: './textures/',
animationPath: './animations/',
// Keep optional Babylon dependencies referenced so bundlers include them.
optionalBabylonDeps,
engine: null,
engineReady: false,
userCodeTimeoutMs: 5000,
eventDebug: false,
modelReadyPromises: new Map(),
pendingMeshCreations: 0,
pendingTriggers: new Map(),
pendingIntersections: new Map(),
_nameRegistry: new Map(),
_liveNameCache: new Map(),
_animationFileCache: {},
_ambiguousLiveNames: new Set(),
getModelDisplayName,
characterNames: characterNames,
alert: alert,
BABYLON: BABYLON,
BABYLON_LOADER: BABYLON_LOADER,
GradientMaterial: GradientMaterial,
ShadowOnlyMaterial: ShadowOnlyMaterial,
scene: null,
highlighter: null,
glowLayer: null,
mainLight: null,
shadowLight: null,
shadowGenerator: null,
shadowCasters: new Set(),
shadowExcluded: new Set(),
xrFramebufferScale: 1.2,
xrFixedFoveation: 0.5,
hk: null,
havokInstance: null,
initialClearColor: null,
ground: null,
sky: null,
GUI: null,
EXPORT: null,
controlsTexture: null,
_xrHUDControlRoots: [],
inputManager: null,
canvas: null,
abortController: null,
// Set only while a section's own setup code (loadSection) is running.
_currentSection: null,
_renderLoop: null,
_renderLoopStopped: false,
_contextLostAt: null,
_escalationTimer: null,
_webglVisibilityListenerAdded: false,
_webglContextLostListenerAdded: false,
_audioFreezeListenerAdded: false,
_audioSuspendedByFreeze: false,
document: document,
disposed: null,
events: new Map(),
modelCache: {},
globalSounds: [],
originalModelTransformations: {},
modelsBeingLoaded: {},
geometryCache: {},
materialCache: {},
physicsShapeCache: {},
flockNotReady: true,
// Diagnostic flag like memoryDebug: logs raw micro:bit serial lines and
// WebUSB status chatter to the console (debug level) while true.
microbitDebug: false,
lastFrameTime: 0,
savedCamera: null,
_editorView: false,
...xr.createFlockXRState(),
...csg.flockCSG,
...animate.flockAnimate,
...sound.flockSound,
...ui.flockUI,
...movement.flockMovement,
...models.flockModels,
...shapes.flockShapes,
...transform.flockTransform,
...material.flockMaterial,
...effects.flockEffects,
...physics.flockPhysics,
...scene.flockScene,
...mesh.flockMesh,
...camera.flockCamera,
...xr.flockXR,
...control.flockControl,
...events.flockEvents,
...sections.flockSections,
...microbit.flockMicrobit,
...sensing.flockSensing,
...math.flockMath,
...sandbox.flockSandbox,
...readiness.flockReadiness,
...lifecycle.flockLifecycle,
...diagnostics.flockDiagnostics,
...freeze.flockFreeze,
// onBlockError lets the UI show a translated message from key + values.
onBlockError: null,
_debugLogging: undefined,
// Runtime helper
describeMesh(meshName, text) {
const safeText = flock.sanitizeInlineText(String(text ?? '').substring(0, 256));
if (!safeText) return;
return flock.whenModelReady(meshName, (readyMesh) => {
if (!readyMesh) return;
const root = readyMesh.metadata?.boundingBox ?? readyMesh;
if (root.metadata) {
root.metadata.displayName = safeText;
}
const setOnChildren = (m) => {
if (m.metadata) {
m.metadata.displayName = safeText;
}
};
if (typeof root.getChildMeshes === 'function') {
root.getChildMeshes(false).forEach(setOnChildren);
}
}).then(() => {});
},
sanitizeInlineText(input) {
return String(input)
.replace(/\r?\n/g, ' ')
.replace(/\*\//g, '*∕')
.replace(/\/\//g, '∕∕')
.replace(/`/g, 'ˋ');
},
download(filename, data, mimeType) {
const blob = new Blob([data], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = flock.document.createElement('a');
a.href = url;
a.download = filename;
flock.document.body.appendChild(a);
a.click();
flock.document.body.removeChild(a);
URL.revokeObjectURL(url);
},
};
function bindFlockModules() {
for (const module of flockModules) {
module.setFlockReference(flock);
}
}
flock._bindModules = bindFlockModules;
// Bind API module closures at load time so workspace-load paths (e.g. the XR
// hint) can call flock helpers before the first scene runs. initializeNewScene
// re-asserts the same reference after each reset.
bindFlockModules();
flock.runtimeUrl = flockRuntimeUrl;
export { initializeFlock } from './standalone.js';
window.setBPM = sound.flockSound.setBPM;
window.updateListenerPositionAndOrientation = sound.flockSound.updateListenerPositionAndOrientation;
window.speak = sound.flockSound.speak;
document.addEventListener('visibilitychange', () => {
flock.freezeEvents.dispatchEvent(new Event('change'));
});
startStandaloneWhenReady();