Skip to content

Commit 8a72f0f

Browse files
committed
FilterTexture converted.
1 parent 281e928 commit 8a72f0f

7 files changed

Lines changed: 169 additions & 5 deletions

File tree

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
<script src="$path/src/renderer/webgl/WebGLRenderer.js"></script>
208208
<script src="$path/src/renderer/webgl/ShaderManager.js"></script>
209209
<script src="$path/src/renderer/webgl/StencilManager.js"></script>
210+
<script src="$path/src/renderer/webgl/FilterTexture.js"></script>
210211
<script src="$path/src/renderer/webgl/FilterManager.js"></script>
211212
<script src="$path/src/renderer/webgl/SpriteBatch.js"></script>
212213
<script src="$path/src/renderer/webgl/shaders/Sprite.js"></script>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Mat Groves (@Doormat23)
4+
* @copyright 2016 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
8+
/**
9+
* New version of PIXI.FilterTexture
10+
*
11+
* @class Phaser.Renderer.Canvas
12+
* @constructor
13+
* @param {Phaser.Game} game - Game reference to the currently running game.
14+
* @param width {Number} the horizontal range of the filter
15+
* @param height {Number} the vertical range of the filter
16+
* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values
17+
*/
18+
Phaser.Renderer.WebGL.FilterTexture = function (renderer, width, height, scaleMode, textureUnit)
19+
{
20+
if (textureUnit === undefined) { textureUnit = 0; }
21+
22+
this.renderer = renderer;
23+
24+
// WebGLContext
25+
this.gl = renderer.gl;
26+
27+
/**
28+
* @property frameBuffer
29+
* @type Any
30+
*/
31+
this.frameBuffer = this.renderer.createFramebuffer(width, height, scaleMode || Phaser.scaleModes.DEFAULT, textureUnit);
32+
33+
/**
34+
* @property texture
35+
* @type Any
36+
*/
37+
this.texture = this.frameBuffer.targetTexture;
38+
39+
this.width = width;
40+
41+
this.height = height;
42+
43+
this.renderBuffer = this.frameBuffer.renderBuffer;
44+
45+
};
46+
47+
Phaser.Renderer.WebGL.FilterTexture.prototype.constructor = Phaser.Renderer.WebGL.FilterTexture;
48+
49+
Phaser.Renderer.WebGL.FilterTexture.prototype = {
50+
51+
clear: function ()
52+
{
53+
this.gl.clearColor(0, 0, 0, 0);
54+
this.gl.clear(gl.COLOR_BUFFER_BIT);
55+
},
56+
57+
/**
58+
* Resizes the texture to the specified width and height
59+
*
60+
* @method resize
61+
* @param width {Number} the new width of the texture
62+
* @param height {Number} the new height of the texture
63+
*/
64+
resize = function (width, height)
65+
{
66+
if (this.width === width && this.height === height)
67+
{
68+
return;
69+
}
70+
71+
this.width = width;
72+
this.height = height;
73+
74+
var gl = this.gl;
75+
76+
gl.bindTexture(gl.TEXTURE_2D, this.texture);
77+
78+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
79+
80+
gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer);
81+
82+
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
83+
},
84+
85+
/**
86+
* Destroys the filter texture.
87+
*
88+
* @method destroy
89+
*/
90+
destroy: function ()
91+
{
92+
var gl = this.gl;
93+
94+
gl.deleteFramebuffer(this.frameBuffer);
95+
gl.deleteTexture(this.texture);
96+
97+
this.frameBuffer = null;
98+
this.texture = null;
99+
100+
this.gl = null;
101+
this.renderer = null;
102+
}
103+
104+
};

src/renderer/webgl/WebGLRenderer.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Phaser.Renderer.WebGL = function (game)
133133
* @property filterManager
134134
* @type WebGLFilterManager
135135
*/
136-
// this.filterManager = new PIXI.WebGLFilterManager();
136+
this.filterManager = new Phaser.Renderer.WebGL.FilterManager(this);
137137

138138
/**
139139
* Manages the stencil buffer
@@ -151,6 +151,13 @@ Phaser.Renderer.WebGL = function (game)
151151
this.drawCount = 0;
152152
this.flipY = 1;
153153

154+
this._fbErrors = {
155+
36054: 'Incomplete attachment',
156+
36055: 'Missing attachment',
157+
36057: 'Incomplete dimensions',
158+
36061: 'Framebuffer unsupported'
159+
};
160+
154161
this.init();
155162

156163
};
@@ -610,6 +617,58 @@ Phaser.Renderer.WebGL.prototype = {
610617
return shaderProgram;
611618
},
612619

620+
createEmptyTexture: function (width, height, scaleMode)
621+
{
622+
var gl = this.gl;
623+
var texture = gl.createTexture();
624+
var glScaleMode = (scaleMode === Phaser.scaleModes.LINEAR) ? gl.LINEAR : gl.NEAREST;
625+
626+
gl.bindTexture(gl.TEXTURE_2D, texture);
627+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
628+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
629+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glScaleMode);
630+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glScaleMode);
631+
632+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
633+
634+
return texture;
635+
},
636+
637+
createFramebuffer: function (width, height, scaleMode, textureUnit)
638+
{
639+
var gl = this.gl;
640+
var framebuffer = gl.createFramebuffer();
641+
var depthStencilBuffer = gl.createRenderbuffer();
642+
var colorBuffer = null;
643+
var fbStatus = 0;
644+
645+
gl.activeTexture(gl.TEXTURE0 + textureUnit);
646+
647+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
648+
gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencilBuffer);
649+
650+
// `this.renderBuffer` = undefined? FilterTexture has a renderBuffer, but `this` doesn't.
651+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer);
652+
653+
colorBuffer = this.createEmptyTexture(width, height, scaleMode);
654+
655+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorBuffer, 0);
656+
657+
fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
658+
659+
if (fbStatus !== gl.FRAMEBUFFER_COMPLETE)
660+
{
661+
console.error('Incomplete GL framebuffer. ', this._fbErrors[fbStatus]);
662+
}
663+
664+
framebuffer.width = width;
665+
framebuffer.height = height;
666+
framebuffer.targetTexture = colorBuffer;
667+
framebuffer.renderBuffer = depthStencilBuffer;
668+
669+
return framebuffer;
670+
},
671+
613672
destroy: function ()
614673
{
615674
PIXI.glContexts[this.glContextId] = null;

src/renderer/webgl/shaders/PrimitiveGraphics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
/**
9-
* New version of PIXI.DefaultShader
9+
* New version of PIXI.PrimitiveShader
1010
*
1111
* @class Phaser.Renderer.Canvas
1212
* @constructor

src/renderer/webgl/shaders/Sprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
/**
9-
* New version of PIXI.DefaultShader
9+
* New version of PIXI.DefaultShader which was PIXI.PixiShader
1010
*
1111
* @class Phaser.Renderer.Canvas
1212
* @constructor

src/renderer/webgl/shaders/SpriteBatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
/**
9-
* New version of PIXI.DefaultShader
9+
* New version of PIXI.PixiFastShader
1010
*
1111
* @class Phaser.Renderer.Canvas
1212
* @constructor

src/renderer/webgl/shaders/Strip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
/**
9-
* New version of PIXI.DefaultShader
9+
* New version of PIXI.StripShader
1010
*
1111
* @class Phaser.Renderer.Canvas
1212
* @constructor

0 commit comments

Comments
 (0)