Skip to content

Commit a0ef6e9

Browse files
committed
Added default shader
1 parent 27d6bd5 commit a0ef6e9

2 files changed

Lines changed: 68 additions & 25 deletions

File tree

src/gameobjects/shader/Shader.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
3741
var 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
/**

src/gameobjects/shader/ShaderFactory.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,23 @@ var GameObjectFactory = require('../GameObjectFactory');
1212
*
1313
* Note: This method will only be available if the Shader Game Object and WebGL support have been built into Phaser.
1414
*
15-
* @method Phaser.GameObjects.GameObjectFactory#mesh
15+
* @method Phaser.GameObjects.GameObjectFactory#shader
1616
* @webglOnly
17-
* @since 3.0.0
17+
* @since 3.17.0
1818
*
19-
* @param {number} x - The horizontal position of this Game Object in the world.
20-
* @param {number} y - The vertical position of this Game Object in the world.
21-
* @param {number[]} vertices - An array containing the vertices data for this Shader.
22-
* @param {number[]} uv - An array containing the uv data for this Shader.
23-
* @param {number[]} colors - An array containing the color data for this Shader.
24-
* @param {number[]} alphas - An array containing the alpha data for this Shader.
25-
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
26-
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
19+
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
20+
* @param {number} [y=0] - The vertical position of this Game Object in the world.
21+
* @param {number} [width=128] - The width of the Game Object.
22+
* @param {number} [height=128] - The height of the Game Object.
23+
* @param {string} [fragSource] - The source code of the fragment shader.
24+
* @param {string} [vertSource] - The source code of the vertex shader.
2725
*
2826
* @return {Phaser.GameObjects.Shader} The Game Object that was created.
2927
*/
3028
if (typeof WEBGL_RENDERER)
3129
{
32-
GameObjectFactory.register('shader', function (x, y, width, height, vert, frag)
30+
GameObjectFactory.register('shader', function (x, y, width, height, fragSource, vertSource)
3331
{
34-
return this.displayList.add(new Shader(this.scene, x, y, width, height, vert, frag));
32+
return this.displayList.add(new Shader(this.scene, x, y, width, height, fragSource, vertSource));
3533
});
3634
}
37-
38-
// When registering a factory function 'this' refers to the GameObjectFactory context.
39-
//
40-
// There are several properties available to use:
41-
//
42-
// this.scene - a reference to the Scene that owns the GameObjectFactory
43-
// this.displayList - a reference to the Display List the Scene owns
44-
// this.updateList - a reference to the Update List the Scene owns

0 commit comments

Comments
 (0)