Skip to content

Commit 272fbfc

Browse files
committed
Improved jsdocs, finished splitting up render process
1 parent 38d8ae7 commit 272fbfc

5 files changed

Lines changed: 61 additions & 44 deletions

File tree

src/gameobjects/shader/Shader.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var TransformMatrix = require('../components/TransformMatrix');
3636
* @extends Phaser.GameObjects.Components.Visible
3737
*
3838
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
39-
* @param {string} key -
39+
* @param {string} key - The key of the shader to use from the shader cache.
4040
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
4141
* @param {number} [y=0] - The vertical position of this Game Object in the world.
4242
* @param {number} [width=128] - The width of the Game Object.
@@ -523,23 +523,25 @@ var Shader = new Class({
523523
* Called automatically during render.
524524
*
525525
* This method performs matrix ITRS and then stores the resulting value in the `uViewMatrix` uniform.
526-
* It then sets up the vertex buffer and shader, updates and syncs the uniforms and finally draws it
527-
* to a single quad.
526+
* It then sets up the vertex buffer and shader, updates and syncs the uniforms ready
527+
* for flush to be called.
528528
*
529529
* @method Phaser.GameObjects.Shader#load
530-
* @private
531530
* @since 3.17.0
531+
*
532+
* @param {Phaser.GameObjects.Components.TransformMatrix} matrix2D - The transform matrix to use during rendering.
532533
*/
533534
load: function (matrix2D)
534535
{
535536
// ITRS
536537

538+
var width = this.width;
539+
var height = this.height;
540+
var renderer = this.renderer;
537541
var program = this.program;
538542

539543
var x = -this._displayOriginX;
540544
var y = -this._displayOriginY;
541-
var width = this.width;
542-
var height = this.height;
543545

544546
var vm = this.viewMatrix;
545547

@@ -554,25 +556,6 @@ var Shader = new Class({
554556

555557
this.renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
556558

557-
// Bind
558-
559-
var gl = this.gl;
560-
var vertexBuffer = this.vertexBuffer;
561-
var renderer = this.renderer;
562-
var vertexSize = Float32Array.BYTES_PER_ELEMENT * 2;
563-
564-
renderer.setProgram(program);
565-
renderer.setVertexBuffer(vertexBuffer);
566-
567-
var location = gl.getAttribLocation(program, 'inPosition');
568-
569-
if (location !== -1)
570-
{
571-
gl.enableVertexAttribArray(location);
572-
573-
gl.vertexAttribPointer(location, 2, gl.FLOAT, false, vertexSize, 0);
574-
}
575-
576559
// Update common uniforms
577560

578561
var uniforms = this.uniforms;
@@ -597,6 +580,40 @@ var Shader = new Class({
597580
}
598581

599582
this.syncUniforms();
583+
},
584+
585+
/**
586+
* Called automatically during render.
587+
*
588+
* Sets the active shader, loads the vertex buffer and then draws.
589+
*
590+
* @method Phaser.GameObjects.Shader#flush
591+
* @since 3.17.0
592+
*/
593+
flush: function ()
594+
{
595+
// Bind
596+
597+
var width = this.width;
598+
var height = this.height;
599+
var program = this.program;
600+
601+
var gl = this.gl;
602+
var vertexBuffer = this.vertexBuffer;
603+
var renderer = this.renderer;
604+
var vertexSize = Float32Array.BYTES_PER_ELEMENT * 2;
605+
606+
renderer.setProgram(program);
607+
renderer.setVertexBuffer(vertexBuffer);
608+
609+
var location = gl.getAttribLocation(program, 'inPosition');
610+
611+
if (location !== -1)
612+
{
613+
gl.enableVertexAttribArray(location);
614+
615+
gl.vertexAttribPointer(location, 2, gl.FLOAT, false, vertexSize, 0);
616+
}
600617

601618
// Draw
602619

@@ -643,12 +660,13 @@ var Shader = new Class({
643660
},
644661

645662
/**
646-
* Removes all object references in this WebGL Pipeline and removes its program from the WebGL context.
663+
* Internal destroy handler, called as part of the destroy process.
647664
*
648-
* @method Phaser.GameObjects.Shader#destroy
649-
* @since 3.17.0
665+
* @method Phaser.GameObjects.RenderTexture#preDestroy
666+
* @protected
667+
* @since 3.9.0
650668
*/
651-
destroy: function ()
669+
preDestroy: function ()
652670
{
653671
var gl = this.gl;
654672

src/gameobjects/shader/ShaderCanvasRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* This is a stub function for Shader.Render. There is no Canvas renderer for Shader objects.
99
*
1010
* @method Phaser.GameObjects.Shader#renderCanvas
11-
* @since 3.0.0
11+
* @since 3.17.0
1212
* @private
1313
*
1414
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.

src/gameobjects/shader/ShaderCreator.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,41 @@
77
var BuildGameObject = require('../BuildGameObject');
88
var GameObjectCreator = require('../GameObjectCreator');
99
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
10-
var GetValue = require('../../utils/object/GetValue');
1110
var Shader = require('./Shader');
1211

1312
/**
1413
* Creates a new Shader Game Object and returns it.
1514
*
1615
* Note: This method will only be available if the Shader Game Object and WebGL support have been built into Phaser.
1716
*
18-
* @method Phaser.GameObjects.GameObjectCreator#mesh
19-
* @since 3.0.0
17+
* @method Phaser.GameObjects.GameObjectCreator#shader
18+
* @since 3.17.0
2019
*
2120
* @param {object} config - The configuration object this Game Object will use to create itself.
2221
* @param {boolean} [addToScene] - Add this Game Object to the Scene after creating it? If set this argument overrides the `add` property in the config object.
2322
*
2423
* @return {Phaser.GameObjects.Shader} The Game Object that was created.
2524
*/
26-
GameObjectCreator.register('mesh', function (config, addToScene)
25+
GameObjectCreator.register('shader', function (config, addToScene)
2726
{
2827
if (config === undefined) { config = {}; }
2928

3029
var key = GetAdvancedValue(config, 'key', null);
31-
var frame = GetAdvancedValue(config, 'frame', null);
32-
var vertices = GetValue(config, 'vertices', []);
33-
var colors = GetValue(config, 'colors', []);
34-
var alphas = GetValue(config, 'alphas', []);
35-
var uv = GetValue(config, 'uv', []);
30+
var x = GetAdvancedValue(config, 'x', 0);
31+
var y = GetAdvancedValue(config, 'y', 0);
32+
var width = GetAdvancedValue(config, 'width', 128);
33+
var height = GetAdvancedValue(config, 'height', 128);
3634

37-
var mesh = new Shader(this.scene, 0, 0, vertices, uv, colors, alphas, key, frame);
35+
var shader = new Shader(this.scene, key, x, y, width, height);
3836

3937
if (addToScene !== undefined)
4038
{
4139
config.add = addToScene;
4240
}
4341

44-
BuildGameObject(this.scene, mesh, config);
42+
BuildGameObject(this.scene, shader, config);
4543

46-
return mesh;
44+
return shader;
4745
});
4846

4947
// When registering a factory function 'this' refers to the GameObjectCreator context.

src/gameobjects/shader/ShaderFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var GameObjectFactory = require('../GameObjectFactory');
1616
* @webglOnly
1717
* @since 3.17.0
1818
*
19-
* @param {string} key -
19+
* @param {string} key - The key of the shader to use from the shader cache.
2020
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
2121
* @param {number} [y=0] - The vertical position of this Game Object in the world.
2222
* @param {number} [width=128] - The width of the Game Object.

src/gameobjects/shader/ShaderWebGLRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* This method should not be called directly. It is a utility function of the Render module.
1111
*
1212
* @method Phaser.GameObjects.Shader#renderWebGL
13-
* @since 3.0.0
13+
* @since 3.17.0
1414
* @private
1515
*
1616
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
@@ -57,6 +57,7 @@ var ShaderWebGLRenderer = function (renderer, src, interpolationPercentage, came
5757
}
5858

5959
src.load(calcMatrix.matrix);
60+
src.flush();
6061

6162
renderer.rebindPipeline(pipeline);
6263
};

0 commit comments

Comments
 (0)