@@ -31,8 +31,12 @@ var TransformMatrix = require('../components/TransformMatrix');
3131 * @extends Phaser.GameObjects.Components.Visible
3232 *
3333 * @param {Phaser.Scene } scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
34- * @param {number } x - The horizontal position of this Game Object in the world.
35- * @param {number } y - The vertical position of this Game Object in the world.
34+ * @param {number } [x=0] - The horizontal position of this Game Object in the world.
35+ * @param {number } [y=0] - The vertical position of this Game Object in the world.
36+ * @param {number } [width=128] - The width of the Game Object.
37+ * @param {number } [height=128] - The height of the Game Object.
38+ * @param {string } [fragSource] - The source code of the fragment shader.
39+ * @param {string } [vertSource] - The source code of the vertex shader.
3640 */
3741var Shader = new Class ( {
3842
@@ -52,13 +56,56 @@ var Shader = new Class({
5256
5357 initialize :
5458
55- function Shader ( scene , x , y , width , height , vert , frag )
59+ function Shader ( scene , x , y , width , height , fragSource , vertSource )
5660 {
61+ if ( x === undefined ) { x = 0 ; }
62+ if ( y === undefined ) { y = 0 ; }
63+ if ( width === undefined ) { width = 128 ; }
64+ if ( height === undefined ) { height = 128 ; }
65+
66+ if ( fragSource === undefined )
67+ {
68+ fragSource = [
69+ 'precision mediump float;' ,
70+
71+ 'uniform vec2 resolution;' ,
72+
73+ 'varying vec2 fragCoord;' ,
74+
75+ 'void main () {' ,
76+ ' vec2 uv = fragCoord / resolution.xy;' ,
77+ ' gl_FragColor = vec4(uv.xyx, 1.0);' ,
78+ '}'
79+ ] . join ( '\n' ) ;
80+ }
81+
82+ if ( vertSource === undefined )
83+ {
84+ vertSource = [
85+ 'precision mediump float;' ,
86+
87+ 'uniform mat4 uProjectionMatrix;' ,
88+ 'uniform mat4 uViewMatrix;' ,
89+
90+ 'attribute vec2 inPosition;' ,
91+
92+ 'varying vec2 fragCoord;' ,
93+
94+ 'void main () {' ,
95+ 'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);' ,
96+ 'fragCoord = inPosition;' ,
97+ '}'
98+ ] . join ( '\n' ) ;
99+ }
100+
57101 GameObject . call ( this , scene , 'Shader' ) ;
58102
59103 // This Game Object cannot have a blend mode, so skip all checks
60104 this . blendMode = - 1 ;
61105
106+ this . vertSource = vertSource ;
107+ this . fragSource = fragSource ;
108+
62109 this . vertexCount = 0 ;
63110 this . vertexCapacity = 6 ;
64111
@@ -214,13 +261,15 @@ var Shader = new Class({
214261 this . setSize ( width , height ) ;
215262 this . setOrigin ( 0.5 , 0.5 ) ;
216263
217- this . setShader ( vert , frag ) ;
264+ this . setShader ( fragSource , vertSource ) ;
218265
219266 this . projOrtho ( 0 , renderer . width , renderer . height , 0 ) ;
220267 } ,
221268
222- setShader : function ( vertSource , fragSource )
269+ setShader : function ( fragSource , vertSource )
223270 {
271+ if ( vertSource === undefined ) { vertSource = this . vertSource ; }
272+
224273 var gl = this . gl ;
225274 var renderer = this . renderer ;
226275
@@ -235,6 +284,10 @@ var Shader = new Class({
235284 renderer . setMatrix4 ( program , 'uProjectionMatrix' , false , this . projectionMatrix ) ;
236285
237286 this . program = program ;
287+ this . fragSource = fragSource ;
288+ this . vertSource = vertSource ;
289+
290+ return this ;
238291 } ,
239292
240293 /**
0 commit comments