melonjs 10.7.1 → 10.10.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/LICENSE.md +1 -1
- package/README.md +29 -23
- package/dist/melonjs.js +2220 -1070
- package/dist/melonjs.min.js +4 -4
- package/dist/melonjs.module.d.ts +1395 -485
- package/dist/melonjs.module.js +2244 -1131
- package/package.json +17 -14
- package/src/camera/camera2d.js +1 -1
- package/src/entity/entity.js +6 -7
- package/src/game.js +2 -2
- package/src/geometries/ellipse.js +20 -21
- package/src/geometries/line.js +7 -7
- package/src/geometries/path2d.js +319 -0
- package/src/geometries/poly.js +27 -27
- package/src/geometries/rectangle.js +19 -19
- package/src/geometries/roundrect.js +164 -0
- package/src/index.js +12 -2
- package/src/input/gamepad.js +2 -2
- package/src/input/pointerevent.js +1 -1
- package/src/lang/deprecated.js +8 -6
- package/src/level/tiled/TMXLayer.js +1 -1
- package/src/level/tiled/TMXObject.js +9 -12
- package/src/level/tiled/TMXTileMap.js +23 -4
- package/src/level/tiled/TMXUtils.js +1 -1
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
- package/src/loader/loader.js +4 -4
- package/src/loader/loadingscreen.js +17 -6
- package/src/math/color.js +6 -5
- package/src/math/matrix2.js +1 -1
- package/src/math/matrix3.js +1 -1
- package/src/math/observable_vector2.js +1 -1
- package/src/math/observable_vector3.js +1 -1
- package/src/math/vector2.js +1 -1
- package/src/math/vector3.js +1 -1
- package/src/particles/emitter.js +34 -26
- package/src/particles/particle.js +3 -2
- package/src/physics/body.js +67 -51
- package/src/physics/bounds.js +8 -9
- package/src/physics/world.js +1 -1
- package/src/polyfill/index.js +1 -0
- package/src/polyfill/roundrect.js +235 -0
- package/src/renderable/GUI.js +5 -5
- package/src/renderable/collectable.js +9 -2
- package/src/renderable/colorlayer.js +1 -1
- package/src/renderable/container.js +27 -27
- package/src/renderable/imagelayer.js +3 -3
- package/src/renderable/light2d.js +115 -0
- package/src/renderable/renderable.js +23 -22
- package/src/renderable/sprite.js +15 -16
- package/src/renderable/trigger.js +10 -4
- package/src/state/stage.js +73 -3
- package/src/state/state.js +1 -1
- package/src/system/device.js +10 -8
- package/src/system/pooling.js +156 -149
- package/src/text/bitmaptext.js +1 -1
- package/src/text/text.js +14 -18
- package/src/utils/utils.js +2 -2
- package/src/video/canvas/canvas_renderer.js +144 -81
- package/src/video/renderer.js +64 -37
- package/src/video/{texture.js → texture/atlas.js} +8 -8
- package/src/video/{texture_cache.js → texture/cache.js} +4 -4
- package/src/video/texture/canvas_texture.js +87 -0
- package/src/video/webgl/glshader.js +29 -193
- package/src/video/webgl/utils/attributes.js +16 -0
- package/src/video/webgl/utils/precision.js +11 -0
- package/src/video/webgl/utils/program.js +58 -0
- package/src/video/webgl/utils/string.js +16 -0
- package/src/video/webgl/utils/uniforms.js +87 -0
- package/src/video/webgl/webgl_compositor.js +1 -14
- package/src/video/webgl/webgl_renderer.js +191 -231
package/src/video/renderer.js
CHANGED
|
@@ -5,10 +5,12 @@ import * as event from "./../system/event.js";
|
|
|
5
5
|
import device from "./../system/device.js";
|
|
6
6
|
import { setPrefixed } from "./../utils/agent.js";
|
|
7
7
|
import Rect from "./../geometries/rectangle.js";
|
|
8
|
+
import RoundRect from "./../geometries/roundrect.js";
|
|
8
9
|
import Ellipse from "./../geometries/ellipse.js";
|
|
9
10
|
import Polygon from "./../geometries/poly.js";
|
|
10
11
|
import Line from "./../geometries/line.js";
|
|
11
12
|
import Bounds from "./../physics/bounds.js";
|
|
13
|
+
import Path2D from "./../geometries/path2d.js";
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* @classdesc
|
|
@@ -43,17 +45,30 @@ class Renderer {
|
|
|
43
45
|
/**
|
|
44
46
|
* true if the current rendering context is valid
|
|
45
47
|
* @name isContextValid
|
|
46
|
-
* @memberof Renderer
|
|
48
|
+
* @memberof Renderer#
|
|
47
49
|
* @default true
|
|
48
50
|
* type {boolean}
|
|
49
51
|
*/
|
|
50
52
|
this.isContextValid = true;
|
|
51
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The Path2D instance used by the renderer to draw primitives
|
|
56
|
+
* @name path2D
|
|
57
|
+
* @type {Path2D}
|
|
58
|
+
* @memberof Renderer#
|
|
59
|
+
*/
|
|
60
|
+
this.path2D = new Path2D();
|
|
61
|
+
|
|
52
62
|
/**
|
|
53
63
|
* @ignore
|
|
54
64
|
*/
|
|
55
65
|
this.currentScissor = new Int32Array([ 0, 0, this.settings.width, this.settings.height ]);
|
|
56
66
|
|
|
67
|
+
/**
|
|
68
|
+
* @ignore
|
|
69
|
+
*/
|
|
70
|
+
this.maskLevel = 0;
|
|
71
|
+
|
|
57
72
|
/**
|
|
58
73
|
* @ignore
|
|
59
74
|
*/
|
|
@@ -97,7 +112,7 @@ class Renderer {
|
|
|
97
112
|
/**
|
|
98
113
|
* prepare the framebuffer for drawing a new frame
|
|
99
114
|
* @name clear
|
|
100
|
-
* @memberof Renderer
|
|
115
|
+
* @memberof Renderer
|
|
101
116
|
* @function
|
|
102
117
|
*/
|
|
103
118
|
clear() {}
|
|
@@ -105,7 +120,7 @@ class Renderer {
|
|
|
105
120
|
/**
|
|
106
121
|
* Reset context state
|
|
107
122
|
* @name reset
|
|
108
|
-
* @memberof Renderer
|
|
123
|
+
* @memberof Renderer
|
|
109
124
|
* @function
|
|
110
125
|
*/
|
|
111
126
|
reset() {
|
|
@@ -118,12 +133,13 @@ class Renderer {
|
|
|
118
133
|
this.currentScissor[1] = 0;
|
|
119
134
|
this.currentScissor[2] = this.backBufferCanvas.width;
|
|
120
135
|
this.currentScissor[3] = this.backBufferCanvas.height;
|
|
136
|
+
this.clearMask();
|
|
121
137
|
}
|
|
122
138
|
|
|
123
139
|
/**
|
|
124
140
|
* return a reference to the system canvas
|
|
125
141
|
* @name getCanvas
|
|
126
|
-
* @memberof Renderer
|
|
142
|
+
* @memberof Renderer
|
|
127
143
|
* @function
|
|
128
144
|
* @returns {HTMLCanvasElement}
|
|
129
145
|
*/
|
|
@@ -134,7 +150,7 @@ class Renderer {
|
|
|
134
150
|
/**
|
|
135
151
|
* return a reference to the screen canvas
|
|
136
152
|
* @name getScreenCanvas
|
|
137
|
-
* @memberof Renderer
|
|
153
|
+
* @memberof Renderer
|
|
138
154
|
* @function
|
|
139
155
|
* @returns {HTMLCanvasElement}
|
|
140
156
|
*/
|
|
@@ -146,7 +162,7 @@ class Renderer {
|
|
|
146
162
|
* return a reference to the screen canvas corresponding 2d Context<br>
|
|
147
163
|
* (will return buffered context if double buffering is enabled, or a reference to the Screen Context)
|
|
148
164
|
* @name getScreenContext
|
|
149
|
-
* @memberof Renderer
|
|
165
|
+
* @memberof Renderer
|
|
150
166
|
* @function
|
|
151
167
|
* @returns {CanvasRenderingContext2D}
|
|
152
168
|
*/
|
|
@@ -157,7 +173,7 @@ class Renderer {
|
|
|
157
173
|
/**
|
|
158
174
|
* returns the current blend mode for this renderer
|
|
159
175
|
* @name getBlendMode
|
|
160
|
-
* @memberof Renderer
|
|
176
|
+
* @memberof Renderer
|
|
161
177
|
* @function
|
|
162
178
|
* @returns {string} blend mode
|
|
163
179
|
*/
|
|
@@ -169,7 +185,7 @@ class Renderer {
|
|
|
169
185
|
* Returns the 2D Context object of the given Canvas<br>
|
|
170
186
|
* Also configures anti-aliasing and blend modes based on constructor options.
|
|
171
187
|
* @name getContext2d
|
|
172
|
-
* @memberof Renderer
|
|
188
|
+
* @memberof Renderer
|
|
173
189
|
* @function
|
|
174
190
|
* @param {HTMLCanvasElement} canvas
|
|
175
191
|
* @param {boolean} [transparent=true] use false to disable transparency
|
|
@@ -207,7 +223,7 @@ class Renderer {
|
|
|
207
223
|
/**
|
|
208
224
|
* return the width of the system Canvas
|
|
209
225
|
* @name getWidth
|
|
210
|
-
* @memberof Renderer
|
|
226
|
+
* @memberof Renderer
|
|
211
227
|
* @function
|
|
212
228
|
* @returns {number}
|
|
213
229
|
*/
|
|
@@ -218,7 +234,7 @@ class Renderer {
|
|
|
218
234
|
/**
|
|
219
235
|
* return the height of the system Canvas
|
|
220
236
|
* @name getHeight
|
|
221
|
-
* @memberof Renderer
|
|
237
|
+
* @memberof Renderer
|
|
222
238
|
* @function
|
|
223
239
|
* @returns {number} height of the system Canvas
|
|
224
240
|
*/
|
|
@@ -229,7 +245,7 @@ class Renderer {
|
|
|
229
245
|
/**
|
|
230
246
|
* get the current fill & stroke style color.
|
|
231
247
|
* @name getColor
|
|
232
|
-
* @memberof Renderer
|
|
248
|
+
* @memberof Renderer
|
|
233
249
|
* @function
|
|
234
250
|
* @returns {Color} current global color
|
|
235
251
|
*/
|
|
@@ -240,7 +256,7 @@ class Renderer {
|
|
|
240
256
|
/**
|
|
241
257
|
* return the current global alpha
|
|
242
258
|
* @name globalAlpha
|
|
243
|
-
* @memberof Renderer
|
|
259
|
+
* @memberof Renderer
|
|
244
260
|
* @function
|
|
245
261
|
* @returns {number}
|
|
246
262
|
*/
|
|
@@ -251,7 +267,7 @@ class Renderer {
|
|
|
251
267
|
/**
|
|
252
268
|
* check if the given rect or bounds overlaps with the renderer screen coordinates
|
|
253
269
|
* @name overlaps
|
|
254
|
-
* @memberof Renderer
|
|
270
|
+
* @memberof Renderer
|
|
255
271
|
* @function
|
|
256
272
|
* @param {Rect|Bounds} bounds
|
|
257
273
|
* @returns {boolean} true if overlaps
|
|
@@ -267,7 +283,7 @@ class Renderer {
|
|
|
267
283
|
/**
|
|
268
284
|
* resizes the system canvas
|
|
269
285
|
* @name resize
|
|
270
|
-
* @memberof Renderer
|
|
286
|
+
* @memberof Renderer
|
|
271
287
|
* @function
|
|
272
288
|
* @param {number} width new width of the canvas
|
|
273
289
|
* @param {number} height new height of the canvas
|
|
@@ -288,7 +304,7 @@ class Renderer {
|
|
|
288
304
|
/**
|
|
289
305
|
* enable/disable image smoothing (scaling interpolation) for the given context
|
|
290
306
|
* @name setAntiAlias
|
|
291
|
-
* @memberof Renderer
|
|
307
|
+
* @memberof Renderer
|
|
292
308
|
* @function
|
|
293
309
|
* @param {CanvasRenderingContext2D} context
|
|
294
310
|
* @param {boolean} [enable=false]
|
|
@@ -318,7 +334,7 @@ class Renderer {
|
|
|
318
334
|
/**
|
|
319
335
|
* set/change the current projection matrix (WebGL only)
|
|
320
336
|
* @name setProjection
|
|
321
|
-
* @memberof Renderer
|
|
337
|
+
* @memberof Renderer
|
|
322
338
|
* @function
|
|
323
339
|
* @param {Matrix3d} matrix
|
|
324
340
|
*/
|
|
@@ -329,17 +345,25 @@ class Renderer {
|
|
|
329
345
|
/**
|
|
330
346
|
* stroke the given shape
|
|
331
347
|
* @name stroke
|
|
332
|
-
* @memberof Renderer
|
|
348
|
+
* @memberof Renderer
|
|
333
349
|
* @function
|
|
334
|
-
* @param {Rect|Polygon|Line|Ellipse} shape a shape object to stroke
|
|
350
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to stroke
|
|
335
351
|
* @param {boolean} [fill=false] fill the shape with the current color if true
|
|
336
352
|
*/
|
|
337
353
|
stroke(shape, fill) {
|
|
354
|
+
if (shape instanceof RoundRect) {
|
|
355
|
+
this.strokeRoundRect(shape.left, shape.top, shape.width, shape.height, shape.radius, fill);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
338
358
|
if (shape instanceof Rect || shape instanceof Bounds) {
|
|
339
359
|
this.strokeRect(shape.left, shape.top, shape.width, shape.height, fill);
|
|
340
|
-
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (shape instanceof Line || shape instanceof Polygon) {
|
|
341
363
|
this.strokePolygon(shape, fill);
|
|
342
|
-
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (shape instanceof Ellipse) {
|
|
343
367
|
this.strokeEllipse(
|
|
344
368
|
shape.pos.x,
|
|
345
369
|
shape.pos.y,
|
|
@@ -347,13 +371,26 @@ class Renderer {
|
|
|
347
371
|
shape.radiusV.y,
|
|
348
372
|
fill
|
|
349
373
|
);
|
|
374
|
+
return;
|
|
350
375
|
}
|
|
376
|
+
throw new Error("Invalid geometry for fill/stroke");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* fill the given shape
|
|
381
|
+
* @name fill
|
|
382
|
+
* @memberof Renderer
|
|
383
|
+
* @function
|
|
384
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to fill
|
|
385
|
+
*/
|
|
386
|
+
fill(shape) {
|
|
387
|
+
this.stroke(shape, true);
|
|
351
388
|
}
|
|
352
389
|
|
|
353
390
|
/**
|
|
354
391
|
* tint the given image or canvas using the given color
|
|
355
392
|
* @name tint
|
|
356
|
-
* @memberof Renderer
|
|
393
|
+
* @memberof Renderer
|
|
357
394
|
* @function
|
|
358
395
|
* @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} src the source image to be tinted
|
|
359
396
|
* @param {Color|string} color the color that will be used to tint the image
|
|
@@ -379,25 +416,15 @@ class Renderer {
|
|
|
379
416
|
return canvas;
|
|
380
417
|
}
|
|
381
418
|
|
|
382
|
-
/**
|
|
383
|
-
* fill the given shape
|
|
384
|
-
* @name fill
|
|
385
|
-
* @memberof Renderer.prototype
|
|
386
|
-
* @function
|
|
387
|
-
* @param {Rect|Polygon|Line|Ellipse} shape a shape object to fill
|
|
388
|
-
*/
|
|
389
|
-
fill(shape) {
|
|
390
|
-
this.stroke(shape, true);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
419
|
/**
|
|
394
420
|
* A mask limits rendering elements to the shape and position of the given mask object.
|
|
395
421
|
* So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
|
|
396
422
|
* Mask are not preserved through renderer context save and restore.
|
|
397
423
|
* @name setMask
|
|
398
|
-
* @memberof Renderer
|
|
424
|
+
* @memberof Renderer
|
|
399
425
|
* @function
|
|
400
|
-
* @param {Rect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
|
|
426
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
|
|
427
|
+
* @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
|
|
401
428
|
*/
|
|
402
429
|
// eslint-disable-next-line no-unused-vars
|
|
403
430
|
setMask(mask) {}
|
|
@@ -406,7 +433,7 @@ class Renderer {
|
|
|
406
433
|
* disable (remove) the rendering mask set through setMask.
|
|
407
434
|
* @name clearMask
|
|
408
435
|
* @see Renderer#setMask
|
|
409
|
-
* @memberof Renderer
|
|
436
|
+
* @memberof Renderer
|
|
410
437
|
* @function
|
|
411
438
|
*/
|
|
412
439
|
clearMask() {}
|
|
@@ -414,7 +441,7 @@ class Renderer {
|
|
|
414
441
|
/**
|
|
415
442
|
* set a coloring tint for sprite based renderables
|
|
416
443
|
* @name setTint
|
|
417
|
-
* @memberof Renderer
|
|
444
|
+
* @memberof Renderer
|
|
418
445
|
* @function
|
|
419
446
|
* @param {Color} tint the tint color
|
|
420
447
|
* @param {number} [alpha] an alpha value to be applied to the tint
|
|
@@ -429,7 +456,7 @@ class Renderer {
|
|
|
429
456
|
* clear the rendering tint set through setTint.
|
|
430
457
|
* @name clearTint
|
|
431
458
|
* @see Renderer#setTint
|
|
432
|
-
* @memberof Renderer
|
|
459
|
+
* @memberof Renderer
|
|
433
460
|
* @function
|
|
434
461
|
*/
|
|
435
462
|
clearTint() {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import Vector2d from "
|
|
2
|
-
import WebGLRenderer from "
|
|
3
|
-
import TextureCache from "
|
|
4
|
-
import Sprite from "
|
|
5
|
-
import { renderer } from "
|
|
6
|
-
import
|
|
7
|
-
import loader from "
|
|
8
|
-
import { ETA } from "
|
|
1
|
+
import Vector2d from "./../../math/vector2.js";
|
|
2
|
+
import WebGLRenderer from "./../webgl/webgl_renderer.js";
|
|
3
|
+
import TextureCache from "./../texture/cache.js";
|
|
4
|
+
import Sprite from "./../../renderable/sprite.js";
|
|
5
|
+
import { renderer } from "./../video.js";
|
|
6
|
+
import pool from "./../../system/pooling.js";
|
|
7
|
+
import loader from "./../../loader/loader.js";
|
|
8
|
+
import { ETA } from "./../../math/math.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* create a simple 1 frame texture atlas based on the given parameters
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { renderer } from "
|
|
2
|
-
import * as fileUtil from "
|
|
3
|
-
import { TextureAtlas, createAtlas } from "./
|
|
4
|
-
import { isPowerOfTwo} from "
|
|
1
|
+
import { renderer } from "./../video.js";
|
|
2
|
+
import * as fileUtil from "./../../utils/file.js";
|
|
3
|
+
import { TextureAtlas, createAtlas } from "./atlas.js";
|
|
4
|
+
import { isPowerOfTwo} from "./../../math/math.js";
|
|
5
5
|
import {ArrayMultimap} from "@teppeis/multimaps";
|
|
6
6
|
|
|
7
7
|
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createCanvas } from "./../video.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Canvas Texture of the given size
|
|
5
|
+
*/
|
|
6
|
+
class CanvasTexture {
|
|
7
|
+
/**
|
|
8
|
+
* @param {number} width the desired width of the canvas
|
|
9
|
+
* @param {number} height the desired height of the canvas
|
|
10
|
+
* @param {number} [offscreenCanvas=true] if the the canvas should be an OffscreenCanvas
|
|
11
|
+
*/
|
|
12
|
+
constructor(width, height, offscreenCanvas = true) {
|
|
13
|
+
/**
|
|
14
|
+
* the canvas created for this CanvasTexture
|
|
15
|
+
* @type {HTMLCanvasElement|OffscreenCanvas}
|
|
16
|
+
*/
|
|
17
|
+
this.canvas = createCanvas(width, height, offscreenCanvas);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* the rendering context of this CanvasTexture
|
|
21
|
+
* @type {CanvasRenderingContext2D}
|
|
22
|
+
*/
|
|
23
|
+
this.context = this.canvas.getContext("2d");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @ignore
|
|
28
|
+
*/
|
|
29
|
+
onResetEvent(width, height) {
|
|
30
|
+
this.clear();
|
|
31
|
+
this.resize(width, height);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Clears the content of the canvas texture
|
|
36
|
+
*/
|
|
37
|
+
clear() {
|
|
38
|
+
this.context.setTransform(1, 0, 0, 1, 0, 0);
|
|
39
|
+
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resizes the canvas texture to the given width and height.
|
|
44
|
+
* @param {number} width the desired width
|
|
45
|
+
* @param {number} height the desired height
|
|
46
|
+
*/
|
|
47
|
+
resize(width, height) {
|
|
48
|
+
this.canvas.width = Math.round(width);
|
|
49
|
+
this.canvas.height = Math.round(height);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @ignore
|
|
54
|
+
*/
|
|
55
|
+
destroy() {
|
|
56
|
+
this.context = undefined;
|
|
57
|
+
this.canvas = undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The width of this canvas texture in pixels
|
|
62
|
+
* @public
|
|
63
|
+
* @type {number}
|
|
64
|
+
*/
|
|
65
|
+
get width() {
|
|
66
|
+
return this.canvas.width;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
set width(val) {
|
|
70
|
+
this.canvas.width = Math.round(val);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The height of this canvas texture in pixels
|
|
75
|
+
* @public
|
|
76
|
+
* @type {number}
|
|
77
|
+
*/
|
|
78
|
+
get height() {
|
|
79
|
+
return this.canvas.height;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
set height(val) {
|
|
83
|
+
this.canvas.height = Math.round(val);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default CanvasTexture;
|
|
@@ -1,198 +1,10 @@
|
|
|
1
1
|
import * as event from "./../../system/event.js";
|
|
2
2
|
import device from "./../../system/device.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var uniforms = {},
|
|
9
|
-
uniRx = /uniform\s+(\w+)\s+(\w+)/g,
|
|
10
|
-
uniformsData = {},
|
|
11
|
-
descriptor = {},
|
|
12
|
-
locations = {},
|
|
13
|
-
match;
|
|
14
|
-
|
|
15
|
-
// Detect all uniform names and types
|
|
16
|
-
[ shader.vertex, shader.fragment ].forEach(function (shader) {
|
|
17
|
-
while ((match = uniRx.exec(shader))) {
|
|
18
|
-
uniformsData[match[2]] = match[1];
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// Get uniform references
|
|
23
|
-
Object.keys(uniformsData).forEach(function (name) {
|
|
24
|
-
var type = uniformsData[name];
|
|
25
|
-
locations[name] = gl.getUniformLocation(shader.program, name);
|
|
26
|
-
|
|
27
|
-
descriptor[name] = {
|
|
28
|
-
"get" : (function (name) {
|
|
29
|
-
/**
|
|
30
|
-
* A getter for the uniform location
|
|
31
|
-
* @ignore
|
|
32
|
-
*/
|
|
33
|
-
return function () {
|
|
34
|
-
return locations[name];
|
|
35
|
-
};
|
|
36
|
-
})(name),
|
|
37
|
-
"set" : (function (name, type, fn) {
|
|
38
|
-
if (type.indexOf("mat") === 0) {
|
|
39
|
-
/**
|
|
40
|
-
* A generic setter for uniform matrices
|
|
41
|
-
* @ignore
|
|
42
|
-
*/
|
|
43
|
-
return function (val) {
|
|
44
|
-
gl[fn](locations[name], false, val);
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
/**
|
|
49
|
-
* A generic setter for uniform vectors
|
|
50
|
-
* @ignore
|
|
51
|
-
*/
|
|
52
|
-
return function (val) {
|
|
53
|
-
var fnv = fn;
|
|
54
|
-
if (val.length && fn.substr(-1) !== "v") {
|
|
55
|
-
fnv += "v";
|
|
56
|
-
}
|
|
57
|
-
gl[fnv](locations[name], val);
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
})(name, type, "uniform" + fnHash[type])
|
|
61
|
-
};
|
|
62
|
-
});
|
|
63
|
-
Object.defineProperties(uniforms, descriptor);
|
|
64
|
-
|
|
65
|
-
return uniforms;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @ignore
|
|
70
|
-
*/
|
|
71
|
-
function extractAttributes(gl, shader) {
|
|
72
|
-
var attributes = {},
|
|
73
|
-
attrRx = /attribute\s+\w+\s+(\w+)/g,
|
|
74
|
-
match,
|
|
75
|
-
i = 0;
|
|
76
|
-
|
|
77
|
-
// Detect all attribute names
|
|
78
|
-
while ((match = attrRx.exec(shader.vertex))) {
|
|
79
|
-
attributes[match[1]] = i++;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return attributes;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @ignore
|
|
87
|
-
*/
|
|
88
|
-
function compileShader(gl, type, source) {
|
|
89
|
-
var shader = gl.createShader(type);
|
|
90
|
-
gl.shaderSource(shader, source);
|
|
91
|
-
gl.compileShader(shader);
|
|
92
|
-
|
|
93
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
94
|
-
throw new Error(gl.getShaderInfoLog(shader));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return shader;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Compile GLSL into a shader object
|
|
102
|
-
* @ignore
|
|
103
|
-
*/
|
|
104
|
-
function compileProgram(gl, vertex, fragment, attributes) {
|
|
105
|
-
var vertShader = compileShader(gl, gl.VERTEX_SHADER, vertex);
|
|
106
|
-
var fragShader = compileShader(gl, gl.FRAGMENT_SHADER, fragment);
|
|
107
|
-
|
|
108
|
-
var program = gl.createProgram();
|
|
109
|
-
|
|
110
|
-
gl.attachShader(program, vertShader);
|
|
111
|
-
gl.attachShader(program, fragShader);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// force vertex attributes to use location 0 as starting location to prevent
|
|
115
|
-
// browser to do complicated emulation when running on desktop OpenGL (e.g. on macOS)
|
|
116
|
-
for (var location in attributes) {
|
|
117
|
-
gl.bindAttribLocation(program, attributes[location], location);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
gl.linkProgram(program);
|
|
121
|
-
|
|
122
|
-
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
123
|
-
var error_msg =
|
|
124
|
-
"Error initializing Shader " + this + "\n" +
|
|
125
|
-
"gl.VALIDATE_STATUS: " + gl.getProgramParameter(program, gl.VALIDATE_STATUS) + "\n" +
|
|
126
|
-
"gl.getError()" + gl.getError() + "\n" +
|
|
127
|
-
"gl.getProgramInfoLog()" + gl.getProgramInfoLog(program);
|
|
128
|
-
// house cleaning
|
|
129
|
-
gl.deleteProgram(program);
|
|
130
|
-
program = null;
|
|
131
|
-
// throw the exception
|
|
132
|
-
throw new Error(error_msg);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
gl.useProgram(program);
|
|
136
|
-
|
|
137
|
-
// clean-up
|
|
138
|
-
gl.deleteShader(vertShader);
|
|
139
|
-
gl.deleteShader(fragShader);
|
|
140
|
-
|
|
141
|
-
return program;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Hash map of GLSL data types to WebGL Uniform methods
|
|
147
|
-
* @ignore
|
|
148
|
-
*/
|
|
149
|
-
var fnHash = {
|
|
150
|
-
"bool" : "1i",
|
|
151
|
-
"int" : "1i",
|
|
152
|
-
"float" : "1f",
|
|
153
|
-
"vec2" : "2fv",
|
|
154
|
-
"vec3" : "3fv",
|
|
155
|
-
"vec4" : "4fv",
|
|
156
|
-
"bvec2" : "2iv",
|
|
157
|
-
"bvec3" : "3iv",
|
|
158
|
-
"bvec4" : "4iv",
|
|
159
|
-
"ivec2" : "2iv",
|
|
160
|
-
"ivec3" : "3iv",
|
|
161
|
-
"ivec4" : "4iv",
|
|
162
|
-
"mat2" : "Matrix2fv",
|
|
163
|
-
"mat3" : "Matrix3fv",
|
|
164
|
-
"mat4" : "Matrix4fv",
|
|
165
|
-
"sampler2D" : "1i"
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* set precision for the fiven shader source
|
|
170
|
-
* won't do anything if the precision is already specified
|
|
171
|
-
* @ignore
|
|
172
|
-
*/
|
|
173
|
-
function setPrecision(src, precision) {
|
|
174
|
-
if (src.substring(0, 9) !== "precision") {
|
|
175
|
-
return "precision " + precision + " float;" + src;
|
|
176
|
-
}
|
|
177
|
-
return src;
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* clean the given source from space, comments, etc...
|
|
182
|
-
* @ignore
|
|
183
|
-
*/
|
|
184
|
-
function minify(src) {
|
|
185
|
-
// remove comments
|
|
186
|
-
src = src.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, "$1");
|
|
187
|
-
// Remove leading and trailing whitespace from lines
|
|
188
|
-
src = src.replace(/(\\n\s+)|(\s+\\n)/g, "");
|
|
189
|
-
// Remove line breaks
|
|
190
|
-
src = src.replace(/(\\r|\\n)+/g, "");
|
|
191
|
-
// Remove unnecessary whitespace
|
|
192
|
-
src = src.replace(/\s*([;,[\](){}\\\/\-+*|^&!=<>?~%])\s*/g, "$1");
|
|
193
|
-
|
|
194
|
-
return src;
|
|
195
|
-
};
|
|
3
|
+
import { extractUniforms } from "./utils/uniforms.js";
|
|
4
|
+
import { extractAttributes } from "./utils/attributes.js";
|
|
5
|
+
import { compileProgram } from "./utils/program.js";
|
|
6
|
+
import { setPrecision } from "./utils/precision.js";
|
|
7
|
+
import { minify } from "./utils/string.js";
|
|
196
8
|
|
|
197
9
|
/**
|
|
198
10
|
* @classdesc
|
|
@@ -337,6 +149,30 @@ class GLShader {
|
|
|
337
149
|
}
|
|
338
150
|
}
|
|
339
151
|
|
|
152
|
+
/**
|
|
153
|
+
* activate the given vertex attribute for this shader
|
|
154
|
+
* @name setVertexAttributes
|
|
155
|
+
* @memberof GLShader
|
|
156
|
+
* @function
|
|
157
|
+
* @param {WebGLRenderingContext} gl the current WebGL rendering context
|
|
158
|
+
* @param {object[]} attributes an array of vertex attributes
|
|
159
|
+
* @param {number} vertexByteSize the size of a single vertex in bytes
|
|
160
|
+
*/
|
|
161
|
+
setVertexAttributes(gl, attributes, vertexByteSize) {
|
|
162
|
+
// set the vertex attributes
|
|
163
|
+
for (var index = 0; index < attributes.length; ++index) {
|
|
164
|
+
var element = attributes[index];
|
|
165
|
+
var location = this.getAttribLocation(element.name);
|
|
166
|
+
|
|
167
|
+
if (location !== -1) {
|
|
168
|
+
gl.enableVertexAttribArray(location);
|
|
169
|
+
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexByteSize, element.offset);
|
|
170
|
+
} else {
|
|
171
|
+
gl.disableVertexAttribArray(index);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
340
176
|
/**
|
|
341
177
|
* destroy this shader objects resources (program, attributes, uniforms)
|
|
342
178
|
* @name destroy
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ignore
|
|
3
|
+
*/
|
|
4
|
+
export function extractAttributes(gl, shader) {
|
|
5
|
+
var attributes = {},
|
|
6
|
+
attrRx = /attribute\s+\w+\s+(\w+)/g,
|
|
7
|
+
match,
|
|
8
|
+
i = 0;
|
|
9
|
+
|
|
10
|
+
// Detect all attribute names
|
|
11
|
+
while ((match = attrRx.exec(shader.vertex))) {
|
|
12
|
+
attributes[match[1]] = i++;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return attributes;
|
|
16
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* set precision for the fiven shader source
|
|
3
|
+
* won't do anything if the precision is already specified
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
export function setPrecision(src, precision) {
|
|
7
|
+
if (src.substring(0, 9) !== "precision") {
|
|
8
|
+
return "precision " + precision + " float;" + src;
|
|
9
|
+
}
|
|
10
|
+
return src;
|
|
11
|
+
};
|