melonjs 14.0.2 → 14.1.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 +2 -0
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
- package/dist/melonjs.mjs/_virtual/howler.js +10 -0
- package/dist/melonjs.mjs/_virtual/index.js +10 -0
- package/dist/melonjs.mjs/_virtual/index2.js +10 -0
- package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
- package/dist/melonjs.mjs/application/application.js +238 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +731 -0
- package/dist/melonjs.mjs/entity/entity.js +247 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
- package/dist/melonjs.mjs/geometries/line.js +115 -0
- package/dist/melonjs.mjs/geometries/path2d.js +318 -0
- package/dist/melonjs.mjs/geometries/point.js +88 -0
- package/dist/melonjs.mjs/geometries/poly.js +498 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +167 -0
- package/dist/melonjs.mjs/index.js +248 -0
- package/dist/melonjs.mjs/input/gamepad.js +501 -0
- package/dist/melonjs.mjs/input/input.js +26 -0
- package/dist/melonjs.mjs/input/keyboard.js +470 -0
- package/dist/melonjs.mjs/input/pointer.js +393 -0
- package/dist/melonjs.mjs/input/pointerevent.js +818 -0
- package/dist/melonjs.mjs/lang/deprecated.js +157 -0
- package/dist/melonjs.mjs/level/level.js +297 -0
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +446 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -0
- package/dist/melonjs.mjs/loader/loader.js +801 -0
- package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
- package/dist/melonjs.mjs/math/color.js +616 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +501 -0
- package/dist/melonjs.mjs/math/matrix3.js +679 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
- package/dist/melonjs.mjs/math/vector2.js +526 -0
- package/dist/melonjs.mjs/math/vector3.js +567 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
- package/dist/melonjs.mjs/particles/emitter.js +265 -0
- package/dist/melonjs.mjs/particles/particle.js +186 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +702 -0
- package/dist/melonjs.mjs/physics/bounds.js +459 -0
- package/dist/melonjs.mjs/physics/collision.js +132 -0
- package/dist/melonjs.mjs/physics/detector.js +194 -0
- package/dist/melonjs.mjs/physics/quadtree.js +394 -0
- package/dist/melonjs.mjs/physics/response.js +57 -0
- package/dist/melonjs.mjs/physics/sat.js +483 -0
- package/dist/melonjs.mjs/physics/world.js +219 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +60 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
- package/dist/melonjs.mjs/renderable/container.js +1016 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
- package/dist/melonjs.mjs/renderable/light2d.js +155 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
- package/dist/melonjs.mjs/renderable/renderable.js +781 -0
- package/dist/melonjs.mjs/renderable/sprite.js +653 -0
- package/dist/melonjs.mjs/renderable/trigger.js +156 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
- package/dist/melonjs.mjs/state/stage.js +236 -0
- package/dist/melonjs.mjs/state/state.js +596 -0
- package/dist/melonjs.mjs/system/device.js +909 -0
- package/dist/melonjs.mjs/system/dom.js +78 -0
- package/dist/melonjs.mjs/system/event.js +537 -0
- package/dist/melonjs.mjs/system/platform.js +41 -0
- package/dist/melonjs.mjs/system/pooling.js +209 -0
- package/dist/melonjs.mjs/system/save.js +157 -0
- package/dist/melonjs.mjs/system/timer.js +286 -0
- package/dist/melonjs.mjs/text/bitmaptext.js +363 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
- package/dist/melonjs.mjs/text/glyph.js +65 -0
- package/dist/melonjs.mjs/text/text.js +452 -0
- package/dist/melonjs.mjs/text/textmetrics.js +175 -0
- package/dist/melonjs.mjs/text/textstyle.js +23 -0
- package/dist/melonjs.mjs/tweens/easing.js +336 -0
- package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
- package/dist/melonjs.mjs/tweens/tween.js +479 -0
- package/dist/melonjs.mjs/utils/agent.js +76 -0
- package/dist/melonjs.mjs/utils/array.js +63 -0
- package/dist/melonjs.mjs/utils/file.js +42 -0
- package/dist/melonjs.mjs/utils/function.js +70 -0
- package/dist/melonjs.mjs/utils/string.js +82 -0
- package/dist/melonjs.mjs/utils/utils.js +173 -0
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +806 -0
- package/dist/melonjs.mjs/video/renderer.js +410 -0
- package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
- package/dist/melonjs.mjs/video/texture/cache.js +143 -0
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
- package/dist/melonjs.mjs/video/video.js +462 -0
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +142 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +167 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
- package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
- package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +494 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
- package/dist/melonjs.module.d.ts +1298 -1359
- package/dist/melonjs.module.js +2072 -3520
- package/package.json +21 -16
- package/src/application/application.js +4 -5
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +32 -33
- package/src/entity/entity.js +18 -19
- package/src/geometries/ellipse.js +17 -18
- package/src/geometries/line.js +6 -7
- package/src/geometries/path2d.js +33 -34
- package/src/geometries/point.js +1 -2
- package/src/geometries/poly.js +16 -18
- package/src/geometries/rectangle.js +19 -20
- package/src/geometries/roundrect.js +9 -10
- package/src/input/gamepad.js +15 -15
- package/src/input/keyboard.js +12 -12
- package/src/input/pointer.js +6 -6
- package/src/input/pointerevent.js +12 -12
- package/src/lang/deprecated.js +12 -12
- package/src/level/level.js +25 -25
- package/src/level/tiled/TMXLayer.js +23 -24
- package/src/level/tiled/TMXTile.js +6 -7
- package/src/level/tiled/TMXTileMap.js +8 -10
- package/src/level/tiled/TMXTileset.js +3 -4
- package/src/level/tiled/TMXTilesetGroup.js +1 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
- package/src/level/tiled/renderer/TMXRenderer.js +20 -21
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +21 -22
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +17 -18
- package/src/math/matrix3.js +26 -27
- package/src/math/observable_vector2.js +15 -16
- package/src/math/observable_vector3.js +17 -18
- package/src/math/vector2.js +10 -11
- package/src/math/vector3.js +11 -12
- package/src/particles/emitter.js +7 -8
- package/src/particles/particle.js +3 -4
- package/src/physics/body.js +29 -30
- package/src/physics/bounds.js +10 -10
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +18 -23
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +6 -7
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +4 -6
- package/src/renderable/colorlayer.js +6 -8
- package/src/renderable/container.js +25 -27
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +14 -15
- package/src/renderable/light2d.js +4 -5
- package/src/renderable/nineslicesprite.js +17 -18
- package/src/renderable/renderable.js +26 -28
- package/src/renderable/sprite.js +29 -30
- package/src/renderable/trigger.js +16 -17
- package/src/renderable/ui/uibaseelement.js +8 -9
- package/src/renderable/ui/uispriteelement.js +8 -8
- package/src/renderable/ui/uitextbutton.js +15 -15
- package/src/state/stage.js +8 -9
- package/src/state/state.js +17 -17
- package/src/system/device.js +11 -11
- package/src/system/event.js +10 -10
- package/src/system/pooling.js +9 -9
- package/src/system/save.js +2 -2
- package/src/system/timer.js +10 -10
- package/src/text/bitmaptext.js +19 -20
- package/src/text/bitmaptextdata.js +3 -4
- package/src/text/glyph.js +1 -2
- package/src/text/text.js +24 -25
- package/src/text/textmetrics.js +9 -10
- package/src/tweens/tween.js +20 -21
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +4 -4
- package/src/utils/file.js +2 -2
- package/src/utils/function.js +6 -6
- package/src/utils/string.js +5 -5
- package/src/utils/utils.js +4 -4
- package/src/video/canvas/canvas_renderer.js +72 -73
- package/src/video/renderer.js +27 -28
- package/src/video/texture/atlas.js +22 -22
- package/src/video/texture/canvas_texture.js +9 -9
- package/src/video/video.js +17 -17
- package/src/video/webgl/buffer/vertex.js +1 -2
- package/src/video/webgl/glshader.js +12 -12
- package/src/video/webgl/webgl_compositor.js +42 -43
- package/src/video/webgl/webgl_renderer.js +76 -77
|
@@ -0,0 +1,1035 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v14.1.1
|
|
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 - 2022 Olivier Biot (AltByte Pte Ltd)
|
|
7
|
+
*/
|
|
8
|
+
import Color from '../../math/color.js';
|
|
9
|
+
import Matrix2d from '../../math/matrix2.js';
|
|
10
|
+
import WebGLCompositor from './webgl_compositor.js';
|
|
11
|
+
import Renderer from '../renderer.js';
|
|
12
|
+
import TextureCache from '../texture/cache.js';
|
|
13
|
+
import { TextureAtlas, createAtlas } from '../texture/atlas.js';
|
|
14
|
+
import { createCanvas, renderer } from '../video.js';
|
|
15
|
+
import { emit, ONCONTEXT_LOST, ONCONTEXT_RESTORED } from '../../system/event.js';
|
|
16
|
+
import pool from '../../system/pooling.js';
|
|
17
|
+
import { isPowerOfTwo, nextPowerOfTwo } from '../../math/math.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @classdesc
|
|
21
|
+
* a WebGL renderer object
|
|
22
|
+
* @augments Renderer
|
|
23
|
+
*/
|
|
24
|
+
class WebGLRenderer extends Renderer {
|
|
25
|
+
/**
|
|
26
|
+
* @param {object} options - The renderer parameters
|
|
27
|
+
* @param {number} options.width - The width of the canvas without scaling
|
|
28
|
+
* @param {number} options.height - The height of the canvas without scaling
|
|
29
|
+
* @param {HTMLCanvasElement} [options.canvas] - The html canvas to draw to on screen
|
|
30
|
+
* @param {boolean} [options.antiAlias=false] - Whether to enable anti-aliasing
|
|
31
|
+
* @param {boolean} [options.failIfMajorPerformanceCaveat=true] - If true, the renderer will switch to CANVAS mode if the performances of a WebGL context would be dramatically lower than that of a native application making equivalent OpenGL calls.
|
|
32
|
+
* @param {boolean} [options.transparent=false] - Whether to enable transparency on the canvas
|
|
33
|
+
* @param {boolean} [options.premultipliedAlpha=true] - in WebGL, whether the renderer will assume that colors have premultiplied alpha when canvas transparency is enabled
|
|
34
|
+
* @param {boolean} [options.subPixel=false] - Whether to enable subpixel renderering (performance hit when enabled)
|
|
35
|
+
* @param {boolean} [options.preferWebGL1=false] - if true the renderer will only use WebGL 1
|
|
36
|
+
* @param {string} [options.powerPreference="default"] - a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
|
|
37
|
+
* @param {number} [options.zoomX=width] - The actual width of the canvas with scaling applied
|
|
38
|
+
* @param {number} [options.zoomY=height] - The actual height of the canvas with scaling applied
|
|
39
|
+
* @param {WebGLCompositor} [options.compositor] - A class that implements the compositor API
|
|
40
|
+
*/
|
|
41
|
+
constructor(options) {
|
|
42
|
+
|
|
43
|
+
// parent contructor
|
|
44
|
+
super(options);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The WebGL version used by this renderer (1 or 2)
|
|
48
|
+
* @type {number}
|
|
49
|
+
* @default 1
|
|
50
|
+
* @readonly
|
|
51
|
+
*/
|
|
52
|
+
this.WebGLVersion = 1;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The vendor string of the underlying graphics driver.
|
|
56
|
+
* @type {string}
|
|
57
|
+
* @default null
|
|
58
|
+
* @readonly
|
|
59
|
+
*/
|
|
60
|
+
this.GPUVendor = null;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The renderer string of the underlying graphics driver.
|
|
64
|
+
* @type {string}
|
|
65
|
+
* @default null
|
|
66
|
+
* @readonly
|
|
67
|
+
*/
|
|
68
|
+
this.GPURenderer = null;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The WebGL context
|
|
72
|
+
* @name gl
|
|
73
|
+
* @type {WebGLRenderingContext}
|
|
74
|
+
*/
|
|
75
|
+
this.context = this.gl = this.getContextGL(this.getCanvas(), options.transparent);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Maximum number of texture unit supported under the current context
|
|
79
|
+
* @type {number}
|
|
80
|
+
* @readonly
|
|
81
|
+
*/
|
|
82
|
+
this.maxTextures = this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @ignore
|
|
86
|
+
*/
|
|
87
|
+
this._colorStack = [];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @ignore
|
|
91
|
+
*/
|
|
92
|
+
this._matrixStack = [];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @ignore
|
|
96
|
+
*/
|
|
97
|
+
this._scissorStack = [];
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @ignore
|
|
101
|
+
*/
|
|
102
|
+
this._blendStack = [];
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The current transformation matrix used for transformations on the overall scene
|
|
106
|
+
* @type {Matrix2d}
|
|
107
|
+
*/
|
|
108
|
+
this.currentTransform = new Matrix2d();
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The current compositor used by the renderer
|
|
112
|
+
* @type {WebGLCompositor}
|
|
113
|
+
*/
|
|
114
|
+
this.currentCompositor = null;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The list of active compositors
|
|
118
|
+
* @type {Map<WebGLCompositor>}
|
|
119
|
+
*/
|
|
120
|
+
this.compositors = new Map();
|
|
121
|
+
|
|
122
|
+
// Create a default compositor
|
|
123
|
+
var compositor = new (this.settings.compositor || WebGLCompositor)(this);
|
|
124
|
+
this.compositors.set("default", compositor);
|
|
125
|
+
this.setCompositor(compositor);
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
// default WebGL state(s)
|
|
129
|
+
this.gl.disable(this.gl.DEPTH_TEST);
|
|
130
|
+
this.gl.disable(this.gl.SCISSOR_TEST);
|
|
131
|
+
this.gl.enable(this.gl.BLEND);
|
|
132
|
+
|
|
133
|
+
// set default mode
|
|
134
|
+
this.setBlendMode(this.settings.blendMode);
|
|
135
|
+
|
|
136
|
+
// get GPU vendor and renderer
|
|
137
|
+
var debugInfo = this.gl.getExtension("WEBGL_debug_renderer_info");
|
|
138
|
+
if (debugInfo !== null) {
|
|
139
|
+
this.GPUVendor = this.gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
|
|
140
|
+
this.GPURenderer = this.gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Create a texture cache
|
|
144
|
+
this.cache = new TextureCache(this.maxTextures);
|
|
145
|
+
|
|
146
|
+
// to simulate context lost and restore in WebGL:
|
|
147
|
+
// var ctx = me.video.renderer.context.getExtension('WEBGL_lose_context');
|
|
148
|
+
// ctx.loseContext()
|
|
149
|
+
this.getCanvas().addEventListener("webglcontextlost", (e) => {
|
|
150
|
+
e.preventDefault();
|
|
151
|
+
this.isContextValid = false;
|
|
152
|
+
emit(ONCONTEXT_LOST, this);
|
|
153
|
+
}, false );
|
|
154
|
+
// ctx.restoreContext()
|
|
155
|
+
this.getCanvas().addEventListener("webglcontextrestored", () => {
|
|
156
|
+
this.reset();
|
|
157
|
+
this.isContextValid = true;
|
|
158
|
+
emit(ONCONTEXT_RESTORED, this);
|
|
159
|
+
}, false );
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Reset context state
|
|
164
|
+
*/
|
|
165
|
+
reset() {
|
|
166
|
+
super.reset();
|
|
167
|
+
|
|
168
|
+
this.compositors.forEach((compositor) => {
|
|
169
|
+
if (this.isContextValid === false) {
|
|
170
|
+
// on context lost/restore
|
|
171
|
+
compositor.init(this);
|
|
172
|
+
} else {
|
|
173
|
+
compositor.reset();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
this.gl.disable(this.gl.SCISSOR_TEST);
|
|
178
|
+
if (typeof this.fontContext2D !== "undefined" ) {
|
|
179
|
+
this.createFontTexture(this.cache);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* set the active compositor for this renderer
|
|
186
|
+
* @param {WebGLCompositor|string} compositor - a compositor name or instance
|
|
187
|
+
*/
|
|
188
|
+
setCompositor(compositor = "default") {
|
|
189
|
+
|
|
190
|
+
if (typeof compositor === "string") {
|
|
191
|
+
compositor = this.compositors.get(compositor);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (typeof compositor === "undefined") {
|
|
195
|
+
throw new Error("Invalid WebGL Compositor");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (this.currentCompositor !== compositor) {
|
|
199
|
+
if (this.currentCompositor !== null) {
|
|
200
|
+
// flush the current compositor
|
|
201
|
+
this.currentCompositor.flush();
|
|
202
|
+
}
|
|
203
|
+
// set given one as current
|
|
204
|
+
this.currentCompositor = compositor;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Reset the gl transform to identity
|
|
210
|
+
*/
|
|
211
|
+
resetTransform() {
|
|
212
|
+
this.currentTransform.identity();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @ignore
|
|
217
|
+
*/
|
|
218
|
+
createFontTexture(cache) {
|
|
219
|
+
if (typeof this.fontTexture === "undefined") {
|
|
220
|
+
var canvas = this.getCanvas();
|
|
221
|
+
var width = canvas.width;
|
|
222
|
+
var height = canvas.height;
|
|
223
|
+
|
|
224
|
+
if (this.WebGLVersion === 1) {
|
|
225
|
+
if (!isPowerOfTwo(width)) {
|
|
226
|
+
width = nextPowerOfTwo(canvas.width);
|
|
227
|
+
}
|
|
228
|
+
if (!isPowerOfTwo(height)) {
|
|
229
|
+
height = nextPowerOfTwo(canvas.height);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
var image = createCanvas(width, height, true);
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @ignore
|
|
237
|
+
*/
|
|
238
|
+
this.fontContext2D = this.getContext2d(image);
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @ignore
|
|
242
|
+
*/
|
|
243
|
+
this.fontTexture = new TextureAtlas(createAtlas(canvas.width, canvas.height, "fontTexture"), image, cache);
|
|
244
|
+
this.currentCompositor.uploadTexture(this.fontTexture, 0, 0, 0);
|
|
245
|
+
|
|
246
|
+
} else {
|
|
247
|
+
// fontTexture was already created, just add it back into the cache
|
|
248
|
+
cache.set(this.fontContext2D.canvas, this.fontTexture);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Create a pattern with the specified repetition
|
|
254
|
+
* @param {Image} image - Source image
|
|
255
|
+
* @param {string} repeat - Define how the pattern should be repeated
|
|
256
|
+
* @returns {TextureAtlas}
|
|
257
|
+
* @see ImageLayer#repeat
|
|
258
|
+
* @example
|
|
259
|
+
* var tileable = renderer.createPattern(image, "repeat");
|
|
260
|
+
* var horizontal = renderer.createPattern(image, "repeat-x");
|
|
261
|
+
* var vertical = renderer.createPattern(image, "repeat-y");
|
|
262
|
+
* var basic = renderer.createPattern(image, "no-repeat");
|
|
263
|
+
*/
|
|
264
|
+
createPattern(image, repeat) {
|
|
265
|
+
|
|
266
|
+
if (renderer.WebGLVersion === 1 && (!isPowerOfTwo(image.width) || !isPowerOfTwo(image.height))) {
|
|
267
|
+
var src = typeof image.src !== "undefined" ? image.src : image;
|
|
268
|
+
throw new Error(
|
|
269
|
+
"[WebGL Renderer] " + src + " is not a POT texture " +
|
|
270
|
+
"(" + image.width + "x" + image.height + ")"
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
var texture = new TextureAtlas(createAtlas(image.width, image.height, "pattern", repeat), image);
|
|
275
|
+
|
|
276
|
+
// FIXME: Remove old cache entry and texture when changing the repeat mode
|
|
277
|
+
this.currentCompositor.uploadTexture(texture);
|
|
278
|
+
|
|
279
|
+
return texture;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Flush the compositor to the frame buffer
|
|
284
|
+
*/
|
|
285
|
+
flush() {
|
|
286
|
+
this.currentCompositor.flush();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* set/change the current projection matrix (WebGL only)
|
|
291
|
+
* @param {Matrix3d} matrix
|
|
292
|
+
*/
|
|
293
|
+
setProjection(matrix) {
|
|
294
|
+
super.setProjection(matrix);
|
|
295
|
+
this.currentCompositor.setProjection(matrix);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* prepare the framebuffer for drawing a new frame
|
|
300
|
+
*/
|
|
301
|
+
clear() {
|
|
302
|
+
this.currentCompositor.clear(this.settings.transparent ? 0.0 : 1.0);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Clears the gl context with the given color.
|
|
307
|
+
* @param {Color|string} [color="#000000"] - CSS color.
|
|
308
|
+
* @param {boolean} [opaque=false] - Allow transparency [default] or clear the surface completely [true]
|
|
309
|
+
*/
|
|
310
|
+
clearColor(color = "#000000", opaque = false) {
|
|
311
|
+
var glArray;
|
|
312
|
+
|
|
313
|
+
if (color instanceof Color) {
|
|
314
|
+
glArray = color.toArray();
|
|
315
|
+
} else {
|
|
316
|
+
var _color = pool.pull("me.Color");
|
|
317
|
+
// reuse temporary the renderer default color object
|
|
318
|
+
glArray = _color.parseCSS(color).toArray();
|
|
319
|
+
pool.push(_color);
|
|
320
|
+
}
|
|
321
|
+
// clear gl context with the specified color
|
|
322
|
+
this.currentCompositor.clearColor(glArray[0], glArray[1], glArray[2], (opaque === true) ? 1.0 : glArray[3]);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Erase the pixels in the given rectangular area by setting them to transparent black (rgba(0,0,0,0)).
|
|
327
|
+
* @param {number} x - x axis of the coordinate for the rectangle starting point.
|
|
328
|
+
* @param {number} y - y axis of the coordinate for the rectangle starting point.
|
|
329
|
+
* @param {number} width - The rectangle's width.
|
|
330
|
+
* @param {number} height - The rectangle's height.
|
|
331
|
+
*/
|
|
332
|
+
clearRect(x, y, width, height) {
|
|
333
|
+
this.save();
|
|
334
|
+
this.clipRect(x, y, width, height);
|
|
335
|
+
this.clearColor();
|
|
336
|
+
this.restore();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @ignore
|
|
341
|
+
*/
|
|
342
|
+
drawFont(bounds) {
|
|
343
|
+
var fontContext = this.getFontContext();
|
|
344
|
+
|
|
345
|
+
// Force-upload the new texture
|
|
346
|
+
this.currentCompositor.uploadTexture(this.fontTexture, 0, 0, 0, true);
|
|
347
|
+
|
|
348
|
+
// Add the new quad
|
|
349
|
+
var uvs = this.fontTexture.getUVs(bounds.left + "," + bounds.top + "," + bounds.width + "," + bounds.height);
|
|
350
|
+
this.currentCompositor.addQuad(
|
|
351
|
+
this.fontTexture,
|
|
352
|
+
bounds.left,
|
|
353
|
+
bounds.top,
|
|
354
|
+
bounds.width,
|
|
355
|
+
bounds.height,
|
|
356
|
+
uvs[0],
|
|
357
|
+
uvs[1],
|
|
358
|
+
uvs[2],
|
|
359
|
+
uvs[3],
|
|
360
|
+
this.currentTint.toUint32(this.getGlobalAlpha())
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
// Clear font context2D
|
|
364
|
+
fontContext.clearRect(
|
|
365
|
+
bounds.left,
|
|
366
|
+
bounds.top,
|
|
367
|
+
bounds.width,
|
|
368
|
+
bounds.height
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Draw an image to the gl context
|
|
374
|
+
* @param {Image} image - An element to draw into the context. The specification permits any canvas image source (CanvasImageSource), specifically, a CSSImageValue, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, or an OffscreenCanvas.
|
|
375
|
+
* @param {number} sx - The X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
|
|
376
|
+
* @param {number} sy - The Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
|
|
377
|
+
* @param {number} sw - The width of the sub-rectangle of the source image to draw into the destination context. If not specified, the entire rectangle from the coordinates specified by sx and sy to the bottom-right corner of the image is used.
|
|
378
|
+
* @param {number} sh - The height of the sub-rectangle of the source image to draw into the destination context.
|
|
379
|
+
* @param {number} dx - The X coordinate in the destination canvas at which to place the top-left corner of the source image.
|
|
380
|
+
* @param {number} dy - The Y coordinate in the destination canvas at which to place the top-left corner of the source image.
|
|
381
|
+
* @param {number} dw - The width to draw the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in width when drawn.
|
|
382
|
+
* @param {number} dh - The height to draw the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in height when drawn.
|
|
383
|
+
* @example
|
|
384
|
+
* // Position the image on the canvas:
|
|
385
|
+
* renderer.drawImage(image, dx, dy);
|
|
386
|
+
* // Position the image on the canvas, and specify width and height of the image:
|
|
387
|
+
* renderer.drawImage(image, dx, dy, dWidth, dHeight);
|
|
388
|
+
* // Clip the image and position the clipped part on the canvas:
|
|
389
|
+
* renderer.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
|
|
390
|
+
*/
|
|
391
|
+
drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) {
|
|
392
|
+
if (typeof sw === "undefined") {
|
|
393
|
+
sw = dw = image.width;
|
|
394
|
+
sh = dh = image.height;
|
|
395
|
+
dx = sx;
|
|
396
|
+
dy = sy;
|
|
397
|
+
sx = 0;
|
|
398
|
+
sy = 0;
|
|
399
|
+
}
|
|
400
|
+
else if (typeof dx === "undefined") {
|
|
401
|
+
dx = sx;
|
|
402
|
+
dy = sy;
|
|
403
|
+
dw = sw;
|
|
404
|
+
dh = sh;
|
|
405
|
+
sw = image.width;
|
|
406
|
+
sh = image.height;
|
|
407
|
+
sx = 0;
|
|
408
|
+
sy = 0;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (this.settings.subPixel === false) {
|
|
412
|
+
// clamp to pixel grid
|
|
413
|
+
dx |= 0;
|
|
414
|
+
dy |= 0;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
var texture = this.cache.get(image);
|
|
418
|
+
var uvs = texture.getUVs(sx + "," + sy + "," + sw + "," + sh);
|
|
419
|
+
this.currentCompositor.addQuad(texture, dx, dy, dw, dh, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32(this.getGlobalAlpha()));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Draw a pattern within the given rectangle.
|
|
424
|
+
* @param {TextureAtlas} pattern - Pattern object
|
|
425
|
+
* @param {number} x
|
|
426
|
+
* @param {number} y
|
|
427
|
+
* @param {number} width
|
|
428
|
+
* @param {number} height
|
|
429
|
+
* @see WebGLRenderer#createPattern
|
|
430
|
+
*/
|
|
431
|
+
drawPattern(pattern, x, y, width, height) {
|
|
432
|
+
var uvs = pattern.getUVs("0,0," + width + "," + height);
|
|
433
|
+
this.currentCompositor.addQuad(pattern, x, y, width, height, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32(this.getGlobalAlpha()));
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Returns the WebGL Context object of the given canvas element
|
|
438
|
+
* @param {HTMLCanvasElement} canvas
|
|
439
|
+
* @param {boolean} [transparent=false] - use true to enable transparency
|
|
440
|
+
* @returns {WebGLRenderingContext}
|
|
441
|
+
*/
|
|
442
|
+
getContextGL(canvas, transparent = false) {
|
|
443
|
+
if (typeof canvas === "undefined" || canvas === null) {
|
|
444
|
+
throw new Error(
|
|
445
|
+
"You must pass a canvas element in order to create " +
|
|
446
|
+
"a GL context"
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
var attr = {
|
|
451
|
+
alpha : transparent,
|
|
452
|
+
antialias : this.settings.antiAlias,
|
|
453
|
+
depth : false,
|
|
454
|
+
stencil: true,
|
|
455
|
+
preserveDrawingBuffer : false,
|
|
456
|
+
premultipliedAlpha: transparent ? this.settings.premultipliedAlpha : false,
|
|
457
|
+
powerPreference: this.settings.powerPreference,
|
|
458
|
+
failIfMajorPerformanceCaveat : this.settings.failIfMajorPerformanceCaveat
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
var gl;
|
|
462
|
+
|
|
463
|
+
// attempt to create a WebGL2 context if requested
|
|
464
|
+
if (this.settings.preferWebGL1 === false) {
|
|
465
|
+
gl = canvas.getContext("webgl2", attr);
|
|
466
|
+
if (gl) {
|
|
467
|
+
this.WebGLVersion = 2;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// fallback to WebGL1
|
|
472
|
+
if (!gl) {
|
|
473
|
+
this.WebGLVersion = 1;
|
|
474
|
+
gl = canvas.getContext("webgl", attr) || canvas.getContext("experimental-webgl", attr);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (!gl) {
|
|
478
|
+
throw new Error(
|
|
479
|
+
"A WebGL context could not be created."
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return gl;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Returns the WebGLContext instance for the renderer
|
|
488
|
+
* return a reference to the system 2d Context
|
|
489
|
+
* @returns {WebGLRenderingContext}
|
|
490
|
+
*/
|
|
491
|
+
getContext() {
|
|
492
|
+
return this.gl;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* set a blend mode for the given context. <br>
|
|
497
|
+
* Supported blend mode between Canvas and WebGL remderer : <br>
|
|
498
|
+
* - "normal" : this is the default mode and draws new content on top of the existing content <br>
|
|
499
|
+
* <img src="images/normal-blendmode.png" width="510"/> <br>
|
|
500
|
+
* - "multiply" : the pixels of the top layer are multiplied with the corresponding pixel of the bottom layer. A darker picture is the result. <br>
|
|
501
|
+
* <img src="images/multiply-blendmode.png" width="510"/> <br>
|
|
502
|
+
* - "additive or lighter" : where both content overlap the color is determined by adding color values. <br>
|
|
503
|
+
* <img src="images/lighter-blendmode.png" width="510"/> <br>
|
|
504
|
+
* - "screen" : The pixels are inverted, multiplied, and inverted again. A lighter picture is the result (opposite of multiply) <br>
|
|
505
|
+
* <img src="images/screen-blendmode.png" width="510"/> <br>
|
|
506
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
|
|
507
|
+
* @param {string} [mode="normal"] - blend mode : "normal", "multiply", "lighter", "additive", "screen"
|
|
508
|
+
* @param {WebGLRenderingContext} [gl]
|
|
509
|
+
*/
|
|
510
|
+
setBlendMode(mode = "normal", gl = this.gl) {
|
|
511
|
+
|
|
512
|
+
if (this.currentBlendMode !== mode) {
|
|
513
|
+
this.flush();
|
|
514
|
+
gl.enable(gl.BLEND);
|
|
515
|
+
this.currentBlendMode = mode;
|
|
516
|
+
|
|
517
|
+
switch (mode) {
|
|
518
|
+
case "screen" :
|
|
519
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_COLOR);
|
|
520
|
+
break;
|
|
521
|
+
|
|
522
|
+
case "lighter" :
|
|
523
|
+
case "additive" :
|
|
524
|
+
gl.blendFunc(gl.ONE, gl.ONE);
|
|
525
|
+
break;
|
|
526
|
+
|
|
527
|
+
case "multiply" :
|
|
528
|
+
gl.blendFunc(gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA);
|
|
529
|
+
break;
|
|
530
|
+
|
|
531
|
+
default :
|
|
532
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
533
|
+
this.currentBlendMode = "normal";
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* return a reference to the font 2d Context
|
|
541
|
+
* @ignore
|
|
542
|
+
*/
|
|
543
|
+
getFontContext() {
|
|
544
|
+
if (typeof this.fontContext2D === "undefined" ) {
|
|
545
|
+
// warn the end user about performance impact
|
|
546
|
+
console.warn("[WebGL Renderer] WARNING : Using Standard me.Text with WebGL will severly impact performances !");
|
|
547
|
+
// create the font texture if not done yet
|
|
548
|
+
this.createFontTexture(this.cache);
|
|
549
|
+
}
|
|
550
|
+
return this.fontContext2D;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* restores the canvas context
|
|
555
|
+
*/
|
|
556
|
+
restore() {
|
|
557
|
+
// do nothing if there is no saved states
|
|
558
|
+
if (this._matrixStack.length !== 0) {
|
|
559
|
+
var color = this._colorStack.pop();
|
|
560
|
+
var matrix = this._matrixStack.pop();
|
|
561
|
+
|
|
562
|
+
// restore the previous context
|
|
563
|
+
this.currentColor.copy(color);
|
|
564
|
+
this.currentTransform.copy(matrix);
|
|
565
|
+
|
|
566
|
+
this.setBlendMode(this._blendStack.pop());
|
|
567
|
+
|
|
568
|
+
// recycle objects
|
|
569
|
+
pool.push(color);
|
|
570
|
+
pool.push(matrix);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (this._scissorStack.length !== 0) {
|
|
574
|
+
// FIXME : prevent `scissor` object realloc and GC
|
|
575
|
+
this.currentScissor.set(this._scissorStack.pop());
|
|
576
|
+
} else {
|
|
577
|
+
// turn off scissor test
|
|
578
|
+
this.gl.disable(this.gl.SCISSOR_TEST);
|
|
579
|
+
this.currentScissor[0] = 0;
|
|
580
|
+
this.currentScissor[1] = 0;
|
|
581
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
582
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* saves the canvas context
|
|
588
|
+
*/
|
|
589
|
+
save() {
|
|
590
|
+
this._colorStack.push(this.currentColor.clone());
|
|
591
|
+
this._matrixStack.push(this.currentTransform.clone());
|
|
592
|
+
|
|
593
|
+
if (this.gl.isEnabled(this.gl.SCISSOR_TEST)) {
|
|
594
|
+
// FIXME avoid slice and object realloc
|
|
595
|
+
this._scissorStack.push(this.currentScissor.slice());
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
this._blendStack.push(this.getBlendMode());
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* rotates the uniform matrix
|
|
603
|
+
* @param {number} angle - in radians
|
|
604
|
+
*/
|
|
605
|
+
rotate(angle) {
|
|
606
|
+
this.currentTransform.rotate(angle);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* scales the uniform matrix
|
|
611
|
+
* @param {number} x
|
|
612
|
+
* @param {number} y
|
|
613
|
+
*/
|
|
614
|
+
scale(x, y) {
|
|
615
|
+
this.currentTransform.scale(x, y);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* not used by this renderer?
|
|
620
|
+
* @ignore
|
|
621
|
+
*/
|
|
622
|
+
setAntiAlias(context, enable) {
|
|
623
|
+
super.setAntiAlias(context, enable);
|
|
624
|
+
// TODO: perhaps handle GLNEAREST or other options with texture binding
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Set the global alpha
|
|
629
|
+
* @param {number} alpha - 0.0 to 1.0 values accepted.
|
|
630
|
+
*/
|
|
631
|
+
setGlobalAlpha(alpha) {
|
|
632
|
+
this.currentColor.alpha = alpha;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Return the global alpha
|
|
637
|
+
* @returns {number} global alpha value
|
|
638
|
+
*/
|
|
639
|
+
getGlobalAlpha() {
|
|
640
|
+
return this.currentColor.alpha;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Set the current fill & stroke style color.
|
|
645
|
+
* By default, or upon reset, the value is set to #000000.
|
|
646
|
+
* @param {Color|string} color - css color string.
|
|
647
|
+
*/
|
|
648
|
+
setColor(color) {
|
|
649
|
+
var alpha = this.currentColor.alpha;
|
|
650
|
+
this.currentColor.copy(color);
|
|
651
|
+
this.currentColor.alpha *= alpha;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Set the line width
|
|
656
|
+
* @param {number} width - Line width
|
|
657
|
+
*/
|
|
658
|
+
setLineWidth(width) {
|
|
659
|
+
this.getContext().lineWidth(width);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Stroke an arc at the specified coordinates with given radius, start and end points
|
|
664
|
+
* @param {number} x - arc center point x-axis
|
|
665
|
+
* @param {number} y - arc center point y-axis
|
|
666
|
+
* @param {number} radius
|
|
667
|
+
* @param {number} start - start angle in radians
|
|
668
|
+
* @param {number} end - end angle in radians
|
|
669
|
+
* @param {boolean} [antiClockwise=false] - draw arc anti-clockwise
|
|
670
|
+
* @param {boolean} [fill=false]
|
|
671
|
+
*/
|
|
672
|
+
strokeArc(x, y, radius, start, end, antiClockwise = false, fill = false) {
|
|
673
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
674
|
+
// Fast path: don't draw fully transparent
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
this.path2D.beginPath();
|
|
678
|
+
this.path2D.arc(x, y, radius, start, end, antiClockwise);
|
|
679
|
+
if (fill === false) {
|
|
680
|
+
this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
|
|
681
|
+
} else {
|
|
682
|
+
this.path2D.closePath();
|
|
683
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Fill an arc at the specified coordinates with given radius, start and end points
|
|
689
|
+
* @param {number} x - arc center point x-axis
|
|
690
|
+
* @param {number} y - arc center point y-axis
|
|
691
|
+
* @param {number} radius
|
|
692
|
+
* @param {number} start - start angle in radians
|
|
693
|
+
* @param {number} end - end angle in radians
|
|
694
|
+
* @param {boolean} [antiClockwise=false] - draw arc anti-clockwise
|
|
695
|
+
*/
|
|
696
|
+
fillArc(x, y, radius, start, end, antiClockwise = false) {
|
|
697
|
+
this.strokeArc(x, y, radius, start, end, antiClockwise, true);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Stroke an ellipse at the specified coordinates with given radius
|
|
702
|
+
* @param {number} x - ellipse center point x-axis
|
|
703
|
+
* @param {number} y - ellipse center point y-axis
|
|
704
|
+
* @param {number} w - horizontal radius of the ellipse
|
|
705
|
+
* @param {number} h - vertical radius of the ellipse
|
|
706
|
+
* @param {boolean} [fill=false] - also fill the shape with the current color if true
|
|
707
|
+
*/
|
|
708
|
+
strokeEllipse(x, y, w, h, fill = false) {
|
|
709
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
710
|
+
// Fast path: don't draw fully transparent
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
this.path2D.beginPath();
|
|
714
|
+
this.path2D.ellipse(x, y, w, h, 0, 0, 360);
|
|
715
|
+
this.path2D.closePath();
|
|
716
|
+
if (fill === false) {
|
|
717
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
718
|
+
} else {
|
|
719
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Fill an ellipse at the specified coordinates with given radius
|
|
725
|
+
* @param {number} x - ellipse center point x-axis
|
|
726
|
+
* @param {number} y - ellipse center point y-axis
|
|
727
|
+
* @param {number} w - horizontal radius of the ellipse
|
|
728
|
+
* @param {number} h - vertical radius of the ellipse
|
|
729
|
+
*/
|
|
730
|
+
fillEllipse(x, y, w, h) {
|
|
731
|
+
this.strokeEllipse(x, y, w, h, false);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Stroke a line of the given two points
|
|
736
|
+
* @param {number} startX - the start x coordinate
|
|
737
|
+
* @param {number} startY - the start y coordinate
|
|
738
|
+
* @param {number} endX - the end x coordinate
|
|
739
|
+
* @param {number} endY - the end y coordinate
|
|
740
|
+
*/
|
|
741
|
+
strokeLine(startX, startY, endX, endY) {
|
|
742
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
743
|
+
// Fast path: don't draw fully transparent
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
this.path2D.beginPath();
|
|
747
|
+
this.path2D.moveTo(startX, startY);
|
|
748
|
+
this.path2D.lineTo(endX, endY);
|
|
749
|
+
this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Fill a line of the given two points
|
|
755
|
+
* @param {number} startX - the start x coordinate
|
|
756
|
+
* @param {number} startY - the start y coordinate
|
|
757
|
+
* @param {number} endX - the end x coordinate
|
|
758
|
+
* @param {number} endY - the end y coordinate
|
|
759
|
+
*/
|
|
760
|
+
fillLine(startX, startY, endX, endY) {
|
|
761
|
+
this.strokeLine(startX, startY, endX, endY);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Stroke a me.Polygon on the screen with a specified color
|
|
766
|
+
* @param {Polygon} poly - the shape to draw
|
|
767
|
+
* @param {boolean} [fill=false] - also fill the shape with the current color if true
|
|
768
|
+
*/
|
|
769
|
+
strokePolygon(poly, fill = false) {
|
|
770
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
771
|
+
// Fast path: don't draw fully transparent
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
this.translate(poly.pos.x, poly.pos.y);
|
|
775
|
+
this.path2D.beginPath();
|
|
776
|
+
this.path2D.moveTo(poly.points[0].x, poly.points[0].y);
|
|
777
|
+
var point;
|
|
778
|
+
for (var i = 1; i < poly.points.length; i++) {
|
|
779
|
+
point = poly.points[i];
|
|
780
|
+
this.path2D.lineTo(point.x, point.y);
|
|
781
|
+
}
|
|
782
|
+
this.path2D.lineTo(poly.points[0].x, poly.points[0].y);
|
|
783
|
+
this.path2D.closePath();
|
|
784
|
+
if (fill === false) {
|
|
785
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
786
|
+
} else {
|
|
787
|
+
// draw all triangles
|
|
788
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
789
|
+
}
|
|
790
|
+
this.translate(-poly.pos.x, -poly.pos.y);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Fill a me.Polygon on the screen
|
|
795
|
+
* @param {Polygon} poly - the shape to draw
|
|
796
|
+
*/
|
|
797
|
+
fillPolygon(poly) {
|
|
798
|
+
this.strokePolygon(poly, true);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Draw a stroke rectangle at the specified coordinates
|
|
803
|
+
* @param {number} x
|
|
804
|
+
* @param {number} y
|
|
805
|
+
* @param {number} width
|
|
806
|
+
* @param {number} height
|
|
807
|
+
* @param {boolean} [fill=false] - also fill the shape with the current color if true
|
|
808
|
+
*/
|
|
809
|
+
strokeRect(x, y, width, height, fill = false) {
|
|
810
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
811
|
+
// Fast path: don't draw fully transparent
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
this.path2D.beginPath();
|
|
815
|
+
this.path2D.rect(x, y, width, height);
|
|
816
|
+
if (fill === false) {
|
|
817
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
818
|
+
} else {
|
|
819
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Draw a filled rectangle at the specified coordinates
|
|
825
|
+
* @param {number} x
|
|
826
|
+
* @param {number} y
|
|
827
|
+
* @param {number} width
|
|
828
|
+
* @param {number} height
|
|
829
|
+
*/
|
|
830
|
+
fillRect(x, y, width, height) {
|
|
831
|
+
this.strokeRect(x, y, width, height, true);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Stroke a rounded rectangle at the specified coordinates
|
|
836
|
+
* @param {number} x
|
|
837
|
+
* @param {number} y
|
|
838
|
+
* @param {number} width
|
|
839
|
+
* @param {number} height
|
|
840
|
+
* @param {number} radius
|
|
841
|
+
* @param {boolean} [fill=false] - also fill the shape with the current color if true
|
|
842
|
+
*/
|
|
843
|
+
strokeRoundRect(x, y, width, height, radius, fill = false) {
|
|
844
|
+
if (this.getGlobalAlpha() < 1 / 255) {
|
|
845
|
+
// Fast path: don't draw fully transparent
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
this.path2D.beginPath();
|
|
849
|
+
this.path2D.roundRect(x, y, width, height, radius);
|
|
850
|
+
if (fill === false) {
|
|
851
|
+
this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
|
|
852
|
+
} else {
|
|
853
|
+
this.path2D.closePath();
|
|
854
|
+
this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* Draw a rounded filled rectangle at the specified coordinates
|
|
860
|
+
* @param {number} x
|
|
861
|
+
* @param {number} y
|
|
862
|
+
* @param {number} width
|
|
863
|
+
* @param {number} height
|
|
864
|
+
* @param {number} radius
|
|
865
|
+
*/
|
|
866
|
+
fillRoundRect(x, y, width, height, radius) {
|
|
867
|
+
this.strokeRoundRect(x, y, width, height, radius, true);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Stroke a Point at the specified coordinates
|
|
872
|
+
* @param {number} x
|
|
873
|
+
* @param {number} y
|
|
874
|
+
*/
|
|
875
|
+
strokePoint(x, y) {
|
|
876
|
+
this.strokeLine(x, y, x + 1, y + 1);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Draw a a point at the specified coordinates
|
|
881
|
+
* @param {number} x
|
|
882
|
+
* @param {number} y
|
|
883
|
+
* @param {number} width
|
|
884
|
+
* @param {number} height
|
|
885
|
+
*/
|
|
886
|
+
fillPoint(x, y) {
|
|
887
|
+
this.strokePoint(x, y);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Reset (overrides) the renderer transformation matrix to the
|
|
892
|
+
* identity one, and then apply the given transformation matrix.
|
|
893
|
+
* @param {Matrix2d} mat2d - Matrix to transform by
|
|
894
|
+
*/
|
|
895
|
+
setTransform(mat2d) {
|
|
896
|
+
this.resetTransform();
|
|
897
|
+
this.transform(mat2d);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Multiply given matrix into the renderer tranformation matrix
|
|
902
|
+
* @param {Matrix2d} mat2d - Matrix to transform by
|
|
903
|
+
*/
|
|
904
|
+
transform(mat2d) {
|
|
905
|
+
var currentTransform = this.currentTransform;
|
|
906
|
+
currentTransform.multiply(mat2d);
|
|
907
|
+
if (this.settings.subPixel === false) {
|
|
908
|
+
// snap position values to pixel grid
|
|
909
|
+
var a = currentTransform.toArray();
|
|
910
|
+
a[6] |= 0;
|
|
911
|
+
a[7] |= 0;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Translates the uniform matrix by the given coordinates
|
|
917
|
+
* @param {number} x
|
|
918
|
+
* @param {number} y
|
|
919
|
+
*/
|
|
920
|
+
translate(x, y) {
|
|
921
|
+
var currentTransform = this.currentTransform;
|
|
922
|
+
currentTransform.translate(x, y);
|
|
923
|
+
if (this.settings.subPixel === false) {
|
|
924
|
+
// snap position values to pixel grid
|
|
925
|
+
var a = currentTransform.toArray();
|
|
926
|
+
a[6] |= 0;
|
|
927
|
+
a[7] |= 0;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* clip the given region from the original canvas. Once a region is clipped,
|
|
933
|
+
* all future drawing will be limited to the clipped region.
|
|
934
|
+
* You can however save the current region using the save(),
|
|
935
|
+
* and restore it (with the restore() method) any time in the future.
|
|
936
|
+
* (<u>this is an experimental feature !</u>)
|
|
937
|
+
* @param {number} x
|
|
938
|
+
* @param {number} y
|
|
939
|
+
* @param {number} width
|
|
940
|
+
* @param {number} height
|
|
941
|
+
*/
|
|
942
|
+
clipRect(x, y, width, height) {
|
|
943
|
+
var canvas = this.getCanvas();
|
|
944
|
+
var gl = this.gl;
|
|
945
|
+
// if requested box is different from the current canvas size
|
|
946
|
+
if (x !== 0 || y !== 0 || width !== canvas.width || height !== canvas.height) {
|
|
947
|
+
var currentScissor = this.currentScissor;
|
|
948
|
+
if (gl.isEnabled(gl.SCISSOR_TEST)) {
|
|
949
|
+
// if same as the current scissor box do nothing
|
|
950
|
+
if (currentScissor[0] === x && currentScissor[1] === y &&
|
|
951
|
+
currentScissor[2] === width && currentScissor[3] === height) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
// flush the compositor
|
|
956
|
+
this.flush();
|
|
957
|
+
// turn on scissor test
|
|
958
|
+
gl.enable(this.gl.SCISSOR_TEST);
|
|
959
|
+
// set the scissor rectangle (note : coordinates are left/bottom)
|
|
960
|
+
gl.scissor(
|
|
961
|
+
// scissor does not account for currentTransform, so manually adjust
|
|
962
|
+
x + this.currentTransform.tx,
|
|
963
|
+
canvas.height -height -y -this.currentTransform.ty,
|
|
964
|
+
width,
|
|
965
|
+
height
|
|
966
|
+
);
|
|
967
|
+
// save the new currentScissor box
|
|
968
|
+
currentScissor[0] = x;
|
|
969
|
+
currentScissor[1] = y;
|
|
970
|
+
currentScissor[2] = width;
|
|
971
|
+
currentScissor[3] = height;
|
|
972
|
+
} else {
|
|
973
|
+
// turn off scissor test
|
|
974
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* A mask limits rendering elements to the shape and position of the given mask object.
|
|
980
|
+
* So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
|
|
981
|
+
* Mask are not preserved through renderer context save and restore.
|
|
982
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] - a shape defining the mask to be applied
|
|
983
|
+
* @param {boolean} [invert=false] - either the given shape should define what is visible (default) or the opposite
|
|
984
|
+
*/
|
|
985
|
+
setMask(mask, invert = false) {
|
|
986
|
+
var gl = this.gl;
|
|
987
|
+
|
|
988
|
+
// flush the compositor
|
|
989
|
+
this.flush();
|
|
990
|
+
|
|
991
|
+
if (this.maskLevel === 0) {
|
|
992
|
+
// Enable and setup GL state to write to stencil buffer
|
|
993
|
+
gl.enable(gl.STENCIL_TEST);
|
|
994
|
+
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
this.maskLevel++;
|
|
998
|
+
|
|
999
|
+
gl.colorMask(false, false, false, false);
|
|
1000
|
+
gl.stencilFunc(gl.EQUAL, this.maskLevel, 1);
|
|
1001
|
+
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
// fill the given mask shape
|
|
1005
|
+
this.fill(mask);
|
|
1006
|
+
|
|
1007
|
+
// flush the compositor
|
|
1008
|
+
this.flush();
|
|
1009
|
+
|
|
1010
|
+
gl.colorMask(true, true, true, true);
|
|
1011
|
+
|
|
1012
|
+
// Use stencil buffer to affect next rendering object
|
|
1013
|
+
if (invert === true) {
|
|
1014
|
+
gl.stencilFunc(gl.EQUAL, this.maskLevel + 1, 1);
|
|
1015
|
+
} else {
|
|
1016
|
+
gl.stencilFunc(gl.NOTEQUAL, this.maskLevel + 1, 1);
|
|
1017
|
+
}
|
|
1018
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* disable (remove) the rendering mask set through setMask.
|
|
1023
|
+
* @see WebGLRenderer#setMask
|
|
1024
|
+
*/
|
|
1025
|
+
clearMask() {
|
|
1026
|
+
if (this.maskLevel > 0) {
|
|
1027
|
+
// flush the compositor
|
|
1028
|
+
this.flush();
|
|
1029
|
+
this.maskLevel = 0;
|
|
1030
|
+
this.gl.disable(this.gl.STENCIL_TEST);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
export { WebGLRenderer as default };
|