From a0ef6e9d1bba7ea9dc257106c39c367c8e237930 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 25 Apr 2019 15:07:46 +0100 Subject: [PATCH] Added default shader --- src/gameobjects/shader/Shader.js | 63 +++++++++++++++++++++++-- src/gameobjects/shader/ShaderFactory.js | 30 ++++-------- 2 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/gameobjects/shader/Shader.js b/src/gameobjects/shader/Shader.js index a9b8c3479..327ac860e 100644 --- a/src/gameobjects/shader/Shader.js +++ b/src/gameobjects/shader/Shader.js @@ -31,8 +31,12 @@ var TransformMatrix = require('../components/TransformMatrix'); * @extends Phaser.GameObjects.Components.Visible * * @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time. - * @param {number} x - The horizontal position of this Game Object in the world. - * @param {number} y - The vertical position of this Game Object in the world. + * @param {number} [x=0] - The horizontal position of this Game Object in the world. + * @param {number} [y=0] - The vertical position of this Game Object in the world. + * @param {number} [width=128] - The width of the Game Object. + * @param {number} [height=128] - The height of the Game Object. + * @param {string} [fragSource] - The source code of the fragment shader. + * @param {string} [vertSource] - The source code of the vertex shader. */ var Shader = new Class({ @@ -52,13 +56,56 @@ var Shader = new Class({ initialize: - function Shader (scene, x, y, width, height, vert, frag) + function Shader (scene, x, y, width, height, fragSource, vertSource) { + if (x === undefined) { x = 0; } + if (y === undefined) { y = 0; } + if (width === undefined) { width = 128; } + if (height === undefined) { height = 128; } + + if (fragSource === undefined) + { + fragSource = [ + 'precision mediump float;', + + 'uniform vec2 resolution;', + + 'varying vec2 fragCoord;', + + 'void main () {', + ' vec2 uv = fragCoord / resolution.xy;', + ' gl_FragColor = vec4(uv.xyx, 1.0);', + '}' + ].join('\n'); + } + + if (vertSource === undefined) + { + vertSource = [ + 'precision mediump float;', + + 'uniform mat4 uProjectionMatrix;', + 'uniform mat4 uViewMatrix;', + + 'attribute vec2 inPosition;', + + 'varying vec2 fragCoord;', + + 'void main () {', + 'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);', + 'fragCoord = inPosition;', + '}' + ].join('\n'); + } + GameObject.call(this, scene, 'Shader'); // This Game Object cannot have a blend mode, so skip all checks this.blendMode = -1; + this.vertSource = vertSource; + this.fragSource = fragSource; + this.vertexCount = 0; this.vertexCapacity = 6; @@ -214,13 +261,15 @@ var Shader = new Class({ this.setSize(width, height); this.setOrigin(0.5, 0.5); - this.setShader(vert, frag); + this.setShader(fragSource, vertSource); this.projOrtho(0, renderer.width, renderer.height, 0); }, - setShader: function (vertSource, fragSource) + setShader: function (fragSource, vertSource) { + if (vertSource === undefined) { vertSource = this.vertSource; } + var gl = this.gl; var renderer = this.renderer; @@ -235,6 +284,10 @@ var Shader = new Class({ renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix); this.program = program; + this.fragSource = fragSource; + this.vertSource = vertSource; + + return this; }, /** diff --git a/src/gameobjects/shader/ShaderFactory.js b/src/gameobjects/shader/ShaderFactory.js index a1113da34..fba6450e1 100644 --- a/src/gameobjects/shader/ShaderFactory.js +++ b/src/gameobjects/shader/ShaderFactory.js @@ -12,33 +12,23 @@ var GameObjectFactory = require('../GameObjectFactory'); * * Note: This method will only be available if the Shader Game Object and WebGL support have been built into Phaser. * - * @method Phaser.GameObjects.GameObjectFactory#mesh + * @method Phaser.GameObjects.GameObjectFactory#shader * @webglOnly - * @since 3.0.0 + * @since 3.17.0 * - * @param {number} x - The horizontal position of this Game Object in the world. - * @param {number} y - The vertical position of this Game Object in the world. - * @param {number[]} vertices - An array containing the vertices data for this Shader. - * @param {number[]} uv - An array containing the uv data for this Shader. - * @param {number[]} colors - An array containing the color data for this Shader. - * @param {number[]} alphas - An array containing the alpha data for this Shader. - * @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager. - * @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with. + * @param {number} [x=0] - The horizontal position of this Game Object in the world. + * @param {number} [y=0] - The vertical position of this Game Object in the world. + * @param {number} [width=128] - The width of the Game Object. + * @param {number} [height=128] - The height of the Game Object. + * @param {string} [fragSource] - The source code of the fragment shader. + * @param {string} [vertSource] - The source code of the vertex shader. * * @return {Phaser.GameObjects.Shader} The Game Object that was created. */ if (typeof WEBGL_RENDERER) { - GameObjectFactory.register('shader', function (x, y, width, height, vert, frag) + GameObjectFactory.register('shader', function (x, y, width, height, fragSource, vertSource) { - return this.displayList.add(new Shader(this.scene, x, y, width, height, vert, frag)); + return this.displayList.add(new Shader(this.scene, x, y, width, height, fragSource, vertSource)); }); } - -// When registering a factory function 'this' refers to the GameObjectFactory context. -// -// There are several properties available to use: -// -// this.scene - a reference to the Scene that owns the GameObjectFactory -// this.displayList - a reference to the Display List the Scene owns -// this.updateList - a reference to the Update List the Scene owns