|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @author Mat Groves (@Doormat23) |
| 4 | +* @copyright 2016 Photon Storm Ltd. |
| 5 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 6 | +*/ |
| 7 | + |
| 8 | +/** |
| 9 | +* New version of PIXI.WebGLShaderManager |
| 10 | +* |
| 11 | +* @class Phaser.Renderer.Canvas |
| 12 | +* @constructor |
| 13 | +* @param {Phaser.Game} game - Game reference to the currently running game. |
| 14 | +*/ |
| 15 | +Phaser.Renderer.WebGL.ShaderManager = function (renderer) |
| 16 | +{ |
| 17 | + this.renderer = renderer; |
| 18 | + |
| 19 | + this.gl = null; |
| 20 | + |
| 21 | + this.maxAttribs = 10; |
| 22 | + |
| 23 | + this.attribState = []; |
| 24 | + |
| 25 | + this.tempAttribState = []; |
| 26 | + |
| 27 | + this.stack = []; |
| 28 | + |
| 29 | + this._currentId = -1; |
| 30 | + this.currentShader = null; |
| 31 | + |
| 32 | + this.primitiveShader = null; |
| 33 | + this.complexPrimitiveShader = null; |
| 34 | + this.defaultShader = null; |
| 35 | + this.fastShader = null; |
| 36 | + this.stripShader = null; |
| 37 | + |
| 38 | + for (var i = 0; i < this.maxAttribs; i++) |
| 39 | + { |
| 40 | + this.attribState[i] = false; |
| 41 | + } |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | +Phaser.Renderer.WebGL.ShaderManager.prototype.constructor = Phaser.Renderer.WebGL.ShaderManager; |
| 46 | + |
| 47 | +Phaser.Renderer.WebGL.ShaderManager.prototype = { |
| 48 | + |
| 49 | + init: function () |
| 50 | + { |
| 51 | + this.gl = this.renderer.gl; |
| 52 | + |
| 53 | + // the next one is used for rendering primitives |
| 54 | + // this.primitiveShader = new PIXI.PrimitiveShader(this); |
| 55 | + |
| 56 | + // the next one is used for rendering triangle strips |
| 57 | + // this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(this); |
| 58 | + |
| 59 | + // this shader is used for the default sprite rendering |
| 60 | + this.defaultShader = new Phaser.Renderer.WebGL.Shaders.Sprite(this.renderer); |
| 61 | + |
| 62 | + // this shader is used for the fast sprite rendering |
| 63 | + // this.fastShader = new PIXI.PixiFastShader(this); |
| 64 | + |
| 65 | + // the next one is used for rendering triangle strips |
| 66 | + // this.stripShader = new PIXI.StripShader(this); |
| 67 | + |
| 68 | + this.setShader(this.defaultShader); |
| 69 | + }, |
| 70 | + |
| 71 | + setAttribs: function (attribs) |
| 72 | + { |
| 73 | + // reset temp state |
| 74 | + var i; |
| 75 | + |
| 76 | + for (i = 0; i < this.tempAttribState.length; i++) |
| 77 | + { |
| 78 | + this.tempAttribState[i] = false; |
| 79 | + } |
| 80 | + |
| 81 | + // set the new attribs |
| 82 | + for (i = 0; i < attribs.length; i++) |
| 83 | + { |
| 84 | + var attribId = attribs[i]; |
| 85 | + this.tempAttribState[attribId] = true; |
| 86 | + } |
| 87 | + |
| 88 | + for (i = 0; i < this.attribState.length; i++) |
| 89 | + { |
| 90 | + if (this.attribState[i] !== this.tempAttribState[i]) |
| 91 | + { |
| 92 | + this.attribState[i] = this.tempAttribState[i]; |
| 93 | + |
| 94 | + if (this.tempAttribState[i]) |
| 95 | + { |
| 96 | + this.gl.enableVertexAttribArray(i); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + this.gl.disableVertexAttribArray(i); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + setShader: function (shader) |
| 107 | + { |
| 108 | + if (this._currentId === shader._UID) |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + this._currentId = shader._UID; |
| 114 | + |
| 115 | + this.currentShader = shader; |
| 116 | + |
| 117 | + this.gl.useProgram(shader.program); |
| 118 | + |
| 119 | + this.setAttribs(shader.attributes); |
| 120 | + |
| 121 | + return true; |
| 122 | + }, |
| 123 | + |
| 124 | + destroy: function () |
| 125 | + { |
| 126 | + this.renderer = null; |
| 127 | + this.gl = null; |
| 128 | + |
| 129 | + this.attribState = []; |
| 130 | + this.tempAttribState = []; |
| 131 | + this.stack = []; |
| 132 | + |
| 133 | + this.currentShader = null; |
| 134 | + |
| 135 | + this.primitiveShader.destroy(); |
| 136 | + this.complexPrimitiveShader.destroy(); |
| 137 | + this.defaultShader.destroy(); |
| 138 | + this.fastShader.destroy(); |
| 139 | + this.stripShader.destroy(); |
| 140 | + } |
| 141 | + |
| 142 | +}; |
0 commit comments