2019-04-25 02:15:51 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
|
|
|
var GameObject = require('../GameObject');
|
2019-04-30 23:05:52 +00:00
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
2019-04-30 16:11:55 +00:00
|
|
|
var Merge = require('../../utils/object/Merge');
|
2019-05-01 11:30:09 +00:00
|
|
|
var SetValue = require('../../utils/object/SetValue');
|
2019-04-25 02:15:51 +00:00
|
|
|
var ShaderRender = require('./ShaderRender');
|
2019-04-25 10:30:23 +00:00
|
|
|
var TransformMatrix = require('../components/TransformMatrix');
|
2019-04-25 02:15:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A Shader Game Object.
|
2019-04-30 16:11:55 +00:00
|
|
|
*
|
2019-04-25 02:15:51 +00:00
|
|
|
* @class Shader
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
|
|
|
* @memberof Phaser.GameObjects
|
|
|
|
* @constructor
|
|
|
|
* @webglOnly
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
2019-04-25 13:03:50 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.ComputedSize
|
2019-04-25 02:15:51 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @extends Phaser.GameObjects.Components.GetBounds
|
|
|
|
* @extends Phaser.GameObjects.Components.Mask
|
|
|
|
* @extends Phaser.GameObjects.Components.Origin
|
2019-04-25 13:03:50 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.ScrollFactor
|
2019-04-25 02:15:51 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Transform
|
|
|
|
* @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.
|
2019-04-30 18:43:50 +00:00
|
|
|
* @param {string} key - The key of the shader to use from the shader cache.
|
2019-04-25 14:07:46 +00:00
|
|
|
* @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.
|
2019-04-25 02:15:51 +00:00
|
|
|
*/
|
|
|
|
var Shader = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
2019-04-25 13:03:50 +00:00
|
|
|
Components.ComputedSize,
|
2019-04-25 02:15:51 +00:00
|
|
|
Components.Depth,
|
|
|
|
Components.GetBounds,
|
|
|
|
Components.Mask,
|
|
|
|
Components.Origin,
|
2019-04-25 13:03:50 +00:00
|
|
|
Components.ScrollFactor,
|
2019-04-25 02:15:51 +00:00
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
ShaderRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2019-04-30 23:05:52 +00:00
|
|
|
function Shader (scene, key, x, y, width, height, textures)
|
2019-04-25 02:15:51 +00:00
|
|
|
{
|
2019-04-25 14:07:46 +00:00
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (width === undefined) { width = 128; }
|
|
|
|
if (height === undefined) { height = 128; }
|
|
|
|
|
2019-04-25 02:15:51 +00:00
|
|
|
GameObject.call(this, scene, 'Shader');
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* This Game Object cannot have a blend mode, so skip all checks.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#blendMode
|
|
|
|
* @type {integer}
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
this.blendMode = -1;
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
2019-04-30 16:11:55 +00:00
|
|
|
* The underlying shader object being used.
|
2019-04-30 18:11:29 +00:00
|
|
|
* Empty by default and set during a call to the `setShader` method.
|
2019-04-30 10:08:34 +00:00
|
|
|
*
|
2019-04-30 16:11:55 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#shader
|
|
|
|
* @type {Phaser.Display.Shader}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-30 16:11:55 +00:00
|
|
|
this.shader;
|
2019-04-25 14:07:46 +00:00
|
|
|
|
2019-04-25 13:03:50 +00:00
|
|
|
var renderer = scene.sys.renderer;
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* A reference to the current renderer.
|
|
|
|
* Shaders only work with the WebGL Renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#renderer
|
|
|
|
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
this.renderer = renderer;
|
2019-04-25 02:15:51 +00:00
|
|
|
|
2019-04-25 10:30:23 +00:00
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The WebGL context belonging to the renderer.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#gl
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {WebGLRenderingContext}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-30 10:08:34 +00:00
|
|
|
this.gl = renderer.gl;
|
2019-04-25 10:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* Raw byte buffer of vertices this Shader uses.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#vertexData
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {ArrayBuffer}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
2019-04-25 15:57:12 +00:00
|
|
|
this.vertexData = new ArrayBuffer(6 * (Float32Array.BYTES_PER_ELEMENT * 2));
|
2019-04-25 10:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The WebGL vertex buffer object this shader uses.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#vertexBuffer
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {WebGLBuffer}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
this.vertexBuffer = renderer.createVertexBuffer(this.vertexData.byteLength, this.gl.STREAM_DRAW);
|
2019-04-25 10:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The WebGL shader program this shader uses.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#program
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {WebGLProgram}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
this.program = null;
|
2019-04-25 10:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* Uint8 view to the vertex raw buffer. Used for uploading vertex buffer resources to the GPU.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#bytes
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {Uint8Array}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
|
|
|
this.bytes = new Uint8Array(this.vertexData);
|
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* Float32 view of the array buffer containing the shaders vertices.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#vertexViewF32
|
2019-04-25 10:30:23 +00:00
|
|
|
* @type {Float32Array}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
this.vertexViewF32 = new Float32Array(this.vertexData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A temporary Transform Matrix, re-used internally during batching.
|
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#_tempMatrix1
|
2019-04-25 10:30:23 +00:00
|
|
|
* @private
|
|
|
|
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
this._tempMatrix1 = new TransformMatrix();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A temporary Transform Matrix, re-used internally during batching.
|
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#_tempMatrix2
|
2019-04-25 10:30:23 +00:00
|
|
|
* @private
|
|
|
|
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
this._tempMatrix2 = new TransformMatrix();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A temporary Transform Matrix, re-used internally during batching.
|
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#_tempMatrix3
|
2019-04-25 10:30:23 +00:00
|
|
|
* @private
|
|
|
|
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
this._tempMatrix3 = new TransformMatrix();
|
|
|
|
|
2019-04-25 13:03:50 +00:00
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The view matrix the shader uses during rendering.
|
2019-04-25 13:03:50 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#viewMatrix
|
|
|
|
* @type {Float32Array}
|
2019-04-25 13:03:50 +00:00
|
|
|
* @readonly
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 13:03:50 +00:00
|
|
|
*/
|
|
|
|
this.viewMatrix = new Float32Array([ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]);
|
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The projection matrix the shader uses during rendering.
|
2019-04-25 13:03:50 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#projectionMatrix
|
|
|
|
* @type {Float32Array}
|
2019-04-25 13:03:50 +00:00
|
|
|
* @readonly
|
2019-04-30 10:08:34 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 13:03:50 +00:00
|
|
|
*/
|
|
|
|
this.projectionMatrix = new Float32Array([ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]);
|
|
|
|
|
2019-04-25 15:57:12 +00:00
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* The default uniform mappings. These can be added to (or replaced) by specifying your own uniforms when
|
|
|
|
* creating this shader game object. The uniforms are updated automatically during the render step.
|
|
|
|
*
|
|
|
|
* The defaults are:
|
|
|
|
*
|
|
|
|
* `resolution` (2f) - Set to the size of this shader.
|
|
|
|
* `time` (1f) - The elapsed game time, in seconds.
|
|
|
|
* `mouse` (2f) - If a pointer has been bound (with `setPointer`), this uniform contains its position each frame.
|
|
|
|
* `date` (4fv) - A vec4 containing the year, month, day and time in seconds.
|
|
|
|
* `sampleRate` (1f) - Sound sample rate. 44100 by default.
|
|
|
|
* `iChannel0...3` (sampler2D) - Input channels 0 to 3. `null` by default.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#uniforms
|
|
|
|
* @type {any}
|
|
|
|
* @since 3.17.0
|
2019-04-25 15:57:12 +00:00
|
|
|
*/
|
2019-04-30 16:11:55 +00:00
|
|
|
this.uniforms = {};
|
2019-04-25 15:57:12 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* The pointer bound to this shader, if any.
|
|
|
|
* Set via the chainable `setPointer` method, or by modifying this property directly.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#pointer
|
|
|
|
* @type {Phaser.Input.Pointer}
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 23:56:53 +00:00
|
|
|
this.pointer = null;
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* The cached width of the renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#_rendererWidth
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
this._rendererWidth = renderer.width;
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* The cached height of the renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Shader#_rendererHeight
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
this._rendererHeight = renderer.height;
|
2019-04-25 10:30:23 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
2019-05-01 11:30:09 +00:00
|
|
|
* Internal texture count tracker.
|
2019-04-30 10:08:34 +00:00
|
|
|
*
|
2019-05-01 11:30:09 +00:00
|
|
|
* @name Phaser.GameObjects.Shader#_textureCount
|
|
|
|
* @type {number}
|
2019-04-30 10:08:34 +00:00
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-05-01 11:30:09 +00:00
|
|
|
this._textureCount = 0;
|
2019-04-30 23:05:52 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSize(width, height);
|
|
|
|
this.setOrigin(0.5, 0.5);
|
2019-04-30 23:05:52 +00:00
|
|
|
this.setShader(key, textures);
|
2019-04-25 10:30:23 +00:00
|
|
|
},
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Sets the fragment and, optionally, the vertex shader source code that this Shader will use.
|
|
|
|
* This will immediately delete the active shader program, if set, and then create a new one
|
|
|
|
* with the given source. Finally, the shader uniforms are initialized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setShader
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
2019-04-30 16:11:55 +00:00
|
|
|
* @param {string} key - The key of the shader stored in the shader cache to use.
|
2019-04-30 10:08:34 +00:00
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
2019-04-30 23:05:52 +00:00
|
|
|
setShader: function (key, textures)
|
2019-04-25 10:30:23 +00:00
|
|
|
{
|
2019-04-30 23:05:52 +00:00
|
|
|
if (textures === undefined) { textures = []; }
|
|
|
|
|
2019-04-30 18:56:03 +00:00
|
|
|
var cache = this.scene.sys.cache.shader;
|
|
|
|
|
|
|
|
if (!cache.has(key))
|
|
|
|
{
|
|
|
|
console.warn('Shader missing: ' + key);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.shader = cache.get(key);
|
2019-04-25 14:07:46 +00:00
|
|
|
|
2019-04-25 13:03:50 +00:00
|
|
|
var gl = this.gl;
|
|
|
|
var renderer = this.renderer;
|
2019-04-25 10:30:23 +00:00
|
|
|
|
2019-04-25 13:03:50 +00:00
|
|
|
if (this.program)
|
|
|
|
{
|
|
|
|
gl.deleteProgram(this.program);
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:11:55 +00:00
|
|
|
var program = renderer.createProgram(this.shader.vertexSrc, this.shader.fragmentSrc);
|
2019-04-25 13:03:50 +00:00
|
|
|
|
|
|
|
renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
|
2019-04-25 13:21:33 +00:00
|
|
|
renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix);
|
2019-04-25 13:03:50 +00:00
|
|
|
|
|
|
|
this.program = program;
|
2019-04-30 16:11:55 +00:00
|
|
|
|
|
|
|
var d = new Date();
|
|
|
|
|
2019-04-30 23:05:52 +00:00
|
|
|
var defaultUniforms = {
|
2019-04-30 16:11:55 +00:00
|
|
|
resolution: { type: '2f', value: { x: this.width, y: this.height }},
|
|
|
|
time: { type: '1f', value: 0 },
|
2019-04-30 18:11:29 +00:00
|
|
|
mouse: { type: '2f', value: { x: this.width / 2, y: this.height / 2 } },
|
2019-04-30 16:11:55 +00:00
|
|
|
date: { type: '4fv', value: [ d.getFullYear(), d.getMonth(), d.getDate(), d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds() ] },
|
|
|
|
sampleRate: { type: '1f', value: 44100.0 },
|
|
|
|
iChannel0: { type: 'sampler2D', value: null, textureData: { repeat: true } },
|
|
|
|
iChannel1: { type: 'sampler2D', value: null, textureData: { repeat: true } },
|
|
|
|
iChannel2: { type: 'sampler2D', value: null, textureData: { repeat: true } },
|
|
|
|
iChannel3: { type: 'sampler2D', value: null, textureData: { repeat: true } }
|
2019-04-30 23:05:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (this.shader.uniforms)
|
|
|
|
{
|
|
|
|
this.uniforms = Merge(this.shader.uniforms, defaultUniforms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.uniforms = defaultUniforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (textures[i])
|
|
|
|
{
|
|
|
|
this.setSampler2D('iChannel' + i, textures[i], i);
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 14:07:46 +00:00
|
|
|
|
2019-04-25 16:13:31 +00:00
|
|
|
this.initUniforms();
|
|
|
|
|
2019-04-30 18:56:03 +00:00
|
|
|
this.projOrtho(0, renderer.width, renderer.height, 0);
|
|
|
|
|
2019-04-25 14:07:46 +00:00
|
|
|
return this;
|
2019-04-25 10:30:23 +00:00
|
|
|
},
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Binds a Phaser Pointer object to this Shader.
|
|
|
|
*
|
|
|
|
* The screen position of the pointer will be set in to the shaders `mouse` uniform
|
|
|
|
* automatically every frame. Call this method with no arguments to unbind the pointer.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setPointer
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Input.Pointer} [pointer] - The Pointer to bind to this shader.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
2019-04-25 23:56:53 +00:00
|
|
|
setPointer: function (pointer)
|
|
|
|
{
|
|
|
|
this.pointer = pointer;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2019-04-25 10:30:23 +00:00
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* Sets this shader to use an orthographic projection matrix.
|
|
|
|
* This matrix is stored locally in the `projectionMatrix` property,
|
|
|
|
* as well as being bound to the `uProjectionMatrix` uniform.
|
2019-04-25 13:03:50 +00:00
|
|
|
*
|
2019-04-30 10:08:34 +00:00
|
|
|
* @method Phaser.GameObjects.Shader#projOrtho
|
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-25 13:03:50 +00:00
|
|
|
* @param {number} left - The left value.
|
|
|
|
* @param {number} right - The right value.
|
|
|
|
* @param {number} bottom - The bottom value.
|
|
|
|
* @param {number} top - The top value.
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
projOrtho: function (left, right, bottom, top)
|
|
|
|
{
|
|
|
|
var near = -1000;
|
|
|
|
var far = 1000;
|
|
|
|
|
|
|
|
var leftRight = 1 / (left - right);
|
|
|
|
var bottomTop = 1 / (bottom - top);
|
|
|
|
var nearFar = 1 / (near - far);
|
|
|
|
|
|
|
|
var pm = this.projectionMatrix;
|
|
|
|
|
|
|
|
pm[0] = -2 * leftRight;
|
|
|
|
pm[5] = -2 * bottomTop;
|
|
|
|
pm[10] = 2 * nearFar;
|
|
|
|
pm[12] = (left + right) * leftRight;
|
|
|
|
pm[13] = (top + bottom) * bottomTop;
|
|
|
|
pm[14] = (far + near) * nearFar;
|
|
|
|
|
|
|
|
var program = this.program;
|
|
|
|
|
|
|
|
this.renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix);
|
|
|
|
|
|
|
|
this._rendererWidth = right;
|
|
|
|
this._rendererHeight = bottom;
|
|
|
|
},
|
|
|
|
|
2019-04-25 15:57:12 +00:00
|
|
|
// Uniforms are specified in the GLSL_ES Specification: http://www.khronos.org/registry/webgl/specs/latest/1.0/
|
|
|
|
// http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Initializes all of the uniforms this shader uses.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#initUniforms
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 15:57:12 +00:00
|
|
|
initUniforms: function ()
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
2019-05-01 11:30:09 +00:00
|
|
|
var map = this.renderer.glFuncMap;
|
2019-04-25 15:57:12 +00:00
|
|
|
var program = this.program;
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
this._textureCount = 0;
|
2019-04-30 23:05:52 +00:00
|
|
|
|
2019-04-25 15:57:12 +00:00
|
|
|
for (var key in this.uniforms)
|
|
|
|
{
|
|
|
|
var uniform = this.uniforms[key];
|
|
|
|
|
|
|
|
var type = uniform.type;
|
|
|
|
var data = map[type];
|
|
|
|
|
2019-04-30 23:05:52 +00:00
|
|
|
uniform.uniformLocation = gl.getUniformLocation(program, key);
|
|
|
|
|
|
|
|
if (type !== 'sampler2D')
|
2019-04-25 15:57:12 +00:00
|
|
|
{
|
|
|
|
uniform.glMatrix = data.matrix;
|
|
|
|
uniform.glValueLength = data.length;
|
|
|
|
uniform.glFunc = data.func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* Sets a sampler2D uniform on this shader.
|
|
|
|
*
|
|
|
|
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
|
|
|
|
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setSampler2D
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} uniformKey - The key of the sampler2D uniform to be updated, i.e. `iChannel0`.
|
|
|
|
* @param {string} textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
|
|
|
|
* @param {integer} [textureIndex=0] - The texture index.
|
|
|
|
* @param {any} [textureData] - Additional texture data.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setSampler2D: function (uniformKey, textureKey, textureIndex, textureData)
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
|
|
|
if (textureIndex === undefined) { textureIndex = 0; }
|
|
|
|
|
|
|
|
var textureManager = this.scene.sys.textures;
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
if (textureManager.exists(textureKey))
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
2019-05-01 11:30:09 +00:00
|
|
|
var frame = textureManager.getFrame(textureKey);
|
2019-04-30 23:05:52 +00:00
|
|
|
var uniform = this.uniforms[uniformKey];
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
uniform.textureKey = textureKey;
|
2019-04-30 23:05:52 +00:00
|
|
|
uniform.source = frame.source.image;
|
|
|
|
uniform.value = frame.glTexture;
|
|
|
|
|
|
|
|
if (textureData)
|
|
|
|
{
|
|
|
|
uniform.textureData = textureData;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
this._textureCount = textureIndex;
|
2019-04-30 23:05:52 +00:00
|
|
|
|
|
|
|
this.initSampler2D(uniform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* Sets a property of a uniform already present on this shader.
|
|
|
|
*
|
|
|
|
* To modify the value of a uniform such as a 1f or 1i use the `value` property directly:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* shader.setUniform('size.value', 16);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can use dot notation to access deeper values, for example:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* shader.setUniform('resolution.value.x', 512);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The change to the uniform will take effect the next time the shader is rendered.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setUniform
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} key - The key of the uniform to modify. Use dots for deep properties, i.e. `resolution.value.x`.
|
|
|
|
* @param {any} value - The value to set into the uniform.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setUniform: function (key, value)
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
2019-05-01 11:30:09 +00:00
|
|
|
SetValue(this.uniforms, key, value);
|
|
|
|
|
|
|
|
return this;
|
2019-04-30 23:05:52 +00:00
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* A short-cut method that will directly set the texture being used by the `iChannel0` sampler2D uniform.
|
|
|
|
*
|
|
|
|
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
|
|
|
|
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setChannel0
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
|
|
|
|
* @param {any} [textureData] - Additional texture data.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setChannel0: function (textureKey, textureData)
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
2019-05-01 11:30:09 +00:00
|
|
|
return this.setSampler2D('iChannel0', textureKey, 0, textureData);
|
2019-04-30 23:05:52 +00:00
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* A short-cut method that will directly set the texture being used by the `iChannel1` sampler2D uniform.
|
|
|
|
*
|
|
|
|
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
|
|
|
|
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setChannel1
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
|
|
|
|
* @param {any} [textureData] - Additional texture data.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setChannel1: function (textureKey, textureData)
|
|
|
|
{
|
|
|
|
return this.setSampler2D('iChannel1', textureKey, 1, textureData);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A short-cut method that will directly set the texture being used by the `iChannel2` sampler2D uniform.
|
|
|
|
*
|
|
|
|
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
|
|
|
|
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setChannel2
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
|
|
|
|
* @param {any} [textureData] - Additional texture data.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setChannel2: function (textureKey, textureData)
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
2019-05-01 11:30:09 +00:00
|
|
|
return this.setSampler2D('iChannel2', textureKey, 2, textureData);
|
2019-04-30 23:05:52 +00:00
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* A short-cut method that will directly set the texture being used by the `iChannel3` sampler2D uniform.
|
|
|
|
*
|
|
|
|
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
|
|
|
|
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setChannel3
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {string} textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
|
|
|
|
* @param {any} [textureData] - Additional texture data.
|
|
|
|
*
|
|
|
|
* @return {this} This Shader instance.
|
|
|
|
*/
|
|
|
|
setChannel3: function (textureKey, textureData)
|
2019-04-30 23:05:52 +00:00
|
|
|
{
|
2019-05-01 11:30:09 +00:00
|
|
|
return this.setSampler2D('iChannel3', textureKey, 3, textureData);
|
2019-04-30 23:05:52 +00:00
|
|
|
},
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
/**
|
|
|
|
* Internal method that takes a sampler2D uniform and prepares it for use by setting the
|
|
|
|
* gl texture parameters.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#initSampler2D
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {any} uniform - The sampler2D uniform to process.
|
|
|
|
*/
|
2019-04-30 23:05:52 +00:00
|
|
|
initSampler2D: function (uniform)
|
|
|
|
{
|
|
|
|
if (!uniform.value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var gl = this.gl;
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
gl.activeTexture(gl.TEXTURE0 + this._textureCount);
|
2019-04-30 23:05:52 +00:00
|
|
|
gl.bindTexture(gl.TEXTURE_2D, uniform.value);
|
|
|
|
|
|
|
|
// Extended texture data
|
|
|
|
|
|
|
|
var data = uniform.textureData;
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texImage2D
|
|
|
|
|
|
|
|
// mag / minFilter can be: gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR or gl.NEAREST
|
|
|
|
// wrapS/T can be: gl.CLAMP_TO_EDGE or gl.REPEAT
|
|
|
|
// format can be: gl.LUMINANCE or gl.RGBA
|
|
|
|
|
|
|
|
var magFilter = gl[GetFastValue(data, 'magFilter', 'linear').toUpperCase()];
|
|
|
|
var minFilter = gl[GetFastValue(data, 'minFilter', 'linear').toUpperCase()];
|
|
|
|
var wrapS = gl[GetFastValue(data, 'wrapS', 'repeat').toUpperCase()];
|
|
|
|
var wrapT = gl[GetFastValue(data, 'wrapT', 'repeat').toUpperCase()];
|
|
|
|
var format = gl[GetFastValue(data, 'format', 'rgba').toUpperCase()];
|
|
|
|
|
|
|
|
if (data.repeat)
|
|
|
|
{
|
|
|
|
wrapS = gl.REPEAT;
|
|
|
|
wrapT = gl.REPEAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, !!data.flipY);
|
|
|
|
|
|
|
|
if (data.width)
|
|
|
|
{
|
|
|
|
var width = GetFastValue(data, 'width', 512);
|
|
|
|
var height = GetFastValue(data, 'height', 2);
|
|
|
|
var border = GetFastValue(data, 'border', 0);
|
|
|
|
|
|
|
|
// texImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, ArrayBufferView? pixels)
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, border, format, gl.UNSIGNED_BYTE, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// texImage2D(GLenum target, GLint level, GLenum internalformat, GLenum format, GLenum type, ImageData? pixels)
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, format, gl.RGBA, gl.UNSIGNED_BYTE, uniform.source);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.renderer.setProgram(this.program);
|
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
gl.uniform1i(uniform.uniformLocation, this._textureCount);
|
2019-04-30 23:05:52 +00:00
|
|
|
|
2019-05-01 11:30:09 +00:00
|
|
|
this._textureCount++;
|
2019-04-30 23:05:52 +00:00
|
|
|
},
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Synchronizes all of the uniforms this shader uses.
|
|
|
|
* Each uniforms gl function is called in turn.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#syncUniforms
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
2019-04-25 15:57:12 +00:00
|
|
|
syncUniforms: function ()
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
|
2019-04-30 18:11:29 +00:00
|
|
|
var uniforms = this.uniforms;
|
2019-04-25 15:57:12 +00:00
|
|
|
var uniform;
|
|
|
|
var length;
|
|
|
|
var glFunc;
|
|
|
|
var location;
|
|
|
|
var value;
|
2019-04-30 23:05:52 +00:00
|
|
|
var textureCount = 0;
|
2019-04-25 15:57:12 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
for (var key in uniforms)
|
2019-04-25 15:57:12 +00:00
|
|
|
{
|
2019-04-30 10:08:34 +00:00
|
|
|
uniform = uniforms[key];
|
2019-04-25 15:57:12 +00:00
|
|
|
|
|
|
|
glFunc = uniform.glFunc;
|
|
|
|
length = uniform.glValueLength;
|
|
|
|
location = uniform.uniformLocation;
|
|
|
|
value = uniform.value;
|
|
|
|
|
|
|
|
if (length === 1)
|
|
|
|
{
|
|
|
|
if (uniform.glMatrix)
|
|
|
|
{
|
|
|
|
glFunc.call(gl, location, uniform.transpose, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glFunc.call(gl, location, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (length === 2)
|
|
|
|
{
|
|
|
|
glFunc.call(gl, location, value.x, value.y);
|
|
|
|
}
|
|
|
|
else if (length === 3)
|
|
|
|
{
|
|
|
|
glFunc.call(gl, location, value.x, value.y, value.z);
|
|
|
|
}
|
|
|
|
else if (length === 4)
|
|
|
|
{
|
|
|
|
glFunc.call(gl, location, value.x, value.y, value.z, value.w);
|
|
|
|
}
|
|
|
|
else if (uniform.type === 'sampler2D')
|
|
|
|
{
|
2019-04-30 23:05:52 +00:00
|
|
|
gl.activeTexture(gl['TEXTURE' + textureCount]);
|
2019-04-25 15:57:12 +00:00
|
|
|
|
2019-04-30 23:05:52 +00:00
|
|
|
gl.bindTexture(gl.TEXTURE_2D, value);
|
|
|
|
|
|
|
|
gl.uniform1i(location, textureCount);
|
|
|
|
|
|
|
|
textureCount++;
|
2019-04-25 15:57:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Called automatically during render.
|
|
|
|
*
|
|
|
|
* This method performs matrix ITRS and then stores the resulting value in the `uViewMatrix` uniform.
|
2019-04-30 18:43:50 +00:00
|
|
|
* It then sets up the vertex buffer and shader, updates and syncs the uniforms ready
|
|
|
|
* for flush to be called.
|
2019-04-30 10:08:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#load
|
|
|
|
* @since 3.17.0
|
2019-04-30 18:43:50 +00:00
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.Components.TransformMatrix} matrix2D - The transform matrix to use during rendering.
|
2019-04-30 10:08:34 +00:00
|
|
|
*/
|
2019-04-25 13:03:50 +00:00
|
|
|
load: function (matrix2D)
|
2019-04-25 10:30:23 +00:00
|
|
|
{
|
2019-04-25 13:03:50 +00:00
|
|
|
// ITRS
|
|
|
|
|
2019-04-30 18:43:50 +00:00
|
|
|
var width = this.width;
|
|
|
|
var height = this.height;
|
|
|
|
var renderer = this.renderer;
|
2019-04-25 13:03:50 +00:00
|
|
|
var program = this.program;
|
|
|
|
|
|
|
|
var x = -this._displayOriginX;
|
2019-04-25 14:08:32 +00:00
|
|
|
var y = -this._displayOriginY;
|
2019-04-25 13:03:50 +00:00
|
|
|
|
|
|
|
var vm = this.viewMatrix;
|
|
|
|
|
|
|
|
vm[0] = matrix2D[0];
|
|
|
|
vm[1] = matrix2D[1];
|
|
|
|
vm[4] = matrix2D[2];
|
|
|
|
vm[5] = matrix2D[3];
|
|
|
|
vm[8] = matrix2D[4];
|
|
|
|
vm[9] = matrix2D[5];
|
|
|
|
vm[12] = vm[0] * x + vm[4] * y;
|
|
|
|
vm[13] = vm[1] * x + vm[5] * y;
|
|
|
|
|
|
|
|
this.renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
|
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
// Update common uniforms
|
|
|
|
|
|
|
|
var uniforms = this.uniforms;
|
|
|
|
var res = uniforms.resolution;
|
|
|
|
|
|
|
|
res.value.x = width;
|
|
|
|
res.value.y = height;
|
|
|
|
|
|
|
|
uniforms.time.value = renderer.game.loop.getDuration();
|
2019-04-25 15:57:12 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
var pointer = this.pointer;
|
2019-04-25 10:30:23 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
if (pointer)
|
2019-04-25 23:56:53 +00:00
|
|
|
{
|
2019-04-30 10:08:34 +00:00
|
|
|
var mouse = uniforms.mouse;
|
|
|
|
|
|
|
|
var px = pointer.x / width;
|
|
|
|
var py = 1 - pointer.y / height;
|
2019-04-25 23:56:53 +00:00
|
|
|
|
2019-04-30 10:08:34 +00:00
|
|
|
mouse.value.x = px.toFixed(2);
|
|
|
|
mouse.value.y = py.toFixed(2);
|
2019-04-25 23:56:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 15:57:12 +00:00
|
|
|
this.syncUniforms();
|
2019-04-30 18:43:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called automatically during render.
|
|
|
|
*
|
|
|
|
* Sets the active shader, loads the vertex buffer and then draws.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#flush
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
flush: function ()
|
|
|
|
{
|
|
|
|
// Bind
|
|
|
|
|
|
|
|
var width = this.width;
|
|
|
|
var height = this.height;
|
|
|
|
var program = this.program;
|
|
|
|
|
|
|
|
var gl = this.gl;
|
|
|
|
var vertexBuffer = this.vertexBuffer;
|
|
|
|
var renderer = this.renderer;
|
|
|
|
var vertexSize = Float32Array.BYTES_PER_ELEMENT * 2;
|
|
|
|
|
|
|
|
renderer.setProgram(program);
|
|
|
|
renderer.setVertexBuffer(vertexBuffer);
|
|
|
|
|
|
|
|
var location = gl.getAttribLocation(program, 'inPosition');
|
|
|
|
|
|
|
|
if (location !== -1)
|
|
|
|
{
|
|
|
|
gl.enableVertexAttribArray(location);
|
|
|
|
|
|
|
|
gl.vertexAttribPointer(location, 2, gl.FLOAT, false, vertexSize, 0);
|
|
|
|
}
|
2019-04-25 10:30:23 +00:00
|
|
|
|
2019-04-25 13:03:50 +00:00
|
|
|
// Draw
|
|
|
|
|
|
|
|
var vf = this.vertexViewF32;
|
|
|
|
|
2019-04-25 13:21:33 +00:00
|
|
|
vf[3] = height;
|
|
|
|
vf[4] = width;
|
|
|
|
vf[5] = height;
|
|
|
|
vf[8] = width;
|
|
|
|
vf[9] = height;
|
|
|
|
vf[10] = width;
|
2019-04-25 13:03:50 +00:00
|
|
|
|
|
|
|
// Flush
|
|
|
|
|
2019-04-25 10:30:23 +00:00
|
|
|
var vertexCount = 6;
|
|
|
|
|
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.bytes.subarray(0, vertexCount * vertexSize));
|
|
|
|
|
2019-04-25 15:57:12 +00:00
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, vertexCount);
|
2019-04-25 10:30:23 +00:00
|
|
|
},
|
|
|
|
|
2019-04-29 16:14:05 +00:00
|
|
|
/**
|
|
|
|
* A NOOP method so you can pass a Shader to a Container.
|
|
|
|
* Calling this method will do nothing. It is intentionally empty.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setAlpha
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
setAlpha: function ()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-04-30 10:08:34 +00:00
|
|
|
* A NOOP method so you can pass a Shader to a Container.
|
2019-04-29 16:14:05 +00:00
|
|
|
* Calling this method will do nothing. It is intentionally empty.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Shader#setBlendMode
|
|
|
|
* @private
|
|
|
|
* @since 3.17.0
|
|
|
|
*/
|
|
|
|
setBlendMode: function ()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
|
2019-04-25 10:30:23 +00:00
|
|
|
/**
|
2019-04-30 18:43:50 +00:00
|
|
|
* Internal destroy handler, called as part of the destroy process.
|
2019-04-25 10:30:23 +00:00
|
|
|
*
|
2019-04-30 18:43:50 +00:00
|
|
|
* @method Phaser.GameObjects.RenderTexture#preDestroy
|
|
|
|
* @protected
|
2019-05-01 11:30:09 +00:00
|
|
|
* @since 3.17.0
|
2019-04-25 10:30:23 +00:00
|
|
|
*/
|
2019-04-30 18:43:50 +00:00
|
|
|
preDestroy: function ()
|
2019-04-25 10:30:23 +00:00
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
|
|
|
|
gl.deleteProgram(this.program);
|
|
|
|
gl.deleteBuffer(this.vertexBuffer);
|
2019-04-25 02:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Shader;
|