@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,225 @@
|
|
|
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 { generateShaderID } from '../../../util/string';
|
|
8
|
+
import { compileProgram } from '../util';
|
|
9
|
+
|
|
10
|
+
const defaultVertexSrc = [
|
|
11
|
+
'attribute vec2 aVertexPosition;',
|
|
12
|
+
'attribute vec2 aTextureCoord;',
|
|
13
|
+
'attribute vec4 aColor;',
|
|
14
|
+
|
|
15
|
+
'uniform vec2 projectionVector;',
|
|
16
|
+
'uniform vec2 offsetVector;',
|
|
17
|
+
|
|
18
|
+
'varying vec2 vTextureCoord;',
|
|
19
|
+
'varying vec4 vColor;',
|
|
20
|
+
|
|
21
|
+
'const vec2 center = vec2(-1.0, 1.0);',
|
|
22
|
+
|
|
23
|
+
'void main(void) {',
|
|
24
|
+
' gl_Position = vec4( ((aVertexPosition + offsetVector) / projectionVector) + center , 0.0, 1.0);',
|
|
25
|
+
' vTextureCoord = aTextureCoord;',
|
|
26
|
+
' vColor = vec4(aColor.rgb * aColor.a, aColor.a);',
|
|
27
|
+
'}',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
// this shader is used for the default sprite rendering
|
|
31
|
+
|
|
32
|
+
export default class {
|
|
33
|
+
|
|
34
|
+
constructor(gl) {
|
|
35
|
+
this.gl = gl;
|
|
36
|
+
this._UID = generateShaderID();
|
|
37
|
+
this.program = null;
|
|
38
|
+
this.fragmentSrc = [
|
|
39
|
+
'precision lowp float;',
|
|
40
|
+
'varying vec2 vTextureCoord;',
|
|
41
|
+
'varying vec4 vColor;',
|
|
42
|
+
'uniform sampler2D uSampler;',
|
|
43
|
+
'void main(void) {',
|
|
44
|
+
' gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor ;',
|
|
45
|
+
'}',
|
|
46
|
+
];
|
|
47
|
+
this.textureCount = 0;
|
|
48
|
+
this.firstRun = true;
|
|
49
|
+
this.dirty = true;
|
|
50
|
+
this.uniforms = {};
|
|
51
|
+
this.attributes = [];
|
|
52
|
+
this.init();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
init() {
|
|
56
|
+
const gl = this.gl;
|
|
57
|
+
const program = compileProgram(gl, this.vertexSrc || defaultVertexSrc, this.fragmentSrc);
|
|
58
|
+
gl.useProgram(program);
|
|
59
|
+
// get and store the uniforms for the shader
|
|
60
|
+
this.uSampler = gl.getUniformLocation(program, 'uSampler');
|
|
61
|
+
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
|
|
62
|
+
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
|
|
63
|
+
this.dimensions = gl.getUniformLocation(program, 'dimensions');
|
|
64
|
+
// get and store the attributes
|
|
65
|
+
this.aVertexPosition = gl.getAttribLocation(program, 'aVertexPosition');
|
|
66
|
+
this.aTextureCoord = gl.getAttribLocation(program, 'aTextureCoord');
|
|
67
|
+
this.colorAttribute = gl.getAttribLocation(program, 'aColor');
|
|
68
|
+
// Begin worst hack eva //
|
|
69
|
+
// WHY??? ONLY on my chrome pixel the line above returns -1 when using filters?
|
|
70
|
+
// maybe its something to do with the current state of the gl context.
|
|
71
|
+
// I'm convinced this is a bug in the chrome browser as there is NO reason why this should be returning -1 especially as it only manifests on my chrome pixel
|
|
72
|
+
// If theres any webGL people that know why could happen please help :)
|
|
73
|
+
if (this.colorAttribute === -1) {
|
|
74
|
+
this.colorAttribute = 2;
|
|
75
|
+
}
|
|
76
|
+
this.attributes = [this.aVertexPosition, this.aTextureCoord, this.colorAttribute];
|
|
77
|
+
// End worst hack eva //
|
|
78
|
+
// add those custom shaders!
|
|
79
|
+
const keys = Object.keys(this.uniforms);
|
|
80
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
81
|
+
// get the uniform locations..
|
|
82
|
+
this.uniforms[keys[i]].uniformLocation = gl.getUniformLocation(program, keys[i]);
|
|
83
|
+
}
|
|
84
|
+
this.initUniforms();
|
|
85
|
+
this.program = program;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
initUniforms() {
|
|
89
|
+
this.textureCount = 1;
|
|
90
|
+
const gl = this.gl;
|
|
91
|
+
let uniform;
|
|
92
|
+
const keys = Object.keys(this.uniforms);
|
|
93
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
94
|
+
const key = keys[i];
|
|
95
|
+
uniform = this.uniforms[key];
|
|
96
|
+
const type = uniform.type;
|
|
97
|
+
if (type === 'sampler2D') {
|
|
98
|
+
uniform._init = false;
|
|
99
|
+
if (uniform.value !== null) {
|
|
100
|
+
this.initSampler2D(uniform);
|
|
101
|
+
}
|
|
102
|
+
} else if (type === 'mat2' || type === 'mat3' || type === 'mat4') {
|
|
103
|
+
// These require special handling
|
|
104
|
+
uniform.glMatrix = true;
|
|
105
|
+
uniform.glValueLength = 1;
|
|
106
|
+
if (type === 'mat2') {
|
|
107
|
+
uniform.glFunc = gl.uniformMatrix2fv;
|
|
108
|
+
} else if (type === 'mat3') {
|
|
109
|
+
uniform.glFunc = gl.uniformMatrix3fv;
|
|
110
|
+
} else if (type === 'mat4') {
|
|
111
|
+
uniform.glFunc = gl.uniformMatrix4fv;
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
// GL function reference
|
|
115
|
+
uniform.glFunc = gl['uniform' + type];
|
|
116
|
+
if (type === '2f' || type === '2i') {
|
|
117
|
+
uniform.glValueLength = 2;
|
|
118
|
+
} else if (type === '3f' || type === '3i') {
|
|
119
|
+
uniform.glValueLength = 3;
|
|
120
|
+
} else if (type === '4f' || type === '4i') {
|
|
121
|
+
uniform.glValueLength = 4;
|
|
122
|
+
} else {
|
|
123
|
+
uniform.glValueLength = 1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
initSampler2D(uniform) {
|
|
130
|
+
if (!uniform.value || !uniform.value.baseTexture || !uniform.value.baseTexture.hasLoaded) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const gl = this.gl;
|
|
134
|
+
gl.activeTexture(gl['TEXTURE' + this.textureCount]);
|
|
135
|
+
gl.bindTexture(gl.TEXTURE_2D, uniform.value.baseTexture._glTextures[gl.id]);
|
|
136
|
+
// Extended texture data
|
|
137
|
+
if (uniform.textureData) {
|
|
138
|
+
const data = uniform.textureData;
|
|
139
|
+
// GLTexture = mag linear, min linear_mipmap_linear, wrap repeat + gl.generateMipmap(gl.TEXTURE_2D);
|
|
140
|
+
// GLTextureLinear = mag/min linear, wrap clamp
|
|
141
|
+
// GLTextureNearestRepeat = mag/min NEAREST, wrap repeat
|
|
142
|
+
// GLTextureNearest = mag/min nearest, wrap clamp
|
|
143
|
+
// AudioTexture = whatever + luminance + width 512, height 2, border 0
|
|
144
|
+
// KeyTexture = whatever + luminance + width 256, height 2, border 0
|
|
145
|
+
// magFilter can be: gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR or gl.NEAREST
|
|
146
|
+
// wrapS/T can be: gl.CLAMP_TO_EDGE or gl.REPEAT
|
|
147
|
+
const magFilter = (data.magFilter) ? data.magFilter : gl.LINEAR;
|
|
148
|
+
const minFilter = (data.minFilter) ? data.minFilter : gl.LINEAR;
|
|
149
|
+
let wrapS = (data.wrapS) ? data.wrapS : gl.CLAMP_TO_EDGE;
|
|
150
|
+
let wrapT = (data.wrapT) ? data.wrapT : gl.CLAMP_TO_EDGE;
|
|
151
|
+
const format = (data.luminance) ? gl.LUMINANCE : gl.RGBA;
|
|
152
|
+
if (data.repeat) {
|
|
153
|
+
wrapS = gl.REPEAT;
|
|
154
|
+
wrapT = gl.REPEAT;
|
|
155
|
+
}
|
|
156
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, !!data.flipY);
|
|
157
|
+
if (data.width) {
|
|
158
|
+
const width = (data.width) ? data.width : 512;
|
|
159
|
+
const height = (data.height) ? data.height : 2;
|
|
160
|
+
const border = (data.border) ? data.border : 0;
|
|
161
|
+
|
|
162
|
+
// void texImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, ArrayBufferView? pixels);
|
|
163
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, border, format, gl.UNSIGNED_BYTE, null);
|
|
164
|
+
} else {
|
|
165
|
+
// void texImage2D(GLenum target, GLint level, GLenum internalformat, GLenum format, GLenum type, ImageData? pixels);
|
|
166
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, format, gl.RGBA, gl.UNSIGNED_BYTE, uniform.value.baseTexture.source);
|
|
167
|
+
}
|
|
168
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
|
|
169
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
|
|
170
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
|
|
171
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
|
|
172
|
+
}
|
|
173
|
+
gl.uniform1i(uniform.uniformLocation, this.textureCount);
|
|
174
|
+
uniform._init = true;
|
|
175
|
+
this.textureCount += 1;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
syncUniforms() {
|
|
179
|
+
this.textureCount = 1;
|
|
180
|
+
let uniform;
|
|
181
|
+
const gl = this.gl;
|
|
182
|
+
// This would probably be faster in an array and it would guarantee key order
|
|
183
|
+
const keys = Object.keys(this.uniforms);
|
|
184
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
185
|
+
const key = keys[i];
|
|
186
|
+
uniform = this.uniforms[key];
|
|
187
|
+
if (uniform.glValueLength === 1) {
|
|
188
|
+
if (uniform.glMatrix === true) {
|
|
189
|
+
uniform.glFunc.call(gl, uniform.uniformLocation, uniform.transpose, uniform.value);
|
|
190
|
+
} else {
|
|
191
|
+
uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value);
|
|
192
|
+
}
|
|
193
|
+
} else if (uniform.glValueLength === 2) {
|
|
194
|
+
uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y);
|
|
195
|
+
} else if (uniform.glValueLength === 3) {
|
|
196
|
+
uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y, uniform.value.z);
|
|
197
|
+
} else if (uniform.glValueLength === 4) {
|
|
198
|
+
uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y, uniform.value.z, uniform.value.w);
|
|
199
|
+
} else if (uniform.type === 'sampler2D') {
|
|
200
|
+
if (uniform._init) {
|
|
201
|
+
gl.activeTexture(gl['TEXTURE' + this.textureCount]);
|
|
202
|
+
if (uniform.value.baseTexture._dirty[gl.id]) {
|
|
203
|
+
window.PhaserRegistry.INSTANCES[gl.id].updateTexture(uniform.value.baseTexture);
|
|
204
|
+
} else {
|
|
205
|
+
// bind the current texture
|
|
206
|
+
gl.bindTexture(gl.TEXTURE_2D, uniform.value.baseTexture._glTextures[gl.id]);
|
|
207
|
+
}
|
|
208
|
+
// gl.bindTexture(gl.TEXTURE_2D, uniform.value.baseTexture._glTextures[gl.id] || PIXI.createWebGLTexture( uniform.value.baseTexture, gl));
|
|
209
|
+
gl.uniform1i(uniform.uniformLocation, this.textureCount);
|
|
210
|
+
this.textureCount += 1;
|
|
211
|
+
} else {
|
|
212
|
+
this.initSampler2D(uniform);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
destroy() {
|
|
219
|
+
this.gl.deleteProgram(this.program);
|
|
220
|
+
this.uniforms = null;
|
|
221
|
+
this.gl = null;
|
|
222
|
+
this.attributes = null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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 { generateShaderID } from '../../../util/string';
|
|
8
|
+
import { compileProgram } from '../util';
|
|
9
|
+
|
|
10
|
+
// the next one is used for rendering primitives
|
|
11
|
+
|
|
12
|
+
export default class {
|
|
13
|
+
|
|
14
|
+
constructor(gl) {
|
|
15
|
+
this.gl = gl;
|
|
16
|
+
this._UID = generateShaderID();
|
|
17
|
+
this.program = null;
|
|
18
|
+
this.fragmentSrc = [
|
|
19
|
+
'precision mediump float;',
|
|
20
|
+
'varying vec4 vColor;',
|
|
21
|
+
|
|
22
|
+
'void main(void) {',
|
|
23
|
+
' gl_FragColor = vColor;',
|
|
24
|
+
'}',
|
|
25
|
+
];
|
|
26
|
+
this.vertexSrc = [
|
|
27
|
+
'attribute vec2 aVertexPosition;',
|
|
28
|
+
'attribute vec4 aColor;',
|
|
29
|
+
'uniform mat3 translationMatrix;',
|
|
30
|
+
'uniform vec2 projectionVector;',
|
|
31
|
+
'uniform vec2 offsetVector;',
|
|
32
|
+
'uniform float alpha;',
|
|
33
|
+
'uniform float flipY;',
|
|
34
|
+
'uniform vec3 tint;',
|
|
35
|
+
'varying vec4 vColor;',
|
|
36
|
+
|
|
37
|
+
'void main(void) {',
|
|
38
|
+
' vec3 v = translationMatrix * vec3(aVertexPosition , 1.0);',
|
|
39
|
+
' v -= offsetVector.xyx;',
|
|
40
|
+
' gl_Position = vec4( v.x / projectionVector.x -1.0, (v.y / projectionVector.y * -flipY) + flipY , 0.0, 1.0);',
|
|
41
|
+
' vColor = aColor * vec4(tint * alpha, alpha);',
|
|
42
|
+
'}',
|
|
43
|
+
];
|
|
44
|
+
this.init();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
init() {
|
|
48
|
+
const gl = this.gl;
|
|
49
|
+
const program = compileProgram(gl, this.vertexSrc, this.fragmentSrc);
|
|
50
|
+
gl.useProgram(program);
|
|
51
|
+
// get and store the uniforms for the shader
|
|
52
|
+
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
|
|
53
|
+
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
|
|
54
|
+
this.tintColor = gl.getUniformLocation(program, 'tint');
|
|
55
|
+
this.flipY = gl.getUniformLocation(program, 'flipY');
|
|
56
|
+
// get and store the attributes
|
|
57
|
+
this.aVertexPosition = gl.getAttribLocation(program, 'aVertexPosition');
|
|
58
|
+
this.colorAttribute = gl.getAttribLocation(program, 'aColor');
|
|
59
|
+
this.attributes = [this.aVertexPosition, this.colorAttribute];
|
|
60
|
+
this.translationMatrix = gl.getUniformLocation(program, 'translationMatrix');
|
|
61
|
+
this.alpha = gl.getUniformLocation(program, 'alpha');
|
|
62
|
+
this.program = program;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
destroy() {
|
|
66
|
+
this.gl.deleteProgram(this.program);
|
|
67
|
+
this.uniforms = null;
|
|
68
|
+
this.gl = null;
|
|
69
|
+
this.attributes = null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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 { generateShaderID } from '../../../util/string';
|
|
8
|
+
import { compileProgram } from '../util';
|
|
9
|
+
|
|
10
|
+
// the next one is used for rendering triangle strips
|
|
11
|
+
|
|
12
|
+
export default class {
|
|
13
|
+
|
|
14
|
+
constructor(gl) {
|
|
15
|
+
this.gl = gl;
|
|
16
|
+
this._UID = generateShaderID();
|
|
17
|
+
this.program = null;
|
|
18
|
+
this.fragmentSrc = [
|
|
19
|
+
'precision mediump float;',
|
|
20
|
+
'varying vec2 vTextureCoord;',
|
|
21
|
+
// 'varying float vColor;',
|
|
22
|
+
'uniform float alpha;',
|
|
23
|
+
'uniform sampler2D uSampler;',
|
|
24
|
+
'void main(void) {',
|
|
25
|
+
' gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.x, vTextureCoord.y)) * alpha;',
|
|
26
|
+
// ' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);',//gl_FragColor * alpha;',
|
|
27
|
+
'}',
|
|
28
|
+
];
|
|
29
|
+
this.vertexSrc = [
|
|
30
|
+
'attribute vec2 aVertexPosition;',
|
|
31
|
+
'attribute vec2 aTextureCoord;',
|
|
32
|
+
'uniform mat3 translationMatrix;',
|
|
33
|
+
'uniform vec2 projectionVector;',
|
|
34
|
+
'uniform vec2 offsetVector;',
|
|
35
|
+
// 'uniform float alpha;',
|
|
36
|
+
// 'uniform vec3 tint;',
|
|
37
|
+
'varying vec2 vTextureCoord;',
|
|
38
|
+
// 'varying vec4 vColor;',
|
|
39
|
+
'void main(void) {',
|
|
40
|
+
' vec3 v = translationMatrix * vec3(aVertexPosition , 1.0);',
|
|
41
|
+
' v -= offsetVector.xyx;',
|
|
42
|
+
' gl_Position = vec4( v.x / projectionVector.x -1.0, v.y / -projectionVector.y + 1.0 , 0.0, 1.0);',
|
|
43
|
+
' vTextureCoord = aTextureCoord;',
|
|
44
|
+
// ' vColor = aColor * vec4(tint * alpha, alpha);',
|
|
45
|
+
'}',
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
this.init();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
init() {
|
|
52
|
+
const gl = this.gl;
|
|
53
|
+
const program = compileProgram(gl, this.vertexSrc, this.fragmentSrc);
|
|
54
|
+
gl.useProgram(program);
|
|
55
|
+
// get and store the uniforms for the shader
|
|
56
|
+
this.uSampler = gl.getUniformLocation(program, 'uSampler');
|
|
57
|
+
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
|
|
58
|
+
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
|
|
59
|
+
this.colorAttribute = gl.getAttribLocation(program, 'aColor');
|
|
60
|
+
// this.dimensions = gl.getUniformLocation(this.program, 'dimensions');
|
|
61
|
+
// get and store the attributes
|
|
62
|
+
this.aVertexPosition = gl.getAttribLocation(program, 'aVertexPosition');
|
|
63
|
+
this.aTextureCoord = gl.getAttribLocation(program, 'aTextureCoord');
|
|
64
|
+
this.attributes = [this.aVertexPosition, this.aTextureCoord];
|
|
65
|
+
this.translationMatrix = gl.getUniformLocation(program, 'translationMatrix');
|
|
66
|
+
this.alpha = gl.getUniformLocation(program, 'alpha');
|
|
67
|
+
this.program = program;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
destroy() {
|
|
71
|
+
this.gl.deleteProgram(this.program);
|
|
72
|
+
this.uniforms = null;
|
|
73
|
+
this.gl = null;
|
|
74
|
+
this.attribute = null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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 PrimitiveShader from './shader/primitive';
|
|
8
|
+
import ComplexPrimitiveShader from './shader/complex';
|
|
9
|
+
import NormalShader from './shader/normal';
|
|
10
|
+
import FastShader from './shader/fast';
|
|
11
|
+
import StripShader from './shader/strip';
|
|
12
|
+
|
|
13
|
+
export default class {
|
|
14
|
+
|
|
15
|
+
constructor() {
|
|
16
|
+
this.gl = null;
|
|
17
|
+
this.primitiveShader = null;
|
|
18
|
+
this.complexPrimitiveShader = null;
|
|
19
|
+
this.defaultShader = null;
|
|
20
|
+
this.fastShader = null;
|
|
21
|
+
this.stripShader = null;
|
|
22
|
+
this.maxAttibs = 10;
|
|
23
|
+
this.attribState = [];
|
|
24
|
+
this.tempAttribState = [];
|
|
25
|
+
for (let i = 0; i < this.maxAttibs; i += 1) {
|
|
26
|
+
this.attribState[i] = false;
|
|
27
|
+
}
|
|
28
|
+
this.stack = [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setContext(gl) {
|
|
32
|
+
this.gl = gl;
|
|
33
|
+
this.primitiveShader = new PrimitiveShader(gl);
|
|
34
|
+
this.complexPrimitiveShader = new ComplexPrimitiveShader(gl);
|
|
35
|
+
this.defaultShader = new NormalShader(gl);
|
|
36
|
+
this.fastShader = new FastShader(gl);
|
|
37
|
+
this.stripShader = new StripShader(gl);
|
|
38
|
+
this.setShader(this.defaultShader);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
setAttribs(attribs) {
|
|
42
|
+
// reset temp state
|
|
43
|
+
let i;
|
|
44
|
+
for (i = 0; i < this.tempAttribState.length; i += 1) {
|
|
45
|
+
this.tempAttribState[i] = false;
|
|
46
|
+
}
|
|
47
|
+
// set the new attribs
|
|
48
|
+
for (i = 0; i < attribs.length; i += 1) {
|
|
49
|
+
const attribId = attribs[i];
|
|
50
|
+
this.tempAttribState[attribId] = true;
|
|
51
|
+
}
|
|
52
|
+
const gl = this.gl;
|
|
53
|
+
for (i = 0; i < this.attribState.length; i += 1) {
|
|
54
|
+
if (this.attribState[i] !== this.tempAttribState[i]) {
|
|
55
|
+
this.attribState[i] = this.tempAttribState[i];
|
|
56
|
+
if (this.tempAttribState[i]) {
|
|
57
|
+
gl.enableVertexAttribArray(i);
|
|
58
|
+
} else {
|
|
59
|
+
gl.disableVertexAttribArray(i);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setShader(shader) {
|
|
66
|
+
if (this._currentId === shader._UID) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
this._currentId = shader._UID;
|
|
70
|
+
this.currentShader = shader;
|
|
71
|
+
this.gl.useProgram(shader.program);
|
|
72
|
+
this.setAttribs(shader.attributes);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
destroy() {
|
|
77
|
+
this.attribState = null;
|
|
78
|
+
this.tempAttribState = null;
|
|
79
|
+
this.currentShader = null;
|
|
80
|
+
this._currentId = null;
|
|
81
|
+
this.primitiveShader.destroy();
|
|
82
|
+
this.complexPrimitiveShader.destroy();
|
|
83
|
+
this.defaultShader.destroy();
|
|
84
|
+
this.fastShader.destroy();
|
|
85
|
+
this.stripShader.destroy();
|
|
86
|
+
this.gl = null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
}
|