melonjs 14.5.0 → 15.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/LICENSE.md +1 -1
- package/README.md +2 -2
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +1 -1
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +1 -1
- package/dist/melonjs.mjs/_virtual/earcut.js +1 -1
- package/dist/melonjs.mjs/_virtual/howler.js +1 -1
- package/dist/melonjs.mjs/_virtual/index.js +1 -1
- package/dist/melonjs.mjs/_virtual/index2.js +1 -1
- package/dist/melonjs.mjs/_virtual/multimap.js +1 -1
- package/dist/melonjs.mjs/_virtual/setmultimap.js +1 -1
- package/dist/melonjs.mjs/application/application.js +1 -1
- package/dist/melonjs.mjs/application/header.js +1 -1
- package/dist/melonjs.mjs/application/resize.js +1 -1
- package/dist/melonjs.mjs/application/settings.js +1 -1
- package/dist/melonjs.mjs/audio/audio.js +2 -4
- package/dist/melonjs.mjs/camera/camera2d.js +1 -1
- package/dist/melonjs.mjs/const.js +1 -1
- package/dist/melonjs.mjs/entity/entity.js +1 -1
- package/dist/melonjs.mjs/geometries/ellipse.js +1 -1
- package/dist/melonjs.mjs/geometries/line.js +1 -1
- package/dist/melonjs.mjs/geometries/path2d.js +4 -4
- package/dist/melonjs.mjs/geometries/point.js +1 -1
- package/dist/melonjs.mjs/geometries/poly.js +1 -1
- package/dist/melonjs.mjs/geometries/rectangle.js +1 -1
- package/dist/melonjs.mjs/geometries/roundrect.js +1 -1
- package/dist/melonjs.mjs/index.js +5 -3
- package/dist/melonjs.mjs/input/gamepad.js +1 -1
- package/dist/melonjs.mjs/input/input.js +1 -1
- package/dist/melonjs.mjs/input/keyboard.js +1 -1
- package/dist/melonjs.mjs/input/pointer.js +1 -1
- package/dist/melonjs.mjs/input/pointerevent.js +2 -2
- package/dist/melonjs.mjs/lang/console.js +1 -1
- package/dist/melonjs.mjs/lang/deprecated.js +1 -1
- package/dist/melonjs.mjs/level/level.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +1 -1
- package/dist/melonjs.mjs/level/tiled/constants.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/autodetect.js +1 -1
- package/dist/melonjs.mjs/loader/cache.js +1 -1
- package/dist/melonjs.mjs/loader/loader.js +1 -1
- package/dist/melonjs.mjs/loader/loadingscreen.js +1 -1
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +1 -1
- package/dist/melonjs.mjs/loader/parser.js +1 -1
- package/dist/melonjs.mjs/loader/settings.js +1 -1
- package/dist/melonjs.mjs/math/color.js +9 -8
- package/dist/melonjs.mjs/math/math.js +1 -1
- package/dist/melonjs.mjs/math/matrix2.js +1 -1
- package/dist/melonjs.mjs/math/matrix3.js +1 -1
- package/dist/melonjs.mjs/math/observable_vector2.js +1 -1
- package/dist/melonjs.mjs/math/observable_vector3.js +1 -1
- package/dist/melonjs.mjs/math/vector2.js +1 -1
- package/dist/melonjs.mjs/math/vector3.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +1 -1
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +1 -1
- package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +1 -1
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +1 -1
- package/dist/melonjs.mjs/particles/emitter.js +1 -1
- package/dist/melonjs.mjs/particles/particle.js +1 -1
- package/dist/melonjs.mjs/particles/settings.js +1 -1
- package/dist/melonjs.mjs/physics/body.js +1 -1
- package/dist/melonjs.mjs/physics/bounds.js +1 -1
- package/dist/melonjs.mjs/physics/collision.js +1 -1
- package/dist/melonjs.mjs/physics/detector.js +1 -1
- package/dist/melonjs.mjs/physics/quadtree.js +1 -1
- package/dist/melonjs.mjs/physics/response.js +1 -1
- package/dist/melonjs.mjs/physics/sat.js +1 -1
- package/dist/melonjs.mjs/physics/world.js +1 -1
- package/dist/melonjs.mjs/plugin/plugin.js +3 -3
- package/dist/melonjs.mjs/renderable/collectable.js +1 -1
- package/dist/melonjs.mjs/renderable/colorlayer.js +1 -1
- package/dist/melonjs.mjs/renderable/container.js +1 -1
- package/dist/melonjs.mjs/renderable/dragndrop.js +1 -1
- package/dist/melonjs.mjs/renderable/imagelayer.js +1 -1
- package/dist/melonjs.mjs/renderable/light2d.js +1 -1
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +1 -1
- package/dist/melonjs.mjs/renderable/renderable.js +25 -3
- package/dist/melonjs.mjs/renderable/sprite.js +1 -1
- package/dist/melonjs.mjs/renderable/trigger.js +1 -1
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +1 -1
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +1 -1
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +1 -3
- package/dist/melonjs.mjs/state/stage.js +1 -1
- package/dist/melonjs.mjs/state/state.js +1 -1
- package/dist/melonjs.mjs/system/device.js +2 -2
- package/dist/melonjs.mjs/system/dom.js +1 -1
- package/dist/melonjs.mjs/system/event.js +1 -1
- package/dist/melonjs.mjs/system/platform.js +1 -1
- package/dist/melonjs.mjs/system/pooling.js +1 -1
- package/dist/melonjs.mjs/system/save.js +1 -1
- package/dist/melonjs.mjs/system/timer.js +1 -1
- package/dist/melonjs.mjs/text/bitmaptext.js +1 -1
- package/dist/melonjs.mjs/text/bitmaptextdata.js +1 -1
- package/dist/melonjs.mjs/text/glyph.js +1 -1
- package/dist/melonjs.mjs/text/text.js +56 -86
- package/dist/melonjs.mjs/text/textmetrics.js +1 -1
- package/dist/melonjs.mjs/text/textstyle.js +4 -6
- package/dist/melonjs.mjs/tweens/easing.js +1 -1
- package/dist/melonjs.mjs/tweens/interpolation.js +1 -1
- package/dist/melonjs.mjs/tweens/tween.js +1 -1
- package/dist/melonjs.mjs/utils/agent.js +1 -1
- package/dist/melonjs.mjs/utils/array.js +1 -1
- package/dist/melonjs.mjs/utils/file.js +1 -1
- package/dist/melonjs.mjs/utils/function.js +1 -1
- package/dist/melonjs.mjs/utils/string.js +1 -1
- package/dist/melonjs.mjs/utils/utils.js +1 -1
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +7 -11
- package/dist/melonjs.mjs/video/renderer.js +2 -13
- package/dist/melonjs.mjs/video/texture/atlas.js +3 -9
- package/dist/melonjs.mjs/video/texture/cache.js +1 -1
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +1 -1
- package/dist/melonjs.mjs/video/utils/autodetect.js +1 -1
- package/dist/melonjs.mjs/video/video.js +1 -1
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +21 -17
- package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +77 -77
- package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +77 -0
- package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +249 -0
- package/dist/melonjs.mjs/video/webgl/compositors/webgl_compositor.js +1 -1
- package/dist/melonjs.mjs/video/webgl/glshader.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +2 -2
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +2 -2
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/program.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/string.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +1 -1
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +139 -149
- package/dist/melonjs.module.js +25111 -25202
- package/dist/types/entity/entity.d.ts +1 -1
- package/dist/types/index.d.ts +4 -2
- package/dist/types/renderable/renderable.d.ts +6 -0
- package/dist/types/renderable/ui/uitextbutton.d.ts +0 -1
- package/dist/types/text/text.d.ts +6 -20
- package/dist/types/text/textstyle.d.ts +1 -1
- package/dist/types/video/canvas/canvas_renderer.d.ts +0 -5
- package/dist/types/video/renderer.d.ts +1 -5
- package/dist/types/video/webgl/buffer/vertex.d.ts +3 -3
- package/dist/types/video/webgl/compositors/compositor.d.ts +38 -32
- package/dist/types/video/webgl/compositors/primitive_compositor.d.ts +22 -0
- package/dist/types/video/webgl/compositors/quad_compositor.d.ts +72 -0
- package/dist/types/video/webgl/webgl_renderer.d.ts +34 -28
- package/package.json +13 -14
- package/src/audio/audio.js +1 -3
- package/src/geometries/path2d.js +3 -3
- package/src/index.js +6 -2
- package/src/math/color.js +8 -7
- package/src/polyfill/index.js +0 -2
- package/src/renderable/renderable.js +24 -2
- package/src/renderable/ui/uitextbutton.js +0 -2
- package/src/system/device.js +1 -1
- package/src/text/text.js +55 -85
- package/src/text/textstyle.js +3 -5
- package/src/video/canvas/canvas_renderer.js +5 -9
- package/src/video/renderer.js +0 -11
- package/src/video/texture/atlas.js +2 -8
- package/src/video/webgl/buffer/vertex.js +20 -16
- package/src/video/webgl/compositors/compositor.js +76 -77
- package/src/video/webgl/compositors/primitive_compositor.js +68 -0
- package/src/video/webgl/compositors/{webgl_compositor.js → quad_compositor.js} +38 -98
- package/src/video/webgl/shaders/primitive.vert +2 -5
- package/src/video/webgl/shaders/quad.vert +3 -1
- package/src/video/webgl/webgl_renderer.js +137 -147
- package/dist/melonjs.mjs/_virtual/make-built-in.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-define-property.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-descriptor.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-names.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-symbols.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-property-is-enumerable.js +0 -10
- package/dist/melonjs.mjs/_virtual/shared.js +0 -10
- package/dist/melonjs.mjs/game.js +0 -29
- package/dist/melonjs.mjs/polyfill/console.js +0 -18
- package/dist/melonjs.mjs/polyfill/performance.js +0 -27
- package/dist/melonjs.mjs/polyfill/requestAnimationFrame.js +0 -46
- package/dist/melonjs.mjs/polyfill/roundrect.js +0 -242
- package/dist/melonjs.mjs/renderable/re_container.js +0 -1016
- package/dist/melonjs.mjs/video/utils/resize.js +0 -116
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +0 -494
- package/src/polyfill/performance.js +0 -20
- package/src/polyfill/requestAnimationFrame.js +0 -39
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine -
|
|
2
|
+
* melonJS Game Engine - v15.0.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
6
6
|
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import VertexArrayBuffer from '../buffer/vertex.js';
|
|
9
|
+
import GLShader from '../glshader.js';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* @classdesc
|
|
@@ -14,19 +15,26 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
14
15
|
class Compositor {
|
|
15
16
|
/**
|
|
16
17
|
* @param {WebGLRenderer} renderer - the current WebGL renderer session
|
|
18
|
+
* @param {Object} settings - additional settings to initialize this compositors
|
|
19
|
+
* @param {object[]} attribute - an array of attributes definition
|
|
20
|
+
* @param {string} attribute.name - name of the attribute in the vertex shader
|
|
21
|
+
* @param {number} attribute.size - number of components per vertex attribute. Must be 1, 2, 3, or 4.
|
|
22
|
+
* @param {GLenum} attribute.type - data type of each component in the array
|
|
23
|
+
* @param {boolean} attribute.normalized - whether integer data values should be normalized into a certain range when being cast to a float
|
|
24
|
+
* @param {number} attribute.offset - offset in bytes of the first component in the vertex attribute array
|
|
25
|
+
* @param {object} shader - an array of attributes definition
|
|
26
|
+
* @param {string} shader.vertex - a string containing the GLSL source code to set
|
|
27
|
+
* @param {string} shader.fragment - a string containing the GLSL source code to set
|
|
17
28
|
*/
|
|
18
|
-
constructor (renderer) {
|
|
19
|
-
this.init(renderer);
|
|
29
|
+
constructor (renderer, settings) {
|
|
30
|
+
this.init(renderer, settings);
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
/**
|
|
23
34
|
* Initialize the compositor
|
|
24
35
|
* @ignore
|
|
25
36
|
*/
|
|
26
|
-
init (renderer) {
|
|
27
|
-
// local reference
|
|
28
|
-
var gl = renderer.gl;
|
|
29
|
-
|
|
37
|
+
init (renderer, settings) {
|
|
30
38
|
// the associated renderer
|
|
31
39
|
this.renderer = renderer;
|
|
32
40
|
|
|
@@ -40,17 +48,23 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
40
48
|
this.viewMatrix = renderer.currentTransform;
|
|
41
49
|
|
|
42
50
|
/**
|
|
43
|
-
*
|
|
51
|
+
* the default shader created by this compositor
|
|
44
52
|
* @type {GLShader}
|
|
45
53
|
*/
|
|
46
|
-
this.
|
|
54
|
+
this.defaultShader = undefined;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* the shader currently used by this compositor
|
|
58
|
+
* @type {GLShader}
|
|
59
|
+
*/
|
|
60
|
+
this.currentShader = undefined;
|
|
47
61
|
|
|
48
62
|
/**
|
|
49
63
|
* primitive type to render (gl.POINTS, gl.LINE_STRIP, gl.LINE_LOOP, gl.LINES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.TRIANGLES)
|
|
50
64
|
* @type {number}
|
|
51
65
|
* @default gl.TRIANGLES
|
|
52
66
|
*/
|
|
53
|
-
this.mode = gl.TRIANGLES;
|
|
67
|
+
this.mode = this.gl.TRIANGLES;
|
|
54
68
|
|
|
55
69
|
/**
|
|
56
70
|
* an array of vertex attribute properties
|
|
@@ -75,11 +89,28 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
75
89
|
*/
|
|
76
90
|
this.vertexSize = 0;
|
|
77
91
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
92
|
+
/**
|
|
93
|
+
* the vertex data buffer used by this compositor
|
|
94
|
+
* @type {VertexArrayBuffer}
|
|
95
|
+
*/
|
|
96
|
+
this.vertexData = null;
|
|
97
|
+
|
|
98
|
+
// parse given attibrutes
|
|
99
|
+
if (typeof settings !== "undefined" && Array.isArray(settings.attributes)) {
|
|
100
|
+
settings.attributes.forEach((attr) => {
|
|
101
|
+
this.addAttribute(attr.name, attr.size, attr.type, attr.normalized, attr.offset);
|
|
102
|
+
this.vertexData = new VertexArrayBuffer(this.vertexSize, 6);
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
throw new Error("attributes definition missing");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// parse and instantiate the default shader
|
|
109
|
+
if (typeof settings !== "undefined" && typeof settings.shader !== "undefined") {
|
|
110
|
+
this.defaultShader = new GLShader(this.gl, settings.shader.vertex, settings.shader.fragment);
|
|
111
|
+
} else {
|
|
112
|
+
throw new Error("shader definition missing");
|
|
113
|
+
}
|
|
83
114
|
}
|
|
84
115
|
|
|
85
116
|
/**
|
|
@@ -90,17 +121,35 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
90
121
|
// WebGL context
|
|
91
122
|
this.gl = this.renderer.gl;
|
|
92
123
|
|
|
93
|
-
|
|
124
|
+
// clear the vertex data buffer
|
|
125
|
+
this.vertexData.clear();
|
|
126
|
+
}
|
|
94
127
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
)
|
|
128
|
+
/**
|
|
129
|
+
* @ignore
|
|
130
|
+
* called by the WebGL renderer when a compositor become the current one
|
|
131
|
+
*/
|
|
132
|
+
bind() {
|
|
133
|
+
if (this.renderer.currentProgram !== this.defaultShader.program) {
|
|
134
|
+
this.useShader(this.defaultShader);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
101
137
|
|
|
102
|
-
|
|
103
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Select the shader to use for compositing
|
|
140
|
+
* @see GLShader
|
|
141
|
+
* @param {GLShader} shader - a reference to a GLShader instance
|
|
142
|
+
*/
|
|
143
|
+
useShader(shader) {
|
|
144
|
+
if (this.renderer.currentProgram !== shader.program) {
|
|
145
|
+
this.flush();
|
|
146
|
+
shader.bind();
|
|
147
|
+
shader.setUniform("uProjectionMatrix", this.renderer.projectionMatrix);
|
|
148
|
+
shader.setVertexAttributes(this.gl, this.attributes, this.vertexByteSize);
|
|
149
|
+
|
|
150
|
+
this.currentShader = shader;
|
|
151
|
+
this.renderer.currentProgram = this.currentShader.program;
|
|
152
|
+
}
|
|
104
153
|
}
|
|
105
154
|
|
|
106
155
|
/**
|
|
@@ -148,46 +197,20 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
148
197
|
this.vertexSize = this.vertexByteSize / Float32Array.BYTES_PER_ELEMENT;
|
|
149
198
|
}
|
|
150
199
|
|
|
151
|
-
/**
|
|
152
|
-
* Sets the viewport
|
|
153
|
-
* @param {number} x - x position of viewport
|
|
154
|
-
* @param {number} y - y position of viewport
|
|
155
|
-
* @param {number} w - width of viewport
|
|
156
|
-
* @param {number} h - height of viewport
|
|
157
|
-
*/
|
|
158
|
-
setViewport(x, y, w, h) {
|
|
159
|
-
this.gl.viewport(x, y, w, h);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
200
|
/**
|
|
163
201
|
* set/change the current projection matrix
|
|
164
202
|
* @param {Matrix3d} matrix
|
|
165
203
|
*/
|
|
166
204
|
setProjection(matrix) {
|
|
167
|
-
this.
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Select the shader to use for compositing
|
|
172
|
-
* @see GLShader
|
|
173
|
-
* @param {GLShader} shader - a reference to a GLShader instance
|
|
174
|
-
*/
|
|
175
|
-
useShader(shader) {
|
|
176
|
-
if (this.activeShader !== shader) {
|
|
177
|
-
this.flush();
|
|
178
|
-
this.activeShader = shader;
|
|
179
|
-
this.activeShader.bind();
|
|
180
|
-
this.activeShader.setUniform("uProjectionMatrix", this.renderer.projectionMatrix);
|
|
181
|
-
this.activeShader.setVertexAttributes(this.gl, this.attributes, this.vertexByteSize);
|
|
182
|
-
}
|
|
205
|
+
this.currentShader.setUniform("uProjectionMatrix", matrix);
|
|
183
206
|
}
|
|
184
207
|
|
|
185
208
|
/**
|
|
186
|
-
* Flush batched
|
|
209
|
+
* Flush batched vertex data to the GPU
|
|
187
210
|
* @param {number} [mode=gl.TRIANGLES] - the GL drawing mode
|
|
188
211
|
*/
|
|
189
212
|
flush(mode = this.mode) {
|
|
190
|
-
var vertex = this.
|
|
213
|
+
var vertex = this.vertexData;
|
|
191
214
|
var vertexCount = vertex.vertexCount;
|
|
192
215
|
|
|
193
216
|
if (vertexCount > 0) {
|
|
@@ -207,29 +230,6 @@ import { on, CANVAS_ONRESIZE } from '../../../system/event.js';
|
|
|
207
230
|
vertex.clear();
|
|
208
231
|
}
|
|
209
232
|
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Clear the frame buffer
|
|
213
|
-
* @param {number} [alpha = 0.0] - the alpha value used when clearing the framebuffer
|
|
214
|
-
*/
|
|
215
|
-
clear(alpha = 0) {
|
|
216
|
-
var gl = this.gl;
|
|
217
|
-
gl.clearColor(0, 0, 0, alpha);
|
|
218
|
-
gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Specify the color values used when clearing color buffers. The values are clamped between 0 and 1.
|
|
223
|
-
* @param {number} [r = 0] - the red color value used when the color buffers are cleared
|
|
224
|
-
* @param {number} [g = 0] - the green color value used when the color buffers are cleared
|
|
225
|
-
* @param {number} [b = 0] - the blue color value used when the color buffers are cleared
|
|
226
|
-
* @param {number} [a = 0] - the alpha color value used when the color buffers are cleared
|
|
227
|
-
*/
|
|
228
|
-
clearColor(r = 0, g = 0, b = 0, a = 0) {
|
|
229
|
-
var gl = this.gl;
|
|
230
|
-
gl.clearColor(r, g, b, a);
|
|
231
|
-
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
232
|
-
}
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
export { Compositor as default };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v15.0.0
|
|
3
|
+
* http://www.melonjs.org
|
|
4
|
+
* melonjs is licensed under the MIT License.
|
|
5
|
+
* http://www.opensource.org/licenses/mit-license
|
|
6
|
+
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
|
+
*/
|
|
8
|
+
import primitiveVertex from '../shaders/primitive.vert.js';
|
|
9
|
+
import primitiveFragment from '../shaders/primitive.frag.js';
|
|
10
|
+
import Compositor from './compositor.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @classdesc
|
|
14
|
+
* A WebGL Compositor object. This class handles all of the WebGL state<br>
|
|
15
|
+
* Pushes texture regions or shape geometry into WebGL buffers, automatically flushes to GPU
|
|
16
|
+
* @augments Compositor
|
|
17
|
+
*/
|
|
18
|
+
class PrimitiveCompositor extends Compositor {
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Initialize the compositor
|
|
22
|
+
* @ignore
|
|
23
|
+
*/
|
|
24
|
+
init(renderer) {
|
|
25
|
+
super.init(renderer, {
|
|
26
|
+
attributes: [
|
|
27
|
+
{name: "aVertex", size: 2, type: renderer.gl.FLOAT, normalized: false, offset: 0 * Float32Array.BYTES_PER_ELEMENT},
|
|
28
|
+
{name: "aColor", size: 4, type: renderer.gl.UNSIGNED_BYTE, normalized: true, offset: 2 * Float32Array.BYTES_PER_ELEMENT}
|
|
29
|
+
],
|
|
30
|
+
shader: {
|
|
31
|
+
vertex: primitiveVertex, fragment: primitiveFragment
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Draw an array of vertices
|
|
38
|
+
* @param {GLenum} mode - primitive type to render (gl.POINTS, gl.LINE_STRIP, gl.LINE_LOOP, gl.LINES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.TRIANGLES)
|
|
39
|
+
* @param {Point[]} verts - an array of vertices
|
|
40
|
+
* @param {number} [vertexCount=verts.length] - amount of points defined in the points array
|
|
41
|
+
*/
|
|
42
|
+
drawVertices(mode, verts, vertexCount = verts.length) {
|
|
43
|
+
var viewMatrix = this.viewMatrix;
|
|
44
|
+
var vertexData = this.vertexData;
|
|
45
|
+
var color = this.renderer.currentColor;
|
|
46
|
+
var alpha = this.renderer.getGlobalAlpha();
|
|
47
|
+
|
|
48
|
+
if (vertexData.isFull(vertexCount)) {
|
|
49
|
+
// is the vertex buffer full if we add more vertices
|
|
50
|
+
this.flush();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// flush if drawing vertices with a different drawing mode
|
|
54
|
+
if (mode !== this.mode) {
|
|
55
|
+
this.flush(this.mode);
|
|
56
|
+
this.mode = mode;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!viewMatrix.isIdentity()) {
|
|
60
|
+
verts.forEach((vert) => {
|
|
61
|
+
viewMatrix.apply(vert);
|
|
62
|
+
vertexData.push(vert.x, vert.y, undefined, undefined, color.toUint32(alpha));
|
|
63
|
+
});
|
|
64
|
+
} else {
|
|
65
|
+
verts.forEach((vert) => {
|
|
66
|
+
vertexData.push(vert.x, vert.y, undefined, undefined, color.toUint32(alpha));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// force flush for primitive using LINE_STRIP or LINE_LOOP
|
|
71
|
+
if (this.mode === this.gl.LINE_STRIP || this.mode === this.gl.LINE_LOOP) {
|
|
72
|
+
this.flush(this.mode);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { PrimitiveCompositor as default };
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v15.0.0
|
|
3
|
+
* http://www.melonjs.org
|
|
4
|
+
* melonjs is licensed under the MIT License.
|
|
5
|
+
* http://www.opensource.org/licenses/mit-license
|
|
6
|
+
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
|
+
*/
|
|
8
|
+
import Vector2d from '../../../math/vector2.js';
|
|
9
|
+
import { isPowerOfTwo } from '../../../math/math.js';
|
|
10
|
+
import quadVertex from '../shaders/quad.vert.js';
|
|
11
|
+
import quadFragment from '../shaders/quad.frag.js';
|
|
12
|
+
import Compositor from './compositor.js';
|
|
13
|
+
|
|
14
|
+
// a pool of resuable vectors
|
|
15
|
+
var V_ARRAY = [
|
|
16
|
+
new Vector2d(),
|
|
17
|
+
new Vector2d(),
|
|
18
|
+
new Vector2d(),
|
|
19
|
+
new Vector2d()
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @classdesc
|
|
24
|
+
* A WebGL Compositor object. This class handles all of the WebGL state<br>
|
|
25
|
+
* Pushes texture regions or shape geometry into WebGL buffers, automatically flushes to GPU
|
|
26
|
+
* @augments Compositor
|
|
27
|
+
*/
|
|
28
|
+
class QuadCompositor extends Compositor {
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Initialize the compositor
|
|
32
|
+
* @ignore
|
|
33
|
+
*/
|
|
34
|
+
init (renderer) {
|
|
35
|
+
super.init(renderer, {
|
|
36
|
+
attributes: [
|
|
37
|
+
{name: "aVertex", size: 2, type: renderer.gl.FLOAT, normalized: false, offset: 0 * Float32Array.BYTES_PER_ELEMENT},
|
|
38
|
+
{name: "aRegion", size: 2, type: renderer.gl.FLOAT, normalized: false, offset: 2 * Float32Array.BYTES_PER_ELEMENT},
|
|
39
|
+
{name: "aColor", size: 4, type: renderer.gl.UNSIGNED_BYTE, normalized: true, offset: 4 * Float32Array.BYTES_PER_ELEMENT}
|
|
40
|
+
],
|
|
41
|
+
shader: {
|
|
42
|
+
vertex: quadVertex, fragment: quadFragment
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// list of active texture units
|
|
47
|
+
this.currentTextureUnit = -1;
|
|
48
|
+
this.boundTextures = [];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Reset compositor internal state
|
|
53
|
+
* @ignore
|
|
54
|
+
*/
|
|
55
|
+
reset() {
|
|
56
|
+
super.reset();
|
|
57
|
+
|
|
58
|
+
// delete all related bound texture
|
|
59
|
+
for (var i = 0; i < this.renderer.maxTextures; i++) {
|
|
60
|
+
var texture2D = this.getTexture2D(i);
|
|
61
|
+
if (typeof texture2D !== "undefined") {
|
|
62
|
+
this.deleteTexture2D(texture2D);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this.currentTextureUnit = -1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a WebGL texture from an image
|
|
70
|
+
* @param {number} unit - Destination texture unit
|
|
71
|
+
* @param {Image|HTMLCanvasElement|ImageData|Uint8Array[]|Float32Array[]} [pixels=null] - Source image
|
|
72
|
+
* @param {number} filter - gl.LINEAR or gl.NEAREST
|
|
73
|
+
* @param {string} [repeat="no-repeat"] - Image repeat behavior (see {@link ImageLayer#repeat})
|
|
74
|
+
* @param {number} [w=pixels.width] - Source image width (Only use with UInt8Array[] or Float32Array[] source image)
|
|
75
|
+
* @param {number} [h=pixels.height] - Source image height (Only use with UInt8Array[] or Float32Array[] source image)
|
|
76
|
+
* @param {boolean} [premultipliedAlpha=true] - Multiplies the alpha channel into the other color channels
|
|
77
|
+
* @param {boolean} [mipmap=true] - Whether mipmap levels should be generated for this texture
|
|
78
|
+
* @returns {WebGLTexture} a WebGL texture
|
|
79
|
+
*/
|
|
80
|
+
createTexture2D(unit, pixels = null, filter, repeat = "no-repeat", w = pixels.width, h = pixels.height, premultipliedAlpha = true, mipmap = true) {
|
|
81
|
+
var gl = this.gl;
|
|
82
|
+
var isPOT = isPowerOfTwo(w) && isPowerOfTwo(h);
|
|
83
|
+
var rs = (repeat.search(/^repeat(-x)?$/) === 0) && (isPOT || this.renderer.WebGLVersion > 1) ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
84
|
+
var rt = (repeat.search(/^repeat(-y)?$/) === 0) && (isPOT || this.renderer.WebGLVersion > 1) ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
85
|
+
|
|
86
|
+
var texture = gl.createTexture();
|
|
87
|
+
|
|
88
|
+
this.bindTexture2D(texture, unit);
|
|
89
|
+
|
|
90
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, rs);
|
|
91
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, rt);
|
|
92
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
|
|
93
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
|
|
94
|
+
|
|
95
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultipliedAlpha);
|
|
96
|
+
|
|
97
|
+
if (pixels === null || typeof pixels.byteLength !== "undefined") {
|
|
98
|
+
// if pixels is undefined, or if it's Uint8Array/Float32Array TypedArray
|
|
99
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels, 0);
|
|
100
|
+
} else {
|
|
101
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// generate the sprite mimap (used when scaling) if a PowerOfTwo texture
|
|
105
|
+
if (isPOT && mipmap === true) {
|
|
106
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return texture;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* delete the given WebGL texture
|
|
114
|
+
* @param {WebGLTexture} [texture] - a WebGL texture to delete
|
|
115
|
+
* @param {number} [unit] - Texture unit to delete
|
|
116
|
+
*/
|
|
117
|
+
deleteTexture2D(texture) {
|
|
118
|
+
this.gl.deleteTexture(texture);
|
|
119
|
+
this.unbindTexture2D(texture);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* returns the WebGL texture associated to the given texture unit
|
|
124
|
+
* @param {number} unit - Texture unit to which a texture is bound
|
|
125
|
+
* @returns {WebGLTexture} texture a WebGL texture
|
|
126
|
+
*/
|
|
127
|
+
getTexture2D(unit) {
|
|
128
|
+
return this.boundTextures[unit];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* assign the given WebGL texture to the current batch
|
|
133
|
+
* @param {WebGLTexture} texture - a WebGL texture
|
|
134
|
+
* @param {number} unit - Texture unit to which the given texture is bound
|
|
135
|
+
*/
|
|
136
|
+
bindTexture2D(texture, unit) {
|
|
137
|
+
var gl = this.gl;
|
|
138
|
+
|
|
139
|
+
if (texture !== this.boundTextures[unit]) {
|
|
140
|
+
this.flush();
|
|
141
|
+
if (this.currentTextureUnit !== unit) {
|
|
142
|
+
this.currentTextureUnit = unit;
|
|
143
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
147
|
+
this.boundTextures[unit] = texture;
|
|
148
|
+
|
|
149
|
+
} else if (this.currentTextureUnit !== unit) {
|
|
150
|
+
this.flush();
|
|
151
|
+
this.currentTextureUnit = unit;
|
|
152
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* unbind the given WebGL texture, forcing it to be reuploaded
|
|
158
|
+
* @param {WebGLTexture} [texture] - a WebGL texture
|
|
159
|
+
* @param {number} [unit] - a WebGL texture
|
|
160
|
+
* @returns {number} unit the unit number that was associated with the given texture
|
|
161
|
+
*/
|
|
162
|
+
unbindTexture2D(texture, unit) {
|
|
163
|
+
if (typeof unit === "undefined") {
|
|
164
|
+
unit = this.boundTextures.indexOf(texture);
|
|
165
|
+
}
|
|
166
|
+
if (unit !== -1) {
|
|
167
|
+
delete this.boundTextures[unit];
|
|
168
|
+
if (unit === this.currentTextureUnit) {
|
|
169
|
+
this.currentTextureUnit = -1;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return unit;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @ignore
|
|
177
|
+
*/
|
|
178
|
+
uploadTexture(texture, w, h, force = false) {
|
|
179
|
+
var unit = this.renderer.cache.getUnit(texture);
|
|
180
|
+
var texture2D = this.boundTextures[unit];
|
|
181
|
+
|
|
182
|
+
if (typeof texture2D === "undefined" || force) {
|
|
183
|
+
this.createTexture2D(
|
|
184
|
+
unit,
|
|
185
|
+
texture.getTexture(),
|
|
186
|
+
this.renderer.settings.antiAlias ? this.gl.LINEAR : this.gl.NEAREST,
|
|
187
|
+
texture.repeat,
|
|
188
|
+
w,
|
|
189
|
+
h,
|
|
190
|
+
texture.premultipliedAlpha
|
|
191
|
+
);
|
|
192
|
+
} else {
|
|
193
|
+
this.bindTexture2D(texture2D, unit);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return this.currentTextureUnit;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Add a textured quad
|
|
201
|
+
* @param {TextureAtlas} texture - Source texture atlas
|
|
202
|
+
* @param {number} x - Destination x-coordinate
|
|
203
|
+
* @param {number} y - Destination y-coordinate
|
|
204
|
+
* @param {number} w - Destination width
|
|
205
|
+
* @param {number} h - Destination height
|
|
206
|
+
* @param {number} u0 - Texture UV (u0) value.
|
|
207
|
+
* @param {number} v0 - Texture UV (v0) value.
|
|
208
|
+
* @param {number} u1 - Texture UV (u1) value.
|
|
209
|
+
* @param {number} v1 - Texture UV (v1) value.
|
|
210
|
+
* @param {number} tint - tint color to be applied to the texture in UINT32 (argb) format
|
|
211
|
+
*/
|
|
212
|
+
addQuad(texture, x, y, w, h, u0, v0, u1, v1, tint) {
|
|
213
|
+
var vertexData = this.vertexData;
|
|
214
|
+
|
|
215
|
+
if (vertexData.isFull(6)) {
|
|
216
|
+
// is the vertex buffer full if we add 6 more vertices
|
|
217
|
+
this.flush();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// upload and activate the texture if necessary
|
|
221
|
+
var unit = this.uploadTexture(texture);
|
|
222
|
+
|
|
223
|
+
// set fragment sampler accordingly
|
|
224
|
+
this.currentShader.setUniform("uSampler", unit);
|
|
225
|
+
|
|
226
|
+
// Transform vertices
|
|
227
|
+
var m = this.viewMatrix,
|
|
228
|
+
vec0 = V_ARRAY[0].set(x, y),
|
|
229
|
+
vec1 = V_ARRAY[1].set(x + w, y),
|
|
230
|
+
vec2 = V_ARRAY[2].set(x, y + h),
|
|
231
|
+
vec3 = V_ARRAY[3].set(x + w, y + h);
|
|
232
|
+
|
|
233
|
+
if (!m.isIdentity()) {
|
|
234
|
+
m.apply(vec0);
|
|
235
|
+
m.apply(vec1);
|
|
236
|
+
m.apply(vec2);
|
|
237
|
+
m.apply(vec3);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
vertexData.push(vec0.x, vec0.y, u0, v0, tint);
|
|
241
|
+
vertexData.push(vec1.x, vec1.y, u1, v0, tint);
|
|
242
|
+
vertexData.push(vec2.x, vec2.y, u0, v1, tint);
|
|
243
|
+
vertexData.push(vec2.x, vec2.y, u0, v1, tint);
|
|
244
|
+
vertexData.push(vec1.x, vec1.y, u1, v0, tint);
|
|
245
|
+
vertexData.push(vec3.x, vec3.y, u1, v1, tint);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export { QuadCompositor as default };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine -
|
|
2
|
+
* melonJS Game Engine - v15.0.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
6
6
|
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
7
|
*/
|
|
8
|
-
var primitiveVertex = "// Current vertex point\nattribute vec2 aVertex;\n\n// Projection matrix\nuniform mat4 uProjectionMatrix;\n\
|
|
8
|
+
var primitiveVertex = "// Current vertex point\nattribute vec2 aVertex;\nattribute vec4 aColor;\n\n// Projection matrix\nuniform mat4 uProjectionMatrix;\n\nvarying vec4 vColor;\n\nvoid main(void) {\n // Transform the vertex position by the projection matrix\n gl_Position = uProjectionMatrix * vec4(aVertex, 0.0, 1.0);\n // Pass the remaining attributes to the fragment shader\n vColor = vec4(aColor.bgr * aColor.a, aColor.a);\n}\n";
|
|
9
9
|
|
|
10
10
|
export { primitiveVertex as default };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine -
|
|
2
|
+
* melonJS Game Engine - v15.0.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
6
6
|
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
7
|
*/
|
|
8
|
-
var quadVertex = "
|
|
8
|
+
var quadVertex = "// Current vertex point\nattribute vec2 aVertex;\nattribute vec2 aRegion;\nattribute vec4 aColor;\n\n// Projection matrix\nuniform mat4 uProjectionMatrix;\n\nvarying vec2 vRegion;\nvarying vec4 vColor;\n\nvoid main(void) {\n // Transform the vertex position by the projection matrix\n gl_Position = uProjectionMatrix * vec4(aVertex, 0.0, 1.0);\n // Pass the remaining attributes to the fragment shader\n vColor = vec4(aColor.bgr * aColor.a, aColor.a);\n vRegion = aRegion;\n}\n";
|
|
9
9
|
|
|
10
10
|
export { quadVertex as default };
|