@vpmedia/phaser 1.0.1 → 1.0.2
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/phaser.cjs.LICENSE.txt +1 -1
- package/dist/phaser.js.LICENSE.txt +1 -1
- package/package.json +2 -3
- package/src/index.js +99 -0
- package/src/phaser/core/animation.js +355 -0
- package/src/phaser/core/animation_manager.js +238 -0
- package/src/phaser/core/animation_parser.js +130 -0
- package/src/phaser/core/array_set.js +108 -0
- package/src/phaser/core/cache.js +558 -0
- package/src/phaser/core/const.js +106 -0
- package/src/phaser/core/device.js +67 -0
- package/src/phaser/core/device_util.js +386 -0
- package/src/phaser/core/dom.js +207 -0
- package/src/phaser/core/event_manager.js +243 -0
- package/src/phaser/core/factory.js +74 -0
- package/src/phaser/core/frame.js +75 -0
- package/src/phaser/core/frame_data.js +84 -0
- package/src/phaser/core/frame_util.js +31 -0
- package/src/phaser/core/game.js +412 -0
- package/src/phaser/core/input.js +401 -0
- package/src/phaser/core/input_button.js +102 -0
- package/src/phaser/core/input_handler.js +687 -0
- package/src/phaser/core/input_mouse.js +289 -0
- package/src/phaser/core/input_mspointer.js +197 -0
- package/src/phaser/core/input_pointer.js +427 -0
- package/src/phaser/core/input_touch.js +157 -0
- package/src/phaser/core/loader.js +946 -0
- package/src/phaser/core/loader_parser.js +105 -0
- package/src/phaser/core/raf.js +46 -0
- package/src/phaser/core/raf_fb.js +75 -0
- package/src/phaser/core/raf_to.js +34 -0
- package/src/phaser/core/scale_manager.js +806 -0
- package/src/phaser/core/scene.js +66 -0
- package/src/phaser/core/scene_manager.js +310 -0
- package/src/phaser/core/signal.js +175 -0
- package/src/phaser/core/signal_binding.js +69 -0
- package/src/phaser/core/sound.js +538 -0
- package/src/phaser/core/sound_manager.js +365 -0
- package/src/phaser/core/stage.js +108 -0
- package/src/phaser/core/time.js +203 -0
- package/src/phaser/core/timer.js +276 -0
- package/src/phaser/core/timer_event.js +21 -0
- package/src/phaser/core/tween.js +329 -0
- package/src/phaser/core/tween_data.js +258 -0
- package/src/phaser/core/tween_easing.js +316 -0
- package/src/phaser/core/tween_manager.js +185 -0
- package/src/phaser/core/world.js +18 -0
- package/src/phaser/display/bitmap_text.js +322 -0
- package/src/phaser/display/button.js +194 -0
- package/src/phaser/display/canvas/buffer.js +36 -0
- package/src/phaser/display/canvas/graphics.js +227 -0
- package/src/phaser/display/canvas/masker.js +39 -0
- package/src/phaser/display/canvas/pool.js +121 -0
- package/src/phaser/display/canvas/renderer.js +123 -0
- package/src/phaser/display/canvas/tinter.js +141 -0
- package/src/phaser/display/canvas/util.js +151 -0
- package/src/phaser/display/display_object.js +597 -0
- package/src/phaser/display/graphics.js +723 -0
- package/src/phaser/display/graphics_data.js +27 -0
- package/src/phaser/display/graphics_data_util.js +14 -0
- package/src/phaser/display/group.js +227 -0
- package/src/phaser/display/image.js +288 -0
- package/src/phaser/display/sprite_batch.js +15 -0
- package/src/phaser/display/sprite_util.js +248 -0
- package/src/phaser/display/text.js +1089 -0
- package/src/phaser/display/webgl/abstract_filter.js +25 -0
- package/src/phaser/display/webgl/base_texture.js +68 -0
- package/src/phaser/display/webgl/blend_manager.js +35 -0
- package/src/phaser/display/webgl/earcut.js +647 -0
- package/src/phaser/display/webgl/earcut_node.js +28 -0
- package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
- package/src/phaser/display/webgl/filter_manager.js +46 -0
- package/src/phaser/display/webgl/filter_texture.js +61 -0
- package/src/phaser/display/webgl/graphics.js +618 -0
- package/src/phaser/display/webgl/graphics_data.js +42 -0
- package/src/phaser/display/webgl/mask_manager.js +36 -0
- package/src/phaser/display/webgl/render_texture.js +81 -0
- package/src/phaser/display/webgl/renderer.js +234 -0
- package/src/phaser/display/webgl/shader/complex.js +74 -0
- package/src/phaser/display/webgl/shader/fast.js +97 -0
- package/src/phaser/display/webgl/shader/normal.js +225 -0
- package/src/phaser/display/webgl/shader/primitive.js +72 -0
- package/src/phaser/display/webgl/shader/strip.js +77 -0
- package/src/phaser/display/webgl/shader_manager.js +89 -0
- package/src/phaser/display/webgl/sprite_batch.js +320 -0
- package/src/phaser/display/webgl/stencil_manager.js +170 -0
- package/src/phaser/display/webgl/texture.js +117 -0
- package/src/phaser/display/webgl/texture_util.js +32 -0
- package/src/phaser/display/webgl/util.js +74 -0
- package/src/phaser/geom/circle.js +186 -0
- package/src/phaser/geom/ellipse.js +65 -0
- package/src/phaser/geom/line.js +190 -0
- package/src/phaser/geom/matrix.js +147 -0
- package/src/phaser/geom/point.js +164 -0
- package/src/phaser/geom/polygon.js +141 -0
- package/src/phaser/geom/rectangle.js +306 -0
- package/src/phaser/geom/rounded_rectangle.js +36 -0
- package/src/phaser/geom/util/circle.js +115 -0
- package/src/phaser/geom/util/ellipse.js +30 -0
- package/src/phaser/geom/util/line.js +130 -0
- package/src/phaser/geom/util/matrix.js +48 -0
- package/src/phaser/geom/util/point.js +276 -0
- package/src/phaser/geom/util/polygon.js +24 -0
- package/src/phaser/geom/util/rectangle.js +212 -0
- package/src/phaser/geom/util/rounded_rectangle.js +28 -0
- package/src/phaser/util/math.js +279 -0
- package/src/phaser/util/string.js +26 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
5
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
6
|
+
*/
|
|
7
|
+
import AbstractFilter from './abstract_filter';
|
|
8
|
+
import NormalShader from './shader/normal';
|
|
9
|
+
|
|
10
|
+
// TODO: fix ++ +=1 conversion issues (when a is 0 than a++ is 0 but a+=1 is 1)
|
|
11
|
+
|
|
12
|
+
export default class {
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
this.vertSize = 5;
|
|
16
|
+
this.size = 2000; // Math.pow(2, 16) / this.vertSize;
|
|
17
|
+
// the total number of bytes in our batch
|
|
18
|
+
const numVerts = this.size * 4 * 4 * this.vertSize;
|
|
19
|
+
// the total number of indices in our batch
|
|
20
|
+
const numIndices = this.size * 6;
|
|
21
|
+
this.vertices = new ArrayBuffer(numVerts);
|
|
22
|
+
this.positions = new Float32Array(this.vertices);
|
|
23
|
+
this.colors = new Uint32Array(this.vertices);
|
|
24
|
+
this.indices = new Uint16Array(numIndices);
|
|
25
|
+
this.lastIndexCount = 0;
|
|
26
|
+
for (let i = 0, j = 0; i < numIndices; i += 6, j += 4) {
|
|
27
|
+
this.indices[i + 0] = j + 0;
|
|
28
|
+
this.indices[i + 1] = j + 1;
|
|
29
|
+
this.indices[i + 2] = j + 2;
|
|
30
|
+
this.indices[i + 3] = j + 0;
|
|
31
|
+
this.indices[i + 4] = j + 2;
|
|
32
|
+
this.indices[i + 5] = j + 3;
|
|
33
|
+
}
|
|
34
|
+
this.drawing = false;
|
|
35
|
+
this.currentBatchSize = 0;
|
|
36
|
+
this.currentBaseTexture = null;
|
|
37
|
+
this.dirty = true;
|
|
38
|
+
this.textures = [];
|
|
39
|
+
this.blendModes = [];
|
|
40
|
+
this.shaders = [];
|
|
41
|
+
this.sprites = [];
|
|
42
|
+
this.defaultShader = new AbstractFilter([
|
|
43
|
+
'precision lowp float;',
|
|
44
|
+
'varying vec2 vTextureCoord;',
|
|
45
|
+
'varying vec4 vColor;',
|
|
46
|
+
'uniform sampler2D uSampler;',
|
|
47
|
+
'void main(void) {',
|
|
48
|
+
' gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor ;',
|
|
49
|
+
'}',
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setContext(gl) {
|
|
54
|
+
this.gl = gl;
|
|
55
|
+
// create a couple of buffers
|
|
56
|
+
this.vertexBuffer = gl.createBuffer();
|
|
57
|
+
this.indexBuffer = gl.createBuffer();
|
|
58
|
+
// 65535 is max index, so 65535 / 6 = 10922.
|
|
59
|
+
// upload the index data
|
|
60
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
|
|
61
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indices, gl.STATIC_DRAW);
|
|
62
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
63
|
+
gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.DYNAMIC_DRAW);
|
|
64
|
+
this.currentBlendMode = 99999;
|
|
65
|
+
const shader = new NormalShader(gl);
|
|
66
|
+
shader.fragmentSrc = this.defaultShader.fragmentSrc;
|
|
67
|
+
shader.uniforms = {};
|
|
68
|
+
shader.init();
|
|
69
|
+
this.defaultShader.shaders[gl.id] = shader;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
begin(renderSession) {
|
|
73
|
+
this.renderSession = renderSession;
|
|
74
|
+
this.shader = this.renderSession.shaderManager.defaultShader;
|
|
75
|
+
this.start();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
end() {
|
|
79
|
+
this.flush();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
render(sprite, matrix) {
|
|
83
|
+
const texture = sprite.texture;
|
|
84
|
+
// They provided an alternative rendering matrix, so use it
|
|
85
|
+
let wt = sprite.worldTransform;
|
|
86
|
+
if (matrix) {
|
|
87
|
+
wt = matrix;
|
|
88
|
+
}
|
|
89
|
+
// check texture..
|
|
90
|
+
if (this.currentBatchSize >= this.size) {
|
|
91
|
+
this.flush();
|
|
92
|
+
this.currentBaseTexture = texture.baseTexture;
|
|
93
|
+
}
|
|
94
|
+
// get the uvs for the texture
|
|
95
|
+
const uvs = texture._uvs;
|
|
96
|
+
// if the uvs have not updated then no point rendering just yet!
|
|
97
|
+
if (!uvs) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const aX = sprite.anchor.x;
|
|
101
|
+
const aY = sprite.anchor.y;
|
|
102
|
+
let w0;
|
|
103
|
+
let w1;
|
|
104
|
+
let h0;
|
|
105
|
+
let h1;
|
|
106
|
+
if (texture.trim) {
|
|
107
|
+
// if the sprite is trimmed then we need to add the extra space before transforming the sprite coords.
|
|
108
|
+
const trim = texture.trim;
|
|
109
|
+
w1 = trim.x - aX * trim.width;
|
|
110
|
+
w0 = w1 + texture.crop.width;
|
|
111
|
+
h1 = trim.y - aY * trim.height;
|
|
112
|
+
h0 = h1 + texture.crop.height;
|
|
113
|
+
} else {
|
|
114
|
+
w0 = (texture.frame.width) * (1 - aX);
|
|
115
|
+
w1 = (texture.frame.width) * -aX;
|
|
116
|
+
h0 = texture.frame.height * (1 - aY);
|
|
117
|
+
h1 = texture.frame.height * -aY;
|
|
118
|
+
}
|
|
119
|
+
const i = this.currentBatchSize * 4 * this.vertSize;
|
|
120
|
+
const resolution = texture.baseTexture.resolution;
|
|
121
|
+
const a = wt.a / resolution;
|
|
122
|
+
const b = wt.b / resolution;
|
|
123
|
+
const c = wt.c / resolution;
|
|
124
|
+
const d = wt.d / resolution;
|
|
125
|
+
const tx = wt.tx;
|
|
126
|
+
const ty = wt.ty;
|
|
127
|
+
const colors = this.colors;
|
|
128
|
+
const positions = this.positions;
|
|
129
|
+
if (this.renderSession.roundPixels) {
|
|
130
|
+
// xy
|
|
131
|
+
positions[i] = a * w1 + c * h1 + tx | 0;
|
|
132
|
+
positions[i + 1] = d * h1 + b * w1 + ty | 0;
|
|
133
|
+
// xy
|
|
134
|
+
positions[i + 5] = a * w0 + c * h1 + tx | 0;
|
|
135
|
+
positions[i + 6] = d * h1 + b * w0 + ty | 0;
|
|
136
|
+
// xy
|
|
137
|
+
positions[i + 10] = a * w0 + c * h0 + tx | 0;
|
|
138
|
+
positions[i + 11] = d * h0 + b * w0 + ty | 0;
|
|
139
|
+
// xy
|
|
140
|
+
positions[i + 15] = a * w1 + c * h0 + tx | 0;
|
|
141
|
+
positions[i + 16] = d * h0 + b * w1 + ty | 0;
|
|
142
|
+
} else {
|
|
143
|
+
// xy
|
|
144
|
+
positions[i] = a * w1 + c * h1 + tx;
|
|
145
|
+
positions[i + 1] = d * h1 + b * w1 + ty;
|
|
146
|
+
// xy
|
|
147
|
+
positions[i + 5] = a * w0 + c * h1 + tx;
|
|
148
|
+
positions[i + 6] = d * h1 + b * w0 + ty;
|
|
149
|
+
// xy
|
|
150
|
+
positions[i + 10] = a * w0 + c * h0 + tx;
|
|
151
|
+
positions[i + 11] = d * h0 + b * w0 + ty;
|
|
152
|
+
// xy
|
|
153
|
+
positions[i + 15] = a * w1 + c * h0 + tx;
|
|
154
|
+
positions[i + 16] = d * h0 + b * w1 + ty;
|
|
155
|
+
}
|
|
156
|
+
// uv
|
|
157
|
+
positions[i + 2] = uvs.x0;
|
|
158
|
+
positions[i + 3] = uvs.y0;
|
|
159
|
+
// uv
|
|
160
|
+
positions[i + 7] = uvs.x1;
|
|
161
|
+
positions[i + 8] = uvs.y1;
|
|
162
|
+
// uv
|
|
163
|
+
positions[i + 12] = uvs.x2;
|
|
164
|
+
positions[i + 13] = uvs.y2;
|
|
165
|
+
// uv
|
|
166
|
+
positions[i + 17] = uvs.x3;
|
|
167
|
+
positions[i + 18] = uvs.y3;
|
|
168
|
+
// color and alpha
|
|
169
|
+
const tint = sprite.tint;
|
|
170
|
+
colors[i + 4] = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16) + (sprite.worldAlpha * 255 << 24);
|
|
171
|
+
colors[i + 9] = colors[i + 4];
|
|
172
|
+
colors[i + 14] = colors[i + 4];
|
|
173
|
+
colors[i + 19] = colors[i + 4];
|
|
174
|
+
// increment the batchsize
|
|
175
|
+
this.sprites[this.currentBatchSize] = sprite;
|
|
176
|
+
this.currentBatchSize += 1;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
renderTilingSprite() {
|
|
180
|
+
// TODO
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
flush() {
|
|
184
|
+
// If the batch is length 0 then return as there is nothing to draw
|
|
185
|
+
if (this.currentBatchSize === 0) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const gl = this.gl;
|
|
189
|
+
let shader;
|
|
190
|
+
if (this.dirty) {
|
|
191
|
+
this.dirty = false;
|
|
192
|
+
// bind the main texture
|
|
193
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
194
|
+
// bind the buffers
|
|
195
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
196
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
|
|
197
|
+
shader = this.defaultShader.shaders[gl.id];
|
|
198
|
+
// this is the same for each shader?
|
|
199
|
+
const stride = this.vertSize * 4;
|
|
200
|
+
gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, stride, 0);
|
|
201
|
+
gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4);
|
|
202
|
+
// color attributes will be interpreted as unsigned bytes and normalized
|
|
203
|
+
gl.vertexAttribPointer(shader.colorAttribute, 4, gl.UNSIGNED_BYTE, true, stride, 4 * 4);
|
|
204
|
+
}
|
|
205
|
+
// upload the verts to the buffer
|
|
206
|
+
if (this.currentBatchSize > (this.size * 0.5)) {
|
|
207
|
+
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices);
|
|
208
|
+
} else {
|
|
209
|
+
const view = this.positions.subarray(0, this.currentBatchSize * 4 * this.vertSize);
|
|
210
|
+
gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
|
|
211
|
+
}
|
|
212
|
+
let nextTexture;
|
|
213
|
+
let nextBlendMode;
|
|
214
|
+
let nextShader;
|
|
215
|
+
let batchSize = 0;
|
|
216
|
+
let start = 0;
|
|
217
|
+
let currentBaseTexture = null;
|
|
218
|
+
let currentBlendMode = this.renderSession.blendModeManager.currentBlendMode;
|
|
219
|
+
let currentShader = null;
|
|
220
|
+
let blendSwap = false;
|
|
221
|
+
let shaderSwap = false;
|
|
222
|
+
let sprite;
|
|
223
|
+
for (let i = 0, j = this.currentBatchSize; i < j; i += 1) {
|
|
224
|
+
sprite = this.sprites[i];
|
|
225
|
+
if (sprite.tilingTexture) {
|
|
226
|
+
nextTexture = sprite.tilingTexture.baseTexture;
|
|
227
|
+
} else {
|
|
228
|
+
nextTexture = sprite.texture.baseTexture;
|
|
229
|
+
}
|
|
230
|
+
nextBlendMode = sprite.blendMode;
|
|
231
|
+
nextShader = sprite.shader || this.defaultShader;
|
|
232
|
+
blendSwap = currentBlendMode !== nextBlendMode;
|
|
233
|
+
shaderSwap = !currentShader || !nextShader || currentShader._UID !== nextShader._UID;
|
|
234
|
+
let skip = nextTexture.skipRender;
|
|
235
|
+
if (skip && sprite.children.length > 0) {
|
|
236
|
+
skip = false;
|
|
237
|
+
}
|
|
238
|
+
if ((currentBaseTexture !== nextTexture && !skip) || blendSwap || shaderSwap) {
|
|
239
|
+
this.renderBatch(currentBaseTexture, batchSize, start);
|
|
240
|
+
start = i;
|
|
241
|
+
batchSize = 0;
|
|
242
|
+
currentBaseTexture = nextTexture;
|
|
243
|
+
if (blendSwap) {
|
|
244
|
+
currentBlendMode = nextBlendMode;
|
|
245
|
+
this.renderSession.blendModeManager.setBlendMode(currentBlendMode);
|
|
246
|
+
}
|
|
247
|
+
if (shaderSwap) {
|
|
248
|
+
currentShader = nextShader;
|
|
249
|
+
shader = currentShader.shaders[gl.id];
|
|
250
|
+
if (!shader) {
|
|
251
|
+
shader = new NormalShader(gl);
|
|
252
|
+
shader.fragmentSrc = currentShader.fragmentSrc;
|
|
253
|
+
shader.uniforms = currentShader.uniforms;
|
|
254
|
+
shader.init();
|
|
255
|
+
currentShader.shaders[gl.id] = shader;
|
|
256
|
+
}
|
|
257
|
+
// set shader function???
|
|
258
|
+
this.renderSession.shaderManager.setShader(shader);
|
|
259
|
+
if (shader.dirty) {
|
|
260
|
+
shader.syncUniforms();
|
|
261
|
+
}
|
|
262
|
+
// both these only need to be set if they are changing..
|
|
263
|
+
// set the projection
|
|
264
|
+
const projection = this.renderSession.projection;
|
|
265
|
+
gl.uniform2f(shader.projectionVector, projection.x, projection.y);
|
|
266
|
+
// TODO - this is temporary!
|
|
267
|
+
const offsetVector = this.renderSession.offset;
|
|
268
|
+
gl.uniform2f(shader.offsetVector, offsetVector.x, offsetVector.y);
|
|
269
|
+
// set the pointers
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
batchSize += 1;
|
|
273
|
+
}
|
|
274
|
+
this.renderBatch(currentBaseTexture, batchSize, start);
|
|
275
|
+
// then reset the batch!
|
|
276
|
+
this.currentBatchSize = 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
renderBatch(texture, size, startIndex) {
|
|
280
|
+
if (size === 0) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const gl = this.gl;
|
|
284
|
+
// check if a texture is dirty..
|
|
285
|
+
if (texture._dirty[gl.id]) {
|
|
286
|
+
if (!this.renderSession.renderer.updateTexture(texture)) {
|
|
287
|
+
// If updateTexture returns false then we cannot render it, so bail out now
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
} else {
|
|
291
|
+
// bind the current texture
|
|
292
|
+
gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]);
|
|
293
|
+
}
|
|
294
|
+
// now draw those suckas!
|
|
295
|
+
gl.drawElements(gl.TRIANGLES, size * 6, gl.UNSIGNED_SHORT, startIndex * 6 * 2);
|
|
296
|
+
// increment the draw count
|
|
297
|
+
this.renderSession.drawCount += 1;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
stop() {
|
|
301
|
+
this.flush();
|
|
302
|
+
this.dirty = true;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
start() {
|
|
306
|
+
this.dirty = true;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
destroy() {
|
|
310
|
+
this.vertices = null;
|
|
311
|
+
this.positions = null;
|
|
312
|
+
this.colors = null;
|
|
313
|
+
this.indices = null;
|
|
314
|
+
this.gl.deleteBuffer(this.vertexBuffer);
|
|
315
|
+
this.gl.deleteBuffer(this.indexBuffer);
|
|
316
|
+
this.currentBaseTexture = null;
|
|
317
|
+
this.gl = null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
5
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
6
|
+
*/
|
|
7
|
+
import { hex2rgb } from '../../util/math';
|
|
8
|
+
|
|
9
|
+
export default class {
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
this.stencilStack = [];
|
|
13
|
+
this.reverse = true;
|
|
14
|
+
this.count = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setContext(gl) {
|
|
18
|
+
this.gl = gl;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
destroy() {
|
|
22
|
+
this.stencilStack = null;
|
|
23
|
+
this.gl = null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
pushStencil(graphics, webGLData, renderSession) {
|
|
27
|
+
const gl = renderSession.gl;
|
|
28
|
+
this.bindGraphics(graphics, webGLData, renderSession);
|
|
29
|
+
if (this.stencilStack.length === 0) {
|
|
30
|
+
gl.enable(gl.STENCIL_TEST);
|
|
31
|
+
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
32
|
+
this.reverse = true;
|
|
33
|
+
this.count = 0;
|
|
34
|
+
}
|
|
35
|
+
this.stencilStack.push(webGLData);
|
|
36
|
+
const level = this.count;
|
|
37
|
+
gl.colorMask(false, false, false, false);
|
|
38
|
+
gl.stencilFunc(gl.ALWAYS, 0, 0xFF);
|
|
39
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INVERT);
|
|
40
|
+
// draw the triangle strip!
|
|
41
|
+
if (webGLData.mode === 1) {
|
|
42
|
+
gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0);
|
|
43
|
+
if (this.reverse) {
|
|
44
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - level, 0xFF);
|
|
45
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
|
|
46
|
+
} else {
|
|
47
|
+
gl.stencilFunc(gl.EQUAL, level, 0xFF);
|
|
48
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
49
|
+
}
|
|
50
|
+
// draw a quad to increment..
|
|
51
|
+
gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, (webGLData.indices.length - 4) * 2);
|
|
52
|
+
if (this.reverse) {
|
|
53
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level + 1), 0xFF);
|
|
54
|
+
} else {
|
|
55
|
+
gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
|
|
56
|
+
}
|
|
57
|
+
this.reverse = !this.reverse;
|
|
58
|
+
} else {
|
|
59
|
+
if (!this.reverse) {
|
|
60
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - level, 0xFF);
|
|
61
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
|
|
62
|
+
} else {
|
|
63
|
+
gl.stencilFunc(gl.EQUAL, level, 0xFF);
|
|
64
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
65
|
+
}
|
|
66
|
+
gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0);
|
|
67
|
+
if (!this.reverse) {
|
|
68
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level + 1), 0xFF);
|
|
69
|
+
} else {
|
|
70
|
+
gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
gl.colorMask(true, true, true, true);
|
|
74
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
75
|
+
this.count += 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
bindGraphics(graphics, webGLData, renderSession) {
|
|
79
|
+
// if(this._currentGraphics === graphics)return;
|
|
80
|
+
// this._currentGraphics = graphics;
|
|
81
|
+
const gl = renderSession.gl;
|
|
82
|
+
// bind the graphics object..
|
|
83
|
+
const projection = renderSession.projection;
|
|
84
|
+
const offset = renderSession.offset;
|
|
85
|
+
let shader; // = renderSession.shaderManager.primitiveShader;
|
|
86
|
+
if (webGLData.mode === 1) {
|
|
87
|
+
shader = renderSession.shaderManager.complexPrimitiveShader;
|
|
88
|
+
renderSession.shaderManager.setShader(shader);
|
|
89
|
+
gl.uniform1f(shader.flipY, renderSession.flipY);
|
|
90
|
+
gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true));
|
|
91
|
+
gl.uniform2f(shader.projectionVector, projection.x, -projection.y);
|
|
92
|
+
gl.uniform2f(shader.offsetVector, -offset.x, -offset.y);
|
|
93
|
+
gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint));
|
|
94
|
+
gl.uniform3fv(shader.color, webGLData.color);
|
|
95
|
+
gl.uniform1f(shader.alpha, graphics.worldAlpha * webGLData.alpha);
|
|
96
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer);
|
|
97
|
+
gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, 4 * 2, 0);
|
|
98
|
+
// now do the rest..
|
|
99
|
+
// set the index buffer!
|
|
100
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer);
|
|
101
|
+
} else {
|
|
102
|
+
// renderSession.shaderManager.activatePrimitiveShader();
|
|
103
|
+
shader = renderSession.shaderManager.primitiveShader;
|
|
104
|
+
renderSession.shaderManager.setShader(shader);
|
|
105
|
+
gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true));
|
|
106
|
+
gl.uniform1f(shader.flipY, renderSession.flipY);
|
|
107
|
+
gl.uniform2f(shader.projectionVector, projection.x, -projection.y);
|
|
108
|
+
gl.uniform2f(shader.offsetVector, -offset.x, -offset.y);
|
|
109
|
+
gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint));
|
|
110
|
+
gl.uniform1f(shader.alpha, graphics.worldAlpha);
|
|
111
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer);
|
|
112
|
+
gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, 4 * 6, 0);
|
|
113
|
+
gl.vertexAttribPointer(shader.colorAttribute, 4, gl.FLOAT, false, 4 * 6, 2 * 4);
|
|
114
|
+
// set the index buffer!
|
|
115
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
popStencil(graphics, webGLData, renderSession) {
|
|
120
|
+
const gl = renderSession.gl;
|
|
121
|
+
this.stencilStack.pop();
|
|
122
|
+
this.count -= 1;
|
|
123
|
+
if (this.stencilStack.length === 0) {
|
|
124
|
+
// the stack is empty!
|
|
125
|
+
gl.disable(gl.STENCIL_TEST);
|
|
126
|
+
} else {
|
|
127
|
+
const level = this.count;
|
|
128
|
+
this.bindGraphics(graphics, webGLData, renderSession);
|
|
129
|
+
gl.colorMask(false, false, false, false);
|
|
130
|
+
if (webGLData.mode === 1) {
|
|
131
|
+
this.reverse = !this.reverse;
|
|
132
|
+
if (this.reverse) {
|
|
133
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level + 1), 0xFF);
|
|
134
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
135
|
+
} else {
|
|
136
|
+
gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
|
|
137
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
|
|
138
|
+
}
|
|
139
|
+
// draw a quad to increment..
|
|
140
|
+
gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, (webGLData.indices.length - 4) * 2);
|
|
141
|
+
gl.stencilFunc(gl.ALWAYS, 0, 0xFF);
|
|
142
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INVERT);
|
|
143
|
+
// draw the triangle strip!
|
|
144
|
+
gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0);
|
|
145
|
+
if (!this.reverse) {
|
|
146
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level), 0xFF);
|
|
147
|
+
} else {
|
|
148
|
+
gl.stencilFunc(gl.EQUAL, level, 0xFF);
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
if (!this.reverse) {
|
|
152
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level + 1), 0xFF);
|
|
153
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
|
|
154
|
+
} else {
|
|
155
|
+
gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
|
|
156
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
|
|
157
|
+
}
|
|
158
|
+
gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0);
|
|
159
|
+
if (!this.reverse) {
|
|
160
|
+
gl.stencilFunc(gl.EQUAL, 0xFF - (level), 0xFF);
|
|
161
|
+
} else {
|
|
162
|
+
gl.stencilFunc(gl.EQUAL, level, 0xFF);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
gl.colorMask(true, true, true, true);
|
|
166
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
5
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
6
|
+
*/
|
|
7
|
+
import Rectangle from '../../geom/rectangle';
|
|
8
|
+
|
|
9
|
+
export class TextureUvs {
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
this.x0 = 0;
|
|
13
|
+
this.y0 = 0;
|
|
14
|
+
this.x1 = 0;
|
|
15
|
+
this.y1 = 0;
|
|
16
|
+
this.x2 = 0;
|
|
17
|
+
this.y2 = 0;
|
|
18
|
+
this.x3 = 0;
|
|
19
|
+
this.y3 = 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class {
|
|
25
|
+
|
|
26
|
+
constructor(baseTexture, frame, crop, trim) {
|
|
27
|
+
this.noFrame = false;
|
|
28
|
+
if (!frame) {
|
|
29
|
+
this.noFrame = true;
|
|
30
|
+
frame = new Rectangle(0, 0, 1, 1);
|
|
31
|
+
}
|
|
32
|
+
if (baseTexture && baseTexture.baseTexture) {
|
|
33
|
+
baseTexture = baseTexture.baseTexture;
|
|
34
|
+
}
|
|
35
|
+
this.baseTexture = baseTexture;
|
|
36
|
+
this.frame = frame;
|
|
37
|
+
this.trim = trim;
|
|
38
|
+
this.valid = false;
|
|
39
|
+
this.isTiling = false;
|
|
40
|
+
this.requiresUpdate = false;
|
|
41
|
+
this.requiresReTint = false;
|
|
42
|
+
this._uvs = null;
|
|
43
|
+
this.width = 0;
|
|
44
|
+
this.height = 0;
|
|
45
|
+
this.crop = crop || new Rectangle(0, 0, 1, 1);
|
|
46
|
+
if (baseTexture.hasLoaded) {
|
|
47
|
+
if (this.noFrame) {
|
|
48
|
+
frame = new Rectangle(0, 0, baseTexture.width, baseTexture.height);
|
|
49
|
+
}
|
|
50
|
+
this.setFrame(frame);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
onBaseTextureLoaded() {
|
|
55
|
+
if (this.noFrame) {
|
|
56
|
+
this.frame = new Rectangle(0, 0, this.baseTexture.width, this.baseTexture.height);
|
|
57
|
+
}
|
|
58
|
+
this.setFrame(this.frame);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
destroy(destroyBase = false) {
|
|
62
|
+
if (destroyBase) {
|
|
63
|
+
this.baseTexture.destroy();
|
|
64
|
+
}
|
|
65
|
+
this.valid = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setFrame(frame) {
|
|
69
|
+
this.noFrame = false;
|
|
70
|
+
this.frame = frame;
|
|
71
|
+
this.width = frame.width;
|
|
72
|
+
this.height = frame.height;
|
|
73
|
+
this.crop.x = frame.x;
|
|
74
|
+
this.crop.y = frame.y;
|
|
75
|
+
this.crop.width = frame.width;
|
|
76
|
+
this.crop.height = frame.height;
|
|
77
|
+
if (!this.trim && (frame.x + frame.width > this.baseTexture.width || frame.y + frame.height > this.baseTexture.height)) {
|
|
78
|
+
// If `true` then `PIXI.Texture.setFrame` will no longer throw an error if the texture dimensions are incorrect.
|
|
79
|
+
// Instead `Texture.valid` will be set to `false` (#1556)
|
|
80
|
+
// TODO: make this configurable
|
|
81
|
+
const isTextureSilentFail = true;
|
|
82
|
+
if (!isTextureSilentFail) {
|
|
83
|
+
throw new Error('Texture Error: frame does not fit inside the base Texture dimensions ' + this);
|
|
84
|
+
}
|
|
85
|
+
this.valid = false;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.valid = frame && frame.width && frame.height && this.baseTexture.source && this.baseTexture.hasLoaded;
|
|
89
|
+
if (this.trim) {
|
|
90
|
+
this.width = this.trim.width;
|
|
91
|
+
this.height = this.trim.height;
|
|
92
|
+
this.frame.width = this.trim.width;
|
|
93
|
+
this.frame.height = this.trim.height;
|
|
94
|
+
}
|
|
95
|
+
if (this.valid) {
|
|
96
|
+
this._updateUvs();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
_updateUvs() {
|
|
101
|
+
if (!this._uvs) {
|
|
102
|
+
this._uvs = new TextureUvs();
|
|
103
|
+
}
|
|
104
|
+
const frame = this.crop;
|
|
105
|
+
const tw = this.baseTexture.width;
|
|
106
|
+
const th = this.baseTexture.height;
|
|
107
|
+
this._uvs.x0 = frame.x / tw;
|
|
108
|
+
this._uvs.y0 = frame.y / th;
|
|
109
|
+
this._uvs.x1 = (frame.x + frame.width) / tw;
|
|
110
|
+
this._uvs.y1 = frame.y / th;
|
|
111
|
+
this._uvs.x2 = (frame.x + frame.width) / tw;
|
|
112
|
+
this._uvs.y2 = (frame.y + frame.height) / th;
|
|
113
|
+
this._uvs.x3 = frame.x / tw;
|
|
114
|
+
this._uvs.y3 = (frame.y + frame.height) / th;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @author Mat Groves http://matgroves.com/ @Doormat23
|
|
5
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
6
|
+
*/
|
|
7
|
+
import BaseTexture from './base_texture';
|
|
8
|
+
import Texture from './texture';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param canvas
|
|
13
|
+
* @param scaleMode
|
|
14
|
+
*/
|
|
15
|
+
export function baseTextureFromCanvas(canvas, scaleMode) {
|
|
16
|
+
if (canvas.width === 0) {
|
|
17
|
+
canvas.width = 1;
|
|
18
|
+
}
|
|
19
|
+
if (canvas.height === 0) {
|
|
20
|
+
canvas.height = 1;
|
|
21
|
+
}
|
|
22
|
+
return new BaseTexture(canvas, scaleMode);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param canvas
|
|
28
|
+
* @param scaleMode
|
|
29
|
+
*/
|
|
30
|
+
export function textureFromCanvas(canvas, scaleMode) {
|
|
31
|
+
return new Texture(baseTextureFromCanvas(canvas, scaleMode));
|
|
32
|
+
}
|