forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadFBO.js
More file actions
397 lines (295 loc) · 10.2 KB
/
Copy pathQuadFBO.js
File metadata and controls
397 lines (295 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Frame Buffer Object with drawing quad + shader
*
* @class Phaser.Renderer.Canvas
* @constructor
* @param {Phaser.Game} game - Game reference to the currently running game.
*/
Phaser.Renderer.WebGL.QuadFBO = function (renderer, x, y, width, height)
{
this.renderer = renderer;
this.gl = null;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this.textureIndex = 0;
this.clipX = function (x)
{
return (renderer.clipUnitX * x) - 1;
};
this.clipY = function (y)
{
return 1 - (renderer.clipUnitY * y);
};
this.vertexBuffer;
this.indexBuffer;
this.textureBuffer;
this.vertices;
this.texture;
this.renderBuffer;
this.frameBuffer;
this.program;
this.aVertexPosition;
this.aTextureCoord;
};
Phaser.Renderer.WebGL.QuadFBO.prototype.constructor = Phaser.Renderer.WebGL.QuadFBO;
Phaser.Renderer.WebGL.QuadFBO.prototype = {
init: function ()
{
this.gl = this.renderer.gl;
var gl = this.gl;
// An FBO quad is made up of 2 triangles (A and B in the image below)
//
// 0 = Bottom Left
// 1 = Bottom Right
// 2 = Top Left
// 3 = Top Right
//
// 2----3
// |\ B|
// | \ |
// | \ |
// | A \|
// | \
// 0----1
this.indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([ 0, 1, 2, 2, 1, 3 ]), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
this.vertices = new Float32Array(8);
this.updateVerts();
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.DYNAMIC_DRAW);
this.textureBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0, 0, 1, 0, 0, 1, 1, 1 ]), gl.STATIC_DRAW);
// Create a texture for our color buffer
this.texture = this.renderer.createEmptyTexture(this._width, this._height, 0, 0);
// The FBO's depth buffer
this.renderBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, this.width, this.height);
this.frameBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
// May need to optionally be: gl.DEPTH_STENCIL_ATTACHMENT
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer);
var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if (fbStatus !== gl.FRAMEBUFFER_COMPLETE)
{
window.console.error('FrameBuffer Error: ', this.renderer._fbErrors[fbStatus]);
}
this.createShader();
this.emptyTexture = this.renderer.createEmptyTexture(1, 1, 0, 0);
// Reset back to defaults
// gl.bindRenderbuffer(gl.RENDERBUFFER, null);
// gl.bindFramebuffer(gl.FRAMEBUFFER, null);
},
// This whole function ought to be split out into the Shader Manager
// so they can easily change the shader being used for an FBO.
// This class will have to expose those shader attribs though.
createShader: function ()
{
// Create the quad shader
var gl = this.gl;
var vertexSrc = [
'attribute vec2 aVertexPosition;',
'attribute vec2 aTextureCoord;',
'varying vec2 vTextureCoord;',
'void main(void) {',
' vTextureCoord = aTextureCoord;',
' gl_Position = vec4(aVertexPosition, 0.0, 1.0);',
'}'
];
var fragmentSrc = [
'precision mediump float;',
'uniform sampler2D uSampler;',
'varying vec2 vTextureCoord;',
'void main(void) {',
' gl_FragColor = texture2D(uSampler, vTextureCoord);',
'}'
];
var twirlFragmentSrc = [
'precision mediump float;',
'uniform sampler2D uSampler;',
'varying vec2 vTextureCoord;',
'const float radius = 0.5;',
'const float angle = 5.0;',
'const vec2 offset = vec2(0.5, 0.5);',
'void main(void) {',
' vec2 coord = vTextureCoord - offset;',
' float distance = length(coord);',
' if (distance < radius) {',
' float ratio = (radius - distance) / radius;',
' float angleMod = ratio * ratio * angle;',
' float s = sin(angleMod);',
' float c = cos(angleMod);',
' coord = vec2(coord.x * c - coord.y * s, coord.x * s + coord.y * c);',
' }',
' gl_FragColor = texture2D(uSampler, coord + offset);',
'}'
];
// This compiles, attaches and links the shader
this.program = this.renderer.compileProgram(vertexSrc, fragmentSrc);
// this.program = this.renderer.compileProgram(vertexSrc, twirlFragmentSrc);
this.aVertexPosition = gl.getAttribLocation(this.program, 'aVertexPosition');
this.aTextureCoord = gl.getAttribLocation(this.program, 'aTextureCoord');
},
setPosition: function (x, y)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (x !== this._x || y !== this._y)
{
this._x = x;
this._y = y;
this.updateVerts();
}
},
setSize: function (width, height)
{
if (width === undefined) { width = this.renderer.width; }
if (height === undefined) { height = this.renderer.height; }
if (width !== this._width || height !== this._height)
{
this._width = width;
this._height = height;
this.updateVerts();
}
},
updateVerts: function ()
{
var x = this._x;
var y = this._y;
var width = this._width;
var height = this._height;
// Bottom Left
this.vertices[0] = this.clipX(x);
this.vertices[1] = this.clipY(y + height);
// Bottom Right
this.vertices[2] = this.clipX(x + width);
this.vertices[3] = this.clipY(y + height);
// Top Left
this.vertices[4] = this.clipX(x);
this.vertices[5] = this.clipY(y);
// Top Right
this.vertices[6] = this.clipX(x + width);
this.vertices[7] = this.clipY(y);
},
activate: function ()
{
var gl = this.gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
// FBO textures always use index zero
this.renderer.textureArray[0] = this.texture;
},
bindShader: function ()
{
var program = this.program;
var gl = this.gl;
gl.useProgram(program);
gl.uniform1i(gl.getUniformLocation(program, 'uSampler'), 0);
gl.enableVertexAttribArray(this.aTextureCoord);
gl.enableVertexAttribArray(this.aVertexPosition);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices);
gl.vertexAttribPointer(this.aVertexPosition, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
gl.vertexAttribPointer(this.aTextureCoord, 2, gl.FLOAT, false, 0, 0);
},
// destinationBuffer MUST be set, even if just to 'null'
render: function (destinationBuffer)
{
var gl = this.gl;
// Set the framebuffer to render to
gl.bindFramebuffer(gl.FRAMEBUFFER, destinationBuffer);
// Bind the texture we rendered to, for reading, always TEXTURE0
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
// The shader that will read from the fbo texture
if (this.renderer.shaderManager.setShader(this.program))
{
this.bindShader();
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
this.renderer.drawCount++;
// Unbind the fbo texture and replace it with an empty texture.
// If we forget this we corrupt the main context texture!
// or get `RENDER WARNING: there is no texture bound to the unit 0` spam in the console
gl.bindTexture(gl.TEXTURE_2D, this.emptyTexture);
},
destroy: function ()
{
// TODO!
}
};
Object.defineProperties(Phaser.Renderer.WebGL.QuadFBO.prototype, {
x: {
enumerable: true,
get: function ()
{
return this._x;
},
set: function (value)
{
if (value !== this._x)
{
this._x = value;
this.updateVerts();
}
}
},
y: {
enumerable: true,
get: function ()
{
return this._y;
},
set: function (value)
{
if (value !== this._y)
{
this._y = value;
this.updateVerts();
}
}
},
width: {
enumerable: true,
get: function ()
{
return this._width;
},
set: function (value)
{
if (value !== this._width)
{
this._width = value;
this.updateVerts();
}
}
},
height: {
enumerable: true,
get: function ()
{
return this._height;
},
set: function (value)
{
if (value !== this._height)
{
this._height = value;
this.updateVerts();
}
}
}
});