-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMain.ts
More file actions
304 lines (261 loc) · 11.2 KB
/
Copy pathMain.ts
File metadata and controls
304 lines (261 loc) · 11.2 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { Camera, Clock, ColorRepresentation, Raycaster, Scene, Vector2, WebGLRenderer, WebGLRendererParameters } from 'three';
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
import { Binding } from '../binding/Binding.js';
import { InteractionManager } from '../events/InteractionManager.js';
import { EventsCache } from '../events/MiscEventsManager.js';
import { RenderManager } from '../rendering/RenderManager.js';
import { RenderView, ViewParameters } from '../rendering/RenderView.js';
import { TweenManager } from '../tweening/TweenManager.js';
import { Stats } from '../utils/Stats.js';
import { RaycasterSortComparer } from '../events/RaycasterManager.js';
import { applyWebGLRendererPatch } from '../patch/WebGLRenderer.js';
/** @internal */
export function setup(): void {
// Script loaded for test. TODO remove this
}
/**
* Configuration parameters for initializing the Main class.
*/
export interface MainParameters {
/** Enable full-screen mode and automatic canvas resizing (default: true). */
fullscreen?: boolean;
/** Display performance statistics (default: true). */
showStats?: boolean;
/** Disable the context menu on right-click (default: true). */
disableContextMenu?: boolean;
/** Default background color (default: 'black'). */
backgroundColor?: ColorRepresentation;
/** Default background alpha (transparency) value (default: 1). */
backgroundAlpha?: number;
/** Callback function executed for each frame. */
animate?: (delta: number, total: number) => void;
/** Configuration parameters for the WebGLRenderer. Ignored if renderer is specified. */
rendererParameters?: WebGLRendererParameters;
/** Enable cursor handling in the application (default: true). */
enableCursor?: boolean;
/** Enable multitouch interactions (default: false). */
multitouch?: boolean;
/** */
renderer?: WebGLRenderer;
}
/**
* The `Main` class serves as the core component for managing a 3D application.
* It provides configuration options and methods for setting up and controlling the application's behavior.
*/
export class Main {
/** A static counter representing the number of animation frames elapsed. */
public ticks = 0;
private _renderManager: RenderManager;
private _interactionManager: InteractionManager;
/** @internal **/ public _stats: Stats;
/** @internal **/ public _showStats: boolean;
private _animate: (delta: number, total: number) => void;
private _clock = new Clock();
/**
* The WebGLRenderer instance used for rendering the 3D scene.
*/
public get renderer(): WebGLRenderer { return this._renderManager.renderer; }
/**
* An array of all RenderView instances managed by the application.
* Lists all views created and managed by the application, each representing a separate viewport or scene.
*/
public get views(): RenderView[] { return this._renderManager.views; }
/**
* The currently active RenderView (activated by mouse position).
*/
public get activeView(): RenderView { return this._renderManager.activeView; }
/**
* The Scene associated with the currently active RenderView.
*/
public get activeScene(): Scene { return this._renderManager.activeView?.scene; }
/**
* The Camera associated with the currently active RenderView.
*/
public get activeCamera(): Camera { return this._renderManager.activeView?.camera; }
/**
* The EffectComposer (used for post-processing) associated with the currently active RenderView.
*/
public get activeComposer(): EffectComposer { return this._renderManager.activeView?.composer; }
/**
* Indicates whether to display performance statistics.
* If set to true, statistics will be shown; otherwise, they will be hidden.
*/
public get showStats(): boolean { return this._showStats; }
public set showStats(value: boolean) {
if (value) {
if (!this._stats) this._stats = new Stats(this.renderer);
document.body.appendChild(this._stats.dom);
} else if (this._stats) {
document.body.removeChild(this._stats.dom);
}
this._showStats = value;
}
/**
* Indicates whether to enable multitouch interactions.
*/
public get multitouch(): boolean { return this._interactionManager.queue.multitouch; }
public set multitouch(value: boolean) { this._interactionManager.queue.multitouch = value; }
/**
* Defines the mouse buttons that can be used for dragging objects.
* Specify the button values as an array of PointerEvent button values.
*/
public get dragButtons(): number[] { return this._interactionManager.dragManager.dragButtons; }
public set dragButtons(value: number[]) { this._interactionManager.dragManager.dragButtons = value; }
/**
* Indicates whether to enable cursor handling in the application.
*/
public get enableCursor(): boolean { return this._interactionManager.cursorManager.enabled; }
public set enableCursor(value: boolean) { this._interactionManager.cursorManager.enabled = value; }
/**
* A custom sorting comparer function used to order intersections when performing raycasting.
*/
public get raycasterSortComparer(): RaycasterSortComparer { return this._interactionManager.raycasterManager.raycasterSortComparer; }
public set raycasterSortComparer(value: RaycasterSortComparer) { this._interactionManager.raycasterManager.raycasterSortComparer = value; }
/**
* A Raycaster instance responsible for handling raycasting operations in the application.
*/
public get raycaster(): Raycaster { return this._interactionManager.raycasterManager.raycaster; }
/**
* The default background color used in the application.
*/
public get backgroundColor(): ColorRepresentation { return this._renderManager.backgroundColor; }
public set backgroundColor(value: ColorRepresentation) { this._renderManager.backgroundColor = value; }
/**
* The default alpha (transparency) value for the background.
*/
public get backgroundAlpha(): number { return this._renderManager.backgroundAlpha; }
public set backgroundAlpha(value: number) { this._renderManager.backgroundAlpha = value; }
/**
* The current mouse position represented as a Vector2.
* Provides the x and y coordinates of the mouse pointer within the application.
*/
public get mousePosition(): Vector2 { return this._interactionManager.raycasterManager.pointer; }
/**
* Indicates if the pointer is over the canvas.
*/
public get pointerOnCanvas(): boolean { return this._interactionManager.raycasterManager.pointerOnCanvas; }
/**
* @param params Configuration parameters for initializing the Main class.
*/
constructor(params: MainParameters = {}) {
if (!params.renderer) this.setDefaultRendererParameters(params);
const renderer = params.renderer ?? new WebGLRenderer(params.rendererParameters);
const appendCanvas = !params.rendererParameters.canvas;
this._renderManager = new RenderManager(renderer, appendCanvas, params.fullscreen, params.backgroundColor, params.backgroundAlpha);
this._interactionManager = new InteractionManager(this._renderManager);
this.multitouch = params.multitouch ?? false;
this.handleContextMenu(params.disableContextMenu);
this.showStats = params.showStats ?? true;
this.enableCursor = params.enableCursor ?? true;
this._animate = params.animate || null;
applyWebGLRendererPatch(this);
}
private setDefaultRendererParameters(parameters: MainParameters): void {
if (!parameters.rendererParameters) parameters.rendererParameters = {};
parameters.rendererParameters.antialias ??= true;
}
private handleContextMenu(disableContextMenu = true): void {
if (disableContextMenu) {
this.renderer.domElement.addEventListener('contextmenu', (e) => e.preventDefault());
}
}
private setAnimationLoop(): void {
this.renderer.setAnimationLoop((time, frame) => {
if (this._showStats) this._stats.begin();
this.ticks++;
const currentDelta = this._clock.getDelta();
this._interactionManager.update();
TweenManager.update(currentDelta * 1000);
this.animate(currentDelta, this._clock.elapsedTime);
const visibleScenes = this._renderManager.getVisibleScenes();
if (visibleScenes) {
for (const scene of visibleScenes) {
const delta = currentDelta * scene.timeScale;
const total = scene.totalTime += delta;
EventsCache.dispatchEvent(scene, 'beforeanimate', { delta, total });
EventsCache.dispatchEvent(scene, 'animate', { delta, total });
EventsCache.dispatchEvent(scene, 'afteranimate', { delta, total });
Binding.compute(scene);
}
this._renderManager.render();
for (const scene of visibleScenes) {
scene.needsRender = !scene.__smartRendering;
}
}
if (this._showStats) {
this._stats.end();
this._stats.update();
}
});
}
private clearAnimationLoop(): void {
this.renderer.setAnimationLoop(null);
}
public animate(delta: number, total: number): void {
if (this._animate) {
this._animate(delta, total);
}
}
/**
* Creates a new RenderView and adds it to the RenderManager.
* @param view The parameters for the new RenderView.
* @returns The created RenderView instance.
*/
public createView(view: ViewParameters): RenderView {
if (this._renderManager.views.length === 0) this.setAnimationLoop();
return this._renderManager.create(view);
}
/**
* Adds a RenderView to the RenderManager.
* @param view The RenderView instance to add.
*/
public addView(view: RenderView): void {
if (this._renderManager.views.length === 0) this.setAnimationLoop();
this._renderManager.add(view);
}
/**
* Removes a RenderView from the RenderManager.
* @param view The RenderView instance to remove.
*/
public removeView(view: RenderView): void {
this._renderManager.remove(view);
if (this._renderManager.views.length === 0) this.clearAnimationLoop(); // TODO consider if we want to move it to renderManager
}
/**
* Removes a RenderView from the RenderManager by its tag.
* @param tag The tag of the RenderView to remove.
*/
public removeViewByTag(tag: string): void {
this._renderManager.removeByTag(tag);
if (this._renderManager.views.length === 0) this.clearAnimationLoop();
}
/**
* Clears all RenderViews from the RenderManager.
*/
public clearViews(): void {
this._renderManager.clear();
this.clearAnimationLoop();
}
/**
* Retrieves a RenderView by its tag.
* @param tag The tag to search for.
* @returns The RenderView with the specified tag, if found, otherwise, undefined.
*/
public getViewByTag(tag: string): RenderView {
return this._renderManager.getByTag(tag);
}
/**
* Retrieves a RenderView by mouse position.
* @param mouse The mouse position as a Vector2.
*/
public getViewByMouse(mouse: Vector2): void {
this._renderManager.getViewByMouse(mouse);
}
/**
* Sets active RenderViews by tag.
* @param tag The tag of the RenderViews to set as active.
*/
public setActiveViewsByTag(tag: string): void {
this._renderManager.setActiveViewsByTag(tag);
}
}