melonjs 10.7.1 → 10.8.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/dist/melonjs.js +1131 -652
- package/dist/melonjs.min.js +4 -4
- package/dist/melonjs.module.d.ts +1038 -198
- package/dist/melonjs.module.js +1234 -763
- package/package.json +7 -7
- package/src/camera/camera2d.js +1 -1
- package/src/entity/entity.js +6 -7
- package/src/geometries/ellipse.js +10 -11
- package/src/geometries/line.js +3 -3
- package/src/geometries/path2d.js +319 -0
- package/src/geometries/poly.js +11 -11
- package/src/geometries/rectangle.js +15 -15
- package/src/geometries/roundrect.js +67 -0
- package/src/index.js +5 -1
- package/src/input/pointerevent.js +1 -1
- package/src/lang/deprecated.js +1 -1
- 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/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 +1 -1
- package/src/math/color.js +1 -1
- 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 +23 -14
- 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/renderable/collectable.js +9 -2
- package/src/renderable/colorlayer.js +1 -1
- package/src/renderable/container.js +1 -1
- package/src/renderable/imagelayer.js +1 -1
- package/src/renderable/renderable.js +1 -1
- package/src/renderable/sprite.js +2 -3
- package/src/renderable/trigger.js +10 -4
- package/src/state/stage.js +1 -1
- 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 +1 -1
- package/src/video/canvas/canvas_renderer.js +89 -34
- package/src/video/renderer.js +26 -14
- package/src/video/texture.js +1 -1
- 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 +123 -181
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Hash map of GLSL data types to WebGL Uniform methods
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
const fnHash = {
|
|
7
|
+
"bool" : "1i",
|
|
8
|
+
"int" : "1i",
|
|
9
|
+
"float" : "1f",
|
|
10
|
+
"vec2" : "2fv",
|
|
11
|
+
"vec3" : "3fv",
|
|
12
|
+
"vec4" : "4fv",
|
|
13
|
+
"bvec2" : "2iv",
|
|
14
|
+
"bvec3" : "3iv",
|
|
15
|
+
"bvec4" : "4iv",
|
|
16
|
+
"ivec2" : "2iv",
|
|
17
|
+
"ivec3" : "3iv",
|
|
18
|
+
"ivec4" : "4iv",
|
|
19
|
+
"mat2" : "Matrix2fv",
|
|
20
|
+
"mat3" : "Matrix3fv",
|
|
21
|
+
"mat4" : "Matrix4fv",
|
|
22
|
+
"sampler2D" : "1i"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @ignore
|
|
27
|
+
*/
|
|
28
|
+
export function extractUniforms(gl, shader) {
|
|
29
|
+
var uniforms = {},
|
|
30
|
+
uniRx = /uniform\s+(\w+)\s+(\w+)/g,
|
|
31
|
+
uniformsData = {},
|
|
32
|
+
descriptor = {},
|
|
33
|
+
locations = {},
|
|
34
|
+
match;
|
|
35
|
+
|
|
36
|
+
// Detect all uniform names and types
|
|
37
|
+
[ shader.vertex, shader.fragment ].forEach(function (shader) {
|
|
38
|
+
while ((match = uniRx.exec(shader))) {
|
|
39
|
+
uniformsData[match[2]] = match[1];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Get uniform references
|
|
44
|
+
Object.keys(uniformsData).forEach(function (name) {
|
|
45
|
+
var type = uniformsData[name];
|
|
46
|
+
locations[name] = gl.getUniformLocation(shader.program, name);
|
|
47
|
+
|
|
48
|
+
descriptor[name] = {
|
|
49
|
+
"get" : (function (name) {
|
|
50
|
+
/**
|
|
51
|
+
* A getter for the uniform location
|
|
52
|
+
* @ignore
|
|
53
|
+
*/
|
|
54
|
+
return function () {
|
|
55
|
+
return locations[name];
|
|
56
|
+
};
|
|
57
|
+
})(name),
|
|
58
|
+
"set" : (function (name, type, fn) {
|
|
59
|
+
if (type.indexOf("mat") === 0) {
|
|
60
|
+
/**
|
|
61
|
+
* A generic setter for uniform matrices
|
|
62
|
+
* @ignore
|
|
63
|
+
*/
|
|
64
|
+
return function (val) {
|
|
65
|
+
gl[fn](locations[name], false, val);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
/**
|
|
70
|
+
* A generic setter for uniform vectors
|
|
71
|
+
* @ignore
|
|
72
|
+
*/
|
|
73
|
+
return function (val) {
|
|
74
|
+
var fnv = fn;
|
|
75
|
+
if (val.length && fn.substr(-1) !== "v") {
|
|
76
|
+
fnv += "v";
|
|
77
|
+
}
|
|
78
|
+
gl[fnv](locations[name], val);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
})(name, type, "uniform" + fnHash[type])
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperties(uniforms, descriptor);
|
|
85
|
+
|
|
86
|
+
return uniforms;
|
|
87
|
+
};
|
|
@@ -384,20 +384,7 @@ class WebGLCompositor {
|
|
|
384
384
|
this.activeShader = shader;
|
|
385
385
|
this.activeShader.bind();
|
|
386
386
|
this.activeShader.setUniform("uProjectionMatrix", this.renderer.projectionMatrix);
|
|
387
|
-
|
|
388
|
-
// set the vertex attributes
|
|
389
|
-
for (var index = 0; index < this.attributes.length; ++index) {
|
|
390
|
-
var gl = this.gl;
|
|
391
|
-
var element = this.attributes[index];
|
|
392
|
-
var location = this.activeShader.getAttribLocation(element.name);
|
|
393
|
-
|
|
394
|
-
if (location !== -1) {
|
|
395
|
-
gl.enableVertexAttribArray(location);
|
|
396
|
-
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, this.vertexByteSize, element.offset);
|
|
397
|
-
} else {
|
|
398
|
-
gl.disableVertexAttribArray(index);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
387
|
+
this.activeShader.setVertexAttributes(this.gl, this.attributes, this.vertexByteSize);
|
|
401
388
|
}
|
|
402
389
|
}
|
|
403
390
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import Color from "./../../math/color.js";
|
|
2
|
-
import Vector2d from "./../../math/vector2.js";
|
|
3
2
|
import Matrix2d from "./../../math/matrix2.js";
|
|
4
3
|
import WebGLCompositor from "./webgl_compositor.js";
|
|
5
4
|
import Renderer from "./../renderer.js";
|
|
@@ -7,8 +6,8 @@ import TextureCache from "./../texture_cache.js";
|
|
|
7
6
|
import { TextureAtlas, createAtlas } from "./../texture.js";
|
|
8
7
|
import { createCanvas, renderer } from "./../video.js";
|
|
9
8
|
import * as event from "./../../system/event.js";
|
|
10
|
-
import
|
|
11
|
-
import { isPowerOfTwo, nextPowerOfTwo
|
|
9
|
+
import pool from "./../../system/pooling.js";
|
|
10
|
+
import { isPowerOfTwo, nextPowerOfTwo } from "./../../math/math.js";
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
/**
|
|
@@ -41,7 +40,7 @@ class WebGLRenderer extends Renderer {
|
|
|
41
40
|
/**
|
|
42
41
|
* The WebGL version used by this renderer (1 or 2)
|
|
43
42
|
* @name WebGLVersion
|
|
44
|
-
* @memberof WebGLRenderer
|
|
43
|
+
* @memberof WebGLRenderer#
|
|
45
44
|
* @type {number}
|
|
46
45
|
* @default 1
|
|
47
46
|
* @readonly
|
|
@@ -51,7 +50,7 @@ class WebGLRenderer extends Renderer {
|
|
|
51
50
|
/**
|
|
52
51
|
* The vendor string of the underlying graphics driver.
|
|
53
52
|
* @name GPUVendor
|
|
54
|
-
* @memberof WebGLRenderer
|
|
53
|
+
* @memberof WebGLRenderer#
|
|
55
54
|
* @type {string}
|
|
56
55
|
* @default null
|
|
57
56
|
* @readonly
|
|
@@ -61,7 +60,7 @@ class WebGLRenderer extends Renderer {
|
|
|
61
60
|
/**
|
|
62
61
|
* The renderer string of the underlying graphics driver.
|
|
63
62
|
* @name GPURenderer
|
|
64
|
-
* @memberof WebGLRenderer
|
|
63
|
+
* @memberof WebGLRenderer#
|
|
65
64
|
* @type {string}
|
|
66
65
|
* @default null
|
|
67
66
|
* @readonly
|
|
@@ -71,7 +70,7 @@ class WebGLRenderer extends Renderer {
|
|
|
71
70
|
/**
|
|
72
71
|
* The WebGL context
|
|
73
72
|
* @name gl
|
|
74
|
-
* @memberof WebGLRenderer
|
|
73
|
+
* @memberof WebGLRenderer#
|
|
75
74
|
* type {WebGLRenderingContext}
|
|
76
75
|
*/
|
|
77
76
|
this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
|
|
@@ -79,7 +78,7 @@ class WebGLRenderer extends Renderer {
|
|
|
79
78
|
/**
|
|
80
79
|
* Maximum number of texture unit supported under the current context
|
|
81
80
|
* @name maxTextures
|
|
82
|
-
* @memberof WebGLRenderer
|
|
81
|
+
* @memberof WebGLRenderer#
|
|
83
82
|
* @type {number}
|
|
84
83
|
* @readonly
|
|
85
84
|
*/
|
|
@@ -105,16 +104,6 @@ class WebGLRenderer extends Renderer {
|
|
|
105
104
|
*/
|
|
106
105
|
this._blendStack = [];
|
|
107
106
|
|
|
108
|
-
/**
|
|
109
|
-
* @ignore
|
|
110
|
-
*/
|
|
111
|
-
this._glPoints = [
|
|
112
|
-
new Vector2d(),
|
|
113
|
-
new Vector2d(),
|
|
114
|
-
new Vector2d(),
|
|
115
|
-
new Vector2d()
|
|
116
|
-
];
|
|
117
|
-
|
|
118
107
|
/**
|
|
119
108
|
* The current transformation matrix used for transformations on the overall scene
|
|
120
109
|
* @name currentTransform
|
|
@@ -134,7 +123,7 @@ class WebGLRenderer extends Renderer {
|
|
|
134
123
|
/**
|
|
135
124
|
* The list of active compositors
|
|
136
125
|
* @name compositors
|
|
137
|
-
* @type {Map}
|
|
126
|
+
* @type {Map<WebGLCompositor>}
|
|
138
127
|
* @memberof WebGLRenderer#
|
|
139
128
|
*/
|
|
140
129
|
this.compositors = new Map();
|
|
@@ -721,6 +710,17 @@ class WebGLRenderer extends Renderer {
|
|
|
721
710
|
this.currentColor.alpha = alpha;
|
|
722
711
|
}
|
|
723
712
|
|
|
713
|
+
/**
|
|
714
|
+
* Return the global alpha
|
|
715
|
+
* @name getGlobalAlpha
|
|
716
|
+
* @memberof WebGLRenderer.prototype
|
|
717
|
+
* @function
|
|
718
|
+
* @returns {number} global alpha value
|
|
719
|
+
*/
|
|
720
|
+
getGlobalAlpha() {
|
|
721
|
+
return this.currentColor.alpha;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
724
|
/**
|
|
725
725
|
* Set the current fill & stroke style color.
|
|
726
726
|
* By default, or upon reset, the value is set to #000000.
|
|
@@ -759,34 +759,18 @@ class WebGLRenderer extends Renderer {
|
|
|
759
759
|
* @param {boolean} [antiClockwise=false] draw arc anti-clockwise
|
|
760
760
|
* @param {boolean} [fill=false]
|
|
761
761
|
*/
|
|
762
|
-
strokeArc(x, y, radius, start, end, antiClockwise = false, fill) {
|
|
763
|
-
if (
|
|
764
|
-
|
|
762
|
+
strokeArc(x, y, radius, start, end, antiClockwise = false, fill = false) {
|
|
763
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
764
|
+
// Fast path: don't draw fully transparent
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
this.path2D.beginPath();
|
|
768
|
+
this.path2D.arc(x, y, radius, start, end, antiClockwise);
|
|
769
|
+
if (fill === false) {
|
|
770
|
+
this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
|
|
765
771
|
} else {
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
var i, len = Math.floor(24 * Math.sqrt(radius * 2));
|
|
769
|
-
var theta = (end - start) / (len * 2);
|
|
770
|
-
var theta2 = theta * 2;
|
|
771
|
-
var cos_theta = Math.cos(theta);
|
|
772
|
-
var sin_theta = Math.sin(theta);
|
|
773
|
-
|
|
774
|
-
// Grow internal points buffer if necessary
|
|
775
|
-
for (i = points.length; i < len + 1; i++) {
|
|
776
|
-
points.push(new Vector2d());
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
// calculate and draw all segments
|
|
780
|
-
for (i = 0; i < len; i++) {
|
|
781
|
-
var angle = ((theta) + start + (theta2 * i));
|
|
782
|
-
var cos = Math.cos(angle);
|
|
783
|
-
var sin = -Math.sin(angle);
|
|
784
|
-
|
|
785
|
-
points[i].x = x + (((cos_theta * cos) + (sin_theta * sin)) * radius);
|
|
786
|
-
points[i].y = y + (((cos_theta * -sin) + (sin_theta * cos)) * radius);
|
|
787
|
-
}
|
|
788
|
-
// batch draw all lines
|
|
789
|
-
this.currentCompositor.drawVertices(this.gl.LINE_STRIP, points, len);
|
|
772
|
+
this.path2D.closePath();
|
|
773
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
790
774
|
}
|
|
791
775
|
}
|
|
792
776
|
|
|
@@ -802,35 +786,8 @@ class WebGLRenderer extends Renderer {
|
|
|
802
786
|
* @param {number} end end angle in radians
|
|
803
787
|
* @param {boolean} [antiClockwise=false] draw arc anti-clockwise
|
|
804
788
|
*/
|
|
805
|
-
fillArc(x, y, radius, start, end
|
|
806
|
-
|
|
807
|
-
var points = this._glPoints;
|
|
808
|
-
var i, index = 0;
|
|
809
|
-
var len = Math.floor(24 * Math.sqrt(radius * 2));
|
|
810
|
-
var theta = (end - start) / (len * 2);
|
|
811
|
-
var theta2 = theta * 2;
|
|
812
|
-
var cos_theta = Math.cos(theta);
|
|
813
|
-
var sin_theta = Math.sin(theta);
|
|
814
|
-
|
|
815
|
-
// Grow internal points buffer if necessary
|
|
816
|
-
for (i = points.length; i < len * 2; i++) {
|
|
817
|
-
points.push(new Vector2d());
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
// calculate and draw all segments
|
|
821
|
-
for (i = 0; i < len - 1; i++) {
|
|
822
|
-
var angle = ((theta) + start + (theta2 * i));
|
|
823
|
-
var cos = Math.cos(angle);
|
|
824
|
-
var sin = -Math.sin(angle);
|
|
825
|
-
|
|
826
|
-
points[index++].set(x, y);
|
|
827
|
-
points[index++].set(
|
|
828
|
-
x - (((cos_theta * cos) + (sin_theta * sin)) * radius),
|
|
829
|
-
y - (((cos_theta * -sin) + (sin_theta * cos)) * radius)
|
|
830
|
-
);
|
|
831
|
-
}
|
|
832
|
-
// batch draw all triangles
|
|
833
|
-
this.currentCompositor.drawVertices(this.gl.TRIANGLE_STRIP, points, index);
|
|
789
|
+
fillArc(x, y, radius, start, end, antiClockwise = false) {
|
|
790
|
+
this.strokeArc(x, y, radius, start, end, antiClockwise, true);
|
|
834
791
|
}
|
|
835
792
|
|
|
836
793
|
/**
|
|
@@ -845,28 +802,17 @@ class WebGLRenderer extends Renderer {
|
|
|
845
802
|
* @param {boolean} [fill=false] also fill the shape with the current color if true
|
|
846
803
|
*/
|
|
847
804
|
strokeEllipse(x, y, w, h, fill = false) {
|
|
848
|
-
if (
|
|
849
|
-
|
|
805
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
806
|
+
// Fast path: don't draw fully transparent
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
this.path2D.beginPath();
|
|
810
|
+
this.path2D.ellipse(x, y, w, h, 0, 0, 360);
|
|
811
|
+
this.path2D.closePath();
|
|
812
|
+
if (fill === false) {
|
|
813
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
850
814
|
} else {
|
|
851
|
-
|
|
852
|
-
var len = Math.floor(24 * Math.sqrt(w)) ||
|
|
853
|
-
Math.floor(12 * Math.sqrt(w + h));
|
|
854
|
-
var segment = (TAU) / len;
|
|
855
|
-
var points = this._glPoints,
|
|
856
|
-
i;
|
|
857
|
-
|
|
858
|
-
// Grow internal points buffer if necessary
|
|
859
|
-
for (i = points.length; i < len; i++) {
|
|
860
|
-
points.push(new Vector2d());
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
// calculate and draw all segments
|
|
864
|
-
for (i = 0; i < len; i++) {
|
|
865
|
-
points[i].x = x + (Math.sin(segment * -i) * w);
|
|
866
|
-
points[i].y = y + (Math.cos(segment * -i) * h);
|
|
867
|
-
}
|
|
868
|
-
// batch draw all lines
|
|
869
|
-
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, len);
|
|
815
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
870
816
|
}
|
|
871
817
|
}
|
|
872
818
|
|
|
@@ -881,28 +827,7 @@ class WebGLRenderer extends Renderer {
|
|
|
881
827
|
* @param {number} h vertical radius of the ellipse
|
|
882
828
|
*/
|
|
883
829
|
fillEllipse(x, y, w, h) {
|
|
884
|
-
|
|
885
|
-
var len = Math.floor(24 * Math.sqrt(w)) ||
|
|
886
|
-
Math.floor(12 * Math.sqrt(w + h));
|
|
887
|
-
var segment = (TAU) / len;
|
|
888
|
-
var points = this._glPoints;
|
|
889
|
-
var index = 0, i;
|
|
890
|
-
|
|
891
|
-
// Grow internal points buffer if necessary
|
|
892
|
-
for (i = points.length; i < (len + 1) * 2; i++) {
|
|
893
|
-
points.push(new Vector2d());
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
// draw all vertices vertex coordinates
|
|
897
|
-
for (i = 0; i < len + 1; i++) {
|
|
898
|
-
points[index++].set(x, y);
|
|
899
|
-
points[index++].set(
|
|
900
|
-
x + (Math.sin(segment * i) * w),
|
|
901
|
-
y + (Math.cos(segment * i) * h)
|
|
902
|
-
);
|
|
903
|
-
}
|
|
904
|
-
// batch draw all triangles
|
|
905
|
-
this.currentCompositor.drawVertices(this.gl.TRIANGLE_STRIP, points, index);
|
|
830
|
+
this.strokeEllipse(x, y, w, h, false);
|
|
906
831
|
}
|
|
907
832
|
|
|
908
833
|
/**
|
|
@@ -916,12 +841,14 @@ class WebGLRenderer extends Renderer {
|
|
|
916
841
|
* @param {number} endY the end y coordinate
|
|
917
842
|
*/
|
|
918
843
|
strokeLine(startX, startY, endX, endY) {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
this.
|
|
844
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
845
|
+
// Fast path: don't draw fully transparent
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
this.path2D.beginPath();
|
|
849
|
+
this.path2D.moveTo(startX, startY);
|
|
850
|
+
this.path2D.lineTo(endX, endY);
|
|
851
|
+
this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
|
|
925
852
|
}
|
|
926
853
|
|
|
927
854
|
|
|
@@ -948,25 +875,27 @@ class WebGLRenderer extends Renderer {
|
|
|
948
875
|
* @param {boolean} [fill=false] also fill the shape with the current color if true
|
|
949
876
|
*/
|
|
950
877
|
strokePolygon(poly, fill = false) {
|
|
951
|
-
if (
|
|
952
|
-
|
|
878
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
879
|
+
// Fast path: don't draw fully transparent
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
this.translate(poly.pos.x, poly.pos.y);
|
|
883
|
+
this.path2D.beginPath();
|
|
884
|
+
this.path2D.moveTo(poly.points[0].x, poly.points[0].y);
|
|
885
|
+
var point;
|
|
886
|
+
for (var i = 1; i < poly.points.length; i++) {
|
|
887
|
+
point = poly.points[i];
|
|
888
|
+
this.path2D.lineTo(point.x, point.y);
|
|
889
|
+
}
|
|
890
|
+
this.path2D.lineTo(poly.points[0].x, poly.points[0].y);
|
|
891
|
+
this.path2D.closePath();
|
|
892
|
+
if (fill === false) {
|
|
893
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
953
894
|
} else {
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
i;
|
|
957
|
-
|
|
958
|
-
// Grow internal points buffer if necessary
|
|
959
|
-
for (i = points.length; i < len; i++) {
|
|
960
|
-
points.push(new Vector2d());
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
// calculate and draw all segments
|
|
964
|
-
for (i = 0; i < len; i++) {
|
|
965
|
-
points[i].x = poly.pos.x + poly.points[i].x;
|
|
966
|
-
points[i].y = poly.pos.y + poly.points[i].y;
|
|
967
|
-
}
|
|
968
|
-
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, len);
|
|
895
|
+
// draw all triangles
|
|
896
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
969
897
|
}
|
|
898
|
+
this.translate(-poly.pos.x, -poly.pos.y);
|
|
970
899
|
}
|
|
971
900
|
|
|
972
901
|
/**
|
|
@@ -977,24 +906,7 @@ class WebGLRenderer extends Renderer {
|
|
|
977
906
|
* @param {Polygon} poly the shape to draw
|
|
978
907
|
*/
|
|
979
908
|
fillPolygon(poly) {
|
|
980
|
-
|
|
981
|
-
var glPoints = this._glPoints;
|
|
982
|
-
var indices = poly.getIndices();
|
|
983
|
-
var x = poly.pos.x, y = poly.pos.y;
|
|
984
|
-
var i;
|
|
985
|
-
|
|
986
|
-
// Grow internal points buffer if necessary
|
|
987
|
-
for (i = glPoints.length; i < indices.length; i++) {
|
|
988
|
-
glPoints.push(new Vector2d());
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
// calculate all vertices
|
|
992
|
-
for (i = 0; i < indices.length; i++ ) {
|
|
993
|
-
glPoints[i].set(x + points[indices[i]].x, y + points[indices[i]].y);
|
|
994
|
-
}
|
|
995
|
-
|
|
996
|
-
// draw all triangle
|
|
997
|
-
this.currentCompositor.drawVertices(this.gl.TRIANGLES, glPoints, indices.length);
|
|
909
|
+
this.strokePolygon(poly, true);
|
|
998
910
|
}
|
|
999
911
|
|
|
1000
912
|
/**
|
|
@@ -1009,19 +921,16 @@ class WebGLRenderer extends Renderer {
|
|
|
1009
921
|
* @param {boolean} [fill=false] also fill the shape with the current color if true
|
|
1010
922
|
*/
|
|
1011
923
|
strokeRect(x, y, width, height, fill = false) {
|
|
1012
|
-
if (
|
|
1013
|
-
|
|
924
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
925
|
+
// Fast path: don't draw fully transparent
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
this.path2D.beginPath();
|
|
929
|
+
this.path2D.rect(x, y, width, height);
|
|
930
|
+
if (fill === false) {
|
|
931
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
1014
932
|
} else {
|
|
1015
|
-
|
|
1016
|
-
points[0].x = x;
|
|
1017
|
-
points[0].y = y;
|
|
1018
|
-
points[1].x = x + width;
|
|
1019
|
-
points[1].y = y;
|
|
1020
|
-
points[2].x = x + width;
|
|
1021
|
-
points[2].y = y + height;
|
|
1022
|
-
points[3].x = x;
|
|
1023
|
-
points[3].y = y + height;
|
|
1024
|
-
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, 4);
|
|
933
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
1025
934
|
}
|
|
1026
935
|
}
|
|
1027
936
|
|
|
@@ -1036,16 +945,49 @@ class WebGLRenderer extends Renderer {
|
|
|
1036
945
|
* @param {number} height
|
|
1037
946
|
*/
|
|
1038
947
|
fillRect(x, y, width, height) {
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
948
|
+
this.strokeRect(x, y, width, height, true);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Stroke a rounded rectangle at the specified coordinates
|
|
953
|
+
* @name strokeRoundRect
|
|
954
|
+
* @memberof WebGLRenderer.prototype
|
|
955
|
+
* @function
|
|
956
|
+
* @param {number} x
|
|
957
|
+
* @param {number} y
|
|
958
|
+
* @param {number} width
|
|
959
|
+
* @param {number} height
|
|
960
|
+
* @param {number} radius
|
|
961
|
+
* @param {boolean} [fill=false] also fill the shape with the current color if true
|
|
962
|
+
*/
|
|
963
|
+
strokeRoundRect(x, y, width, height, radius, fill = false) {
|
|
964
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
965
|
+
// Fast path: don't draw fully transparent
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
this.path2D.beginPath();
|
|
969
|
+
this.path2D.roundRect(x, y, width, height, radius);
|
|
970
|
+
if (fill === false) {
|
|
971
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
972
|
+
} else {
|
|
973
|
+
this.path2D.closePath();
|
|
974
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Draw a rounded filled rectangle at the specified coordinates
|
|
980
|
+
* @name fillRoundRect
|
|
981
|
+
* @memberof WebGLRenderer.prototype
|
|
982
|
+
* @function
|
|
983
|
+
* @param {number} x
|
|
984
|
+
* @param {number} y
|
|
985
|
+
* @param {number} width
|
|
986
|
+
* @param {number} height
|
|
987
|
+
* @param {number} radius
|
|
988
|
+
*/
|
|
989
|
+
fillRoundRect(x, y, width, height, radius) {
|
|
990
|
+
this.strokeRoundRect(x, y, width, height, radius, true);
|
|
1049
991
|
}
|
|
1050
992
|
|
|
1051
993
|
/**
|