bg2e-js 2.3.11 → 2.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bg2e-js.js +356 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +69 -69
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +177 -177
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +328 -328
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +143 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
package/src/app/MainLoop.ts
CHANGED
|
@@ -1,273 +1,273 @@
|
|
|
1
|
-
import Canvas from "./Canvas";
|
|
2
|
-
import AppController from "./AppController";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
MouseButtonEventType,
|
|
6
|
-
createMouseEvent,
|
|
7
|
-
clearMouseButtons,
|
|
8
|
-
setMouseButton
|
|
9
|
-
} from "./Bg2MouseEvent";
|
|
10
|
-
import Bg2MouseEvent from "./Bg2MouseEvent";
|
|
11
|
-
import { createTouchEvent } from "./Bg2TouchEvent";
|
|
12
|
-
import Bg2TouchEvent from "./Bg2TouchEvent";
|
|
13
|
-
import { createKeyboardEvent } from "./Bg2KeyboardEvent";
|
|
14
|
-
import Bg2KeyboardEvent from "./Bg2KeyboardEvent";
|
|
15
|
-
|
|
16
|
-
export enum FrameUpdate {
|
|
17
|
-
AUTO = 0,
|
|
18
|
-
MANUAL = 1
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface AnimationLoopState {
|
|
22
|
-
lastTime: number;
|
|
23
|
-
mainLoop: MainLoop[];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const g_animationLoop: AnimationLoopState = {
|
|
27
|
-
lastTime: 0,
|
|
28
|
-
mainLoop: []
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
async function animationLoop(totalTime?: number): Promise<void> {
|
|
32
|
-
totalTime = totalTime || 0;
|
|
33
|
-
requestAnimationFrame(animationLoop);
|
|
34
|
-
const elapsed = totalTime - g_animationLoop.lastTime;
|
|
35
|
-
g_animationLoop.lastTime = totalTime;
|
|
36
|
-
for (const ml of g_animationLoop.mainLoop) {
|
|
37
|
-
await onUpdate(ml, elapsed);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
class MouseStatus {
|
|
42
|
-
pos: { x: number; y: number };
|
|
43
|
-
leftButton: boolean;
|
|
44
|
-
middleButton: boolean;
|
|
45
|
-
rightButton: boolean;
|
|
46
|
-
|
|
47
|
-
constructor() {
|
|
48
|
-
this.pos = { x: 0, y: 0 };
|
|
49
|
-
this.leftButton = false;
|
|
50
|
-
this.middleButton = false;
|
|
51
|
-
this.rightButton = false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get anyButton(): boolean { return this.leftButton || this.middleButton || this.rightButton; }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export default class MainLoop {
|
|
58
|
-
private _canvas: Canvas;
|
|
59
|
-
private _appController: AppController;
|
|
60
|
-
private _updateMode: FrameUpdate;
|
|
61
|
-
private _firstFrameRendered: boolean;
|
|
62
|
-
private _redisplayFrames: number;
|
|
63
|
-
private _mouseStatus: MouseStatus;
|
|
64
|
-
|
|
65
|
-
constructor(canvas: Canvas, appController: AppController) {
|
|
66
|
-
this._canvas = canvas;
|
|
67
|
-
(this._canvas as any)._mainLoop = this;
|
|
68
|
-
this._appController = appController;
|
|
69
|
-
(this._appController as any)._mainLoop = this;
|
|
70
|
-
this._updateMode = FrameUpdate.AUTO;
|
|
71
|
-
this._firstFrameRendered = false;
|
|
72
|
-
this._redisplayFrames = 1;
|
|
73
|
-
|
|
74
|
-
this._mouseStatus = new MouseStatus();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get canvas(): Canvas { return this._canvas; }
|
|
78
|
-
get appController(): AppController { return this._appController; }
|
|
79
|
-
get renderer(): any { return this._canvas?.renderer; }
|
|
80
|
-
|
|
81
|
-
get updateMode(): FrameUpdate { return this._updateMode; }
|
|
82
|
-
set updateMode(um: FrameUpdate) { this._updateMode = um; }
|
|
83
|
-
|
|
84
|
-
get mouseStatus(): MouseStatus { return this._mouseStatus; }
|
|
85
|
-
|
|
86
|
-
get redisplay(): boolean { return this._redisplayFrames > 0; }
|
|
87
|
-
|
|
88
|
-
async run(): Promise<void> {
|
|
89
|
-
await this.canvas.init();
|
|
90
|
-
await this.appController.init();
|
|
91
|
-
initEvents(this);
|
|
92
|
-
g_animationLoop.mainLoop.push(this);
|
|
93
|
-
animationLoop();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
exit(): void {
|
|
97
|
-
this.appController.destroy();
|
|
98
|
-
const i = g_animationLoop.mainLoop.indexOf(this);
|
|
99
|
-
if (i !== -1) {
|
|
100
|
-
g_animationLoop.mainLoop.splice(i, 1);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
postReshape(): void {
|
|
105
|
-
onResize(this);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
postRedisplay({ frames = 10, timeout = 10 }: { frames?: number; timeout?: number } = {}): void {
|
|
109
|
-
if (timeout <= 0) {
|
|
110
|
-
this._redisplayFrames = frames;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
setTimeout(() => this._redisplayFrames = frames, timeout);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
function initEvents(mainLoop: MainLoop): void {
|
|
120
|
-
onResize(mainLoop);
|
|
121
|
-
|
|
122
|
-
const handlePropagation = (bgEvt: any, evt: Event): boolean => {
|
|
123
|
-
if (bgEvt.isEventPropagationStopped) {
|
|
124
|
-
evt.stopPropagation();
|
|
125
|
-
evt.preventDefault();
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const c = mainLoop.canvas.domElement;
|
|
132
|
-
(c as any).__mainLoop = mainLoop;
|
|
133
|
-
c.addEventListener("mousedown", (evt: any) => {
|
|
134
|
-
return handlePropagation(onMouseDown(evt, (evt.target as any).__mainLoop), evt);
|
|
135
|
-
});
|
|
136
|
-
c.addEventListener("mousemove", (evt: any) => {
|
|
137
|
-
return handlePropagation(onMouseMove(evt, (evt.target as any).__mainLoop), evt);
|
|
138
|
-
});
|
|
139
|
-
c.addEventListener("mouseout", (evt: any) => {
|
|
140
|
-
return handlePropagation(onMouseOut(evt, (evt.target as any).__mainLoop), evt);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
c.addEventListener("mouseover", (evt: any) => {
|
|
144
|
-
return handlePropagation(onMouseOver(evt, (evt.target as any).__mainLoop), evt);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
c.addEventListener("mouseup", (evt: any) => {
|
|
148
|
-
return handlePropagation(onMouseUp(evt, (evt.target as any).__mainLoop), evt);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
c.addEventListener("touchstart", (evt: any) => {
|
|
152
|
-
return handlePropagation(onTouchStart(evt, (evt.target as any).__mainLoop), evt);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
c.addEventListener("touchmove", (evt: any) => {
|
|
156
|
-
return handlePropagation(onTouchMove(evt, (evt.target as any).__mainLoop), evt);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
c.addEventListener("touchend", (evt: any) => {
|
|
160
|
-
return handlePropagation(onTouchEnd(evt, (evt.target as any).__mainLoop), evt);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
const mouseWheelEvt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
|
|
164
|
-
c.addEventListener(mouseWheelEvt, (evt: any) => {
|
|
165
|
-
return handlePropagation(onMouseWheel(evt, (evt.target as any).__mainLoop), evt);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
window.addEventListener("keydown", (evt: any) => {
|
|
169
|
-
g_animationLoop.mainLoop.forEach(ml => onKeyDown(evt, ml));
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
window.addEventListener("keyup", (evt: any) => {
|
|
173
|
-
g_animationLoop.mainLoop.forEach(ml => onKeyUp(evt, ml));
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
window.addEventListener("resize", (evt: Event) => {
|
|
177
|
-
onResize(mainLoop);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
(c as any).oncontextmenu = (evt: Event) => false;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function onResize(mainLoop: MainLoop): void {
|
|
184
|
-
const dpi = window.devicePixelRatio;
|
|
185
|
-
mainLoop.appController.reshape(mainLoop.canvas.width * dpi, mainLoop.canvas.height * dpi);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async function onUpdate(mainLoop: MainLoop, elapsed: number): Promise<void> {
|
|
189
|
-
if (mainLoop.redisplay) {
|
|
190
|
-
if (mainLoop.updateMode === FrameUpdate.AUTO) {
|
|
191
|
-
(mainLoop as any)._redisplayFrames = 1;
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
(mainLoop as any)._redisplayFrames--;
|
|
195
|
-
}
|
|
196
|
-
if ((mainLoop as any)._redisplayFrames > 0 || !(mainLoop as any)._firstFrameRendered) {
|
|
197
|
-
await mainLoop.appController.frame(elapsed);
|
|
198
|
-
mainLoop.appController.display();
|
|
199
|
-
(mainLoop as any)._firstFrameRendered = true;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function onMouseDown(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
205
|
-
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.DOWN);
|
|
206
|
-
setMouseButton(bg2Event, true);
|
|
207
|
-
mainLoop.appController.mouseDown(bg2Event);
|
|
208
|
-
return bg2Event;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function onMouseMove(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
212
|
-
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
213
|
-
mainLoop.appController.mouseMove(bg2Event);
|
|
214
|
-
if (mainLoop.mouseStatus.anyButton) {
|
|
215
|
-
mainLoop.appController.mouseDrag(bg2Event);
|
|
216
|
-
}
|
|
217
|
-
return bg2Event;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function onMouseOut(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
221
|
-
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
222
|
-
clearMouseButtons();
|
|
223
|
-
mainLoop.appController.mouseOut(bg2Event);
|
|
224
|
-
return bg2Event;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function onMouseOver(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
228
|
-
return onMouseMove(evt, mainLoop);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function onMouseUp(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
232
|
-
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.UP);
|
|
233
|
-
setMouseButton(bg2Event, false);
|
|
234
|
-
mainLoop.appController.mouseUp(bg2Event);
|
|
235
|
-
return bg2Event;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function onMouseWheel(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
239
|
-
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
240
|
-
bg2Event.delta = evt.wheelDelta ? evt.wheelDelta * -1 : evt.detail * 10;
|
|
241
|
-
mainLoop.appController.mouseWheel(bg2Event);
|
|
242
|
-
return bg2Event;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function onTouchStart(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
246
|
-
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
247
|
-
mainLoop.appController.touchStart(bgEvent);
|
|
248
|
-
return bgEvent;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function onTouchMove(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
252
|
-
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
253
|
-
mainLoop.appController.touchMove(bgEvent);
|
|
254
|
-
return bgEvent;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function onTouchEnd(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
258
|
-
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
259
|
-
mainLoop.appController.touchEnd(bgEvent);
|
|
260
|
-
return bgEvent;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function onKeyDown(evt: any, mainLoop: MainLoop): Bg2KeyboardEvent {
|
|
264
|
-
const bgEvent = createKeyboardEvent(evt);
|
|
265
|
-
mainLoop.appController.keyDown(bgEvent);
|
|
266
|
-
return bgEvent;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function onKeyUp(evt: any, mainLoop: MainLoop): Bg2KeyboardEvent {
|
|
270
|
-
const bgEvent = createKeyboardEvent(evt);
|
|
271
|
-
mainLoop.appController.keyUp(bgEvent);
|
|
272
|
-
return bgEvent;
|
|
273
|
-
}
|
|
1
|
+
import Canvas from "./Canvas";
|
|
2
|
+
import AppController from "./AppController";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
MouseButtonEventType,
|
|
6
|
+
createMouseEvent,
|
|
7
|
+
clearMouseButtons,
|
|
8
|
+
setMouseButton
|
|
9
|
+
} from "./Bg2MouseEvent";
|
|
10
|
+
import Bg2MouseEvent from "./Bg2MouseEvent";
|
|
11
|
+
import { createTouchEvent } from "./Bg2TouchEvent";
|
|
12
|
+
import Bg2TouchEvent from "./Bg2TouchEvent";
|
|
13
|
+
import { createKeyboardEvent } from "./Bg2KeyboardEvent";
|
|
14
|
+
import Bg2KeyboardEvent from "./Bg2KeyboardEvent";
|
|
15
|
+
|
|
16
|
+
export enum FrameUpdate {
|
|
17
|
+
AUTO = 0,
|
|
18
|
+
MANUAL = 1
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface AnimationLoopState {
|
|
22
|
+
lastTime: number;
|
|
23
|
+
mainLoop: MainLoop[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const g_animationLoop: AnimationLoopState = {
|
|
27
|
+
lastTime: 0,
|
|
28
|
+
mainLoop: []
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
async function animationLoop(totalTime?: number): Promise<void> {
|
|
32
|
+
totalTime = totalTime || 0;
|
|
33
|
+
requestAnimationFrame(animationLoop);
|
|
34
|
+
const elapsed = totalTime - g_animationLoop.lastTime;
|
|
35
|
+
g_animationLoop.lastTime = totalTime;
|
|
36
|
+
for (const ml of g_animationLoop.mainLoop) {
|
|
37
|
+
await onUpdate(ml, elapsed);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class MouseStatus {
|
|
42
|
+
pos: { x: number; y: number };
|
|
43
|
+
leftButton: boolean;
|
|
44
|
+
middleButton: boolean;
|
|
45
|
+
rightButton: boolean;
|
|
46
|
+
|
|
47
|
+
constructor() {
|
|
48
|
+
this.pos = { x: 0, y: 0 };
|
|
49
|
+
this.leftButton = false;
|
|
50
|
+
this.middleButton = false;
|
|
51
|
+
this.rightButton = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get anyButton(): boolean { return this.leftButton || this.middleButton || this.rightButton; }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default class MainLoop {
|
|
58
|
+
private _canvas: Canvas;
|
|
59
|
+
private _appController: AppController;
|
|
60
|
+
private _updateMode: FrameUpdate;
|
|
61
|
+
private _firstFrameRendered: boolean;
|
|
62
|
+
private _redisplayFrames: number;
|
|
63
|
+
private _mouseStatus: MouseStatus;
|
|
64
|
+
|
|
65
|
+
constructor(canvas: Canvas, appController: AppController) {
|
|
66
|
+
this._canvas = canvas;
|
|
67
|
+
(this._canvas as any)._mainLoop = this;
|
|
68
|
+
this._appController = appController;
|
|
69
|
+
(this._appController as any)._mainLoop = this;
|
|
70
|
+
this._updateMode = FrameUpdate.AUTO;
|
|
71
|
+
this._firstFrameRendered = false;
|
|
72
|
+
this._redisplayFrames = 1;
|
|
73
|
+
|
|
74
|
+
this._mouseStatus = new MouseStatus();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get canvas(): Canvas { return this._canvas; }
|
|
78
|
+
get appController(): AppController { return this._appController; }
|
|
79
|
+
get renderer(): any { return this._canvas?.renderer; }
|
|
80
|
+
|
|
81
|
+
get updateMode(): FrameUpdate { return this._updateMode; }
|
|
82
|
+
set updateMode(um: FrameUpdate) { this._updateMode = um; }
|
|
83
|
+
|
|
84
|
+
get mouseStatus(): MouseStatus { return this._mouseStatus; }
|
|
85
|
+
|
|
86
|
+
get redisplay(): boolean { return this._redisplayFrames > 0; }
|
|
87
|
+
|
|
88
|
+
async run(): Promise<void> {
|
|
89
|
+
await this.canvas.init();
|
|
90
|
+
await this.appController.init();
|
|
91
|
+
initEvents(this);
|
|
92
|
+
g_animationLoop.mainLoop.push(this);
|
|
93
|
+
animationLoop();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exit(): void {
|
|
97
|
+
this.appController.destroy();
|
|
98
|
+
const i = g_animationLoop.mainLoop.indexOf(this);
|
|
99
|
+
if (i !== -1) {
|
|
100
|
+
g_animationLoop.mainLoop.splice(i, 1);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
postReshape(): void {
|
|
105
|
+
onResize(this);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
postRedisplay({ frames = 10, timeout = 10 }: { frames?: number; timeout?: number } = {}): void {
|
|
109
|
+
if (timeout <= 0) {
|
|
110
|
+
this._redisplayFrames = frames;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
setTimeout(() => this._redisplayFrames = frames, timeout);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
function initEvents(mainLoop: MainLoop): void {
|
|
120
|
+
onResize(mainLoop);
|
|
121
|
+
|
|
122
|
+
const handlePropagation = (bgEvt: any, evt: Event): boolean => {
|
|
123
|
+
if (bgEvt.isEventPropagationStopped) {
|
|
124
|
+
evt.stopPropagation();
|
|
125
|
+
evt.preventDefault();
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const c = mainLoop.canvas.domElement;
|
|
132
|
+
(c as any).__mainLoop = mainLoop;
|
|
133
|
+
c.addEventListener("mousedown", (evt: any) => {
|
|
134
|
+
return handlePropagation(onMouseDown(evt, (evt.target as any).__mainLoop), evt);
|
|
135
|
+
});
|
|
136
|
+
c.addEventListener("mousemove", (evt: any) => {
|
|
137
|
+
return handlePropagation(onMouseMove(evt, (evt.target as any).__mainLoop), evt);
|
|
138
|
+
});
|
|
139
|
+
c.addEventListener("mouseout", (evt: any) => {
|
|
140
|
+
return handlePropagation(onMouseOut(evt, (evt.target as any).__mainLoop), evt);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
c.addEventListener("mouseover", (evt: any) => {
|
|
144
|
+
return handlePropagation(onMouseOver(evt, (evt.target as any).__mainLoop), evt);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
c.addEventListener("mouseup", (evt: any) => {
|
|
148
|
+
return handlePropagation(onMouseUp(evt, (evt.target as any).__mainLoop), evt);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
c.addEventListener("touchstart", (evt: any) => {
|
|
152
|
+
return handlePropagation(onTouchStart(evt, (evt.target as any).__mainLoop), evt);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
c.addEventListener("touchmove", (evt: any) => {
|
|
156
|
+
return handlePropagation(onTouchMove(evt, (evt.target as any).__mainLoop), evt);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
c.addEventListener("touchend", (evt: any) => {
|
|
160
|
+
return handlePropagation(onTouchEnd(evt, (evt.target as any).__mainLoop), evt);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const mouseWheelEvt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
|
|
164
|
+
c.addEventListener(mouseWheelEvt, (evt: any) => {
|
|
165
|
+
return handlePropagation(onMouseWheel(evt, (evt.target as any).__mainLoop), evt);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
window.addEventListener("keydown", (evt: any) => {
|
|
169
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyDown(evt, ml));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
window.addEventListener("keyup", (evt: any) => {
|
|
173
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyUp(evt, ml));
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
window.addEventListener("resize", (evt: Event) => {
|
|
177
|
+
onResize(mainLoop);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
(c as any).oncontextmenu = (evt: Event) => false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function onResize(mainLoop: MainLoop): void {
|
|
184
|
+
const dpi = window.devicePixelRatio;
|
|
185
|
+
mainLoop.appController.reshape(mainLoop.canvas.width * dpi, mainLoop.canvas.height * dpi);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function onUpdate(mainLoop: MainLoop, elapsed: number): Promise<void> {
|
|
189
|
+
if (mainLoop.redisplay) {
|
|
190
|
+
if (mainLoop.updateMode === FrameUpdate.AUTO) {
|
|
191
|
+
(mainLoop as any)._redisplayFrames = 1;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
(mainLoop as any)._redisplayFrames--;
|
|
195
|
+
}
|
|
196
|
+
if ((mainLoop as any)._redisplayFrames > 0 || !(mainLoop as any)._firstFrameRendered) {
|
|
197
|
+
await mainLoop.appController.frame(elapsed);
|
|
198
|
+
mainLoop.appController.display();
|
|
199
|
+
(mainLoop as any)._firstFrameRendered = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function onMouseDown(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
205
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.DOWN);
|
|
206
|
+
setMouseButton(bg2Event, true);
|
|
207
|
+
mainLoop.appController.mouseDown(bg2Event);
|
|
208
|
+
return bg2Event;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function onMouseMove(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
212
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
213
|
+
mainLoop.appController.mouseMove(bg2Event);
|
|
214
|
+
if (mainLoop.mouseStatus.anyButton) {
|
|
215
|
+
mainLoop.appController.mouseDrag(bg2Event);
|
|
216
|
+
}
|
|
217
|
+
return bg2Event;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function onMouseOut(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
221
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
222
|
+
clearMouseButtons();
|
|
223
|
+
mainLoop.appController.mouseOut(bg2Event);
|
|
224
|
+
return bg2Event;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function onMouseOver(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
228
|
+
return onMouseMove(evt, mainLoop);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function onMouseUp(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
232
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.UP);
|
|
233
|
+
setMouseButton(bg2Event, false);
|
|
234
|
+
mainLoop.appController.mouseUp(bg2Event);
|
|
235
|
+
return bg2Event;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function onMouseWheel(evt: any, mainLoop: MainLoop): Bg2MouseEvent {
|
|
239
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
240
|
+
bg2Event.delta = evt.wheelDelta ? evt.wheelDelta * -1 : evt.detail * 10;
|
|
241
|
+
mainLoop.appController.mouseWheel(bg2Event);
|
|
242
|
+
return bg2Event;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function onTouchStart(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
246
|
+
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
247
|
+
mainLoop.appController.touchStart(bgEvent);
|
|
248
|
+
return bgEvent;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function onTouchMove(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
252
|
+
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
253
|
+
mainLoop.appController.touchMove(bgEvent);
|
|
254
|
+
return bgEvent;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function onTouchEnd(evt: any, mainLoop: MainLoop): Bg2TouchEvent {
|
|
258
|
+
const bgEvent = createTouchEvent(evt, mainLoop);
|
|
259
|
+
mainLoop.appController.touchEnd(bgEvent);
|
|
260
|
+
return bgEvent;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function onKeyDown(evt: any, mainLoop: MainLoop): Bg2KeyboardEvent {
|
|
264
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
265
|
+
mainLoop.appController.keyDown(bgEvent);
|
|
266
|
+
return bgEvent;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function onKeyUp(evt: any, mainLoop: MainLoop): Bg2KeyboardEvent {
|
|
270
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
271
|
+
mainLoop.appController.keyUp(bgEvent);
|
|
272
|
+
return bgEvent;
|
|
273
|
+
}
|
package/src/app/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import AppController from "./AppController";
|
|
2
|
-
import Canvas, {
|
|
3
|
-
getMouseEventOffset,
|
|
4
|
-
getEventTouches
|
|
5
|
-
} from "./Canvas";
|
|
6
|
-
import EventBase from "./EventBase";
|
|
7
|
-
import MainLoop, {
|
|
8
|
-
FrameUpdate
|
|
9
|
-
} from "./MainLoop";
|
|
10
|
-
import Bg2KeyboardEvent from "./Bg2KeyboardEvent";
|
|
11
|
-
import Bg2MouseEvent from "./Bg2MouseEvent";
|
|
12
|
-
import Bg2TouchEvent from "./Bg2TouchEvent";
|
|
13
|
-
|
|
14
|
-
export default {
|
|
15
|
-
AppController,
|
|
16
|
-
FrameUpdate,
|
|
17
|
-
Canvas,
|
|
18
|
-
getMouseEventOffset,
|
|
19
|
-
getEventTouches,
|
|
20
|
-
EventBase,
|
|
21
|
-
Bg2KeyboardEvent,
|
|
22
|
-
MainLoop,
|
|
23
|
-
Bg2MouseEvent,
|
|
24
|
-
Bg2TouchEvent
|
|
1
|
+
import AppController from "./AppController";
|
|
2
|
+
import Canvas, {
|
|
3
|
+
getMouseEventOffset,
|
|
4
|
+
getEventTouches
|
|
5
|
+
} from "./Canvas";
|
|
6
|
+
import EventBase from "./EventBase";
|
|
7
|
+
import MainLoop, {
|
|
8
|
+
FrameUpdate
|
|
9
|
+
} from "./MainLoop";
|
|
10
|
+
import Bg2KeyboardEvent from "./Bg2KeyboardEvent";
|
|
11
|
+
import Bg2MouseEvent from "./Bg2MouseEvent";
|
|
12
|
+
import Bg2TouchEvent from "./Bg2TouchEvent";
|
|
13
|
+
|
|
14
|
+
export default {
|
|
15
|
+
AppController,
|
|
16
|
+
FrameUpdate,
|
|
17
|
+
Canvas,
|
|
18
|
+
getMouseEventOffset,
|
|
19
|
+
getEventTouches,
|
|
20
|
+
EventBase,
|
|
21
|
+
Bg2KeyboardEvent,
|
|
22
|
+
MainLoop,
|
|
23
|
+
Bg2MouseEvent,
|
|
24
|
+
Bg2TouchEvent
|
|
25
25
|
}
|