|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2019 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../../utils/Class'); |
| 8 | + |
| 9 | +/** |
| 10 | + * @classdesc |
| 11 | + * |
| 12 | + * @class Shader |
| 13 | + * @memberof Phaser.Display |
| 14 | + * @constructor |
| 15 | + * @since 3.17.0 |
| 16 | + * |
| 17 | + * @param {string} key - The key of this shader. Must be unique within the shader cache. |
| 18 | + * @param {string} fragmentSrc - |
| 19 | + * @param {string} vertexSrc - |
| 20 | + * @param {any} uniforms - |
| 21 | + */ |
| 22 | +var Shader = new Class({ |
| 23 | + |
| 24 | + initialize: |
| 25 | + |
| 26 | + function Shader (key, fragmentSrc, vertexSrc, uniforms) |
| 27 | + { |
| 28 | + if (fragmentSrc === undefined) |
| 29 | + { |
| 30 | + fragmentSrc = [ |
| 31 | + 'precision mediump float;', |
| 32 | + |
| 33 | + 'uniform vec2 resolution;', |
| 34 | + |
| 35 | + 'varying vec2 fragCoord;', |
| 36 | + |
| 37 | + 'void main () {', |
| 38 | + ' vec2 uv = fragCoord / resolution.xy;', |
| 39 | + ' gl_FragColor = vec4(uv.xyx, 1.0);', |
| 40 | + '}' |
| 41 | + ].join('\n'); |
| 42 | + } |
| 43 | + |
| 44 | + if (vertexSrc === undefined) |
| 45 | + { |
| 46 | + vertexSrc = [ |
| 47 | + 'precision mediump float;', |
| 48 | + |
| 49 | + 'uniform mat4 uProjectionMatrix;', |
| 50 | + 'uniform mat4 uViewMatrix;', |
| 51 | + |
| 52 | + 'attribute vec2 inPosition;', |
| 53 | + |
| 54 | + 'varying vec2 fragCoord;', |
| 55 | + |
| 56 | + 'void main () {', |
| 57 | + 'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);', |
| 58 | + 'fragCoord = inPosition;', |
| 59 | + '}' |
| 60 | + ].join('\n'); |
| 61 | + } |
| 62 | + |
| 63 | + if (uniforms === undefined) { uniforms = {}; } |
| 64 | + |
| 65 | + /** |
| 66 | + * The key of this shader, unique within the shader cache of this Phaser game instance. |
| 67 | + * |
| 68 | + * @name Phaser.Display.Shader#key |
| 69 | + * @type {string} |
| 70 | + * @since 3.17.0 |
| 71 | + */ |
| 72 | + this.key = key; |
| 73 | + |
| 74 | + /** |
| 75 | + * The source code, as a string, of the fragment shader being used. |
| 76 | + * |
| 77 | + * @name Phaser.Display.Shader#fragmentSrc |
| 78 | + * @type {string} |
| 79 | + * @since 3.17.0 |
| 80 | + */ |
| 81 | + this.fragmentSrc = fragmentSrc; |
| 82 | + |
| 83 | + /** |
| 84 | + * The source code, as a string, of the vertex shader being used. |
| 85 | + * |
| 86 | + * @name Phaser.Display.Shader#vertexSrc |
| 87 | + * @type {string} |
| 88 | + * @since 3.17.0 |
| 89 | + */ |
| 90 | + this.vertexSrc = vertexSrc; |
| 91 | + |
| 92 | + /** |
| 93 | + * The default uniforms for this shader. |
| 94 | + * |
| 95 | + * @name Phaser.Display.Shader#uniforms |
| 96 | + * @type {any} |
| 97 | + * @since 3.17.0 |
| 98 | + */ |
| 99 | + this.uniforms = uniforms; |
| 100 | + } |
| 101 | + |
| 102 | +}); |
| 103 | + |
| 104 | +module.exports = Shader; |
0 commit comments