melonjs 10.0.2 → 10.2.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/README.md +7 -13
- package/dist/melonjs.js +1213 -828
- package/dist/melonjs.min.js +3 -3
- package/dist/melonjs.module.d.ts +581 -454
- package/dist/melonjs.module.js +1182 -823
- package/package.json +6 -6
- package/src/camera/camera2d.js +3 -3
- package/src/game.js +1 -1
- package/src/index.js +4 -0
- package/src/input/pointer.js +22 -21
- package/src/input/pointerevent.js +10 -12
- package/src/level/tiled/TMXLayer.js +1 -1
- package/src/loader/loader.js +2 -2
- package/src/loader/loadingscreen.js +40 -72
- package/src/math/color.js +22 -4
- package/src/physics/body.js +1 -7
- package/src/physics/bounds.js +2 -2
- package/src/physics/detector.js +0 -1
- package/src/physics/world.js +2 -1
- package/src/renderable/container.js +1 -0
- package/src/renderable/imagelayer.js +2 -0
- package/src/renderable/nineslicesprite.js +211 -0
- package/src/renderable/renderable.js +3 -3
- package/src/renderable/sprite.js +2 -2
- package/src/state/stage.js +2 -2
- package/src/state/state.js +2 -2
- package/src/system/device.js +15 -1
- package/src/text/text.js +82 -21
- package/src/video/renderer.js +5 -3
- package/src/video/texture.js +17 -7
- package/src/video/texture_cache.js +11 -0
- package/src/video/video.js +3 -3
- package/src/video/webgl/buffer/vertex.js +130 -0
- package/src/video/webgl/shaders/quad.vert +1 -1
- package/src/video/webgl/webgl_compositor.js +104 -167
- package/src/video/webgl/webgl_renderer.js +25 -20
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @classdesc
|
|
3
|
+
* a Vertex Buffer object
|
|
4
|
+
* @class VertexArrayBuffer
|
|
5
|
+
* @private
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
class VertexArrayBuffer {
|
|
9
|
+
|
|
10
|
+
constructor(vertex_size, vertex_per_quad) {
|
|
11
|
+
// the size of one vertex
|
|
12
|
+
this.vertexSize = vertex_size;
|
|
13
|
+
// size of a quad in vertex
|
|
14
|
+
this.quadSize = vertex_per_quad;
|
|
15
|
+
// the maximum number of vertices the vertex array buffer can hold
|
|
16
|
+
this.maxVertex = 256;
|
|
17
|
+
// the current number of vertices added to the vertex array buffer
|
|
18
|
+
this.vertexCount = 0;
|
|
19
|
+
|
|
20
|
+
// the actual vertex data buffer
|
|
21
|
+
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize);
|
|
22
|
+
// Float32 and Uint32 view of the vertex data array buffer
|
|
23
|
+
this.bufferF32 = new Float32Array(this.buffer);
|
|
24
|
+
this.bufferU32 = new Uint32Array(this.buffer);
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* clear the vertex array buffer
|
|
32
|
+
*/
|
|
33
|
+
clear() {
|
|
34
|
+
this.vertexCount = 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* return true if full
|
|
40
|
+
*/
|
|
41
|
+
isFull(vertex = 0) {
|
|
42
|
+
return (this.vertexCount + vertex >= this.maxVertex);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* resize the vertex buffer, retaining its original contents
|
|
47
|
+
*/
|
|
48
|
+
resize() {
|
|
49
|
+
// double the vertex size
|
|
50
|
+
this.maxVertex <<= 1;
|
|
51
|
+
// save a reference to the previous data
|
|
52
|
+
var data = this.bufferF32;
|
|
53
|
+
|
|
54
|
+
// recreate ArrayBuffer and views
|
|
55
|
+
this.buffer = new ArrayBuffer(this.maxVertex * this.vertexSize * this.quadSize);
|
|
56
|
+
this.bufferF32 = new Float32Array(this.buffer);
|
|
57
|
+
this.bufferU32 = new Uint32Array(this.buffer);
|
|
58
|
+
|
|
59
|
+
// copy previous data
|
|
60
|
+
this.bufferF32.set(data);
|
|
61
|
+
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* push a new vertex to the buffer
|
|
67
|
+
*/
|
|
68
|
+
push(x, y, u, v, tint) {
|
|
69
|
+
var offset = this.vertexCount * this.vertexSize;
|
|
70
|
+
|
|
71
|
+
if (this.vertexCount >= this.maxVertex) {
|
|
72
|
+
this.resize();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.bufferF32[offset + 0] = x;
|
|
76
|
+
this.bufferF32[offset + 1] = y;
|
|
77
|
+
|
|
78
|
+
if (typeof u !== "undefined") {
|
|
79
|
+
this.bufferF32[offset + 2] = u;
|
|
80
|
+
this.bufferF32[offset + 3] = v;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (typeof tint !== "undefined") {
|
|
84
|
+
this.bufferU32[offset + 4] = tint;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.vertexCount++;
|
|
88
|
+
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* return a reference to the data in Float32 format
|
|
94
|
+
*/
|
|
95
|
+
toFloat32(begin, end) {
|
|
96
|
+
if (typeof end !== "undefined") {
|
|
97
|
+
return this.bufferF32.subarray(begin, end);
|
|
98
|
+
} else {
|
|
99
|
+
return this.bufferF32;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* return a reference to the data in Uint32 format
|
|
105
|
+
*/
|
|
106
|
+
toUint32(begin, end) {
|
|
107
|
+
if (typeof end !== "undefined") {
|
|
108
|
+
return this.bufferU32.subarray(begin, end);
|
|
109
|
+
} else {
|
|
110
|
+
return this.bufferU32;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* return the size of the vertex in vertex
|
|
116
|
+
*/
|
|
117
|
+
length() {
|
|
118
|
+
return this.vertexCount;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* return true if empty
|
|
123
|
+
*/
|
|
124
|
+
isEmpty() {
|
|
125
|
+
return this.vertexCount === 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export default VertexArrayBuffer;
|
|
@@ -11,6 +11,6 @@ void main(void) {
|
|
|
11
11
|
// Transform the vertex position by the projection matrix
|
|
12
12
|
gl_Position = uProjectionMatrix * vec4(aVertex, 0.0, 1.0);
|
|
13
13
|
// Pass the remaining attributes to the fragment shader
|
|
14
|
-
vColor = vec4(aColor.
|
|
14
|
+
vColor = vec4(aColor.bgr * aColor.a, aColor.a);
|
|
15
15
|
vRegion = aRegion;
|
|
16
16
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Vector2d from "./../../math/vector2.js";
|
|
2
2
|
import GLShader from "./glshader.js";
|
|
3
|
+
import VertexArrayBuffer from "./buffer/vertex.js";
|
|
3
4
|
import * as event from "./../../system/event.js";
|
|
4
5
|
import { isPowerOfTwo } from "./../../math/math.js";
|
|
5
6
|
|
|
@@ -9,6 +10,13 @@ import quadVertex from "./shaders/quad.vert";
|
|
|
9
10
|
import quadFragment from "./shaders/quad.frag";
|
|
10
11
|
|
|
11
12
|
|
|
13
|
+
// a pool of resuable vectors
|
|
14
|
+
var V_ARRAY = [
|
|
15
|
+
new Vector2d(),
|
|
16
|
+
new Vector2d(),
|
|
17
|
+
new Vector2d(),
|
|
18
|
+
new Vector2d()
|
|
19
|
+
];
|
|
12
20
|
|
|
13
21
|
// Handy constants
|
|
14
22
|
var VERTEX_SIZE = 2;
|
|
@@ -18,15 +26,32 @@ var COLOR_SIZE = 4;
|
|
|
18
26
|
var ELEMENT_SIZE = VERTEX_SIZE + REGION_SIZE + COLOR_SIZE;
|
|
19
27
|
var ELEMENT_OFFSET = ELEMENT_SIZE * Float32Array.BYTES_PER_ELEMENT;
|
|
20
28
|
|
|
21
|
-
var VERTEX_ELEMENT = 0;
|
|
22
|
-
var REGION_ELEMENT = VERTEX_ELEMENT + VERTEX_SIZE;
|
|
23
|
-
var COLOR_ELEMENT = REGION_ELEMENT + REGION_SIZE;
|
|
24
|
-
|
|
25
29
|
var ELEMENTS_PER_QUAD = 4;
|
|
26
30
|
var INDICES_PER_QUAD = 6;
|
|
27
31
|
|
|
28
32
|
var MAX_LENGTH = 16000;
|
|
29
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Create a full index buffer for the element array
|
|
36
|
+
* @ignore
|
|
37
|
+
*/
|
|
38
|
+
function createIB() {
|
|
39
|
+
var indices = [
|
|
40
|
+
0, 1, 2,
|
|
41
|
+
2, 1, 3
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
// ~384KB index buffer
|
|
45
|
+
var data = new Array(MAX_LENGTH * INDICES_PER_QUAD);
|
|
46
|
+
for (var i = 0; i < data.length; i++) {
|
|
47
|
+
data[i] = indices[i % INDICES_PER_QUAD] +
|
|
48
|
+
~~(i / INDICES_PER_QUAD) * ELEMENTS_PER_QUAD;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return new Uint16Array(data);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
|
|
30
55
|
/**
|
|
31
56
|
* @classdesc
|
|
32
57
|
* A WebGL Compositor object. This class handles all of the WebGL state<br>
|
|
@@ -57,20 +82,12 @@ class WebGLCompositor {
|
|
|
57
82
|
* @type Number
|
|
58
83
|
* @readonly
|
|
59
84
|
*/
|
|
60
|
-
this.length = 0;
|
|
85
|
+
//this.length = 0;
|
|
61
86
|
|
|
62
87
|
// list of active texture units
|
|
63
88
|
this.currentTextureUnit = -1;
|
|
64
89
|
this.boundTextures = [];
|
|
65
90
|
|
|
66
|
-
// Vector pool
|
|
67
|
-
this.v = [
|
|
68
|
-
new Vector2d(),
|
|
69
|
-
new Vector2d(),
|
|
70
|
-
new Vector2d(),
|
|
71
|
-
new Vector2d()
|
|
72
|
-
];
|
|
73
|
-
|
|
74
91
|
// the associated renderer
|
|
75
92
|
this.renderer = renderer;
|
|
76
93
|
|
|
@@ -80,9 +97,6 @@ class WebGLCompositor {
|
|
|
80
97
|
// Global fill color
|
|
81
98
|
this.color = renderer.currentColor;
|
|
82
99
|
|
|
83
|
-
// Global tint color
|
|
84
|
-
this.tint = renderer.currentTint;
|
|
85
|
-
|
|
86
100
|
// Global transformation matrix
|
|
87
101
|
this.viewMatrix = renderer.currentTransform;
|
|
88
102
|
|
|
@@ -118,9 +132,9 @@ class WebGLCompositor {
|
|
|
118
132
|
/// define all vertex attributes
|
|
119
133
|
this.addAttribute("aVertex", 2, gl.FLOAT, false, 0 * Float32Array.BYTES_PER_ELEMENT); // 0
|
|
120
134
|
this.addAttribute("aRegion", 2, gl.FLOAT, false, 2 * Float32Array.BYTES_PER_ELEMENT); // 1
|
|
121
|
-
this.addAttribute("aColor", 4, gl.
|
|
135
|
+
this.addAttribute("aColor", 4, gl.UNSIGNED_BYTE, true, 4 * Float32Array.BYTES_PER_ELEMENT); // 2
|
|
122
136
|
|
|
123
|
-
//
|
|
137
|
+
// vertex buffer
|
|
124
138
|
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
|
125
139
|
gl.bufferData(
|
|
126
140
|
gl.ARRAY_BUFFER,
|
|
@@ -128,17 +142,11 @@ class WebGLCompositor {
|
|
|
128
142
|
gl.STREAM_DRAW
|
|
129
143
|
);
|
|
130
144
|
|
|
131
|
-
this.
|
|
132
|
-
this.sbIndex = 0;
|
|
145
|
+
this.vertexBuffer = new VertexArrayBuffer(ELEMENT_SIZE, ELEMENTS_PER_QUAD);
|
|
133
146
|
|
|
134
|
-
//
|
|
135
|
-
this.stream = new Float32Array(
|
|
136
|
-
this.sbSize * ELEMENT_SIZE * ELEMENTS_PER_QUAD
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
// Index buffer
|
|
147
|
+
// Cache index buffer (TODO Remove use for cache by replacing drawElements by drawArrays)
|
|
140
148
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.createBuffer());
|
|
141
|
-
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
|
|
149
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, createIB(), gl.STATIC_DRAW);
|
|
142
150
|
|
|
143
151
|
// register to the CANVAS resize channel
|
|
144
152
|
event.on(event.CANVAS_ONRESIZE, (width, height) => {
|
|
@@ -154,9 +162,6 @@ class WebGLCompositor {
|
|
|
154
162
|
* @ignore
|
|
155
163
|
*/
|
|
156
164
|
reset() {
|
|
157
|
-
this.sbIndex = 0;
|
|
158
|
-
this.length = 0;
|
|
159
|
-
|
|
160
165
|
// WebGL context
|
|
161
166
|
this.gl = this.renderer.gl;
|
|
162
167
|
|
|
@@ -194,7 +199,7 @@ class WebGLCompositor {
|
|
|
194
199
|
* @param {String} name name of the attribute in the vertex shader
|
|
195
200
|
* @param {Number} size number of components per vertex attribute. Must be 1, 2, 3, or 4.
|
|
196
201
|
* @param {GLenum} type data type of each component in the array
|
|
197
|
-
* @param {Boolean} normalized whether integer data values should be normalized into a certain
|
|
202
|
+
* @param {Boolean} normalized whether integer data values should be normalized into a certain range when being cast to a float
|
|
198
203
|
* @param {Number} offset offset in bytes of the first component in the vertex attribute array
|
|
199
204
|
*/
|
|
200
205
|
addAttribute(name, size, type, normalized, offset) {
|
|
@@ -242,10 +247,10 @@ class WebGLCompositor {
|
|
|
242
247
|
var gl = this.gl;
|
|
243
248
|
var isPOT = isPowerOfTwo(w || image.width) && isPowerOfTwo(h || image.height);
|
|
244
249
|
var texture = gl.createTexture();
|
|
245
|
-
var rs = (repeat.search(/^repeat(-x)?$/) === 0) && (isPOT || this.renderer.WebGLVersion
|
|
246
|
-
var rt = (repeat.search(/^repeat(-y)?$/) === 0) && (isPOT || this.renderer.WebGLVersion
|
|
250
|
+
var rs = (repeat.search(/^repeat(-x)?$/) === 0) && (isPOT || this.renderer.WebGLVersion > 1) ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
251
|
+
var rt = (repeat.search(/^repeat(-y)?$/) === 0) && (isPOT || this.renderer.WebGLVersion > 1) ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
247
252
|
|
|
248
|
-
this.
|
|
253
|
+
this.bindTexture2D(texture, unit);
|
|
249
254
|
|
|
250
255
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, rs);
|
|
251
256
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, rt);
|
|
@@ -269,13 +274,13 @@ class WebGLCompositor {
|
|
|
269
274
|
|
|
270
275
|
/**
|
|
271
276
|
* assign the given WebGL texture to the current batch
|
|
272
|
-
* @name
|
|
277
|
+
* @name bindTexture2D
|
|
273
278
|
* @memberOf me.WebGLCompositor
|
|
274
279
|
* @function
|
|
275
280
|
* @param {WebGLTexture} a WebGL texture
|
|
276
281
|
* @param {Number} unit Texture unit to which the given texture is bound
|
|
277
282
|
*/
|
|
278
|
-
|
|
283
|
+
bindTexture2D(texture, unit) {
|
|
279
284
|
var gl = this.gl;
|
|
280
285
|
|
|
281
286
|
if (texture !== this.boundTextures[unit]) {
|
|
@@ -295,6 +300,17 @@ class WebGLCompositor {
|
|
|
295
300
|
}
|
|
296
301
|
}
|
|
297
302
|
|
|
303
|
+
/**
|
|
304
|
+
* unbind the given WebGL texture, forcing it to be reuploaded
|
|
305
|
+
* @name unbindTexture2D
|
|
306
|
+
* @memberOf me.WebGLCompositor
|
|
307
|
+
* @function
|
|
308
|
+
* @param {WebGLTexture} a WebGL texture
|
|
309
|
+
*/
|
|
310
|
+
unbindTexture2D(texture) {
|
|
311
|
+
var unit = this.renderer.cache.getUnit(texture);
|
|
312
|
+
this.boundTextures[unit] = null;
|
|
313
|
+
}
|
|
298
314
|
|
|
299
315
|
/**
|
|
300
316
|
* @ignore
|
|
@@ -315,43 +331,12 @@ class WebGLCompositor {
|
|
|
315
331
|
texture.premultipliedAlpha
|
|
316
332
|
);
|
|
317
333
|
} else {
|
|
318
|
-
this.
|
|
334
|
+
this.bindTexture2D(texture2D, unit);
|
|
319
335
|
}
|
|
320
336
|
|
|
321
337
|
return this.currentTextureUnit;
|
|
322
338
|
}
|
|
323
339
|
|
|
324
|
-
/**
|
|
325
|
-
* Create a full index buffer for the element array
|
|
326
|
-
* @ignore
|
|
327
|
-
*/
|
|
328
|
-
createIB() {
|
|
329
|
-
var indices = [
|
|
330
|
-
0, 1, 2,
|
|
331
|
-
2, 1, 3
|
|
332
|
-
];
|
|
333
|
-
|
|
334
|
-
// ~384KB index buffer
|
|
335
|
-
var data = new Array(MAX_LENGTH * INDICES_PER_QUAD);
|
|
336
|
-
for (var i = 0; i < data.length; i++) {
|
|
337
|
-
data[i] = indices[i % INDICES_PER_QUAD] +
|
|
338
|
-
~~(i / INDICES_PER_QUAD) * ELEMENTS_PER_QUAD;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return new Uint16Array(data);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Resize the stream buffer, retaining its original contents
|
|
346
|
-
* @ignore
|
|
347
|
-
*/
|
|
348
|
-
resizeSB() {
|
|
349
|
-
this.sbSize <<= 1;
|
|
350
|
-
var stream = new Float32Array(this.sbSize * ELEMENT_SIZE * ELEMENTS_PER_QUAD);
|
|
351
|
-
stream.set(this.stream);
|
|
352
|
-
this.stream = stream;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
340
|
/**
|
|
356
341
|
* Select the shader to use for compositing
|
|
357
342
|
* @name useShader
|
|
@@ -389,33 +374,29 @@ class WebGLCompositor {
|
|
|
389
374
|
* @memberOf me.WebGLCompositor
|
|
390
375
|
* @function
|
|
391
376
|
* @param {me.Renderer.Texture} texture Source texture
|
|
392
|
-
* @param {String} key Source texture region name
|
|
393
377
|
* @param {Number} x Destination x-coordinate
|
|
394
378
|
* @param {Number} y Destination y-coordinate
|
|
395
379
|
* @param {Number} w Destination width
|
|
396
380
|
* @param {Number} h Destination height
|
|
381
|
+
* @param {number} u0 Texture UV (u0) value.
|
|
382
|
+
* @param {number} v0 Texture UV (v0) value.
|
|
383
|
+
* @param {number} u1 Texture UV (u1) value.
|
|
384
|
+
* @param {number} v1 Texture UV (v1) value.
|
|
385
|
+
* @param {number} tint tint color to be applied to the texture in UINT32 format
|
|
397
386
|
*/
|
|
398
|
-
addQuad(texture,
|
|
399
|
-
//var gl = this.gl;
|
|
400
|
-
var color = this.color.toArray();
|
|
401
|
-
var tint = this.tint.toArray();
|
|
387
|
+
addQuad(texture, x, y, w, h, u0, v0, u1, v1, tint) {
|
|
402
388
|
|
|
403
|
-
if (color
|
|
389
|
+
if (this.color.alpha < 1 / 255) {
|
|
404
390
|
// Fast path: don't send fully transparent quads
|
|
405
391
|
return;
|
|
406
|
-
} else {
|
|
407
|
-
// use the global alpha
|
|
408
|
-
tint[3] = color[3];
|
|
409
392
|
}
|
|
410
393
|
|
|
411
|
-
|
|
394
|
+
this.useShader(this.quadShader);
|
|
395
|
+
|
|
396
|
+
if (this.vertexBuffer.isFull(4)) {
|
|
397
|
+
// is the vertex buffer full if we add 4 more vertices
|
|
412
398
|
this.flush();
|
|
413
399
|
}
|
|
414
|
-
if (this.length >= this.sbSize) {
|
|
415
|
-
this.resizeSB();
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
this.useShader(this.quadShader);
|
|
419
400
|
|
|
420
401
|
// upload and activate the texture if necessary
|
|
421
402
|
var unit = this.uploadTexture(texture);
|
|
@@ -424,56 +405,22 @@ class WebGLCompositor {
|
|
|
424
405
|
|
|
425
406
|
// Transform vertices
|
|
426
407
|
var m = this.viewMatrix,
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
408
|
+
vec0 = V_ARRAY[0].set(x, y),
|
|
409
|
+
vec1 = V_ARRAY[1].set(x + w, y),
|
|
410
|
+
vec2 = V_ARRAY[2].set(x, y + h),
|
|
411
|
+
vec3 = V_ARRAY[3].set(x + w, y + h);
|
|
431
412
|
|
|
432
413
|
if (!m.isIdentity()) {
|
|
433
|
-
m.apply(
|
|
434
|
-
m.apply(
|
|
435
|
-
m.apply(
|
|
436
|
-
m.apply(
|
|
414
|
+
m.apply(vec0);
|
|
415
|
+
m.apply(vec1);
|
|
416
|
+
m.apply(vec2);
|
|
417
|
+
m.apply(vec3);
|
|
437
418
|
}
|
|
438
419
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
idx3 = idx2 + ELEMENT_SIZE;
|
|
444
|
-
|
|
445
|
-
// Fill vertex buffer
|
|
446
|
-
// FIXME: Pack each vertex vector into single float
|
|
447
|
-
this.stream[idx0 + VERTEX_ELEMENT + 0] = v0.x;
|
|
448
|
-
this.stream[idx0 + VERTEX_ELEMENT + 1] = v0.y;
|
|
449
|
-
this.stream[idx1 + VERTEX_ELEMENT + 0] = v1.x;
|
|
450
|
-
this.stream[idx1 + VERTEX_ELEMENT + 1] = v1.y;
|
|
451
|
-
this.stream[idx2 + VERTEX_ELEMENT + 0] = v2.x;
|
|
452
|
-
this.stream[idx2 + VERTEX_ELEMENT + 1] = v2.y;
|
|
453
|
-
this.stream[idx3 + VERTEX_ELEMENT + 0] = v3.x;
|
|
454
|
-
this.stream[idx3 + VERTEX_ELEMENT + 1] = v3.y;
|
|
455
|
-
|
|
456
|
-
// Fill texture coordinates buffer
|
|
457
|
-
var uvs = texture.getUVs(key);
|
|
458
|
-
// FIXME: Pack each texture coordinate into single floats
|
|
459
|
-
this.stream[idx0 + REGION_ELEMENT + 0] = uvs[0];
|
|
460
|
-
this.stream[idx0 + REGION_ELEMENT + 1] = uvs[1];
|
|
461
|
-
this.stream[idx1 + REGION_ELEMENT + 0] = uvs[2];
|
|
462
|
-
this.stream[idx1 + REGION_ELEMENT + 1] = uvs[1];
|
|
463
|
-
this.stream[idx2 + REGION_ELEMENT + 0] = uvs[0];
|
|
464
|
-
this.stream[idx2 + REGION_ELEMENT + 1] = uvs[3];
|
|
465
|
-
this.stream[idx3 + REGION_ELEMENT + 0] = uvs[2];
|
|
466
|
-
this.stream[idx3 + REGION_ELEMENT + 1] = uvs[3];
|
|
467
|
-
|
|
468
|
-
// Fill color buffer
|
|
469
|
-
// FIXME: Pack color vector into single float
|
|
470
|
-
this.stream.set(tint, idx0 + COLOR_ELEMENT);
|
|
471
|
-
this.stream.set(tint, idx1 + COLOR_ELEMENT);
|
|
472
|
-
this.stream.set(tint, idx2 + COLOR_ELEMENT);
|
|
473
|
-
this.stream.set(tint, idx3 + COLOR_ELEMENT);
|
|
474
|
-
|
|
475
|
-
this.sbIndex += ELEMENT_SIZE * ELEMENTS_PER_QUAD;
|
|
476
|
-
this.length++;
|
|
420
|
+
this.vertexBuffer.push(vec0.x, vec0.y, u0, v0, tint);
|
|
421
|
+
this.vertexBuffer.push(vec1.x, vec1.y, u1, v0, tint);
|
|
422
|
+
this.vertexBuffer.push(vec2.x, vec2.y, u0, v1, tint);
|
|
423
|
+
this.vertexBuffer.push(vec3.x, vec3.y, u1, v1, tint);
|
|
477
424
|
}
|
|
478
425
|
|
|
479
426
|
/**
|
|
@@ -482,28 +429,32 @@ class WebGLCompositor {
|
|
|
482
429
|
* @memberOf me.WebGLCompositor
|
|
483
430
|
* @function
|
|
484
431
|
*/
|
|
485
|
-
flush() {
|
|
486
|
-
|
|
432
|
+
flush(mode = this.mode) {
|
|
433
|
+
var vertex = this.vertexBuffer;
|
|
434
|
+
var vertexCount = vertex.vertexCount;
|
|
435
|
+
|
|
436
|
+
if (vertexCount > 0) {
|
|
487
437
|
var gl = this.gl;
|
|
438
|
+
var vertexSize = vertex.vertexSize;
|
|
488
439
|
|
|
489
440
|
// Copy data into stream buffer
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
);
|
|
441
|
+
if (this.renderer.WebGLVersion > 1) {
|
|
442
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertex.toFloat32(), gl.STREAM_DRAW, 0, vertexCount * vertexSize);
|
|
443
|
+
} else {
|
|
444
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertex.toFloat32(0, vertexCount * vertexSize), gl.STREAM_DRAW);
|
|
445
|
+
}
|
|
496
446
|
|
|
497
447
|
// Draw the stream buffer
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
gl.
|
|
502
|
-
|
|
503
|
-
|
|
448
|
+
// TODO : finalize the WebGLCompositor implementation (splitting this one into two)
|
|
449
|
+
// so that different compositor with different attributes/uniforms & drawing method can be used
|
|
450
|
+
if (this.activeShader === this.primitiveShader) {
|
|
451
|
+
gl.drawArrays(mode, 0, vertexCount);
|
|
452
|
+
} else {
|
|
453
|
+
gl.drawElements(mode, vertexCount / vertex.quadSize * INDICES_PER_QUAD, gl.UNSIGNED_SHORT, 0);
|
|
454
|
+
}
|
|
504
455
|
|
|
505
|
-
|
|
506
|
-
|
|
456
|
+
// clear the vertex buffer
|
|
457
|
+
vertex.clear();
|
|
507
458
|
}
|
|
508
459
|
}
|
|
509
460
|
|
|
@@ -512,43 +463,29 @@ class WebGLCompositor {
|
|
|
512
463
|
* @name drawVertices
|
|
513
464
|
* @memberOf me.WebGLCompositor
|
|
514
465
|
* @function
|
|
515
|
-
* @param {GLENUM}
|
|
516
|
-
* @param {me.Vector2d[]}
|
|
466
|
+
* @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)
|
|
467
|
+
* @param {me.Vector2d[]} verts vertices
|
|
517
468
|
* @param {Number} [vertexCount=verts.length] amount of points defined in the points array
|
|
518
469
|
*/
|
|
519
|
-
drawVertices(mode, verts, vertexCount) {
|
|
520
|
-
var gl = this.gl;
|
|
521
|
-
|
|
522
|
-
vertexCount = vertexCount || verts.length;
|
|
523
|
-
|
|
470
|
+
drawVertices(mode, verts, vertexCount = verts.length) {
|
|
524
471
|
// use the primitive shader
|
|
525
472
|
this.useShader(this.primitiveShader);
|
|
526
|
-
|
|
527
473
|
// Set the line color
|
|
528
474
|
this.primitiveShader.setUniform("uColor", this.color);
|
|
529
475
|
|
|
530
|
-
// Put vertex data into the stream buffer
|
|
531
|
-
var offset = 0;
|
|
532
476
|
var m = this.viewMatrix;
|
|
477
|
+
var vertex = this.vertexBuffer;
|
|
533
478
|
var m_isIdentity = m.isIdentity();
|
|
479
|
+
|
|
534
480
|
for (var i = 0; i < vertexCount; i++) {
|
|
535
481
|
if (!m_isIdentity) {
|
|
536
482
|
m.apply(verts[i]);
|
|
537
483
|
}
|
|
538
|
-
|
|
539
|
-
this.stream[offset + 1] = verts[i].y;
|
|
540
|
-
offset += ELEMENT_SIZE;
|
|
484
|
+
vertex.push(verts[i].x, verts[i].y);
|
|
541
485
|
}
|
|
542
486
|
|
|
543
|
-
//
|
|
544
|
-
|
|
545
|
-
gl.ARRAY_BUFFER,
|
|
546
|
-
this.stream.subarray(0, vertexCount * ELEMENT_SIZE),
|
|
547
|
-
gl.STREAM_DRAW
|
|
548
|
-
);
|
|
549
|
-
|
|
550
|
-
// Draw the stream buffer
|
|
551
|
-
gl.drawArrays(mode, 0, vertexCount);
|
|
487
|
+
// flush
|
|
488
|
+
this.flush(mode);
|
|
552
489
|
}
|
|
553
490
|
|
|
554
491
|
/**
|
|
@@ -40,14 +40,6 @@ class WebGLRenderer extends Renderer {
|
|
|
40
40
|
// parent contructor
|
|
41
41
|
super(options);
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* The WebGL context
|
|
45
|
-
* @name gl
|
|
46
|
-
* @memberOf me.WebGLRenderer
|
|
47
|
-
* type {WebGLRenderingContext}
|
|
48
|
-
*/
|
|
49
|
-
this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
|
|
50
|
-
|
|
51
43
|
/**
|
|
52
44
|
* The WebGL version used by this renderer (1 or 2)
|
|
53
45
|
* @name WebGLVersion
|
|
@@ -56,7 +48,7 @@ class WebGLRenderer extends Renderer {
|
|
|
56
48
|
* @default 1
|
|
57
49
|
* @readonly
|
|
58
50
|
*/
|
|
59
|
-
this.
|
|
51
|
+
this.WebGLVersion = 1;
|
|
60
52
|
|
|
61
53
|
/**
|
|
62
54
|
* The vendor string of the underlying graphics driver.
|
|
@@ -78,6 +70,14 @@ class WebGLRenderer extends Renderer {
|
|
|
78
70
|
*/
|
|
79
71
|
this.GPURenderer = null;
|
|
80
72
|
|
|
73
|
+
/**
|
|
74
|
+
* The WebGL context
|
|
75
|
+
* @name gl
|
|
76
|
+
* @memberOf me.WebGLRenderer
|
|
77
|
+
* type {WebGLRenderingContext}
|
|
78
|
+
*/
|
|
79
|
+
this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
|
|
80
|
+
|
|
81
81
|
/**
|
|
82
82
|
* Maximum number of texture unit supported under the current context
|
|
83
83
|
* @name maxTextures
|
|
@@ -354,14 +354,18 @@ class WebGLRenderer extends Renderer {
|
|
|
354
354
|
this.currentCompositor.uploadTexture(this.fontTexture, 0, 0, 0, true);
|
|
355
355
|
|
|
356
356
|
// Add the new quad
|
|
357
|
-
var
|
|
357
|
+
var uvs = this.fontTexture.getUVs(bounds.left + "," + bounds.top + "," + bounds.width + "," + bounds.height);
|
|
358
358
|
this.currentCompositor.addQuad(
|
|
359
359
|
this.fontTexture,
|
|
360
|
-
key,
|
|
361
360
|
bounds.left,
|
|
362
361
|
bounds.top,
|
|
363
362
|
bounds.width,
|
|
364
|
-
bounds.height
|
|
363
|
+
bounds.height,
|
|
364
|
+
uvs[0],
|
|
365
|
+
uvs[1],
|
|
366
|
+
uvs[2],
|
|
367
|
+
uvs[3],
|
|
368
|
+
this.currentTint.toUint32()
|
|
365
369
|
);
|
|
366
370
|
|
|
367
371
|
// Clear font context2D
|
|
@@ -421,8 +425,9 @@ class WebGLRenderer extends Renderer {
|
|
|
421
425
|
dy |= 0;
|
|
422
426
|
}
|
|
423
427
|
|
|
424
|
-
var
|
|
425
|
-
|
|
428
|
+
var texture = this.cache.get(image);
|
|
429
|
+
var uvs = texture.getUVs(sx + "," + sy + "," + sw + "," + sh);
|
|
430
|
+
this.currentCompositor.addQuad(texture, dx, dy, dw, dh, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32());
|
|
426
431
|
}
|
|
427
432
|
|
|
428
433
|
/**
|
|
@@ -438,8 +443,8 @@ class WebGLRenderer extends Renderer {
|
|
|
438
443
|
* @see me.WebGLRenderer#createPattern
|
|
439
444
|
*/
|
|
440
445
|
drawPattern(pattern, x, y, width, height) {
|
|
441
|
-
var
|
|
442
|
-
this.currentCompositor.addQuad(pattern,
|
|
446
|
+
var uvs = pattern.getUVs("0,0," + width + "," + height);
|
|
447
|
+
this.currentCompositor.addQuad(pattern, x, y, width, height, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32());
|
|
443
448
|
}
|
|
444
449
|
|
|
445
450
|
|
|
@@ -653,7 +658,7 @@ class WebGLRenderer extends Renderer {
|
|
|
653
658
|
* @param {Number} alpha 0.0 to 1.0 values accepted.
|
|
654
659
|
*/
|
|
655
660
|
setGlobalAlpha(a) {
|
|
656
|
-
this.currentColor.
|
|
661
|
+
this.currentColor.alpha = a;
|
|
657
662
|
}
|
|
658
663
|
|
|
659
664
|
/**
|
|
@@ -665,9 +670,9 @@ class WebGLRenderer extends Renderer {
|
|
|
665
670
|
* @param {me.Color|String} color css color string.
|
|
666
671
|
*/
|
|
667
672
|
setColor(color) {
|
|
668
|
-
var alpha = this.currentColor.
|
|
673
|
+
var alpha = this.currentColor.alpha;
|
|
669
674
|
this.currentColor.copy(color);
|
|
670
|
-
this.currentColor.
|
|
675
|
+
this.currentColor.alpha *= alpha;
|
|
671
676
|
}
|
|
672
677
|
|
|
673
678
|
/**
|
|
@@ -924,7 +929,7 @@ class WebGLRenderer extends Renderer {
|
|
|
924
929
|
}
|
|
925
930
|
|
|
926
931
|
// calculate all vertices
|
|
927
|
-
for (i = 0; i < indices.length; i
|
|
932
|
+
for (i = 0; i < indices.length; i++ ) {
|
|
928
933
|
glPoints[i].set(x + points[indices[i]].x, y + points[indices[i]].y);
|
|
929
934
|
}
|
|
930
935
|
|