bg2e-js 1.4.32 → 2.0.0
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/app/AppController.js +32 -0
- package/app/Canvas.js +98 -0
- package/app/EventBase.js +8 -0
- package/app/KeyboardEvent.js +51 -0
- package/app/MainLoop.js +251 -0
- package/app/MouseEvent.js +77 -0
- package/app/TouchEvent.js +15 -0
- package/base/Color.js +131 -0
- package/base/Environment.js +78 -0
- package/base/Light.js +176 -0
- package/base/Material.js +619 -0
- package/base/PolyList.js +336 -0
- package/base/Texture.js +545 -0
- package/db/Bg2LoaderPlugin.js +113 -0
- package/db/DBPluginApi.js +40 -0
- package/db/Loader.js +91 -0
- package/db/LoaderPlugin.js +26 -0
- package/db/MtlParser.js +7 -0
- package/db/ObjLoaderPlugin.js +51 -0
- package/db/ObjParser.js +233 -0
- package/db/ObjWriterPlugin.js +18 -0
- package/db/VitscnjLoaderPlugin.js +87 -0
- package/db/Writer.js +49 -0
- package/db/WriterPlugin.js +20 -0
- package/debug/DebugRenderer.js +138 -0
- package/debug/WebGLTextureViewer.js +67 -0
- package/manipulation/SelectionBuffer.js +65 -0
- package/manipulation/SelectionHighlight.js +69 -0
- package/manipulation/SelectionIdAssignVisitor.js +86 -0
- package/manipulation/SelectionManager.js +134 -0
- package/manipulation/SelectionMode.js +6 -0
- package/math/Mat3.js +248 -0
- package/math/Mat4.js +694 -0
- package/math/MatrixStrategy.js +23 -0
- package/math/Quat.js +60 -0
- package/math/Vec.js +728 -0
- package/math/constants.js +44 -0
- package/math/functions.js +104 -0
- package/math/index.js +74 -0
- package/package.json +26 -23
- package/phsics/joint.js +124 -0
- package/primitives/arrow.js +58 -0
- package/primitives/cone.js +137 -0
- package/primitives/cube.js +59 -0
- package/primitives/cylinder.js +215 -0
- package/primitives/index.js +13 -0
- package/primitives/plane.js +31 -0
- package/primitives/sphere.js +808 -0
- package/render/BRDFIntegrationMap.js +4 -0
- package/render/Environment.js +105 -0
- package/render/EnvironmentRenderer.js +12 -0
- package/render/FrameBuffer.js +26 -0
- package/render/MaterialRenderer.js +28 -0
- package/render/Pipeline.js +83 -0
- package/render/PolyListRenderer.js +42 -0
- package/render/RenderBuffer.js +189 -0
- package/render/RenderQueue.js +162 -0
- package/render/RenderState.js +82 -0
- package/render/Renderer.js +202 -0
- package/render/SceneAppController.js +208 -0
- package/render/SceneRenderer.js +310 -0
- package/render/Shader.js +21 -0
- package/render/ShadowRenderer.js +159 -0
- package/render/SkyCube.js +77 -0
- package/render/SkySphere.js +94 -0
- package/render/TextureMergerRenderer.js +58 -0
- package/render/TextureRenderer.js +29 -0
- package/render/webgl/FrameBuffer.js +9 -0
- package/render/webgl/MaterialRenderer.js +99 -0
- package/render/webgl/Pipeline.js +82 -0
- package/render/webgl/PolyListRenderer.js +224 -0
- package/render/webgl/RenderBuffer.js +244 -0
- package/render/webgl/Renderer.js +239 -0
- package/render/webgl/SceneRenderer.js +43 -0
- package/render/webgl/ShaderProgram.js +348 -0
- package/render/webgl/ShadowRenderer.js +6 -0
- package/render/webgl/SkyCube.js +15 -0
- package/render/webgl/SkySphere.js +14 -0
- package/render/webgl/State.js +149 -0
- package/render/webgl/TextureRenderer.js +167 -0
- package/render/webgl/VertexBuffer.js +128 -0
- package/scene/Camera.js +378 -0
- package/scene/Chain.js +43 -0
- package/scene/ChainJoint.js +55 -0
- package/scene/Component.js +146 -0
- package/scene/ComponentMap.js +99 -0
- package/scene/Drawable.js +130 -0
- package/scene/EnvironmentComponent.js +123 -0
- package/scene/FindNodeVisitor.js +55 -0
- package/scene/LightComponent.js +146 -0
- package/scene/MatrixState.js +39 -0
- package/scene/Node.js +300 -0
- package/scene/NodeVisitor.js +12 -0
- package/scene/OrbitCameraController.js +407 -0
- package/scene/SmoothOrbitCameraController.js +92 -0
- package/scene/Transform.js +74 -0
- package/scene/index.js +24 -0
- package/shaders/BasicDiffuseColorShader.js +91 -0
- package/shaders/BasicPBRLightShader.js +238 -0
- package/shaders/DebugRenderShader.js +79 -0
- package/shaders/DepthRenderShader.js +69 -0
- package/shaders/IrradianceMapCubeShader.js +99 -0
- package/shaders/PBRLightIBLShader.js +380 -0
- package/shaders/PickSelectionShader.js +75 -0
- package/shaders/PresentDebugFramebufferShader.js +90 -0
- package/shaders/PresentTextureShader.js +73 -0
- package/shaders/SelectionHighlightShader.js +98 -0
- package/shaders/ShaderFunction.js +72 -0
- package/shaders/SkyCubeShader.js +78 -0
- package/shaders/SkySphereShader.js +77 -0
- package/shaders/SpecularMapCubeShader.js +145 -0
- package/shaders/TextureMergerShader.js +127 -0
- package/shaders/webgl_shader_lib.js +187 -0
- package/tools/BinaryResourceProvider.js +15 -0
- package/tools/ImageResourceProvider.js +65 -0
- package/tools/MaterialModifier.js +228 -0
- package/tools/Resource.js +177 -0
- package/tools/ResourceProvider.js +56 -0
- package/tools/TextResourceProvider.js +24 -0
- package/tools/TextureCache.js +44 -0
- package/tools/TextureResourceDatabase.js +87 -0
- package/tools/UserAgent.js +294 -0
- package/tools/VideoResourceProvider.js +51 -0
- package/tools/WriteStrategy.js +22 -0
- package/tools/base64.js +15 -0
- package/tools/crypto.js +15 -0
- package/tools/endiantess.js +15 -0
- package/tools/image.js +15 -0
- package/tools/index.js +33 -0
- package/tools/processType.js +56 -0
- package/LICENSE +0 -21
- package/README.md +0 -2
- package/bower.json +0 -12
- package/js/bg2e-es2015.js +0 -27821
- package/js/bg2e.js +0 -22435
- package/js/bg2e.min.js +0 -1
- package/js/traceur-runtime.js +0 -4206
- package/resources/floor_gizmo.vwglb +0 -0
- package/resources/manipulator_v2.png +0 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
export default class AppController {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._mainLoop = null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
get mainLoop() { return this._mainLoop; }
|
|
8
|
+
set mainLoop(ml) { this._mainLoop = ml; }
|
|
9
|
+
|
|
10
|
+
get canvas() { return this._mainLoop?.canvas; }
|
|
11
|
+
|
|
12
|
+
get renderer() { return this._mainLoop?.canvas?.renderer; }
|
|
13
|
+
|
|
14
|
+
get viewport() { return this.canvas?.viewport || { width: 0, height: 0, aspectRatio: 0 }; }
|
|
15
|
+
|
|
16
|
+
async init() {}
|
|
17
|
+
reshape(width,height) {}
|
|
18
|
+
async frame(delta) {}
|
|
19
|
+
display() {}
|
|
20
|
+
destroy() {}
|
|
21
|
+
keyDown(evt) {}
|
|
22
|
+
keyUp(evt) {}
|
|
23
|
+
mouseUp(evt) {}
|
|
24
|
+
mouseDown(evt) {}
|
|
25
|
+
mouseMove(evt) {}
|
|
26
|
+
mouseOut(evt) {}
|
|
27
|
+
mouseDrag(evt) {}
|
|
28
|
+
mouseWheel(evt) {}
|
|
29
|
+
touchStart(evt) {}
|
|
30
|
+
touchMove(evt) {}
|
|
31
|
+
touchEnd(evt) {}
|
|
32
|
+
}
|
package/app/Canvas.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { generateUUID } from "../tools/crypto";
|
|
2
|
+
|
|
3
|
+
export const getMouseEventOffset = (evt,canvas) => {
|
|
4
|
+
const offset = canvas.domElement.getBoundingClientRect();
|
|
5
|
+
return {
|
|
6
|
+
x: evt.clientX - offset.left,
|
|
7
|
+
y: evt.clientY - offset.top
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const getEventTouches = (evt,canvas) => {
|
|
12
|
+
const offset = canvas.domElement.getBoundingClientRect();
|
|
13
|
+
const touches = Array.from(evt.touches).map(touch => {
|
|
14
|
+
return {
|
|
15
|
+
identifier: touch.identifier,
|
|
16
|
+
x: touch.clientX - offset.left,
|
|
17
|
+
y: touch.clientY - offset.top,
|
|
18
|
+
force: touch.force,
|
|
19
|
+
rotationAngle: touch.rotationAngle,
|
|
20
|
+
radiusX: touch.radiusX,
|
|
21
|
+
radiusY: touch.radiusY
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
return touches;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let g_firstCanvas = null;
|
|
28
|
+
|
|
29
|
+
export default class Canvas {
|
|
30
|
+
static FirstCanvas() {
|
|
31
|
+
return g_firstCanvas;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
constructor(domElement,renderer) {
|
|
35
|
+
this._renderer = renderer;
|
|
36
|
+
this._domElement = domElement;
|
|
37
|
+
this._domElement._bg2e_id = generateUUID();
|
|
38
|
+
|
|
39
|
+
g_firstCanvas = g_firstCanvas || this;
|
|
40
|
+
|
|
41
|
+
// Initialized in mainLoop constructor
|
|
42
|
+
this._mainLoop = null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get id() {
|
|
46
|
+
return this._domElement._bg2e_id;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async init() {
|
|
50
|
+
await this._renderer.init(this);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get mainLoop() { return this._mainLoop; }
|
|
54
|
+
|
|
55
|
+
get renderer() { return this._renderer; }
|
|
56
|
+
|
|
57
|
+
get domElement() { return this._domElement; }
|
|
58
|
+
|
|
59
|
+
get width() { return this._domElement.clientWidth; }
|
|
60
|
+
|
|
61
|
+
get height() { return this._domElement.clientHeight; }
|
|
62
|
+
|
|
63
|
+
get viewport() { return { width: this.width, height: this.height, aspectRatio: this.width / this.height }; }
|
|
64
|
+
|
|
65
|
+
updateViewportSize() {
|
|
66
|
+
const sizeInPx = { w: this.domElement.clientWidth, h: this.domElement.clientHeight };
|
|
67
|
+
this.domElement.width = sizeInPx.w * window.devicePixelRatio;
|
|
68
|
+
this.domElement.height = sizeInPx.h * window.devicePixelRatio;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
screenshot(format, width, height) {
|
|
72
|
+
let canvasStyle = "";
|
|
73
|
+
const prevSize = {};
|
|
74
|
+
|
|
75
|
+
if (width) {
|
|
76
|
+
height = height ? height : width;
|
|
77
|
+
canvasStyle = this.domElement.style.cssText;
|
|
78
|
+
prevSize.width = this.domElement.width;
|
|
79
|
+
prevSize.height = this.domElement.height;
|
|
80
|
+
|
|
81
|
+
this.domElement.style.cssText = `top:auto;left:auto;bottom:auto;right:auto;width:${width}px;height:${height}px;`;
|
|
82
|
+
this.domElement.width = width;
|
|
83
|
+
this.domElement.height = height;
|
|
84
|
+
this.mainLoop.appController.reshape(width,height);
|
|
85
|
+
this.mainLoop.appController.display();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const data = this.domElement.toDataURL(format);
|
|
89
|
+
if (width) {
|
|
90
|
+
this.domElement.cssText = canvasStyle;
|
|
91
|
+
this.domElement.width = prevSize.width;
|
|
92
|
+
this.domElement.height = prevSize.height;
|
|
93
|
+
this.mainLoop.appController.reshape(prevSize.width, prevSize.heigth);
|
|
94
|
+
this.mainLoop.appController.display();
|
|
95
|
+
}
|
|
96
|
+
return data;
|
|
97
|
+
}
|
|
98
|
+
}
|
package/app/EventBase.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import EventBase from "./EventBase";
|
|
2
|
+
|
|
3
|
+
export const SpecialKey = {
|
|
4
|
+
BACKSPACE: "Backspace",
|
|
5
|
+
TAB: "Tab",
|
|
6
|
+
ENTER: "Enter",
|
|
7
|
+
SHIFT: "Shift",
|
|
8
|
+
SHIFT_LEFT: "ShiftLeft",
|
|
9
|
+
SHIFT_RIGHT: "ShiftRight",
|
|
10
|
+
CTRL: "Control",
|
|
11
|
+
CTRL_LEFT: "ControlLeft",
|
|
12
|
+
CTRL_LEFT: "ControlRight",
|
|
13
|
+
ALT: "Alt",
|
|
14
|
+
ALT_LEFT: "AltLeft",
|
|
15
|
+
ALT_RIGHT: "AltRight",
|
|
16
|
+
PAUSE: "Pause",
|
|
17
|
+
CAPS_LOCK: "CapsLock",
|
|
18
|
+
ESCAPE: "Escape",
|
|
19
|
+
PAGE_UP: "PageUp",
|
|
20
|
+
PAGEDOWN: "PageDown",
|
|
21
|
+
END: "End",
|
|
22
|
+
HOME: "Home",
|
|
23
|
+
LEFT_ARROW: "ArrowLeft",
|
|
24
|
+
UP_ARROW: "ArrowUp",
|
|
25
|
+
RIGHT_ARROW: "ArrowRight",
|
|
26
|
+
DOWN_ARROW: "ArrowDown",
|
|
27
|
+
INSERT: "Insert",
|
|
28
|
+
DELETE: "Delete",
|
|
29
|
+
SPACE: "Space"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const createKeyboardEvent = (evt) => {
|
|
33
|
+
const code = SpecialKey[evt.code] != null ? evt.keyCode : evt.code;
|
|
34
|
+
return new KeyboardEvent(code, evt);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default class KeyboardEvent extends EventBase {
|
|
38
|
+
static IsSpecialKey(event) {
|
|
39
|
+
return SpecialKey[event.code]!=null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor(key,event) {
|
|
43
|
+
super();
|
|
44
|
+
this.key = key;
|
|
45
|
+
this.event = event;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get isSpecialKey() {
|
|
49
|
+
return KeyboardEvent.IsSpecialKey(this.event);
|
|
50
|
+
}
|
|
51
|
+
}
|
package/app/MainLoop.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
MouseButtonEventType,
|
|
4
|
+
createMouseEvent,
|
|
5
|
+
leftMouseButton,
|
|
6
|
+
middleMouseButton,
|
|
7
|
+
clearMouseButtons,
|
|
8
|
+
setMouseButton
|
|
9
|
+
} from "./MouseEvent";
|
|
10
|
+
import { createTouchEvent } from "./TouchEvent";
|
|
11
|
+
import { createKeyboardEvent } from "./KeyboardEvent";
|
|
12
|
+
|
|
13
|
+
export const FrameUpdate = {
|
|
14
|
+
AUTO: 0,
|
|
15
|
+
MANUAL: 1
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const g_animationLoop = {
|
|
19
|
+
lastTime: 0,
|
|
20
|
+
mainLoop: []
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
async function animationLoop(totalTime) {
|
|
24
|
+
totalTime = totalTime || 0;
|
|
25
|
+
requestAnimationFrame(animationLoop);
|
|
26
|
+
const elapsed = totalTime - g_animationLoop.lastTime;
|
|
27
|
+
g_animationLoop.lastTime = totalTime;
|
|
28
|
+
for (const ml of g_animationLoop.mainLoop) {
|
|
29
|
+
await onUpdate(ml, elapsed);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class MouseStatus {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.pos = {x: 0, y: 0};
|
|
36
|
+
this.leftButton = false;
|
|
37
|
+
this.middleButton = false;
|
|
38
|
+
this.rightButton = false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get anyButton() { return this.leftButton || this.middleButton || this.rightButton; }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default class MainLoop {
|
|
45
|
+
constructor(canvas, appController) {
|
|
46
|
+
this._canvas = canvas;
|
|
47
|
+
this._canvas._mainLoop = this;
|
|
48
|
+
this._appController = appController;
|
|
49
|
+
this._appController._mainLoop = this;
|
|
50
|
+
this._updateMode = FrameUpdate.AUTO;
|
|
51
|
+
this._redisplayFrames = 1;
|
|
52
|
+
|
|
53
|
+
this._mouseStatus = new MouseStatus();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get canvas() { return this._canvas; }
|
|
57
|
+
get appController() { return this._appController; }
|
|
58
|
+
get renderer() { return this._canvas?.renderer; }
|
|
59
|
+
|
|
60
|
+
get updateMode() { return this._updateMode; }
|
|
61
|
+
set updateMode(um) { this._updateMode = um; }
|
|
62
|
+
|
|
63
|
+
get mouseStatus() { return this._mouseStatus; }
|
|
64
|
+
|
|
65
|
+
get redisplay() { return this._redisplayFrames>0; }
|
|
66
|
+
|
|
67
|
+
async run() {
|
|
68
|
+
await this.canvas.init();
|
|
69
|
+
await this.appController.init();
|
|
70
|
+
initEvents(this);
|
|
71
|
+
g_animationLoop.mainLoop.push(this);
|
|
72
|
+
animationLoop();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exit() {
|
|
76
|
+
this.appController.destroy();
|
|
77
|
+
const i = g_animationLoop.mainLoop.indexOf(this);
|
|
78
|
+
if (i!==-1) {
|
|
79
|
+
g_animationLoop.mainLoop.splice(i,1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
postReshape() {
|
|
84
|
+
onResize(this);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
postRedisplay({ frames=2,timeout=10 } = {}) {
|
|
88
|
+
if (timeout <= 0) {
|
|
89
|
+
this._redisplayFrames = frames;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
setTimeout(() => this._redisplayFrames = frames, timeout);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
function initEvents(mainLoop) {
|
|
99
|
+
onResize(mainLoop);
|
|
100
|
+
|
|
101
|
+
const handlePropagation = (bgEvt,evt) => {
|
|
102
|
+
if (bgEvt.isEventPropagationStopped) {
|
|
103
|
+
evt.stopPropagation();
|
|
104
|
+
evt.preventDefault();
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const c = mainLoop.canvas.domElement;
|
|
111
|
+
c.__mainLoop = mainLoop;
|
|
112
|
+
c.addEventListener("mousedown", evt => {
|
|
113
|
+
return handlePropagation(onMouseDown(evt, evt.target.__mainLoop), evt);
|
|
114
|
+
});
|
|
115
|
+
c.addEventListener("mousemove", evt => {
|
|
116
|
+
return handlePropagation(onMouseMove(evt, evt.target.__mainLoop), evt);
|
|
117
|
+
});
|
|
118
|
+
c.addEventListener("mouseout", evt => {
|
|
119
|
+
return handlePropagation(onMouseOut(evt, evt.target.__mainLoop), evt);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
c.addEventListener("mouseover", evt => {
|
|
123
|
+
return handlePropagation(onMouseOver(evt, evt.target.__mainLoop), evt);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
c.addEventListener("mouseup", evt => {
|
|
127
|
+
return handlePropagation(onMouseUp(evt, evt.target.__mainLoop), evt);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
c.addEventListener("touchstart", evt => {
|
|
131
|
+
return handlePropagation(onTouchStart(evt, evt.target.__mainLoop), evt);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
c.addEventListener("touchmove", evt => {
|
|
135
|
+
return handlePropagation(onTouchMove(evt, evt.target.__mainLoop), evt);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
c.addEventListener("touchend", evt => {
|
|
139
|
+
return handlePropagation(onTouchEnd(evt, evt.target.__mainLoop), evt);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const mouseWheelEvt = (/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
|
|
143
|
+
c.addEventListener(mouseWheelEvt, evt => {
|
|
144
|
+
return handlePropagation(onMouseWheel(evt, evt.target.__mainLoop), evt);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
window.addEventListener("keydown", evt => {
|
|
148
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyDown(evt, ml));
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
window.addEventListener("keyup", evt => {
|
|
152
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyUp(evt, ml));
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
window.addEventListener("resize", evt => {
|
|
156
|
+
onResize(mainLoop);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
c.oncontextmenu = evt => false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function onResize(mainLoop) {
|
|
163
|
+
const dpi = window.devicePixelRatio;
|
|
164
|
+
mainLoop.appController.reshape(mainLoop.canvas.width * dpi, mainLoop.canvas.height * dpi);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function onUpdate(mainLoop, elapsed) {
|
|
168
|
+
if (mainLoop.redisplay) {
|
|
169
|
+
if (mainLoop.updateMode === FrameUpdate.AUTO) {
|
|
170
|
+
mainLoop._redisplayFrames = 1;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
mainLoop._redisplayFrames--;
|
|
174
|
+
}
|
|
175
|
+
if (mainLoop._redisplayFrames > 0) {
|
|
176
|
+
await mainLoop.appController.frame(elapsed);
|
|
177
|
+
mainLoop.appController.display();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function onMouseDown(evt,mainLoop) {
|
|
183
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.DOWN);
|
|
184
|
+
setMouseButton(bg2Event, true);
|
|
185
|
+
mainLoop.appController.mouseDown(bg2Event);
|
|
186
|
+
return bg2Event;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function onMouseMove(evt,mainLoop) {
|
|
190
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
191
|
+
mainLoop.appController.mouseMove(bg2Event);
|
|
192
|
+
if (mainLoop.mouseStatus.anyButton) {
|
|
193
|
+
mainLoop.appController.mouseDrag(bg2Event);
|
|
194
|
+
}
|
|
195
|
+
return bg2Event;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function onMouseOut(evt,mainLoop) {
|
|
199
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
200
|
+
clearMouseButtons();
|
|
201
|
+
mainLoop.appController.mouseOut(bg2Event);
|
|
202
|
+
return bg2Event;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function onMouseOver(evt,mainLoop) {
|
|
206
|
+
return onMouseMove(evt,mainLoop)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function onMouseUp(evt,mainLoop) {
|
|
210
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.UP);
|
|
211
|
+
setMouseButton(bg2Event, false);
|
|
212
|
+
mainLoop.appController.mouseUp(bg2Event);
|
|
213
|
+
return bg2Event;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function onMouseWheel(evt,mainLoop) {
|
|
217
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
218
|
+
bg2Event.delta = evt.wheelDelta ? evt.wheelDelta * -1 : evt.detail * 10;
|
|
219
|
+
mainLoop.appController.mouseWheel(bg2Event);
|
|
220
|
+
return bg2Event;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function onTouchStart(evt,mainLoop) {
|
|
224
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
225
|
+
mainLoop.appController.touchStart(bgEvent);
|
|
226
|
+
return bgEvent;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function onTouchMove(evt,mainLoop) {
|
|
230
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
231
|
+
mainLoop.appController.touchMove(bgEvent);
|
|
232
|
+
return bgEvent;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function onTouchEnd(evt,mainLoop) {
|
|
236
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
237
|
+
mainLoop.appController.touchEnd(bgEvent);
|
|
238
|
+
return bgEvent;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function onKeyDown(evt,mainLoop) {
|
|
242
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
243
|
+
mainLoop.appController.keyDown(bgEvent);
|
|
244
|
+
return bgEvent;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function onKeyUp(evt,mainLoop) {
|
|
248
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
249
|
+
mainLoop.appController.keyUp(bgEvent);
|
|
250
|
+
return bgEvent;
|
|
251
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { getMouseEventOffset } from "./Canvas";
|
|
2
|
+
import EventBase from "./EventBase";
|
|
3
|
+
|
|
4
|
+
export const MouseButton = {
|
|
5
|
+
LEFT: 0,
|
|
6
|
+
MIDDLE: 1,
|
|
7
|
+
RIGHT: 2,
|
|
8
|
+
NONE: -1
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const MouseButtonEventType = {
|
|
12
|
+
NONE: 0,
|
|
13
|
+
UP: 1,
|
|
14
|
+
DOWN: 2
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const createMouseEvent = (evt,mainLoop,buttonType) => {
|
|
18
|
+
mainLoop.mouseStatus.pos = getMouseEventOffset(evt, mainLoop.canvas);
|
|
19
|
+
if (buttonType !== MouseButtonEventType.NONE) {
|
|
20
|
+
const buttonStatus = buttonType === MouseButtonEventType.DOWN;
|
|
21
|
+
if (evt.button === MouseButton.LEFT) {
|
|
22
|
+
mainLoop.mouseStatus.leftButton = buttonStatus;
|
|
23
|
+
}
|
|
24
|
+
else if (evt.button === MouseButton.MIDDLE) {
|
|
25
|
+
mainLoop.mouseStatus.middleButton = buttonStatus;
|
|
26
|
+
}
|
|
27
|
+
else if (evt.button === MouseButton.RIGHT) {
|
|
28
|
+
mainLoop.mouseStatus.rightButton = buttonStatus;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return new MouseEvent(evt.button, mainLoop.mouseStatus.pos.x, mainLoop.mouseStatus.pos.y, 0, evt);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const g_mouseButtons = [false,false,false];
|
|
36
|
+
export const leftMouseButton = () => {
|
|
37
|
+
return g_mouseButtons[0];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const middleMouseButton = () => {
|
|
41
|
+
return g_mouseButtons[1];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const rightMouseButton = () => {
|
|
45
|
+
return g_mouseButtons[2];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const clearMouseButtons = () => {
|
|
49
|
+
g_mouseButtons[0] = false;
|
|
50
|
+
g_mouseButtons[1] = false;
|
|
51
|
+
g_mouseButtons[2] = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const setMouseButton = (event, status) => {
|
|
55
|
+
switch (event.button) {
|
|
56
|
+
case MouseButton.LEFT:
|
|
57
|
+
g_mouseButtons[0] = status;
|
|
58
|
+
break;
|
|
59
|
+
case MouseButton.MIDDLE:
|
|
60
|
+
g_mouseButtons[1] = status;
|
|
61
|
+
break;
|
|
62
|
+
case MouseButton.RIGHT:
|
|
63
|
+
g_mouseButtons[2] = status;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export default class MouseEvent extends EventBase {
|
|
68
|
+
|
|
69
|
+
constructor(button = MouseButton.NONE, x=-1, y=-1, delta=0,event=null) {
|
|
70
|
+
super();
|
|
71
|
+
this.button = button;
|
|
72
|
+
this.x = x;
|
|
73
|
+
this.y = y;
|
|
74
|
+
this.delta = delta;
|
|
75
|
+
this.event = event;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getEventTouches } from "./Canvas";
|
|
2
|
+
import EventBase from "./EventBase";
|
|
3
|
+
|
|
4
|
+
export const createTouchEvent = (evt,mainLoop) => {
|
|
5
|
+
const touches = getEventTouches(evt,mainLoop.canvas);
|
|
6
|
+
return new TouchEvent(touches, evt);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default class TouchEvent extends EventBase {
|
|
10
|
+
constructor(touches,event) {
|
|
11
|
+
super();
|
|
12
|
+
this.touches = touches;
|
|
13
|
+
this.event = event;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/base/Color.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import Vec from '../math/Vec';
|
|
2
|
+
|
|
3
|
+
const checkLength = (v1,v2 = null) => {
|
|
4
|
+
if (v1.length < 4 || (v2 !== null && v2.length < 4)) {
|
|
5
|
+
throw new Error(`Invalid color component length`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default class Color extends Vec {
|
|
10
|
+
constructor() {
|
|
11
|
+
if (arguments.length === 1 && arguments[0].length === 4) {
|
|
12
|
+
// 4 elements array
|
|
13
|
+
super(arguments[0]);
|
|
14
|
+
}
|
|
15
|
+
else if (arguments.length === 1 && arguments[0].length === 3) {
|
|
16
|
+
super(arguments[0][0], arguments[0][1], arguments[0][2], 1);
|
|
17
|
+
}
|
|
18
|
+
else if (typeof(arguments) === "object" &&
|
|
19
|
+
arguments[0].rgb !== undefined ||
|
|
20
|
+
arguments[0].r !== undefined ||
|
|
21
|
+
arguments[0].g !== undefined ||
|
|
22
|
+
arguments[0].b !== undefined ||
|
|
23
|
+
arguments[0].a !== undefined
|
|
24
|
+
) {
|
|
25
|
+
const r = arguments[0].r || arguments[0].rgb || 0;
|
|
26
|
+
const g = arguments[0].g || arguments[0].rgb || 0;
|
|
27
|
+
const b = arguments[0].b || arguments[0].rgb || 0;
|
|
28
|
+
const a = arguments[0].a || arguments[0].a || 1;
|
|
29
|
+
super(r, g, b, a);
|
|
30
|
+
}
|
|
31
|
+
else if (arguments.length === 0) {
|
|
32
|
+
super([0, 0, 0, 1]);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error('Invalid initialization parameters in Color constructor');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static Yellow() { return new Color([1.0,1.0,0.0,1.0]); }
|
|
40
|
+
static Orange() { return new Color([1.0,0.5,0.0,1.0]); }
|
|
41
|
+
static Red() { return new Color([1.0,0.0,0.0,1.0]); }
|
|
42
|
+
static Violet() { return new Color([0.5,0.0,1.0,1.0]); }
|
|
43
|
+
static Blue() { return new Color([0.0,0.0,1.0,1.0]); }
|
|
44
|
+
static Green() { return new Color([0.0,1.0,0.0,1.0]); }
|
|
45
|
+
static White() { return new Color([1.0,1.0,1.0,1.0]); }
|
|
46
|
+
static LightGray() { return new Color([0.8,0.8,0.8,1.0]); }
|
|
47
|
+
static Gray() { return new Color([0.5,0.5,0.5,1.0]); }
|
|
48
|
+
static DarkGray() { return new Color([0.2,0.2,0.2,1.0]); }
|
|
49
|
+
static Black() { return new Color([0.0,0.0,0.0,1.0]); }
|
|
50
|
+
static Brown() { return new Color([0.4,0.2,0.0,1.0]); }
|
|
51
|
+
static Transparent() { return new Color([0,0,0,0]); }
|
|
52
|
+
|
|
53
|
+
get r() { return this[0]; }
|
|
54
|
+
set r(v) { this[0] = v; }
|
|
55
|
+
get g() { return this[1]; }
|
|
56
|
+
set g(v) { this[1] = v; }
|
|
57
|
+
get b() { return this[2]; }
|
|
58
|
+
set b(v) { this[2] = v; }
|
|
59
|
+
get a() { return this[3]; }
|
|
60
|
+
set a(v) { this[3] = v; }
|
|
61
|
+
|
|
62
|
+
get rgb() { return new Vec(this[0], this[1], this[2]); }
|
|
63
|
+
set rgb(rgb) {
|
|
64
|
+
if (rgb.length === 3) {
|
|
65
|
+
this[0] = rgb[0];
|
|
66
|
+
this[1] = rgb[1];
|
|
67
|
+
this[2] = rgb[2];
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw new Error("Invalid parameter settings rgb values in Color");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static Max(v1,v2) {
|
|
75
|
+
checkLength(v1, v2);
|
|
76
|
+
return new Color([
|
|
77
|
+
v1[0]>v2[0] ? v1[0] : v2[0],
|
|
78
|
+
v1[1]>v2[1] ? v1[1] : v2[1],
|
|
79
|
+
v1[2]>v2[2] ? v1[2] : v2[2],
|
|
80
|
+
v1[3]>v2[3] ? v1[3] : v2[3]
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static Min(v1,v2) {
|
|
85
|
+
checkLength(v1, v2);
|
|
86
|
+
return new Color([
|
|
87
|
+
v1[0]<v2[0] ? v1[0] : v2[0],
|
|
88
|
+
v1[1]<v2[1] ? v1[1] : v2[1],
|
|
89
|
+
v1[2]<v2[2] ? v1[2] : v2[2],
|
|
90
|
+
v1[3]<v2[3] ? v1[3] : v2[3]
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static Add(v1,v2) {
|
|
95
|
+
checkLength(v1, v2);
|
|
96
|
+
return new Color([
|
|
97
|
+
v1[0] + v2[0],
|
|
98
|
+
v1[1] + v2[1],
|
|
99
|
+
v1[2] + v2[2],
|
|
100
|
+
v1[3] + v2[3]
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static Sub(v1,v2) {
|
|
105
|
+
checkLength(v1, v2);
|
|
106
|
+
return new Color([
|
|
107
|
+
v1[0] - v2[0],
|
|
108
|
+
v1[1] - v2[1],
|
|
109
|
+
v1[2] - v2[2],
|
|
110
|
+
v1[3] - v2[3]
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static Mult(v,s) {
|
|
115
|
+
checkLength(v);
|
|
116
|
+
new Color([ v[0] * s, v[1] * s, v[2] * s, v[3] * s ]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static Div(v,s) {
|
|
120
|
+
switch (v.length) {
|
|
121
|
+
case 2:
|
|
122
|
+
return new Vec([ v[0] / s, v[1] / s ]);
|
|
123
|
+
case 3:
|
|
124
|
+
return new Vec([ v[0] / s, v[1] / s, v[2] / s ]);
|
|
125
|
+
case 4:
|
|
126
|
+
return new Vec([ v[0] / s, v[1] / s, v[2] / s, v[3] / s ]);
|
|
127
|
+
default:
|
|
128
|
+
throw new Error(`Invalid vector size: ${ v.length }`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|