bg2e-js 2.0.0 → 2.0.1
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.d.ts +37 -0
- package/app/Canvas.d.ts +42 -0
- package/app/MainLoop.d.ts +245 -0
- package/package.json +1 -1
- package/render/webgl/ShaderProgram.js +7 -5
- package/shaders/PBRLightIBLShader.js +1 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
import MainLoop from './MainLoop';
|
|
3
|
+
import Canvas from './Canvas';
|
|
4
|
+
import MouseEvent from './MouseEvent';
|
|
5
|
+
import TouchEvent from './TouchEvent';
|
|
6
|
+
import KeyboardEvent from './KeyboardEvent';
|
|
7
|
+
import Renderer from '../render/Renderer';
|
|
8
|
+
|
|
9
|
+
export default class AppController {
|
|
10
|
+
constructor();
|
|
11
|
+
|
|
12
|
+
get mainLoop() ; MainLoop;
|
|
13
|
+
set mainLoop(ml: MainLoop);
|
|
14
|
+
|
|
15
|
+
get canvas() : Canvas;
|
|
16
|
+
|
|
17
|
+
get renderer() : Renderer;
|
|
18
|
+
|
|
19
|
+
get viewport() : { width: number, height: number, aspectRatio: number };
|
|
20
|
+
|
|
21
|
+
async init();
|
|
22
|
+
reshape(width: number, height: number);
|
|
23
|
+
async frame(delta: number);
|
|
24
|
+
display();
|
|
25
|
+
destroy();
|
|
26
|
+
keyDown(evt: KeyboardEvent);
|
|
27
|
+
keyUp(evt: KeyboardEvent);
|
|
28
|
+
mouseUp(evt: MouseEvent);
|
|
29
|
+
mouseDown(evt: MouseEvent);
|
|
30
|
+
mouseMove(evt: MouseEvent);
|
|
31
|
+
mouseOut(evt: MouseEvent);
|
|
32
|
+
mouseDrag(evt: MouseEvent);
|
|
33
|
+
mouseWheel(evt: MouseEvent);
|
|
34
|
+
touchStart(evt: TouchEvent);
|
|
35
|
+
touchMove(evt: TouchEvent);
|
|
36
|
+
touchEnd(evt: TouchEvent);
|
|
37
|
+
}
|
package/app/Canvas.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
import { MainLoop } from './MainLoop';
|
|
3
|
+
|
|
4
|
+
export type Touch = {
|
|
5
|
+
identifier: number
|
|
6
|
+
x: number
|
|
7
|
+
y: number
|
|
8
|
+
force: number
|
|
9
|
+
rotationAngle: number
|
|
10
|
+
radiusX: number
|
|
11
|
+
radiusY: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare function getMouseEventOffset(evt: any, canvas: Canvas) : { x: number, y: number };
|
|
15
|
+
|
|
16
|
+
export declare function getEventTouches(evt: any, canvas: Canvas) : Touch[];
|
|
17
|
+
|
|
18
|
+
export default class Canvas {
|
|
19
|
+
static FirstCanvas() : Canvas;
|
|
20
|
+
|
|
21
|
+
constructor(domElement: HTMLElement, renderer: any);
|
|
22
|
+
|
|
23
|
+
get id() : string;
|
|
24
|
+
|
|
25
|
+
async init() : Promise<void>;
|
|
26
|
+
|
|
27
|
+
get mainLoop() : MainLoop;
|
|
28
|
+
|
|
29
|
+
get renderer() { return this._renderer; }
|
|
30
|
+
|
|
31
|
+
get domElement() { return this._domElement; }
|
|
32
|
+
|
|
33
|
+
get width() { return this._domElement.clientWidth; }
|
|
34
|
+
|
|
35
|
+
get height() { return this._domElement.clientHeight; }
|
|
36
|
+
|
|
37
|
+
get viewport() { return { width: this.width, height: this.height, aspectRatio: this.width / this.height }; }
|
|
38
|
+
|
|
39
|
+
updateViewportSize() : void;
|
|
40
|
+
|
|
41
|
+
screenshot(format: string, width: number, height: number);
|
|
42
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
|
|
2
|
+
import { Canvas } from "./Canvas";
|
|
3
|
+
import { AppController } from "./AppController";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
MouseButtonEventType,
|
|
7
|
+
createMouseEvent,
|
|
8
|
+
leftMouseButton,
|
|
9
|
+
middleMouseButton,
|
|
10
|
+
clearMouseButtons,
|
|
11
|
+
setMouseButton
|
|
12
|
+
} from "./MouseEvent";
|
|
13
|
+
import { createTouchEvent } from "./TouchEvent";
|
|
14
|
+
import { createKeyboardEvent } from "./KeyboardEvent";
|
|
15
|
+
|
|
16
|
+
export const FrameUpdate = {
|
|
17
|
+
AUTO: 0,
|
|
18
|
+
MANUAL: 1
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const g_animationLoop = {
|
|
22
|
+
lastTime: 0,
|
|
23
|
+
mainLoop: []
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
async function animationLoop(totalTime) {
|
|
27
|
+
totalTime = totalTime || 0;
|
|
28
|
+
requestAnimationFrame(animationLoop);
|
|
29
|
+
const elapsed = totalTime - g_animationLoop.lastTime;
|
|
30
|
+
g_animationLoop.lastTime = totalTime;
|
|
31
|
+
for (const ml of g_animationLoop.mainLoop) {
|
|
32
|
+
await onUpdate(ml, elapsed);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class MouseStatus {
|
|
37
|
+
constructor() {
|
|
38
|
+
this.pos = {x: 0, y: 0};
|
|
39
|
+
this.leftButton = false;
|
|
40
|
+
this.middleButton = false;
|
|
41
|
+
this.rightButton = false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get anyButton() { return this.leftButton || this.middleButton || this.rightButton; }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default class MainLoop {
|
|
48
|
+
constructor(canvas: Canvas, appController: AppController);
|
|
49
|
+
|
|
50
|
+
get canvas() : Canvas;
|
|
51
|
+
get appController() : AppController;
|
|
52
|
+
get renderer() { return this._canvas?.renderer; }
|
|
53
|
+
|
|
54
|
+
get updateMode() { return this._updateMode; }
|
|
55
|
+
set updateMode(um) { this._updateMode = um; }
|
|
56
|
+
|
|
57
|
+
get mouseStatus() { return this._mouseStatus; }
|
|
58
|
+
|
|
59
|
+
get redisplay() { return this._redisplayFrames>0; }
|
|
60
|
+
|
|
61
|
+
async run() {
|
|
62
|
+
await this.canvas.init();
|
|
63
|
+
await this.appController.init();
|
|
64
|
+
initEvents(this);
|
|
65
|
+
g_animationLoop.mainLoop.push(this);
|
|
66
|
+
animationLoop();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exit() {
|
|
70
|
+
this.appController.destroy();
|
|
71
|
+
const i = g_animationLoop.mainLoop.indexOf(this);
|
|
72
|
+
if (i!==-1) {
|
|
73
|
+
g_animationLoop.mainLoop.splice(i,1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
postReshape() {
|
|
78
|
+
onResize(this);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
postRedisplay({ frames=2,timeout=10 } = {}) {
|
|
82
|
+
if (timeout <= 0) {
|
|
83
|
+
this._redisplayFrames = frames;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
setTimeout(() => this._redisplayFrames = frames, timeout);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
function initEvents(mainLoop) {
|
|
93
|
+
onResize(mainLoop);
|
|
94
|
+
|
|
95
|
+
const handlePropagation = (bgEvt,evt) => {
|
|
96
|
+
if (bgEvt.isEventPropagationStopped) {
|
|
97
|
+
evt.stopPropagation();
|
|
98
|
+
evt.preventDefault();
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const c = mainLoop.canvas.domElement;
|
|
105
|
+
c.__mainLoop = mainLoop;
|
|
106
|
+
c.addEventListener("mousedown", evt => {
|
|
107
|
+
return handlePropagation(onMouseDown(evt, evt.target.__mainLoop), evt);
|
|
108
|
+
});
|
|
109
|
+
c.addEventListener("mousemove", evt => {
|
|
110
|
+
return handlePropagation(onMouseMove(evt, evt.target.__mainLoop), evt);
|
|
111
|
+
});
|
|
112
|
+
c.addEventListener("mouseout", evt => {
|
|
113
|
+
return handlePropagation(onMouseOut(evt, evt.target.__mainLoop), evt);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
c.addEventListener("mouseover", evt => {
|
|
117
|
+
return handlePropagation(onMouseOver(evt, evt.target.__mainLoop), evt);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
c.addEventListener("mouseup", evt => {
|
|
121
|
+
return handlePropagation(onMouseUp(evt, evt.target.__mainLoop), evt);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
c.addEventListener("touchstart", evt => {
|
|
125
|
+
return handlePropagation(onTouchStart(evt, evt.target.__mainLoop), evt);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
c.addEventListener("touchmove", evt => {
|
|
129
|
+
return handlePropagation(onTouchMove(evt, evt.target.__mainLoop), evt);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
c.addEventListener("touchend", evt => {
|
|
133
|
+
return handlePropagation(onTouchEnd(evt, evt.target.__mainLoop), evt);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const mouseWheelEvt = (/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
|
|
137
|
+
c.addEventListener(mouseWheelEvt, evt => {
|
|
138
|
+
return handlePropagation(onMouseWheel(evt, evt.target.__mainLoop), evt);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
window.addEventListener("keydown", evt => {
|
|
142
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyDown(evt, ml));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
window.addEventListener("keyup", evt => {
|
|
146
|
+
g_animationLoop.mainLoop.forEach(ml => onKeyUp(evt, ml));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
window.addEventListener("resize", evt => {
|
|
150
|
+
onResize(mainLoop);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
c.oncontextmenu = evt => false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function onResize(mainLoop) {
|
|
157
|
+
const dpi = window.devicePixelRatio;
|
|
158
|
+
mainLoop.appController.reshape(mainLoop.canvas.width * dpi, mainLoop.canvas.height * dpi);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function onUpdate(mainLoop, elapsed) {
|
|
162
|
+
if (mainLoop.redisplay) {
|
|
163
|
+
if (mainLoop.updateMode === FrameUpdate.AUTO) {
|
|
164
|
+
mainLoop._redisplayFrames = 1;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
mainLoop._redisplayFrames--;
|
|
168
|
+
}
|
|
169
|
+
if (mainLoop._redisplayFrames > 0) {
|
|
170
|
+
await mainLoop.appController.frame(elapsed);
|
|
171
|
+
mainLoop.appController.display();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function onMouseDown(evt,mainLoop) {
|
|
177
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.DOWN);
|
|
178
|
+
setMouseButton(bg2Event, true);
|
|
179
|
+
mainLoop.appController.mouseDown(bg2Event);
|
|
180
|
+
return bg2Event;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function onMouseMove(evt,mainLoop) {
|
|
184
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
185
|
+
mainLoop.appController.mouseMove(bg2Event);
|
|
186
|
+
if (mainLoop.mouseStatus.anyButton) {
|
|
187
|
+
mainLoop.appController.mouseDrag(bg2Event);
|
|
188
|
+
}
|
|
189
|
+
return bg2Event;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function onMouseOut(evt,mainLoop) {
|
|
193
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
194
|
+
clearMouseButtons();
|
|
195
|
+
mainLoop.appController.mouseOut(bg2Event);
|
|
196
|
+
return bg2Event;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function onMouseOver(evt,mainLoop) {
|
|
200
|
+
return onMouseMove(evt,mainLoop)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function onMouseUp(evt,mainLoop) {
|
|
204
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.UP);
|
|
205
|
+
setMouseButton(bg2Event, false);
|
|
206
|
+
mainLoop.appController.mouseUp(bg2Event);
|
|
207
|
+
return bg2Event;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function onMouseWheel(evt,mainLoop) {
|
|
211
|
+
const bg2Event = createMouseEvent(evt, mainLoop, MouseButtonEventType.NONE);
|
|
212
|
+
bg2Event.delta = evt.wheelDelta ? evt.wheelDelta * -1 : evt.detail * 10;
|
|
213
|
+
mainLoop.appController.mouseWheel(bg2Event);
|
|
214
|
+
return bg2Event;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function onTouchStart(evt,mainLoop) {
|
|
218
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
219
|
+
mainLoop.appController.touchStart(bgEvent);
|
|
220
|
+
return bgEvent;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function onTouchMove(evt,mainLoop) {
|
|
224
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
225
|
+
mainLoop.appController.touchMove(bgEvent);
|
|
226
|
+
return bgEvent;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function onTouchEnd(evt,mainLoop) {
|
|
230
|
+
const bgEvent = createTouchEvent(evt,mainLoop);
|
|
231
|
+
mainLoop.appController.touchEnd(bgEvent);
|
|
232
|
+
return bgEvent;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function onKeyDown(evt,mainLoop) {
|
|
236
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
237
|
+
mainLoop.appController.keyDown(bgEvent);
|
|
238
|
+
return bgEvent;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function onKeyUp(evt,mainLoop) {
|
|
242
|
+
const bgEvent = createKeyboardEvent(evt);
|
|
243
|
+
mainLoop.appController.keyUp(bgEvent);
|
|
244
|
+
return bgEvent;
|
|
245
|
+
}
|
package/package.json
CHANGED
|
@@ -102,11 +102,6 @@ export default class ShaderProgram {
|
|
|
102
102
|
if (!gl.getProgramParameter(this._program, gl.LINK_STATUS)) {
|
|
103
103
|
throw new Error(`Error linking program: \n${gl.getProgramInfoLog(this._program)}`);
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
gl.validateProgram(this._program);
|
|
107
|
-
if (!gl.getProgramParameter(this._program, gl.VALIDATE_STATUS)) {
|
|
108
|
-
throw new Error(`Error validating program:\n${gl.getProgramInfoLog(this._program)}`);
|
|
109
|
-
}
|
|
110
105
|
}
|
|
111
106
|
|
|
112
107
|
useProgram() {
|
|
@@ -345,4 +340,11 @@ export default class ShaderProgram {
|
|
|
345
340
|
}
|
|
346
341
|
this.uniform1i(uniformName, textureUnit);
|
|
347
342
|
}
|
|
343
|
+
|
|
344
|
+
validate() {
|
|
345
|
+
gl.validateProgram(this._program);
|
|
346
|
+
if (!gl.getProgramParameter(this._program, gl.VALIDATE_STATUS)) {
|
|
347
|
+
throw new Error(`Error validating program:\n${gl.getProgramInfoLog(this._program)}`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
348
350
|
}
|
|
@@ -324,7 +324,7 @@ export default class PBRLightIBLShader extends Shader {
|
|
|
324
324
|
materialRenderer.bindValue(this._program, 'roughness', 'uRoughness');
|
|
325
325
|
materialRenderer.bindValue(this._program, 'lightEmission', 'uLightEmission', 0);
|
|
326
326
|
|
|
327
|
-
this._program.uniform1i('uAOMap', material.
|
|
327
|
+
this._program.uniform1i('uAOMap', material.ambientOcclussionUV);
|
|
328
328
|
this._program.uniform1i('uMetallicMap', material.metallicChannel);
|
|
329
329
|
this._program.uniform1i('uRoughnessMap', material.roughnessChannel);
|
|
330
330
|
this._program.uniform1i('uLightemissionMap', material.lightEmissionChannel);
|