diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/utils/WebGLMaskManager.js b/src/renderers/webgl/utils/WebGLMaskManager.js index 607d5dd..f89ffce 100644 --- a/src/renderers/webgl/utils/WebGLMaskManager.js +++ b/src/renderers/webgl/utils/WebGLMaskManager.js @@ -1,69 +1,56 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLMaskManager -* @constructor -* @private -*/ -PIXI.WebGLMaskManager = function() -{ +function WebGLMaskManager() { }; -PIXI.WebGLMaskManager.prototype.constructor = PIXI.WebGLMaskManager; +WebGLMaskManager.prototype.constructor = WebGLMaskManager; +module.exports = WebGLMaskManager; /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLMaskManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLMaskManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param maskData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLMaskManager.prototype.pushMask = function(maskData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param maskData {Array} + * @param renderSession {object} + */ +WebGLMaskManager.prototype.pushMask = function (maskData, renderSession) { var gl = renderSession.gl; - if(maskData.dirty) - { - PIXI.WebGLGraphics.updateGraphics(maskData, gl); + if (maskData.dirty) { + WebGLGraphics.updateGraphics(maskData, gl); } - if(!maskData._webGL[gl.id].data.length)return; + if (!maskData._webGL[gl.id].data.length)return; renderSession.stencilManager.pushStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popMask -* @param maskData {Array} -* @param renderSession {Object} an object containing all the useful parameters -*/ -PIXI.WebGLMaskManager.prototype.popMask = function(maskData, renderSession) -{ + * Removes the last filter from the filter stack and doesn't return it. + * + * @param maskData {Array} + * @param renderSession {object} an object containing all the useful parameters + */ +WebGLMaskManager.prototype.popMask = function (maskData, renderSession) { var gl = this.gl; renderSession.stencilManager.popStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLMaskManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLMaskManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/utils/WebGLMaskManager.js b/src/renderers/webgl/utils/WebGLMaskManager.js index 607d5dd..f89ffce 100644 --- a/src/renderers/webgl/utils/WebGLMaskManager.js +++ b/src/renderers/webgl/utils/WebGLMaskManager.js @@ -1,69 +1,56 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLMaskManager -* @constructor -* @private -*/ -PIXI.WebGLMaskManager = function() -{ +function WebGLMaskManager() { }; -PIXI.WebGLMaskManager.prototype.constructor = PIXI.WebGLMaskManager; +WebGLMaskManager.prototype.constructor = WebGLMaskManager; +module.exports = WebGLMaskManager; /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLMaskManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLMaskManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param maskData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLMaskManager.prototype.pushMask = function(maskData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param maskData {Array} + * @param renderSession {object} + */ +WebGLMaskManager.prototype.pushMask = function (maskData, renderSession) { var gl = renderSession.gl; - if(maskData.dirty) - { - PIXI.WebGLGraphics.updateGraphics(maskData, gl); + if (maskData.dirty) { + WebGLGraphics.updateGraphics(maskData, gl); } - if(!maskData._webGL[gl.id].data.length)return; + if (!maskData._webGL[gl.id].data.length)return; renderSession.stencilManager.pushStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popMask -* @param maskData {Array} -* @param renderSession {Object} an object containing all the useful parameters -*/ -PIXI.WebGLMaskManager.prototype.popMask = function(maskData, renderSession) -{ + * Removes the last filter from the filter stack and doesn't return it. + * + * @param maskData {Array} + * @param renderSession {object} an object containing all the useful parameters + */ +WebGLMaskManager.prototype.popMask = function (maskData, renderSession) { var gl = this.gl; renderSession.stencilManager.popStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLMaskManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLMaskManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLShaderManager.js b/src/renderers/webgl/utils/WebGLShaderManager.js index a15974e..de37591 100644 --- a/src/renderers/webgl/utils/WebGLShaderManager.js +++ b/src/renderers/webgl/utils/WebGLShaderManager.js @@ -1,111 +1,92 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLShaderManager -* @constructor -* @private -*/ -PIXI.WebGLShaderManager = function() -{ +function WebGLShaderManager() { /** - * @property maxAttibs - * @type Number + * @member {number} */ this.maxAttibs = 10; /** - * @property attribState - * @type Array + * @member {Array} */ this.attribState = []; /** - * @property tempAttribState - * @type Array + * @member {Array} */ this.tempAttribState = []; - for (var i = 0; i < this.maxAttibs; i++) - { + for (var i = 0; i < this.maxAttibs; i++) { this.attribState[i] = false; } /** - * @property stack - * @type Array + * @member {Array} */ this.stack = []; }; -PIXI.WebGLShaderManager.prototype.constructor = PIXI.WebGLShaderManager; +WebGLShaderManager.prototype.constructor = WebGLShaderManager; +module.exports = WebGLShaderManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLShaderManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLShaderManager.prototype.setContext = function (gl) { this.gl = gl; - + // the next one is used for rendering primitives - this.primitiveShader = new PIXI.PrimitiveShader(gl); + this.primitiveShader = new PrimitiveShader(gl); // the next one is used for rendering triangle strips - this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(gl); + this.complexPrimitiveShader = new ComplexPrimitiveShader(gl); // this shader is used for the default sprite rendering - this.defaultShader = new PIXI.PixiShader(gl); + this.defaultShader = new PixiShader(gl); // this shader is used for the fast sprite rendering - this.fastShader = new PIXI.PixiFastShader(gl); + this.fastShader = new PixiFastShader(gl); // the next one is used for rendering triangle strips - this.stripShader = new PIXI.StripShader(gl); + this.stripShader = new StripShader(gl); this.setShader(this.defaultShader); }; /** -* Takes the attributes given in parameters. -* -* @method setAttribs -* @param attribs {Array} attribs -*/ -PIXI.WebGLShaderManager.prototype.setAttribs = function(attribs) -{ + * Takes the attributes given in parameters. + * + * @param attribs {Array} attribs + */ +WebGLShaderManager.prototype.setAttribs = function (attribs) { // reset temp state var i; - for (i = 0; i < this.tempAttribState.length; i++) - { + for (i = 0; i < this.tempAttribState.length; i++) { this.tempAttribState[i] = false; } // set the new attribs - for (i = 0; i < attribs.length; i++) - { + for (i = 0; i < attribs.length; i++) { var attribId = attribs[i]; this.tempAttribState[attribId] = true; } var gl = this.gl; - for (i = 0; i < this.attribState.length; i++) - { - if(this.attribState[i] !== this.tempAttribState[i]) - { + for (i = 0; i < this.attribState.length; i++) { + if (this.attribState[i] !== this.tempAttribState[i]) { this.attribState[i] = this.tempAttribState[i]; - if(this.tempAttribState[i]) - { + if (this.tempAttribState[i]) { gl.enableVertexAttribArray(i); } - else - { + else { gl.disableVertexAttribArray(i); } } @@ -113,15 +94,13 @@ }; /** -* Sets the current shader. -* -* @method setShader -* @param shader {Any} -*/ -PIXI.WebGLShaderManager.prototype.setShader = function(shader) -{ - if(this._currentId === shader._UID)return false; - + * Sets the current shader. + * + * @param shader {Any} + */ +WebGLShaderManager.prototype.setShader = function (shader) { + if (this._currentId === shader._UID)return false; + this._currentId = shader._UID; this.currentShader = shader; @@ -133,12 +112,10 @@ }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLShaderManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLShaderManager.prototype.destroy = function () { this.attribState = null; this.tempAttribState = null; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/utils/WebGLMaskManager.js b/src/renderers/webgl/utils/WebGLMaskManager.js index 607d5dd..f89ffce 100644 --- a/src/renderers/webgl/utils/WebGLMaskManager.js +++ b/src/renderers/webgl/utils/WebGLMaskManager.js @@ -1,69 +1,56 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLMaskManager -* @constructor -* @private -*/ -PIXI.WebGLMaskManager = function() -{ +function WebGLMaskManager() { }; -PIXI.WebGLMaskManager.prototype.constructor = PIXI.WebGLMaskManager; +WebGLMaskManager.prototype.constructor = WebGLMaskManager; +module.exports = WebGLMaskManager; /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLMaskManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLMaskManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param maskData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLMaskManager.prototype.pushMask = function(maskData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param maskData {Array} + * @param renderSession {object} + */ +WebGLMaskManager.prototype.pushMask = function (maskData, renderSession) { var gl = renderSession.gl; - if(maskData.dirty) - { - PIXI.WebGLGraphics.updateGraphics(maskData, gl); + if (maskData.dirty) { + WebGLGraphics.updateGraphics(maskData, gl); } - if(!maskData._webGL[gl.id].data.length)return; + if (!maskData._webGL[gl.id].data.length)return; renderSession.stencilManager.pushStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popMask -* @param maskData {Array} -* @param renderSession {Object} an object containing all the useful parameters -*/ -PIXI.WebGLMaskManager.prototype.popMask = function(maskData, renderSession) -{ + * Removes the last filter from the filter stack and doesn't return it. + * + * @param maskData {Array} + * @param renderSession {object} an object containing all the useful parameters + */ +WebGLMaskManager.prototype.popMask = function (maskData, renderSession) { var gl = this.gl; renderSession.stencilManager.popStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLMaskManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLMaskManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLShaderManager.js b/src/renderers/webgl/utils/WebGLShaderManager.js index a15974e..de37591 100644 --- a/src/renderers/webgl/utils/WebGLShaderManager.js +++ b/src/renderers/webgl/utils/WebGLShaderManager.js @@ -1,111 +1,92 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLShaderManager -* @constructor -* @private -*/ -PIXI.WebGLShaderManager = function() -{ +function WebGLShaderManager() { /** - * @property maxAttibs - * @type Number + * @member {number} */ this.maxAttibs = 10; /** - * @property attribState - * @type Array + * @member {Array} */ this.attribState = []; /** - * @property tempAttribState - * @type Array + * @member {Array} */ this.tempAttribState = []; - for (var i = 0; i < this.maxAttibs; i++) - { + for (var i = 0; i < this.maxAttibs; i++) { this.attribState[i] = false; } /** - * @property stack - * @type Array + * @member {Array} */ this.stack = []; }; -PIXI.WebGLShaderManager.prototype.constructor = PIXI.WebGLShaderManager; +WebGLShaderManager.prototype.constructor = WebGLShaderManager; +module.exports = WebGLShaderManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLShaderManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLShaderManager.prototype.setContext = function (gl) { this.gl = gl; - + // the next one is used for rendering primitives - this.primitiveShader = new PIXI.PrimitiveShader(gl); + this.primitiveShader = new PrimitiveShader(gl); // the next one is used for rendering triangle strips - this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(gl); + this.complexPrimitiveShader = new ComplexPrimitiveShader(gl); // this shader is used for the default sprite rendering - this.defaultShader = new PIXI.PixiShader(gl); + this.defaultShader = new PixiShader(gl); // this shader is used for the fast sprite rendering - this.fastShader = new PIXI.PixiFastShader(gl); + this.fastShader = new PixiFastShader(gl); // the next one is used for rendering triangle strips - this.stripShader = new PIXI.StripShader(gl); + this.stripShader = new StripShader(gl); this.setShader(this.defaultShader); }; /** -* Takes the attributes given in parameters. -* -* @method setAttribs -* @param attribs {Array} attribs -*/ -PIXI.WebGLShaderManager.prototype.setAttribs = function(attribs) -{ + * Takes the attributes given in parameters. + * + * @param attribs {Array} attribs + */ +WebGLShaderManager.prototype.setAttribs = function (attribs) { // reset temp state var i; - for (i = 0; i < this.tempAttribState.length; i++) - { + for (i = 0; i < this.tempAttribState.length; i++) { this.tempAttribState[i] = false; } // set the new attribs - for (i = 0; i < attribs.length; i++) - { + for (i = 0; i < attribs.length; i++) { var attribId = attribs[i]; this.tempAttribState[attribId] = true; } var gl = this.gl; - for (i = 0; i < this.attribState.length; i++) - { - if(this.attribState[i] !== this.tempAttribState[i]) - { + for (i = 0; i < this.attribState.length; i++) { + if (this.attribState[i] !== this.tempAttribState[i]) { this.attribState[i] = this.tempAttribState[i]; - if(this.tempAttribState[i]) - { + if (this.tempAttribState[i]) { gl.enableVertexAttribArray(i); } - else - { + else { gl.disableVertexAttribArray(i); } } @@ -113,15 +94,13 @@ }; /** -* Sets the current shader. -* -* @method setShader -* @param shader {Any} -*/ -PIXI.WebGLShaderManager.prototype.setShader = function(shader) -{ - if(this._currentId === shader._UID)return false; - + * Sets the current shader. + * + * @param shader {Any} + */ +WebGLShaderManager.prototype.setShader = function (shader) { + if (this._currentId === shader._UID)return false; + this._currentId = shader._UID; this.currentShader = shader; @@ -133,12 +112,10 @@ }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLShaderManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLShaderManager.prototype.destroy = function () { this.attribState = null; this.tempAttribState = null; diff --git a/src/renderers/webgl/utils/WebGLShaderUtils.js b/src/renderers/webgl/utils/WebGLShaderUtils.js index a7d7826..3d3b2e0 100644 --- a/src/renderers/webgl/utils/WebGLShaderUtils.js +++ b/src/renderers/webgl/utils/WebGLShaderUtils.js @@ -1,88 +1,74 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +var glUtils = module.exports = { + /** + * @static + * @private + */ + initDefaultShaders: function () { + }, -/** -* @method initDefaultShaders -* @static -* @private -*/ -PIXI.initDefaultShaders = function() -{ -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileVertexShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); + }, -/** -* @method CompileVertexShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileVertexShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileFragmentShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); + }, -/** -* @method CompileFragmentShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileFragmentShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); -}; + /** + * @static + * @private + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @param shaderType {number} + * @return {Any} + */ + _CompileShader: function (gl, shaderSrc, shaderType) { + var src = shaderSrc.join("\n"); + var shader = gl.createShader(shaderType); + gl.shaderSource(shader, src); + gl.compileShader(shader); -/** -* @method _CompileShader -* @static -* @private -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @param shaderType {Number} -* @return {Any} -*/ -PIXI._CompileShader = function(gl, shaderSrc, shaderType) -{ - var src = shaderSrc.join("\n"); - var shader = gl.createShader(shaderType); - gl.shaderSource(shader, src); - gl.compileShader(shader); + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + window.console.log(gl.getShaderInfoLog(shader)); + return null; + } - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) - { - window.console.log(gl.getShaderInfoLog(shader)); - return null; + return shader; + }, + + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param vertexSrc {Array} + * @param fragmentSrc {Array} + * @return {Any} + */ + compileProgram: function (gl, vertexSrc, fragmentSrc) { + var fragmentShader = glUtils.CompileFragmentShader(gl, fragmentSrc); + var vertexShader = glUtils.CompileVertexShader(gl, vertexSrc); + + var shaderProgram = gl.createProgram(); + + gl.attachShader(shaderProgram, vertexShader); + gl.attachShader(shaderProgram, fragmentShader); + gl.linkProgram(shaderProgram); + + if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { + window.console.log("Could not initialise shaders"); + } + + return shaderProgram; } - - return shader; -}; - -/** -* @method compileProgram -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param vertexSrc {Array} -* @param fragmentSrc {Array} -* @return {Any} -*/ -PIXI.compileProgram = function(gl, vertexSrc, fragmentSrc) -{ - var fragmentShader = PIXI.CompileFragmentShader(gl, fragmentSrc); - var vertexShader = PIXI.CompileVertexShader(gl, vertexSrc); - - var shaderProgram = gl.createProgram(); - - gl.attachShader(shaderProgram, vertexShader); - gl.attachShader(shaderProgram, fragmentShader); - gl.linkProgram(shaderProgram); - - if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) - { - window.console.log("Could not initialise shaders"); - } - - return shaderProgram; }; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/utils/WebGLMaskManager.js b/src/renderers/webgl/utils/WebGLMaskManager.js index 607d5dd..f89ffce 100644 --- a/src/renderers/webgl/utils/WebGLMaskManager.js +++ b/src/renderers/webgl/utils/WebGLMaskManager.js @@ -1,69 +1,56 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLMaskManager -* @constructor -* @private -*/ -PIXI.WebGLMaskManager = function() -{ +function WebGLMaskManager() { }; -PIXI.WebGLMaskManager.prototype.constructor = PIXI.WebGLMaskManager; +WebGLMaskManager.prototype.constructor = WebGLMaskManager; +module.exports = WebGLMaskManager; /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLMaskManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLMaskManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param maskData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLMaskManager.prototype.pushMask = function(maskData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param maskData {Array} + * @param renderSession {object} + */ +WebGLMaskManager.prototype.pushMask = function (maskData, renderSession) { var gl = renderSession.gl; - if(maskData.dirty) - { - PIXI.WebGLGraphics.updateGraphics(maskData, gl); + if (maskData.dirty) { + WebGLGraphics.updateGraphics(maskData, gl); } - if(!maskData._webGL[gl.id].data.length)return; + if (!maskData._webGL[gl.id].data.length)return; renderSession.stencilManager.pushStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popMask -* @param maskData {Array} -* @param renderSession {Object} an object containing all the useful parameters -*/ -PIXI.WebGLMaskManager.prototype.popMask = function(maskData, renderSession) -{ + * Removes the last filter from the filter stack and doesn't return it. + * + * @param maskData {Array} + * @param renderSession {object} an object containing all the useful parameters + */ +WebGLMaskManager.prototype.popMask = function (maskData, renderSession) { var gl = this.gl; renderSession.stencilManager.popStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLMaskManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLMaskManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLShaderManager.js b/src/renderers/webgl/utils/WebGLShaderManager.js index a15974e..de37591 100644 --- a/src/renderers/webgl/utils/WebGLShaderManager.js +++ b/src/renderers/webgl/utils/WebGLShaderManager.js @@ -1,111 +1,92 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLShaderManager -* @constructor -* @private -*/ -PIXI.WebGLShaderManager = function() -{ +function WebGLShaderManager() { /** - * @property maxAttibs - * @type Number + * @member {number} */ this.maxAttibs = 10; /** - * @property attribState - * @type Array + * @member {Array} */ this.attribState = []; /** - * @property tempAttribState - * @type Array + * @member {Array} */ this.tempAttribState = []; - for (var i = 0; i < this.maxAttibs; i++) - { + for (var i = 0; i < this.maxAttibs; i++) { this.attribState[i] = false; } /** - * @property stack - * @type Array + * @member {Array} */ this.stack = []; }; -PIXI.WebGLShaderManager.prototype.constructor = PIXI.WebGLShaderManager; +WebGLShaderManager.prototype.constructor = WebGLShaderManager; +module.exports = WebGLShaderManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLShaderManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLShaderManager.prototype.setContext = function (gl) { this.gl = gl; - + // the next one is used for rendering primitives - this.primitiveShader = new PIXI.PrimitiveShader(gl); + this.primitiveShader = new PrimitiveShader(gl); // the next one is used for rendering triangle strips - this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(gl); + this.complexPrimitiveShader = new ComplexPrimitiveShader(gl); // this shader is used for the default sprite rendering - this.defaultShader = new PIXI.PixiShader(gl); + this.defaultShader = new PixiShader(gl); // this shader is used for the fast sprite rendering - this.fastShader = new PIXI.PixiFastShader(gl); + this.fastShader = new PixiFastShader(gl); // the next one is used for rendering triangle strips - this.stripShader = new PIXI.StripShader(gl); + this.stripShader = new StripShader(gl); this.setShader(this.defaultShader); }; /** -* Takes the attributes given in parameters. -* -* @method setAttribs -* @param attribs {Array} attribs -*/ -PIXI.WebGLShaderManager.prototype.setAttribs = function(attribs) -{ + * Takes the attributes given in parameters. + * + * @param attribs {Array} attribs + */ +WebGLShaderManager.prototype.setAttribs = function (attribs) { // reset temp state var i; - for (i = 0; i < this.tempAttribState.length; i++) - { + for (i = 0; i < this.tempAttribState.length; i++) { this.tempAttribState[i] = false; } // set the new attribs - for (i = 0; i < attribs.length; i++) - { + for (i = 0; i < attribs.length; i++) { var attribId = attribs[i]; this.tempAttribState[attribId] = true; } var gl = this.gl; - for (i = 0; i < this.attribState.length; i++) - { - if(this.attribState[i] !== this.tempAttribState[i]) - { + for (i = 0; i < this.attribState.length; i++) { + if (this.attribState[i] !== this.tempAttribState[i]) { this.attribState[i] = this.tempAttribState[i]; - if(this.tempAttribState[i]) - { + if (this.tempAttribState[i]) { gl.enableVertexAttribArray(i); } - else - { + else { gl.disableVertexAttribArray(i); } } @@ -113,15 +94,13 @@ }; /** -* Sets the current shader. -* -* @method setShader -* @param shader {Any} -*/ -PIXI.WebGLShaderManager.prototype.setShader = function(shader) -{ - if(this._currentId === shader._UID)return false; - + * Sets the current shader. + * + * @param shader {Any} + */ +WebGLShaderManager.prototype.setShader = function (shader) { + if (this._currentId === shader._UID)return false; + this._currentId = shader._UID; this.currentShader = shader; @@ -133,12 +112,10 @@ }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLShaderManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLShaderManager.prototype.destroy = function () { this.attribState = null; this.tempAttribState = null; diff --git a/src/renderers/webgl/utils/WebGLShaderUtils.js b/src/renderers/webgl/utils/WebGLShaderUtils.js index a7d7826..3d3b2e0 100644 --- a/src/renderers/webgl/utils/WebGLShaderUtils.js +++ b/src/renderers/webgl/utils/WebGLShaderUtils.js @@ -1,88 +1,74 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +var glUtils = module.exports = { + /** + * @static + * @private + */ + initDefaultShaders: function () { + }, -/** -* @method initDefaultShaders -* @static -* @private -*/ -PIXI.initDefaultShaders = function() -{ -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileVertexShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); + }, -/** -* @method CompileVertexShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileVertexShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileFragmentShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); + }, -/** -* @method CompileFragmentShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileFragmentShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); -}; + /** + * @static + * @private + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @param shaderType {number} + * @return {Any} + */ + _CompileShader: function (gl, shaderSrc, shaderType) { + var src = shaderSrc.join("\n"); + var shader = gl.createShader(shaderType); + gl.shaderSource(shader, src); + gl.compileShader(shader); -/** -* @method _CompileShader -* @static -* @private -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @param shaderType {Number} -* @return {Any} -*/ -PIXI._CompileShader = function(gl, shaderSrc, shaderType) -{ - var src = shaderSrc.join("\n"); - var shader = gl.createShader(shaderType); - gl.shaderSource(shader, src); - gl.compileShader(shader); + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + window.console.log(gl.getShaderInfoLog(shader)); + return null; + } - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) - { - window.console.log(gl.getShaderInfoLog(shader)); - return null; + return shader; + }, + + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param vertexSrc {Array} + * @param fragmentSrc {Array} + * @return {Any} + */ + compileProgram: function (gl, vertexSrc, fragmentSrc) { + var fragmentShader = glUtils.CompileFragmentShader(gl, fragmentSrc); + var vertexShader = glUtils.CompileVertexShader(gl, vertexSrc); + + var shaderProgram = gl.createProgram(); + + gl.attachShader(shaderProgram, vertexShader); + gl.attachShader(shaderProgram, fragmentShader); + gl.linkProgram(shaderProgram); + + if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { + window.console.log("Could not initialise shaders"); + } + + return shaderProgram; } - - return shader; -}; - -/** -* @method compileProgram -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param vertexSrc {Array} -* @param fragmentSrc {Array} -* @return {Any} -*/ -PIXI.compileProgram = function(gl, vertexSrc, fragmentSrc) -{ - var fragmentShader = PIXI.CompileFragmentShader(gl, fragmentSrc); - var vertexShader = PIXI.CompileVertexShader(gl, vertexSrc); - - var shaderProgram = gl.createProgram(); - - gl.attachShader(shaderProgram, vertexShader); - gl.attachShader(shaderProgram, fragmentShader); - gl.linkProgram(shaderProgram); - - if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) - { - window.console.log("Could not initialise shaders"); - } - - return shaderProgram; }; diff --git a/src/renderers/webgl/utils/WebGLSpriteBatch.js b/src/renderers/webgl/utils/WebGLSpriteBatch.js index 711b4da..9e28881 100644 --- a/src/renderers/webgl/utils/WebGLSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLSpriteBatch.js @@ -1,80 +1,71 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * Also a thanks to https://github.com/bchevalier for tweaking the tint and alpha so that they now share 4 bytes on the vertex buffer - * + * * Heavily inspired by LibGDX's WebGLSpriteBatch: * https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/WebGLSpriteBatch.java */ - /** +/** * - * @class WebGLSpriteBatch + * @class * @private - * @constructor + * @namespace PIXI */ -PIXI.WebGLSpriteBatch = function() -{ +function WebGLSpriteBatch() { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 5; /** * The number of images in the SpriteBatch before it flushes - * @property size - * @type Number + * @member {number} */ this.size = 2000;//Math.pow(2, 16) / this.vertSize; - //the total number of bytes in our batch + // the total number of bytes in our batch var numVerts = this.size * 4 * 4 * this.vertSize; - //the total number of indices in our batch + // the total number of indices in our batch var numIndices = this.size * 6; /** - * Holds the vertices - * - * @property vertices - * @type ArrayBuffer - */ - this.vertices = new PIXI.ArrayBuffer(numVerts); + * Holds the vertices + * + * @member {ArrayBuffer} + */ + this.vertices = new ArrayBuffer(numVerts); /** - * View on the vertices as a Float32Array - * - * @property positions - * @type Float32Array - */ - this.positions = new PIXI.Float32Array(this.vertices); + * View on the vertices as a Float32Array + * + * @member {Float32Array} + */ + this.positions = new Float32Array(this.vertices); /** - * View on the vertices as a Uint32Array - * - * @property colors - * @type Uint32Array - */ - this.colors = new PIXI.Uint32Array(this.vertices); + * View on the vertices as a Uint32Array + * + * @member {Uint32Array} + */ + this.colors = new Uint32Array(this.vertices); /** * Holds the indices * - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -84,58 +75,49 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; /** - * @property dirty - * @type Boolean + * @member {boolean} */ this.dirty = true; /** - * @property textures - * @type Array + * @member {Array} */ this.textures = []; /** - * @property blendModes - * @type Array + * @member {Array} */ this.blendModes = []; /** - * @property shaders - * @type Array + * @member {Array} */ this.shaders = []; /** - * @property sprites - * @type Array + * @member {Array} */ this.sprites = []; /** - * @property defaultShader - * @type AbstractFilter + * @member {AbstractFilter} */ - this.defaultShader = new PIXI.AbstractFilter([ + this.defaultShader = new AbstractFilter([ 'precision lowp float;', 'varying vec2 vTextureCoord;', 'varying vec4 vColor;', @@ -147,11 +129,9 @@ }; /** -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLSpriteBatch.prototype.setContext = function(gl) -{ + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -169,7 +149,7 @@ this.currentBlendMode = 99999; - var shader = new PIXI.PixiShader(gl); + var shader = new PixiShader(gl); shader.fragmentSrc = this.defaultShader.fragmentSrc; shader.uniforms = {}; @@ -179,11 +159,9 @@ }; /** -* @method begin -* @param renderSession {Object} The RenderSession object -*/ -PIXI.WebGLSpriteBatch.prototype.begin = function(renderSession) -{ + * @param renderSession {object} The RenderSession object + */ +WebGLSpriteBatch.prototype.begin = function (renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.defaultShader; @@ -191,25 +169,20 @@ }; /** -* @method end -*/ -PIXI.WebGLSpriteBatch.prototype.end = function() -{ + */ +WebGLSpriteBatch.prototype.end = function () { this.flush(); }; /** -* @method render -* @param sprite {Sprite} the sprite to render when using this spritebatch -*/ -PIXI.WebGLSpriteBatch.prototype.render = function(sprite) -{ + * @param sprite {Sprite} the sprite to render when using this spritebatch + */ +WebGLSpriteBatch.prototype.render = function (sprite) { var texture = sprite.texture; - //TODO set blend modes.. + //TODO set blend modes.. // check texture.. - if(this.currentBatchSize >= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); this.currentBaseTexture = texture.baseTexture; } @@ -217,16 +190,15 @@ // get the uvs for the texture var uvs = texture._uvs; // if the uvs have not updated then no point rendering just yet! - if(!uvs)return; + if (!uvs)return; // TODO trim?? var aX = sprite.anchor.x; var aY = sprite.anchor.y; var w0, w1, h0, h1; - - if (texture.trim) - { + + if (texture.trim) { // if the sprite is trimmed then we need to add the extra space before transforming the sprite coords.. var trim = texture.trim; @@ -237,8 +209,7 @@ h0 = h1 + texture.crop.height; } - else - { + else { w0 = (texture.frame.width ) * (1-aX); w1 = (texture.frame.width ) * -aX; @@ -247,7 +218,7 @@ } var index = this.currentBatchSize * 4 * this.vertSize; - + var resolution = texture.baseTexture.resolution; var worldTransform = sprite.worldTransform; @@ -262,8 +233,7 @@ var colors = this.colors; var positions = this.positions; - if(this.renderSession.roundPixels) - { + if (this.renderSession.roundPixels) { // xy positions[index] = a * w1 + c * h1 + tx | 0; positions[index+1] = d * h1 + b * w1 + ty | 0; @@ -280,8 +250,7 @@ positions[index+15] = a * w1 + c * h0 + tx | 0; positions[index+16] = d * h0 + b * w1 + ty | 0; } - else - { + else { // xy positions[index] = a * w1 + c * h1 + tx; positions[index+1] = d * h1 + b * w1 + ty; @@ -298,7 +267,7 @@ positions[index+15] = a * w1 + c * h0 + tx; positions[index+16] = d * h0 + b * w1 + ty; } - + // uv positions[index+2] = uvs.x0; positions[index+3] = uvs.y0; @@ -326,18 +295,15 @@ }; /** -* Renders a TilingSprite using the spriteBatch. -* -* @method renderTilingSprite -* @param sprite {TilingSprite} the tilingSprite to render -*/ -PIXI.WebGLSpriteBatch.prototype.renderTilingSprite = function(tilingSprite) -{ + * Renders a TilingSprite using the spriteBatch. + * + * @param sprite {TilingSprite} the tilingSprite to render + */ +WebGLSpriteBatch.prototype.renderTilingSprite = function (tilingSprite) { var texture = tilingSprite.tilingTexture; // check texture.. - if(this.currentBatchSize >= this.size) - { + if (this.currentBatchSize >= this.size) { //return; this.flush(); this.currentBaseTexture = texture.baseTexture; @@ -346,7 +312,7 @@ // set the textures uvs temporarily // TODO create a separate texture so that we can tile part of a texture - if(!tilingSprite._uvs)tilingSprite._uvs = new PIXI.TextureUvs(); + if (!tilingSprite._uvs)tilingSprite._uvs = new TextureUvs(); var uvs = tilingSprite._uvs; @@ -420,7 +386,7 @@ positions[index++] = uvs.y1; // color colors[index++] = color; - + // xy positions[index++] = a * w0 + c * h0 + tx; positions[index++] = d * h0 + b * w0 + ty; @@ -444,20 +410,17 @@ }; /** -* Renders the content and empties the current batch. -* -* @method flush -*/ -PIXI.WebGLSpriteBatch.prototype.flush = function() -{ + * Renders the content and empties the current batch. + * + */ +WebGLSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; var shader; - if(this.dirty) - { + if (this.dirty) { this.dirty = false; // bind the main texture gl.activeTexture(gl.TEXTURE0); @@ -477,13 +440,11 @@ gl.vertexAttribPointer(shader.colorAttribute, 4, gl.UNSIGNED_BYTE, true, stride, 4 * 4); } - // upload the verts to the buffer - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + // upload the verts to the buffer + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.positions.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } @@ -501,7 +462,7 @@ var sprite; for (var i = 0, j = this.currentBatchSize; i < j; i++) { - + sprite = this.sprites[i]; nextTexture = sprite.texture.baseTexture; @@ -511,29 +472,25 @@ blendSwap = currentBlendMode !== nextBlendMode; shaderSwap = currentShader !== nextShader; // should I use _UIDS??? - if(currentBaseTexture !== nextTexture || blendSwap || shaderSwap) - { + if (currentBaseTexture !== nextTexture || blendSwap || shaderSwap) { this.renderBatch(currentBaseTexture, batchSize, start); start = i; batchSize = 0; currentBaseTexture = nextTexture; - if( blendSwap ) - { + if ( blendSwap ) { currentBlendMode = nextBlendMode; this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); } - if( shaderSwap ) - { + if ( shaderSwap ) { currentShader = nextShader; - + shader = currentShader.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc =currentShader.fragmentSrc; shader.uniforms =currentShader.uniforms; @@ -545,8 +502,8 @@ // set shader function??? this.renderSession.shaderManager.setShader(shader); - if(shader.dirty)shader.syncUniforms(); - + if (shader.dirty)shader.syncUniforms(); + // both thease only need to be set if they are changing.. // set the projection var projection = this.renderSession.projection; @@ -570,66 +527,58 @@ }; /** -* @method renderBatch -* @param texture {Texture} -* @param size {Number} -* @param startIndex {Number} -*/ -PIXI.WebGLSpriteBatch.prototype.renderBatch = function(texture, size, startIndex) -{ - if(size === 0)return; + * @param texture {Texture} + * @param size {number} + * @param startIndex {number} + */ +WebGLSpriteBatch.prototype.renderBatch = function (texture, size, startIndex) { + if (size === 0)return; var gl = this.gl; // check if a texture is dirty.. - if(texture._dirty[gl.id]) - { + if (texture._dirty[gl.id]) { this.renderSession.renderer.updateTexture(texture); } - else - { + else { // bind the current texture gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); } // now draw those suckas! gl.drawElements(gl.TRIANGLES, size * 6, gl.UNSIGNED_SHORT, startIndex * 6 * 2); - + // increment the draw count this.renderSession.drawCount++; }; /** -* @method stop -*/ -PIXI.WebGLSpriteBatch.prototype.stop = function() -{ + * + */ +WebGLSpriteBatch.prototype.stop = function () { this.flush(); this.dirty = true; }; /** -* @method start -*/ -PIXI.WebGLSpriteBatch.prototype.start = function() -{ + * + */ +WebGLSpriteBatch.prototype.start = function () { this.dirty = true; }; /** -* Destroys the SpriteBatch. -* -* @method destroy -*/ -PIXI.WebGLSpriteBatch.prototype.destroy = function() -{ + * Destroys the SpriteBatch. + * + */ +WebGLSpriteBatch.prototype.destroy = function () { this.vertices = null; this.indices = null; - + this.gl.deleteBuffer( this.vertexBuffer ); this.gl.deleteBuffer( this.indexBuffer ); - + this.currentBaseTexture = null; - + this.gl = null; -}; \ No newline at end of file +}; diff --git a/src/renderers/webgl/WebGLRenderer.js b/src/renderers/webgl/WebGLRenderer.js index 02b30e4..d43e680 100644 --- a/src/renderers/webgl/WebGLRenderer.js +++ b/src/renderers/webgl/WebGLRenderer.js @@ -1,9 +1,5 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -PIXI.glContexts = []; // this is where we store the webGL contexts for easy access. -PIXI.instances = []; +glContexts = []; // this is where we store the webGL contexts for easy access. +instances = []; /** * The WebGLRenderer draws the stage and all its content onto a webGL enabled canvas. This renderer @@ -11,49 +7,42 @@ * So no need for Sprite Batches or Sprite Clouds. * Don't forget to add the view to your DOM or you will not see anything :) * - * @class WebGLRenderer - * @constructor - * @param [width=0] {Number} the width of the canvas view - * @param [height=0] {Number} the height of the canvas view - * @param [options] {Object} The optional renderer parameters + * @class + * @namespace PIXI + * @param [width=0] {number} the width of the canvas view + * @param [height=0] {number} the height of the canvas view + * @param [options] {object} The optional renderer parameters * @param [options.view] {HTMLCanvasElement} the canvas to use as a view, optional - * @param [options.transparent=false] {Boolean} If the render view is transparent, default false - * @param [options.autoResize=false] {Boolean} If the render view is automatically resized, default false - * @param [options.antialias=false] {Boolean} sets antialias (only applicable in chrome at the moment) - * @param [options.preserveDrawingBuffer=false] {Boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context - * @param [options.resolution=1] {Number} the resolution of the renderer retina would be 2 + * @param [options.transparent=false] {boolean} If the render view is transparent, default false + * @param [options.autoResize=false] {boolean} If the render view is automatically resized, default false + * @param [options.antialias=false] {boolean} sets antialias (only applicable in chrome at the moment) + * @param [options.preserveDrawingBuffer=false] {boolean} enables drawing buffer preservation, enable this if you need to call toDataUrl on the webgl context + * @param [options.resolution=1] {number} the resolution of the renderer retina would be 2 */ -PIXI.WebGLRenderer = function(width, height, options) -{ - if(options) - { - for (var i in PIXI.defaultRenderOptions) - { - if (typeof options[i] === 'undefined') options[i] = PIXI.defaultRenderOptions[i]; +function WebGLRenderer(width, height, options) { + if (options) { + for (var i in defaultRenderOptions) { + if (typeof options[i] === 'undefined') options[i] = defaultRenderOptions[i]; } } - else - { - options = PIXI.defaultRenderOptions; + else { + options = defaultRenderOptions; } - if(!PIXI.defaultRenderer) - { - PIXI.sayHello('webGL'); - PIXI.defaultRenderer = this; + if (!defaultRenderer) { + sayHello('webGL'); + defaultRenderer = this; } /** - * @property type - * @type Number + * @member {number} */ - this.type = PIXI.WEBGL_RENDERER; + this.type = WEBGL_RENDERER; /** * The resolution of the renderer * - * @property resolution - * @type Number + * @member {number} * @default 1 */ this.resolution = options.resolution; @@ -63,24 +52,21 @@ /** * Whether the render view is transparent * - * @property transparent - * @type Boolean + * @member {boolean} */ this.transparent = options.transparent; /** * Whether the render view should be resized automatically * - * @property autoResize - * @type Boolean + * @member {boolean} */ this.autoResize = options.autoResize || false; /** * The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering. * - * @property preserveDrawingBuffer - * @type Boolean + * @member {boolean} */ this.preserveDrawingBuffer = options.preserveDrawingBuffer; @@ -90,8 +76,7 @@ * If the Stage is transparent, Pixi will clear to the target Stage's background color. * Disable this by setting this to false. For example: if your game has a canvas filling background image, you often don't need this set. * - * @property clearBeforeRender - * @type Boolean + * @member {boolean} * @default */ this.clearBeforeRender = options.clearBeforeRender; @@ -99,8 +84,7 @@ /** * The width of the canvas view * - * @property width - * @type Number + * @member {number} * @default 800 */ this.width = width || 800; @@ -108,8 +92,7 @@ /** * The height of the canvas view * - * @property height - * @type Number + * @member {number} * @default 600 */ this.height = height || 600; @@ -117,22 +100,19 @@ /** * The canvas element that everything is drawn to * - * @property view - * @type HTMLCanvasElement + * @member {HTMLCanvasElement} */ this.view = options.view || document.createElement( 'canvas' ); // deal with losing context.. /** - * @property contextLostBound - * @type Function + * @member {Function} */ this.contextLostBound = this.handleContextLost.bind(this); /** - * @property contextRestoredBound - * @type Function + * @member {Function} */ this.contextRestoredBound = this.handleContextRestored.bind(this); @@ -140,8 +120,7 @@ this.view.addEventListener('webglcontextrestored', this.contextRestoredBound, false); /** - * @property _contextOptions - * @type Object + * @member {object} * @private */ this._contextOptions = { @@ -153,65 +132,56 @@ }; /** - * @property projection - * @type Point + * @member {Point} */ - this.projection = new PIXI.Point(); + this.projection = new Point(); /** - * @property offset - * @type Point + * @member {Point} */ - this.offset = new PIXI.Point(0, 0); + this.offset = new Point(0, 0); // time to create the render managers! each one focuses on managing a state in webGL /** * Deals with managing the shader programs and their attribs - * @property shaderManager - * @type WebGLShaderManager + * @member {WebGLShaderManager} */ - this.shaderManager = new PIXI.WebGLShaderManager(); + this.shaderManager = new WebGLShaderManager(); /** * Manages the rendering of sprites - * @property spriteBatch - * @type WebGLSpriteBatch + * @member {WebGLSpriteBatch} */ - this.spriteBatch = new PIXI.WebGLSpriteBatch(); + this.spriteBatch = new WebGLSpriteBatch(); /** * Manages the masks using the stencil buffer - * @property maskManager - * @type WebGLMaskManager + * @member {WebGLMaskManager} */ - this.maskManager = new PIXI.WebGLMaskManager(); + this.maskManager = new WebGLMaskManager(); /** * Manages the filters - * @property filterManager - * @type WebGLFilterManager + * @member {WebGLFilterManager} */ - this.filterManager = new PIXI.WebGLFilterManager(); + this.filterManager = new WebGLFilterManager(); /** * Manages the stencil buffer - * @property stencilManager - * @type WebGLStencilManager + * @member {WebGLStencilManager} */ - this.stencilManager = new PIXI.WebGLStencilManager(); + this.stencilManager = new WebGLStencilManager(); /** * Manages the blendModes - * @property blendModeManager - * @type WebGLBlendModeManager + * @member {WebGLBlendModeManager} */ - this.blendModeManager = new PIXI.WebGLBlendModeManager(); + this.blendModeManager = new WebGLBlendModeManager(); /** * TODO remove - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = {}; this.renderSession.gl = this.gl; @@ -233,13 +203,13 @@ }; // constructor -PIXI.WebGLRenderer.prototype.constructor = PIXI.WebGLRenderer; +WebGLRenderer.prototype.constructor = WebGLRenderer; +module.exports = WebGLRenderer; /** * @method initContext */ -PIXI.WebGLRenderer.prototype.initContext = function() -{ +WebGLRenderer.prototype.initContext = function () { var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions); this.gl = gl; @@ -248,11 +218,11 @@ throw new Error('This browser does not support webGL. Try using the canvas renderer'); } - this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId ++; + this.glContextId = gl.id = WebGLRenderer.glContextId ++; - PIXI.glContexts[this.glContextId] = gl; + glContexts[this.glContextId] = gl; - PIXI.instances[this.glContextId] = this; + instances[this.glContextId] = this; // set up the default pixi settings.. gl.disable(gl.DEPTH_TEST); @@ -276,18 +246,15 @@ /** * Renders the stage to its webGL view * - * @method render * @param stage {Stage} the Stage element to be rendered */ -PIXI.WebGLRenderer.prototype.render = function(stage) -{ +WebGLRenderer.prototype.render = function (stage) { // no point rendering if our context has been blown up! - if(this.contextLost)return; + if (this.contextLost)return; // if rendering a new stage clear the batches.. - if(this.__stage !== stage) - { - if(stage.interactive)stage.interactionManager.removeEvents(); + if (this.__stage !== stage) { + if (stage.interactive)stage.interactionManager.removeEvents(); // TODO make this work // dont think this is needed any more? @@ -300,19 +267,15 @@ var gl = this.gl; // interaction - if(stage._interactive) - { + if (stage._interactive) { //need to add some events! - if(!stage._interactiveEventsAdded) - { + if (!stage._interactiveEventsAdded) { stage._interactiveEventsAdded = true; stage.interactionManager.setTarget(this); } } - else - { - if(stage._interactiveEventsAdded) - { + else { + if (stage._interactiveEventsAdded) { stage._interactiveEventsAdded = false; stage.interactionManager.setTarget(this); } @@ -324,14 +287,11 @@ // make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); - if (this.clearBeforeRender) - { - if(this.transparent) - { + if (this.clearBeforeRender) { + if (this.transparent) { gl.clearColor(0, 0, 0, 0); } - else - { + else { gl.clearColor(stage.backgroundColorSplit[0],stage.backgroundColorSplit[1],stage.backgroundColorSplit[2], 1); } @@ -344,14 +304,12 @@ /** * Renders a Display Object. * - * @method renderDisplayObject * @param displayObject {DisplayObject} The DisplayObject to render * @param projection {Point} The projection * @param buffer {Array} a standard WebGL buffer */ -PIXI.WebGLRenderer.prototype.renderDisplayObject = function(displayObject, projection, buffer) -{ - this.renderSession.blendModeManager.setBlendMode(PIXI.blendModes.NORMAL); +WebGLRenderer.prototype.renderDisplayObject = function (displayObject, projection, buffer) { + this.renderSession.blendModeManager.setBlendMode(blendModes.NORMAL); // reset the render session data.. this.renderSession.drawCount = 0; @@ -381,12 +339,10 @@ /** * Resizes the webGL view to the specified width and height. * - * @method resize - * @param width {Number} the new width of the webGL view - * @param height {Number} the new height of the webGL view + * @param width {number} the new width of the webGL view + * @param height {number} the new height of the webGL view */ -PIXI.WebGLRenderer.prototype.resize = function(width, height) -{ +WebGLRenderer.prototype.resize = function (width, height) { this.width = width * this.resolution; this.height = height * this.resolution; @@ -407,43 +363,37 @@ /** * Updates and Creates a WebGL texture for the renderers context. * - * @method updateTexture * @param texture {Texture} the texture to update */ -PIXI.WebGLRenderer.prototype.updateTexture = function(texture) -{ - if(!texture.hasLoaded )return; +WebGLRenderer.prototype.updateTexture = function (texture) { + if (!texture.hasLoaded )return; var gl = this.gl; - if(!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); + if (!texture._glTextures[gl.id])texture._glTextures[gl.id] = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultipliedAlpha); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - if(texture.mipmap && PIXI.isPowerOfTwo(texture.width, texture.height)) - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); + + if (texture.mipmap && isPowerOfTwo(texture.width, texture.height)) { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST); gl.generateMipmap(gl.TEXTURE_2D); } - else - { - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + else { + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texture.scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); } // reguler... - if(!texture._powerOf2) - { + if (!texture._powerOf2) { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } - else - { + else { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); } @@ -456,12 +406,10 @@ /** * Handles a lost webgl context * - * @method handleContextLost * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextLost = function(event) -{ +WebGLRenderer.prototype.handleContextLost = function (event) { event.preventDefault(); this.contextLost = true; }; @@ -469,18 +417,15 @@ /** * Handles a restored webgl context * - * @method handleContextRestored * @param event {Event} * @private */ -PIXI.WebGLRenderer.prototype.handleContextRestored = function() -{ +WebGLRenderer.prototype.handleContextRestored = function () { this.initContext(); // empty all the ol gl textures as they are useless now - for(var key in PIXI.TextureCache) - { - var texture = PIXI.TextureCache[key].baseTexture; + for (var key in TextureCache) { + var texture = TextureCache[key].baseTexture; texture._glTextures = []; } @@ -490,15 +435,13 @@ /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * - * @method destroy */ -PIXI.WebGLRenderer.prototype.destroy = function() -{ +WebGLRenderer.prototype.destroy = function () { // remove listeners this.view.removeEventListener('webglcontextlost', this.contextLostBound); this.view.removeEventListener('webglcontextrestored', this.contextRestoredBound); - PIXI.glContexts[this.glContextId] = null; + glContexts[this.glContextId] = null; this.projection = null; this.offset = null; @@ -521,34 +464,31 @@ /** * Maps Pixi blend modes to WebGL blend modes. * - * @method mapBlendModes */ -PIXI.WebGLRenderer.prototype.mapBlendModes = function() -{ +WebGLRenderer.prototype.mapBlendModes = function () { var gl = this.gl; - if(!PIXI.blendModesWebGL) - { - PIXI.blendModesWebGL = []; + if (!blendModesWebGL) { + blendModesWebGL = []; - PIXI.blendModesWebGL[PIXI.blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; - PIXI.blendModesWebGL[PIXI.blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; - PIXI.blendModesWebGL[PIXI.blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.NORMAL] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.ADD] = [gl.SRC_ALPHA, gl.DST_ALPHA]; + blendModesWebGL[blendModes.MULTIPLY] = [gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SCREEN] = [gl.SRC_ALPHA, gl.ONE]; + blendModesWebGL[blendModes.OVERLAY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DARKEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LIGHTEN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_DODGE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR_BURN] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HARD_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SOFT_LIGHT] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.DIFFERENCE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.EXCLUSION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.HUE] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.SATURATION] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.COLOR] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; + blendModesWebGL[blendModes.LUMINOSITY] = [gl.ONE, gl.ONE_MINUS_SRC_ALPHA]; } }; -PIXI.WebGLRenderer.glContextId = 0; +WebGLRenderer.glContextId = 0; diff --git a/src/renderers/webgl/utils/FilterTexture.js b/src/renderers/webgl/utils/FilterTexture.js index 0340289..f9961ad 100644 --- a/src/renderers/webgl/utils/FilterTexture.js +++ b/src/renderers/webgl/utils/FilterTexture.js @@ -1,46 +1,37 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + * @param scaleMode {number} See {{#crossLink "PIXI/scaleModes:property"}}scaleModes{{/crossLink}} for possible values */ - -/** -* @class FilterTexture -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -* @param scaleMode {Number} See {{#crossLink "PIXI/scaleModes:property"}}PIXI.scaleModes{{/crossLink}} for possible values -*/ -PIXI.FilterTexture = function(gl, width, height, scaleMode) -{ +function FilterTexture(gl, width, height, scaleMode) { /** - * @property gl - * @type WebGLContext + * @member {WebGLContext} */ this.gl = gl; // next time to create a frame buffer and texture /** - * @property frameBuffer - * @type Any + * @member {Any} */ this.frameBuffer = gl.createFramebuffer(); /** - * @property texture - * @type Any + * @member {Any} */ this.texture = gl.createTexture(); /** - * @property scaleMode - * @type Number + * @member {number} */ - scaleMode = scaleMode || PIXI.scaleModes.DEFAULT; + scaleMode = scaleMode || scaleModes.DEFAULT; gl.bindTexture(gl.TEXTURE_2D, this.texture); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === PIXI.scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, scaleMode === scaleModes.LINEAR ? gl.LINEAR : gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer ); @@ -52,21 +43,20 @@ this.renderBuffer = gl.createRenderbuffer(); gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer); gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer); - + this.resize(width, height); }; -PIXI.FilterTexture.prototype.constructor = PIXI.FilterTexture; +FilterTexture.prototype.constructor = FilterTexture; +module.exports = FilterTexture; /** -* Clears the filter texture. -* -* @method clear -*/ -PIXI.FilterTexture.prototype.clear = function() -{ + * Clears the filter texture. + * + */ +FilterTexture.prototype.clear = function () { var gl = this.gl; - + gl.clearColor(0,0,0, 0); gl.clear(gl.COLOR_BUFFER_BIT); }; @@ -74,13 +64,11 @@ /** * Resizes the texture to the specified width and height * - * @method resize - * @param width {Number} the new width of the texture - * @param height {Number} the new height of the texture + * @param width {number} the new width of the texture + * @param height {number} the new height of the texture */ -PIXI.FilterTexture.prototype.resize = function(width, height) -{ - if(this.width === width && this.height === height) return; +FilterTexture.prototype.resize = function (width, height) { + if (this.width === width && this.height === height) return; this.width = width; this.height = height; @@ -95,12 +83,10 @@ }; /** -* Destroys the filter texture. -* -* @method destroy -*/ -PIXI.FilterTexture.prototype.destroy = function() -{ + * Destroys the filter texture. + * + */ +FilterTexture.prototype.destroy = function () { var gl = this.gl; gl.deleteFramebuffer( this.frameBuffer ); gl.deleteTexture( this.texture ); diff --git a/src/renderers/webgl/utils/WebGLBlendModeManager.js b/src/renderers/webgl/utils/WebGLBlendModeManager.js index 3de0ec9..105ea64 100644 --- a/src/renderers/webgl/utils/WebGLBlendModeManager.js +++ b/src/renderers/webgl/utils/WebGLBlendModeManager.js @@ -1,58 +1,47 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @param gl {WebGLContext} the current WebGL drawing context */ - -/** -* @class WebGLBlendModeManager -* @constructor -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLBlendModeManager = function() -{ +function WebGLBlendModeManager() { /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 99999; }; -PIXI.WebGLBlendModeManager.prototype.constructor = PIXI.WebGLBlendModeManager; +WebGLBlendModeManager.prototype.constructor = WebGLBlendModeManager; +module.exports = WebGLBlendModeManager; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLBlendModeManager.prototype.setContext = function(gl) -{ +WebGLBlendModeManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Sets-up the given blendMode from WebGL's point of view. -* -* @method setBlendMode -* @param blendMode {Number} the blendMode, should be a Pixi const, such as PIXI.BlendModes.ADD -*/ -PIXI.WebGLBlendModeManager.prototype.setBlendMode = function(blendMode) -{ - if(this.currentBlendMode === blendMode)return false; + * Sets-up the given blendMode from WebGL's point of view. + * + * @param blendMode {number} the blendMode, should be a Pixi const, such as BlendModes.ADD + */ +WebGLBlendModeManager.prototype.setBlendMode = function (blendMode) { + if (this.currentBlendMode === blendMode)return false; this.currentBlendMode = blendMode; - - var blendModeWebGL = PIXI.blendModesWebGL[this.currentBlendMode]; + + var blendModeWebGL = blendModesWebGL[this.currentBlendMode]; this.gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]); - + return true; }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLBlendModeManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLBlendModeManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js index d870f50..88481dc 100644 --- a/src/renderers/webgl/utils/WebGLFastSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLFastSpriteBatch.js @@ -1,6 +1,6 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * @@ -9,26 +9,22 @@ */ /** -* @class WebGLFastSpriteBatch -* @constructor -*/ -PIXI.WebGLFastSpriteBatch = function(gl) -{ + * @class + * @namespace PIXI + */ +function WebGLFastSpriteBatch(gl) { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 10; /** - * @property maxSize - * @type Number + * @member {number} */ this.maxSize = 6000;//Math.pow(2, 16) / this.vertSize; /** - * @property size - * @type Number + * @member {number} */ this.size = this.maxSize; @@ -40,38 +36,32 @@ /** * Vertex data - * @property vertices - * @type Float32Array + * @member {Float32Array} */ - this.vertices = new PIXI.Float32Array(numVerts); + this.vertices = new Float32Array(numVerts); /** * Index data - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property vertexBuffer - * @type Object + * @member {object} */ this.vertexBuffer = null; /** - * @property indexBuffer - * @type Object + * @member {object} */ this.indexBuffer = null; /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -81,60 +71,52 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; - + /** - * @property currentBlendMode - * @type Number + * @member {number} */ this.currentBlendMode = 0; /** - * @property renderSession - * @type Object + * @member {object} */ this.renderSession = null; - + /** - * @property shader - * @type Object + * @member {object} */ this.shader = null; /** - * @property matrix - * @type Matrix + * @member {Matrix} */ this.matrix = null; this.setContext(gl); }; -PIXI.WebGLFastSpriteBatch.prototype.constructor = PIXI.WebGLFastSpriteBatch; +WebGLFastSpriteBatch.prototype.constructor = WebGLFastSpriteBatch; +module.exports = WebGLFastSpriteBatch; /** * Sets the WebGL Context. * - * @method setContext * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLFastSpriteBatch.prototype.setContext = function(gl) -{ +WebGLFastSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -152,12 +134,10 @@ }; /** - * @method begin * @param spriteBatch {WebGLSpriteBatch} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLFastSpriteBatch.prototype.begin = function(spriteBatch, renderSession) -{ +WebGLFastSpriteBatch.prototype.begin = function (spriteBatch, renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.fastShader; @@ -167,38 +147,32 @@ }; /** - * @method end */ -PIXI.WebGLFastSpriteBatch.prototype.end = function() -{ +WebGLFastSpriteBatch.prototype.end = function () { this.flush(); }; /** - * @method render * @param spriteBatch {WebGLSpriteBatch} */ -PIXI.WebGLFastSpriteBatch.prototype.render = function(spriteBatch) -{ +WebGLFastSpriteBatch.prototype.render = function (spriteBatch) { var children = spriteBatch.children; var sprite = children[0]; // if the uvs have not updated then no point rendering just yet! - + // check texture. - if(!sprite.texture._uvs)return; - + if (!sprite.texture._uvs)return; + this.currentBaseTexture = sprite.texture.baseTexture; - + // check blend mode - if(sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) - { + if (sprite.blendMode !== this.renderSession.blendModeManager.currentBlendMode) { this.flush(); this.renderSession.blendModeManager.setBlendMode(sprite.blendMode); } - - for(var i=0,j= children.length; i= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); } }; /** - * @method flush + * */ -PIXI.WebGLFastSpriteBatch.prototype.flush = function() -{ +WebGLFastSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; - + // bind the current texture - if(!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); + if (!this.currentBaseTexture._glTextures[gl.id])this.renderSession.renderer.updateTexture(this.currentBaseTexture, gl); gl.bindTexture(gl.TEXTURE_2D, this.currentBaseTexture._glTextures[gl.id]); // upload the verts to the buffer - - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.vertices.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } - + // now draw those suckas! gl.drawElements(gl.TRIANGLES, this.currentBatchSize * 6, gl.UNSIGNED_SHORT, 0); - + // then reset the batch! this.currentBatchSize = 0; @@ -387,18 +352,16 @@ /** - * @method stop + * */ -PIXI.WebGLFastSpriteBatch.prototype.stop = function() -{ +WebGLFastSpriteBatch.prototype.stop = function () { this.flush(); }; /** - * @method start + * */ -PIXI.WebGLFastSpriteBatch.prototype.start = function() -{ +WebGLFastSpriteBatch.prototype.start = function () { var gl = this.gl; // bind the main texture @@ -424,5 +387,5 @@ gl.vertexAttribPointer(this.shader.aRotation, 1, gl.FLOAT, false, stride, 6 * 4); gl.vertexAttribPointer(this.shader.aTextureCoord, 2, gl.FLOAT, false, stride, 7 * 4); gl.vertexAttribPointer(this.shader.colorAttribute, 1, gl.FLOAT, false, stride, 9 * 4); - + }; diff --git a/src/renderers/webgl/utils/WebGLFilterManager.js b/src/renderers/webgl/utils/WebGLFilterManager.js index ddc4691..865b353 100644 --- a/src/renderers/webgl/utils/WebGLFilterManager.js +++ b/src/renderers/webgl/utils/WebGLFilterManager.js @@ -1,42 +1,33 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI */ - -/** -* @class WebGLFilterManager -* @constructor -*/ -PIXI.WebGLFilterManager = function() -{ +function WebGLFilterManager() { /** - * @property filterStack - * @type Array + * @member {Array} */ this.filterStack = []; - + /** - * @property offsetX - * @type Number + * @member {number} */ this.offsetX = 0; /** - * @property offsetY - * @type Number + * @member {number} */ this.offsetY = 0; }; -PIXI.WebGLFilterManager.prototype.constructor = PIXI.WebGLFilterManager; +WebGLFilterManager.prototype.constructor = WebGLFilterManager; +module.exports = WebGLFilterManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLFilterManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLFilterManager.prototype.setContext = function (gl) { this.gl = gl; this.texturePool = []; @@ -44,12 +35,10 @@ }; /** -* @method begin -* @param renderSession {RenderSession} -* @param buffer {ArrayBuffer} -*/ -PIXI.WebGLFilterManager.prototype.begin = function(renderSession, buffer) -{ + * @param renderSession {RenderSession} + * @param buffer {ArrayBuffer} + */ +WebGLFilterManager.prototype.begin = function (renderSession, buffer) { this.renderSession = renderSession; this.defaultShader = renderSession.shaderManager.defaultShader; @@ -60,13 +49,11 @@ }; /** -* Applies the filter and adds it to the current filter stack. -* -* @method pushFilter -* @param filterBlock {Object} the filter that will be pushed to the current filter stack -*/ -PIXI.WebGLFilterManager.prototype.pushFilter = function(filterBlock) -{ + * Applies the filter and adds it to the current filter stack. + * + * @param filterBlock {object} the filter that will be pushed to the current filter stack + */ +WebGLFilterManager.prototype.pushFilter = function (filterBlock) { var gl = this.gl; var projection = this.renderSession.projection; @@ -84,12 +71,10 @@ this.offsetY += filterBlock._filterArea.y; var texture = this.texturePool.pop(); - if(!texture) - { - texture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!texture) { + texture = new FilterTexture(this.gl, this.width, this.height); } - else - { + else { texture.resize(this.width, this.height); } @@ -104,10 +89,10 @@ filterArea.height += padding * 2; // cap filter to screen size.. - if(filterArea.x < 0)filterArea.x = 0; - if(filterArea.width > this.width)filterArea.width = this.width; - if(filterArea.y < 0)filterArea.y = 0; - if(filterArea.height > this.height)filterArea.height = this.height; + if (filterArea.x < 0)filterArea.x = 0; + if (filterArea.width > this.width)filterArea.width = this.width; + if (filterArea.y < 0)filterArea.y = 0; + if (filterArea.height > this.height)filterArea.height = this.height; //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterArea.width, filterArea.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.bindFramebuffer(gl.FRAMEBUFFER, texture.frameBuffer); @@ -136,12 +121,10 @@ }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popFilter -*/ -PIXI.WebGLFilterManager.prototype.popFilter = function() -{ + * Removes the last filter from the filter stack and doesn't return it. + * + */ +WebGLFilterManager.prototype.popFilter = function () { var gl = this.gl; var filterBlock = this.filterStack.pop(); var filterArea = filterBlock._filterArea; @@ -149,8 +132,7 @@ var projection = this.renderSession.projection; var offset = this.renderSession.offset; - if(filterBlock.filterPasses.length > 1) - { + if (filterBlock.filterPasses.length > 1) { gl.viewport(0, 0, filterArea.width, filterArea.height); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer); @@ -180,7 +162,7 @@ var inputTexture = texture; var outputTexture = this.texturePool.pop(); - if(!outputTexture)outputTexture = new PIXI.FilterTexture(this.gl, this.width, this.height); + if (!outputTexture)outputTexture = new FilterTexture(this.gl, this.width, this.height); outputTexture.resize(this.width, this.height); // need to clear this FBO as it may have some left over elements from a previous filter. @@ -189,8 +171,7 @@ gl.disable(gl.BLEND); - for (var i = 0; i < filterBlock.filterPasses.length-1; i++) - { + for (var i = 0; i < filterBlock.filterPasses.length-1; i++) { var filterPass = filterBlock.filterPasses[i]; gl.bindFramebuffer(gl.FRAMEBUFFER, outputTexture.frameBuffer ); @@ -229,12 +210,10 @@ var buffer = this.buffer; // time to render the filters texture to the previous scene - if(this.filterStack.length === 0) - { + if (this.filterStack.length === 0) { gl.colorMask(true, true, true, true);//this.transparent); } - else - { + else { var currentFilter = this.filterStack[this.filterStack.length-1]; filterArea = currentFilter._filterArea; @@ -291,7 +270,7 @@ // bind the buffer gl.bindFramebuffer(gl.FRAMEBUFFER, buffer ); - // set the blend mode! + // set the blend mode! //gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) // set texture @@ -313,23 +292,20 @@ /** -* Applies the filter to the specified area. -* -* @method applyFilterPass -* @param filter {AbstractFilter} the filter that needs to be applied -* @param filterArea {Texture} TODO - might need an update -* @param width {Number} the horizontal range of the filter -* @param height {Number} the vertical range of the filter -*/ -PIXI.WebGLFilterManager.prototype.applyFilterPass = function(filter, filterArea, width, height) -{ + * Applies the filter to the specified area. + * + * @param filter {AbstractFilter} the filter that needs to be applied + * @param filterArea {Texture} TODO - might need an update + * @param width {number} the horizontal range of the filter + * @param height {number} the vertical range of the filter + */ +WebGLFilterManager.prototype.applyFilterPass = function (filter, filterArea, width, height) { // use program var gl = this.gl; var shader = filter.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc = filter.fragmentSrc; shader.uniforms = filter.uniforms; @@ -346,8 +322,7 @@ gl.uniform2f(shader.projectionVector, width/2, -height/2); gl.uniform2f(shader.offsetVector, 0,0); - if(filter.uniforms.dimensions) - { + if (filter.uniforms.dimensions) { filter.uniforms.dimensions.value[0] = this.width;//width; filter.uniforms.dimensions.value[1] = this.height;//height; filter.uniforms.dimensions.value[2] = this.vertexArray[0]; @@ -374,12 +349,10 @@ }; /** -* Initialises the shader buffers. -* -* @method initShaderBuffers -*/ -PIXI.WebGLFilterManager.prototype.initShaderBuffers = function() -{ + * Initialises the shader buffers. + * + */ +WebGLFilterManager.prototype.initShaderBuffers = function () { var gl = this.gl; // create some buffers @@ -390,7 +363,7 @@ // bind and upload the vertexs.. // keep a reference to the vertexFloatData.. - this.vertexArray = new PIXI.Float32Array([0.0, 0.0, + this.vertexArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -399,7 +372,7 @@ gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW); // bind and upload the uv buffer - this.uvArray = new PIXI.Float32Array([0.0, 0.0, + this.uvArray = new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); @@ -407,7 +380,7 @@ gl.bindBuffer(gl.ARRAY_BUFFER, this.uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvArray, gl.STATIC_DRAW); - this.colorArray = new PIXI.Float32Array([1.0, 0xFFFFFF, + this.colorArray = new Float32Array([1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF, 1.0, 0xFFFFFF]); @@ -422,16 +395,14 @@ }; /** -* Destroys the filter and removes it from the filter stack. -* -* @method destroy -*/ -PIXI.WebGLFilterManager.prototype.destroy = function() -{ + * Destroys the filter and removes it from the filter stack. + * + */ +WebGLFilterManager.prototype.destroy = function () { var gl = this.gl; this.filterStack = null; - + this.offsetX = 0; this.offsetY = 0; @@ -439,7 +410,7 @@ for (var i = 0; i < this.texturePool.length; i++) { this.texturePool[i].destroy(); } - + this.texturePool = null; //destroy buffers.. diff --git a/src/renderers/webgl/utils/WebGLGraphics.js b/src/renderers/webgl/utils/WebGLGraphics.js index 710383c..c7c7772 100644 --- a/src/renderers/webgl/utils/WebGLGraphics.js +++ b/src/renderers/webgl/utils/WebGLGraphics.js @@ -1,75 +1,62 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ - -/** * A set of functions used by the webGL renderer to draw the primitive graphics data * - * @class WebGLGraphics + * @namespace PIXI * @private - * @static */ -PIXI.WebGLGraphics = function() -{ -}; +var WebGLGraphics = module.exports = {}; /** * Renders the graphics object * * @static * @private - * @method renderGraphics * @param graphics {Graphics} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLGraphics.renderGraphics = function(graphics, renderSession)//projection, offset) -{ +WebGLGraphics.renderGraphics = function (graphics, renderSession)//projection, offset) { var gl = renderSession.gl; var projection = renderSession.projection, offset = renderSession.offset, shader = renderSession.shaderManager.primitiveShader, webGLData; - if(graphics.dirty) - { - PIXI.WebGLGraphics.updateGraphics(graphics, gl); + if (graphics.dirty) { + WebGLGraphics.updateGraphics(graphics, gl); } var webGL = graphics._webGL[gl.id]; // This could be speeded up for sure! - for (var i = 0; i < webGL.data.length; i++) - { - if(webGL.data[i].mode === 1) - { + for (var i = 0; i < webGL.data.length; i++) { + if (webGL.data[i].mode === 1) { webGLData = webGL.data[i]; renderSession.stencilManager.pushStencil(graphics, webGLData, renderSession); // render quad.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + renderSession.stencilManager.popStencil(graphics, webGLData, renderSession); } - else - { + else { webGLData = webGL.data[i]; - + renderSession.shaderManager.setShader( shader );//activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); - + gl.uniform1f(shader.flipY, 1); - + gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); @@ -88,16 +75,14 @@ * * @static * @private - * @method updateGraphics * @param graphicsData {Graphics} The graphics object to update * @param gl {WebGLContext} the current WebGL drawing context */ -PIXI.WebGLGraphics.updateGraphics = function(graphics, gl) -{ +WebGLGraphics.updateGraphics = function (graphics, gl) { // get the contexts graphics object var webGL = graphics._webGL[gl.id]; // if the graphics object does not exist in the webGL context time to create it! - if(!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + if (!webGL)webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; // flag the graphics as not dirty as we are about to update it... graphics.dirty = false; @@ -105,95 +90,79 @@ var i; // if the user cleared the graphics object we will need to clear every object - if(graphics.clearDirty) - { + if (graphics.clearDirty) { graphics.clearDirty = false; // lop through and return all the webGLDatas to the object pool so than can be reused later on - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { var graphicsData = webGL.data[i]; graphicsData.reset(); - PIXI.WebGLGraphics.graphicsDataPool.push( graphicsData ); + WebGLGraphics.graphicsDataPool.push( graphicsData ); } - // clear the array and reset the index.. + // clear the array and reset the index.. webGL.data = []; webGL.lastIndex = 0; } - + var webGLData; - + // loop through the graphics datas and construct each one.. // if the object is a complex fill then the new stencil buffer technique will be used // other wise graphics objects will be pushed into a batch.. - for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) - { + for (i = webGL.lastIndex; i < graphics.graphicsData.length; i++) { var data = graphics.graphicsData[i]; - if(data.type === PIXI.Graphics.POLY) - { + if (data.type === Graphics.POLY) { // need to add the points the the graphics object.. data.points = data.shape.points.slice(); - if(data.shape.closed) - { + if (data.shape.closed) { // close the poly if the value is true! - if(data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) - { + if (data.points[0] !== data.points[data.points.length-2] || data.points[1] !== data.points[data.points.length-1]) { data.points.push(data.points[0], data.points[1]); } } // MAKE SURE WE HAVE THE CORRECT TYPE.. - if(data.fill) - { - if(data.points.length >= 6) - { - if(data.points.length < 6 * 2) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - var canDrawUsingSimple = PIXI.WebGLGraphics.buildPoly(data, webGLData); + if (data.fill) { + if (data.points.length >= 6) { + if (data.points.length < 6 * 2) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + var canDrawUsingSimple = WebGLGraphics.buildPoly(data, webGLData); // console.log(canDrawUsingSimple); - if(!canDrawUsingSimple) - { + if (!canDrawUsingSimple) { // console.log("<>>>") - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } - + } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 1); - PIXI.WebGLGraphics.buildComplexPoly(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 1); + WebGLGraphics.buildComplexPoly(data, webGLData); } } } - if(data.lineWidth > 0) - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - PIXI.WebGLGraphics.buildLine(data, webGLData); + if (data.lineWidth > 0) { + webGLData = WebGLGraphics.switchMode(webGL, 0); + WebGLGraphics.buildLine(data, webGLData); } } - else - { - webGLData = PIXI.WebGLGraphics.switchMode(webGL, 0); - - if(data.type === PIXI.Graphics.RECT) - { - PIXI.WebGLGraphics.buildRectangle(data, webGLData); + else { + webGLData = WebGLGraphics.switchMode(webGL, 0); + + if (data.type === Graphics.RECT) { + WebGLGraphics.buildRectangle(data, webGLData); } - else if(data.type === PIXI.Graphics.CIRC || data.type === PIXI.Graphics.ELIP) - { - PIXI.WebGLGraphics.buildCircle(data, webGLData); + else if (data.type === Graphics.CIRC || data.type === Graphics.ELIP) { + WebGLGraphics.buildCircle(data, webGLData); } - else if(data.type === PIXI.Graphics.RREC) - { - PIXI.WebGLGraphics.buildRoundedRectangle(data, webGLData); + else if (data.type === Graphics.RREC) { + WebGLGraphics.buildRoundedRectangle(data, webGLData); } } @@ -201,37 +170,31 @@ } // upload all the dirty data... - for (i = 0; i < webGL.data.length; i++) - { + for (i = 0; i < webGL.data.length; i++) { webGLData = webGL.data[i]; - if(webGLData.dirty)webGLData.upload(); + if (webGLData.dirty)webGLData.upload(); } }; /** * @static * @private - * @method switchMode * @param webGL {WebGLContext} - * @param type {Number} + * @param type {number} */ -PIXI.WebGLGraphics.switchMode = function(webGL, type) -{ +WebGLGraphics.switchMode = function (webGL, type) { var webGLData; - if(!webGL.data.length) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (!webGL.data.length) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } - else - { + else { webGLData = webGL.data[webGL.data.length-1]; - if(webGLData.mode !== type || type === 1) - { - webGLData = PIXI.WebGLGraphics.graphicsDataPool.pop() || new PIXI.WebGLGraphicsData(webGL.gl); + if (webGLData.mode !== type || type === 1) { + webGLData = WebGLGraphics.graphicsDataPool.pop() || new WebGLGraphicsData(webGL.gl); webGLData.mode = type; webGL.data.push(webGLData); } @@ -247,12 +210,10 @@ * * @static * @private - * @method buildRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRectangle = function (graphicsData, webGLData) { // --- // // need to convert points to a nice regular data // @@ -262,9 +223,8 @@ var width = rectData.width; var height = rectData.height; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -293,8 +253,7 @@ indices.push(vertPos, vertPos, vertPos+1, vertPos+2, vertPos+3, vertPos+3); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = [x, y, @@ -304,7 +263,7 @@ x, y]; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -315,12 +274,10 @@ * * @static * @private - * @method buildRoundedRectangle * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildRoundedRectangle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildRoundedRectangle = function (graphicsData, webGLData) { var rrectData = graphicsData.shape; var x = rrectData.x; var y = rrectData.y; @@ -331,13 +288,13 @@ var recPoints = []; recPoints.push(x, y + radius); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); - recPoints = recPoints.concat(PIXI.WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x, y + height - radius, x, y + height, x + radius, y + height)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width - radius, y + height, x + width, y + height, x + width, y + height - radius)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + width, y + radius, x + width, y, x + width - radius, y)); + recPoints = recPoints.concat(WebGLGraphics.quadraticBezierCurve(x + radius, y, x, y, x, y + radius)); if (graphicsData.fill) { - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -349,13 +306,12 @@ var vecPos = verts.length/6; - var triangles = PIXI.PolyK.Triangulate(recPoints); + var triangles = PolyK.Triangulate(recPoints); - // - + // + var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vecPos); indices.push(triangles[i] + vecPos); indices.push(triangles[i+1] + vecPos); @@ -364,8 +320,7 @@ } - for (i = 0; i < recPoints.length; i++) - { + for (i = 0; i < recPoints.length; i++) { verts.push(recPoints[i], recPoints[++i], r, g, b, alpha); } } @@ -375,7 +330,7 @@ graphicsData.points = recPoints; - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -387,16 +342,15 @@ * * @static * @private - * @method quadraticBezierCurve - * @param fromX {Number} Origin point x - * @param fromY {Number} Origin point x - * @param cpX {Number} Control point x - * @param cpY {Number} Control point y - * @param toX {Number} Destination point x - * @param toY {Number} Destination point y + * @param fromX {number} Origin point x + * @param fromY {number} Origin point x + * @param cpX {number} Control point x + * @param cpY {number} Control point y + * @param toX {number} Destination point x + * @param toY {number} Destination point y * @return {Array(Number)} */ -PIXI.WebGLGraphics.quadraticBezierCurve = function(fromX, fromY, cpX, cpY, toX, toY) { +WebGLGraphics.quadraticBezierCurve = function (fromX, fromY, cpX, cpY, toX, toY) { var xa, ya, @@ -414,8 +368,7 @@ } var j = 0; - for (var i = 0; i <= n; i++ ) - { + for (var i = 0; i <= n; i++ ) { j = i / n; // The Green Line @@ -438,27 +391,23 @@ * * @static * @private - * @method buildCircle * @param graphicsData {Graphics} The graphics object to draw - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildCircle = function(graphicsData, webGLData) -{ +WebGLGraphics.buildCircle = function (graphicsData, webGLData) { // need to convert points to a nice regular data var circleData = graphicsData.shape; var x = circleData.x; var y = circleData.y; var width; var height; - + // TODO - bit hacky?? - if(graphicsData.type === PIXI.Graphics.CIRC) - { + if (graphicsData.type === Graphics.CIRC) { width = circleData.radius; height = circleData.radius; } - else - { + else { width = circleData.width; height = circleData.height; } @@ -468,9 +417,8 @@ var i = 0; - if(graphicsData.fill) - { - var color = PIXI.hex2rgb(graphicsData.fillColor); + if (graphicsData.fill) { + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; @@ -484,8 +432,7 @@ indices.push(vecPos); - for (i = 0; i < totalSegs + 1 ; i++) - { + for (i = 0; i < totalSegs + 1 ; i++) { verts.push(x,y, r, g, b, alpha); verts.push(x + Math.sin(seg * i) * width, @@ -498,19 +445,17 @@ indices.push(vecPos-1); } - if(graphicsData.lineWidth) - { + if (graphicsData.lineWidth) { var tempPoints = graphicsData.points; graphicsData.points = []; - for (i = 0; i < totalSegs + 1; i++) - { + for (i = 0; i < totalSegs + 1; i++) { graphicsData.points.push(x + Math.sin(seg * i) * width, y + Math.cos(seg * i) * height); } - PIXI.WebGLGraphics.buildLine(graphicsData, webGLData); + WebGLGraphics.buildLine(graphicsData, webGLData); graphicsData.points = tempPoints; } @@ -521,39 +466,35 @@ * * @static * @private - * @method buildLine * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildLine = function(graphicsData, webGLData) -{ +WebGLGraphics.buildLine = function (graphicsData, webGLData) { // TODO OPTIMISE! var i = 0; var points = graphicsData.points; - if(points.length === 0)return; + if (points.length === 0)return; // if the line width is an odd number add 0.5 to align to a whole pixel - if(graphicsData.lineWidth%2) - { + if (graphicsData.lineWidth%2) { for (i = 0; i < points.length; i++) { points[i] += 0.5; } } // get first and last point.. figure out the middle! - var firstPoint = new PIXI.Point( points[0], points[1] ); - var lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + var firstPoint = new Point( points[0], points[1] ); + var lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); // if the first point is the last point - gonna have issues :) - if(firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) - { + if (firstPoint.x === lastPoint.x && firstPoint.y === lastPoint.y) { // need to clone as we are going to slightly modify the shape.. points = points.slice(); points.pop(); points.pop(); - lastPoint = new PIXI.Point( points[points.length - 2], points[points.length - 1] ); + lastPoint = new Point( points[points.length - 2], points[points.length - 1] ); var midPointX = lastPoint.x + (firstPoint.x - lastPoint.x) *0.5; var midPointY = lastPoint.y + (firstPoint.y - lastPoint.y) *0.5; @@ -572,7 +513,7 @@ var width = graphicsData.lineWidth / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.lineColor); + var color = hex2rgb(graphicsData.lineColor); var alpha = graphicsData.lineAlpha; var r = color[0] * alpha; var g = color[1] * alpha; @@ -606,8 +547,7 @@ verts.push(p1x + perpx , p1y + perpy, r, g, b, alpha); - for (i = 1; i < length-1; i++) - { + for (i = 1; i < length-1; i++) { p1x = points[(i-1)*2]; p1y = points[(i-1)*2 + 1]; @@ -644,8 +584,7 @@ denom = a1*b2 - a2*b1; - if(Math.abs(denom) < 0.1 ) - { + if (Math.abs(denom) < 0.1 ) { denom+=10.1; verts.push(p2x - perpx , p2y - perpy, @@ -664,8 +603,7 @@ pdist = (px -p2x) * (px -p2x) + (py -p2y) + (py -p2y); - if(pdist > 140 * 140) - { + if (pdist > 140 * 140) { perp3x = perpx - perp2x; perp3y = perpy - perp2y; @@ -686,8 +624,7 @@ indexCount++; } - else - { + else { verts.push(px , py); verts.push(r, g, b, alpha); @@ -720,8 +657,7 @@ indices.push(indexStart); - for (i = 0; i < indexCount; i++) - { + for (i = 0; i < indexCount; i++) { indices.push(indexStart++); } @@ -733,21 +669,19 @@ * * @static * @private - * @method buildComplexPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildComplexPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildComplexPoly = function (graphicsData, webGLData) { //TODO - no need to copy this as it gets turned into a FLoat32Array anyways.. var points = graphicsData.points.slice(); - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var indices = webGLData.indices; webGLData.points = points; webGLData.alpha = graphicsData.fillAlpha; - webGLData.color = PIXI.hex2rgb(graphicsData.fillColor); + webGLData.color = hex2rgb(graphicsData.fillColor); /* calclate the bounds.. @@ -761,8 +695,7 @@ var x,y; // get size.. - for (var i = 0; i < points.length; i+=2) - { + for (var i = 0; i < points.length; i+=2) { x = points[i]; y = points[i+1]; @@ -779,12 +712,11 @@ maxX, maxY, minX, maxY); - // push a quad onto the end.. - + // push a quad onto the end.. + //TODO - this aint needed! var length = points.length / 2; - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { indices.push( i ); } @@ -795,15 +727,13 @@ * * @static * @private - * @method buildPoly * @param graphicsData {Graphics} The graphics object containing all the necessary properties - * @param webGLData {Object} + * @param webGLData {object} */ -PIXI.WebGLGraphics.buildPoly = function(graphicsData, webGLData) -{ +WebGLGraphics.buildPoly = function (graphicsData, webGLData) { var points = graphicsData.points; - if(points.length < 6)return; + if (points.length < 6)return; // get first and last point.. figure out the middle! var verts = webGLData.points; var indices = webGLData.indices; @@ -811,22 +741,21 @@ var length = points.length / 2; // sort color - var color = PIXI.hex2rgb(graphicsData.fillColor); + var color = hex2rgb(graphicsData.fillColor); var alpha = graphicsData.fillAlpha; var r = color[0] * alpha; var g = color[1] * alpha; var b = color[2] * alpha; - var triangles = PIXI.PolyK.Triangulate(points); + var triangles = PolyK.Triangulate(points); - if(!triangles)return false; + if (!triangles)return false; var vertPos = verts.length / 6; var i = 0; - for (i = 0; i < triangles.length; i+=3) - { + for (i = 0; i < triangles.length; i+=3) { indices.push(triangles[i] + vertPos); indices.push(triangles[i] + vertPos); indices.push(triangles[i+1] + vertPos); @@ -834,8 +763,7 @@ indices.push(triangles[i+2] + vertPos); } - for (i = 0; i < length; i++) - { + for (i = 0; i < length; i++) { verts.push(points[i * 2], points[i * 2 + 1], r, g, b, alpha); } @@ -843,15 +771,14 @@ return true; }; -PIXI.WebGLGraphics.graphicsDataPool = []; +WebGLGraphics.graphicsDataPool = []; /** - * @class WebGLGraphicsData + * @class * @private * @static */ -PIXI.WebGLGraphicsData = function(gl) -{ +function WebGLGraphicsData(gl) { this.gl = gl; //TODO does this need to be split before uploding?? @@ -866,28 +793,26 @@ }; /** - * @method reset + * */ -PIXI.WebGLGraphicsData.prototype.reset = function() -{ +WebGLGraphicsData.prototype.reset = function () { this.points = []; this.indices = []; }; /** - * @method upload + * */ -PIXI.WebGLGraphicsData.prototype.upload = function() -{ +WebGLGraphicsData.prototype.upload = function () { var gl = this.gl; // this.lastIndex = graphics.graphicsData.length; - this.glPoints = new PIXI.Float32Array(this.points); + this.glPoints = new Float32Array(this.points); gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); gl.bufferData(gl.ARRAY_BUFFER, this.glPoints, gl.STATIC_DRAW); - this.glIndicies = new PIXI.Uint16Array(this.indices); + this.glIndicies = new Uint16Array(this.indices); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.glIndicies, gl.STATIC_DRAW); diff --git a/src/renderers/webgl/utils/WebGLMaskManager.js b/src/renderers/webgl/utils/WebGLMaskManager.js index 607d5dd..f89ffce 100644 --- a/src/renderers/webgl/utils/WebGLMaskManager.js +++ b/src/renderers/webgl/utils/WebGLMaskManager.js @@ -1,69 +1,56 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLMaskManager -* @constructor -* @private -*/ -PIXI.WebGLMaskManager = function() -{ +function WebGLMaskManager() { }; -PIXI.WebGLMaskManager.prototype.constructor = PIXI.WebGLMaskManager; +WebGLMaskManager.prototype.constructor = WebGLMaskManager; +module.exports = WebGLMaskManager; /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLMaskManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLMaskManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param maskData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLMaskManager.prototype.pushMask = function(maskData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param maskData {Array} + * @param renderSession {object} + */ +WebGLMaskManager.prototype.pushMask = function (maskData, renderSession) { var gl = renderSession.gl; - if(maskData.dirty) - { - PIXI.WebGLGraphics.updateGraphics(maskData, gl); + if (maskData.dirty) { + WebGLGraphics.updateGraphics(maskData, gl); } - if(!maskData._webGL[gl.id].data.length)return; + if (!maskData._webGL[gl.id].data.length)return; renderSession.stencilManager.pushStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Removes the last filter from the filter stack and doesn't return it. -* -* @method popMask -* @param maskData {Array} -* @param renderSession {Object} an object containing all the useful parameters -*/ -PIXI.WebGLMaskManager.prototype.popMask = function(maskData, renderSession) -{ + * Removes the last filter from the filter stack and doesn't return it. + * + * @param maskData {Array} + * @param renderSession {object} an object containing all the useful parameters + */ +WebGLMaskManager.prototype.popMask = function (maskData, renderSession) { var gl = this.gl; renderSession.stencilManager.popStencil(maskData, maskData._webGL[gl.id].data[0], renderSession); }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLMaskManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLMaskManager.prototype.destroy = function () { this.gl = null; }; diff --git a/src/renderers/webgl/utils/WebGLShaderManager.js b/src/renderers/webgl/utils/WebGLShaderManager.js index a15974e..de37591 100644 --- a/src/renderers/webgl/utils/WebGLShaderManager.js +++ b/src/renderers/webgl/utils/WebGLShaderManager.js @@ -1,111 +1,92 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLShaderManager -* @constructor -* @private -*/ -PIXI.WebGLShaderManager = function() -{ +function WebGLShaderManager() { /** - * @property maxAttibs - * @type Number + * @member {number} */ this.maxAttibs = 10; /** - * @property attribState - * @type Array + * @member {Array} */ this.attribState = []; /** - * @property tempAttribState - * @type Array + * @member {Array} */ this.tempAttribState = []; - for (var i = 0; i < this.maxAttibs; i++) - { + for (var i = 0; i < this.maxAttibs; i++) { this.attribState[i] = false; } /** - * @property stack - * @type Array + * @member {Array} */ this.stack = []; }; -PIXI.WebGLShaderManager.prototype.constructor = PIXI.WebGLShaderManager; +WebGLShaderManager.prototype.constructor = WebGLShaderManager; +module.exports = WebGLShaderManager; /** -* Initialises the context and the properties. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLShaderManager.prototype.setContext = function(gl) -{ + * Initialises the context and the properties. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLShaderManager.prototype.setContext = function (gl) { this.gl = gl; - + // the next one is used for rendering primitives - this.primitiveShader = new PIXI.PrimitiveShader(gl); + this.primitiveShader = new PrimitiveShader(gl); // the next one is used for rendering triangle strips - this.complexPrimitiveShader = new PIXI.ComplexPrimitiveShader(gl); + this.complexPrimitiveShader = new ComplexPrimitiveShader(gl); // this shader is used for the default sprite rendering - this.defaultShader = new PIXI.PixiShader(gl); + this.defaultShader = new PixiShader(gl); // this shader is used for the fast sprite rendering - this.fastShader = new PIXI.PixiFastShader(gl); + this.fastShader = new PixiFastShader(gl); // the next one is used for rendering triangle strips - this.stripShader = new PIXI.StripShader(gl); + this.stripShader = new StripShader(gl); this.setShader(this.defaultShader); }; /** -* Takes the attributes given in parameters. -* -* @method setAttribs -* @param attribs {Array} attribs -*/ -PIXI.WebGLShaderManager.prototype.setAttribs = function(attribs) -{ + * Takes the attributes given in parameters. + * + * @param attribs {Array} attribs + */ +WebGLShaderManager.prototype.setAttribs = function (attribs) { // reset temp state var i; - for (i = 0; i < this.tempAttribState.length; i++) - { + for (i = 0; i < this.tempAttribState.length; i++) { this.tempAttribState[i] = false; } // set the new attribs - for (i = 0; i < attribs.length; i++) - { + for (i = 0; i < attribs.length; i++) { var attribId = attribs[i]; this.tempAttribState[attribId] = true; } var gl = this.gl; - for (i = 0; i < this.attribState.length; i++) - { - if(this.attribState[i] !== this.tempAttribState[i]) - { + for (i = 0; i < this.attribState.length; i++) { + if (this.attribState[i] !== this.tempAttribState[i]) { this.attribState[i] = this.tempAttribState[i]; - if(this.tempAttribState[i]) - { + if (this.tempAttribState[i]) { gl.enableVertexAttribArray(i); } - else - { + else { gl.disableVertexAttribArray(i); } } @@ -113,15 +94,13 @@ }; /** -* Sets the current shader. -* -* @method setShader -* @param shader {Any} -*/ -PIXI.WebGLShaderManager.prototype.setShader = function(shader) -{ - if(this._currentId === shader._UID)return false; - + * Sets the current shader. + * + * @param shader {Any} + */ +WebGLShaderManager.prototype.setShader = function (shader) { + if (this._currentId === shader._UID)return false; + this._currentId = shader._UID; this.currentShader = shader; @@ -133,12 +112,10 @@ }; /** -* Destroys this object. -* -* @method destroy -*/ -PIXI.WebGLShaderManager.prototype.destroy = function() -{ + * Destroys this object. + * + */ +WebGLShaderManager.prototype.destroy = function () { this.attribState = null; this.tempAttribState = null; diff --git a/src/renderers/webgl/utils/WebGLShaderUtils.js b/src/renderers/webgl/utils/WebGLShaderUtils.js index a7d7826..3d3b2e0 100644 --- a/src/renderers/webgl/utils/WebGLShaderUtils.js +++ b/src/renderers/webgl/utils/WebGLShaderUtils.js @@ -1,88 +1,74 @@ -/** - * @author Mat Groves http://matgroves.com/ @Doormat23 - */ +var glUtils = module.exports = { + /** + * @static + * @private + */ + initDefaultShaders: function () { + }, -/** -* @method initDefaultShaders -* @static -* @private -*/ -PIXI.initDefaultShaders = function() -{ -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileVertexShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); + }, -/** -* @method CompileVertexShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileVertexShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.VERTEX_SHADER); -}; + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @return {Any} + */ + CompileFragmentShader: function (gl, shaderSrc) { + return glUtils._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); + }, -/** -* @method CompileFragmentShader -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @return {Any} -*/ -PIXI.CompileFragmentShader = function(gl, shaderSrc) -{ - return PIXI._CompileShader(gl, shaderSrc, gl.FRAGMENT_SHADER); -}; + /** + * @static + * @private + * @param gl {WebGLContext} the current WebGL drawing context + * @param shaderSrc {Array} + * @param shaderType {number} + * @return {Any} + */ + _CompileShader: function (gl, shaderSrc, shaderType) { + var src = shaderSrc.join("\n"); + var shader = gl.createShader(shaderType); + gl.shaderSource(shader, src); + gl.compileShader(shader); -/** -* @method _CompileShader -* @static -* @private -* @param gl {WebGLContext} the current WebGL drawing context -* @param shaderSrc {Array} -* @param shaderType {Number} -* @return {Any} -*/ -PIXI._CompileShader = function(gl, shaderSrc, shaderType) -{ - var src = shaderSrc.join("\n"); - var shader = gl.createShader(shaderType); - gl.shaderSource(shader, src); - gl.compileShader(shader); + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + window.console.log(gl.getShaderInfoLog(shader)); + return null; + } - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) - { - window.console.log(gl.getShaderInfoLog(shader)); - return null; + return shader; + }, + + /** + * @static + * @param gl {WebGLContext} the current WebGL drawing context + * @param vertexSrc {Array} + * @param fragmentSrc {Array} + * @return {Any} + */ + compileProgram: function (gl, vertexSrc, fragmentSrc) { + var fragmentShader = glUtils.CompileFragmentShader(gl, fragmentSrc); + var vertexShader = glUtils.CompileVertexShader(gl, vertexSrc); + + var shaderProgram = gl.createProgram(); + + gl.attachShader(shaderProgram, vertexShader); + gl.attachShader(shaderProgram, fragmentShader); + gl.linkProgram(shaderProgram); + + if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { + window.console.log("Could not initialise shaders"); + } + + return shaderProgram; } - - return shader; -}; - -/** -* @method compileProgram -* @static -* @param gl {WebGLContext} the current WebGL drawing context -* @param vertexSrc {Array} -* @param fragmentSrc {Array} -* @return {Any} -*/ -PIXI.compileProgram = function(gl, vertexSrc, fragmentSrc) -{ - var fragmentShader = PIXI.CompileFragmentShader(gl, fragmentSrc); - var vertexShader = PIXI.CompileVertexShader(gl, vertexSrc); - - var shaderProgram = gl.createProgram(); - - gl.attachShader(shaderProgram, vertexShader); - gl.attachShader(shaderProgram, fragmentShader); - gl.linkProgram(shaderProgram); - - if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) - { - window.console.log("Could not initialise shaders"); - } - - return shaderProgram; }; diff --git a/src/renderers/webgl/utils/WebGLSpriteBatch.js b/src/renderers/webgl/utils/WebGLSpriteBatch.js index 711b4da..9e28881 100644 --- a/src/renderers/webgl/utils/WebGLSpriteBatch.js +++ b/src/renderers/webgl/utils/WebGLSpriteBatch.js @@ -1,80 +1,71 @@ /** * @author Mat Groves - * + * * Big thanks to the very clever Matt DesLauriers https://github.com/mattdesl/ * for creating the original pixi version! * Also a thanks to https://github.com/bchevalier for tweaking the tint and alpha so that they now share 4 bytes on the vertex buffer - * + * * Heavily inspired by LibGDX's WebGLSpriteBatch: * https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/WebGLSpriteBatch.java */ - /** +/** * - * @class WebGLSpriteBatch + * @class * @private - * @constructor + * @namespace PIXI */ -PIXI.WebGLSpriteBatch = function() -{ +function WebGLSpriteBatch() { /** - * @property vertSize - * @type Number + * @member {number} */ this.vertSize = 5; /** * The number of images in the SpriteBatch before it flushes - * @property size - * @type Number + * @member {number} */ this.size = 2000;//Math.pow(2, 16) / this.vertSize; - //the total number of bytes in our batch + // the total number of bytes in our batch var numVerts = this.size * 4 * 4 * this.vertSize; - //the total number of indices in our batch + // the total number of indices in our batch var numIndices = this.size * 6; /** - * Holds the vertices - * - * @property vertices - * @type ArrayBuffer - */ - this.vertices = new PIXI.ArrayBuffer(numVerts); + * Holds the vertices + * + * @member {ArrayBuffer} + */ + this.vertices = new ArrayBuffer(numVerts); /** - * View on the vertices as a Float32Array - * - * @property positions - * @type Float32Array - */ - this.positions = new PIXI.Float32Array(this.vertices); + * View on the vertices as a Float32Array + * + * @member {Float32Array} + */ + this.positions = new Float32Array(this.vertices); /** - * View on the vertices as a Uint32Array - * - * @property colors - * @type Uint32Array - */ - this.colors = new PIXI.Uint32Array(this.vertices); + * View on the vertices as a Uint32Array + * + * @member {Uint32Array} + */ + this.colors = new Uint32Array(this.vertices); /** * Holds the indices * - * @property indices - * @type Uint16Array + * @member {Uint16Array} */ - this.indices = new PIXI.Uint16Array(numIndices); - + this.indices = new Uint16Array(numIndices); + /** - * @property lastIndexCount - * @type Number + * @member {number} */ this.lastIndexCount = 0; - for (var i=0, j=0; i < numIndices; i += 6, j += 4) - { + for (var i=0, j=0; i < numIndices; i += 6, j += 4) { this.indices[i + 0] = j + 0; this.indices[i + 1] = j + 1; this.indices[i + 2] = j + 2; @@ -84,58 +75,49 @@ } /** - * @property drawing - * @type Boolean + * @member {boolean} */ this.drawing = false; /** - * @property currentBatchSize - * @type Number + * @member {number} */ this.currentBatchSize = 0; /** - * @property currentBaseTexture - * @type BaseTexture + * @member {BaseTexture} */ this.currentBaseTexture = null; /** - * @property dirty - * @type Boolean + * @member {boolean} */ this.dirty = true; /** - * @property textures - * @type Array + * @member {Array} */ this.textures = []; /** - * @property blendModes - * @type Array + * @member {Array} */ this.blendModes = []; /** - * @property shaders - * @type Array + * @member {Array} */ this.shaders = []; /** - * @property sprites - * @type Array + * @member {Array} */ this.sprites = []; /** - * @property defaultShader - * @type AbstractFilter + * @member {AbstractFilter} */ - this.defaultShader = new PIXI.AbstractFilter([ + this.defaultShader = new AbstractFilter([ 'precision lowp float;', 'varying vec2 vTextureCoord;', 'varying vec4 vColor;', @@ -147,11 +129,9 @@ }; /** -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLSpriteBatch.prototype.setContext = function(gl) -{ + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLSpriteBatch.prototype.setContext = function (gl) { this.gl = gl; // create a couple of buffers @@ -169,7 +149,7 @@ this.currentBlendMode = 99999; - var shader = new PIXI.PixiShader(gl); + var shader = new PixiShader(gl); shader.fragmentSrc = this.defaultShader.fragmentSrc; shader.uniforms = {}; @@ -179,11 +159,9 @@ }; /** -* @method begin -* @param renderSession {Object} The RenderSession object -*/ -PIXI.WebGLSpriteBatch.prototype.begin = function(renderSession) -{ + * @param renderSession {object} The RenderSession object + */ +WebGLSpriteBatch.prototype.begin = function (renderSession) { this.renderSession = renderSession; this.shader = this.renderSession.shaderManager.defaultShader; @@ -191,25 +169,20 @@ }; /** -* @method end -*/ -PIXI.WebGLSpriteBatch.prototype.end = function() -{ + */ +WebGLSpriteBatch.prototype.end = function () { this.flush(); }; /** -* @method render -* @param sprite {Sprite} the sprite to render when using this spritebatch -*/ -PIXI.WebGLSpriteBatch.prototype.render = function(sprite) -{ + * @param sprite {Sprite} the sprite to render when using this spritebatch + */ +WebGLSpriteBatch.prototype.render = function (sprite) { var texture = sprite.texture; - //TODO set blend modes.. + //TODO set blend modes.. // check texture.. - if(this.currentBatchSize >= this.size) - { + if (this.currentBatchSize >= this.size) { this.flush(); this.currentBaseTexture = texture.baseTexture; } @@ -217,16 +190,15 @@ // get the uvs for the texture var uvs = texture._uvs; // if the uvs have not updated then no point rendering just yet! - if(!uvs)return; + if (!uvs)return; // TODO trim?? var aX = sprite.anchor.x; var aY = sprite.anchor.y; var w0, w1, h0, h1; - - if (texture.trim) - { + + if (texture.trim) { // if the sprite is trimmed then we need to add the extra space before transforming the sprite coords.. var trim = texture.trim; @@ -237,8 +209,7 @@ h0 = h1 + texture.crop.height; } - else - { + else { w0 = (texture.frame.width ) * (1-aX); w1 = (texture.frame.width ) * -aX; @@ -247,7 +218,7 @@ } var index = this.currentBatchSize * 4 * this.vertSize; - + var resolution = texture.baseTexture.resolution; var worldTransform = sprite.worldTransform; @@ -262,8 +233,7 @@ var colors = this.colors; var positions = this.positions; - if(this.renderSession.roundPixels) - { + if (this.renderSession.roundPixels) { // xy positions[index] = a * w1 + c * h1 + tx | 0; positions[index+1] = d * h1 + b * w1 + ty | 0; @@ -280,8 +250,7 @@ positions[index+15] = a * w1 + c * h0 + tx | 0; positions[index+16] = d * h0 + b * w1 + ty | 0; } - else - { + else { // xy positions[index] = a * w1 + c * h1 + tx; positions[index+1] = d * h1 + b * w1 + ty; @@ -298,7 +267,7 @@ positions[index+15] = a * w1 + c * h0 + tx; positions[index+16] = d * h0 + b * w1 + ty; } - + // uv positions[index+2] = uvs.x0; positions[index+3] = uvs.y0; @@ -326,18 +295,15 @@ }; /** -* Renders a TilingSprite using the spriteBatch. -* -* @method renderTilingSprite -* @param sprite {TilingSprite} the tilingSprite to render -*/ -PIXI.WebGLSpriteBatch.prototype.renderTilingSprite = function(tilingSprite) -{ + * Renders a TilingSprite using the spriteBatch. + * + * @param sprite {TilingSprite} the tilingSprite to render + */ +WebGLSpriteBatch.prototype.renderTilingSprite = function (tilingSprite) { var texture = tilingSprite.tilingTexture; // check texture.. - if(this.currentBatchSize >= this.size) - { + if (this.currentBatchSize >= this.size) { //return; this.flush(); this.currentBaseTexture = texture.baseTexture; @@ -346,7 +312,7 @@ // set the textures uvs temporarily // TODO create a separate texture so that we can tile part of a texture - if(!tilingSprite._uvs)tilingSprite._uvs = new PIXI.TextureUvs(); + if (!tilingSprite._uvs)tilingSprite._uvs = new TextureUvs(); var uvs = tilingSprite._uvs; @@ -420,7 +386,7 @@ positions[index++] = uvs.y1; // color colors[index++] = color; - + // xy positions[index++] = a * w0 + c * h0 + tx; positions[index++] = d * h0 + b * w0 + ty; @@ -444,20 +410,17 @@ }; /** -* Renders the content and empties the current batch. -* -* @method flush -*/ -PIXI.WebGLSpriteBatch.prototype.flush = function() -{ + * Renders the content and empties the current batch. + * + */ +WebGLSpriteBatch.prototype.flush = function () { // If the batch is length 0 then return as there is nothing to draw if (this.currentBatchSize===0)return; var gl = this.gl; var shader; - if(this.dirty) - { + if (this.dirty) { this.dirty = false; // bind the main texture gl.activeTexture(gl.TEXTURE0); @@ -477,13 +440,11 @@ gl.vertexAttribPointer(shader.colorAttribute, 4, gl.UNSIGNED_BYTE, true, stride, 4 * 4); } - // upload the verts to the buffer - if(this.currentBatchSize > ( this.size * 0.5 ) ) - { + // upload the verts to the buffer + if (this.currentBatchSize > ( this.size * 0.5 ) ) { gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); } - else - { + else { var view = this.positions.subarray(0, this.currentBatchSize * 4 * this.vertSize); gl.bufferSubData(gl.ARRAY_BUFFER, 0, view); } @@ -501,7 +462,7 @@ var sprite; for (var i = 0, j = this.currentBatchSize; i < j; i++) { - + sprite = this.sprites[i]; nextTexture = sprite.texture.baseTexture; @@ -511,29 +472,25 @@ blendSwap = currentBlendMode !== nextBlendMode; shaderSwap = currentShader !== nextShader; // should I use _UIDS??? - if(currentBaseTexture !== nextTexture || blendSwap || shaderSwap) - { + if (currentBaseTexture !== nextTexture || blendSwap || shaderSwap) { this.renderBatch(currentBaseTexture, batchSize, start); start = i; batchSize = 0; currentBaseTexture = nextTexture; - if( blendSwap ) - { + if ( blendSwap ) { currentBlendMode = nextBlendMode; this.renderSession.blendModeManager.setBlendMode( currentBlendMode ); } - if( shaderSwap ) - { + if ( shaderSwap ) { currentShader = nextShader; - + shader = currentShader.shaders[gl.id]; - if(!shader) - { - shader = new PIXI.PixiShader(gl); + if (!shader) { + shader = new PixiShader(gl); shader.fragmentSrc =currentShader.fragmentSrc; shader.uniforms =currentShader.uniforms; @@ -545,8 +502,8 @@ // set shader function??? this.renderSession.shaderManager.setShader(shader); - if(shader.dirty)shader.syncUniforms(); - + if (shader.dirty)shader.syncUniforms(); + // both thease only need to be set if they are changing.. // set the projection var projection = this.renderSession.projection; @@ -570,66 +527,58 @@ }; /** -* @method renderBatch -* @param texture {Texture} -* @param size {Number} -* @param startIndex {Number} -*/ -PIXI.WebGLSpriteBatch.prototype.renderBatch = function(texture, size, startIndex) -{ - if(size === 0)return; + * @param texture {Texture} + * @param size {number} + * @param startIndex {number} + */ +WebGLSpriteBatch.prototype.renderBatch = function (texture, size, startIndex) { + if (size === 0)return; var gl = this.gl; // check if a texture is dirty.. - if(texture._dirty[gl.id]) - { + if (texture._dirty[gl.id]) { this.renderSession.renderer.updateTexture(texture); } - else - { + else { // bind the current texture gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); } // now draw those suckas! gl.drawElements(gl.TRIANGLES, size * 6, gl.UNSIGNED_SHORT, startIndex * 6 * 2); - + // increment the draw count this.renderSession.drawCount++; }; /** -* @method stop -*/ -PIXI.WebGLSpriteBatch.prototype.stop = function() -{ + * + */ +WebGLSpriteBatch.prototype.stop = function () { this.flush(); this.dirty = true; }; /** -* @method start -*/ -PIXI.WebGLSpriteBatch.prototype.start = function() -{ + * + */ +WebGLSpriteBatch.prototype.start = function () { this.dirty = true; }; /** -* Destroys the SpriteBatch. -* -* @method destroy -*/ -PIXI.WebGLSpriteBatch.prototype.destroy = function() -{ + * Destroys the SpriteBatch. + * + */ +WebGLSpriteBatch.prototype.destroy = function () { this.vertices = null; this.indices = null; - + this.gl.deleteBuffer( this.vertexBuffer ); this.gl.deleteBuffer( this.indexBuffer ); - + this.currentBaseTexture = null; - + this.gl = null; -}; \ No newline at end of file +}; diff --git a/src/renderers/webgl/utils/WebGLStencilManager.js b/src/renderers/webgl/utils/WebGLStencilManager.js index 989d8cf..bf7dd73 100644 --- a/src/renderers/webgl/utils/WebGLStencilManager.js +++ b/src/renderers/webgl/utils/WebGLStencilManager.js @@ -1,45 +1,38 @@ /** - * @author Mat Groves http://matgroves.com/ @Doormat23 + * @class + * @namespace PIXI + * @private */ - -/** -* @class WebGLStencilManager -* @constructor -* @private -*/ -PIXI.WebGLStencilManager = function() -{ +function WebGLStencilManager() { this.stencilStack = []; this.reverse = true; this.count = 0; }; +WebGLStencilManager.prototype.constructor = WebGLStencilManager; +module.exports = WebGLStencilManager; + /** -* Sets the drawing context to the one given in parameter. -* -* @method setContext -* @param gl {WebGLContext} the current WebGL drawing context -*/ -PIXI.WebGLStencilManager.prototype.setContext = function(gl) -{ + * Sets the drawing context to the one given in parameter. + * + * @param gl {WebGLContext} the current WebGL drawing context + */ +WebGLStencilManager.prototype.setContext = function (gl) { this.gl = gl; }; /** -* Applies the Mask and adds it to the current filter stack. -* -* @method pushMask -* @param graphics {Graphics} -* @param webGLData {Array} -* @param renderSession {Object} -*/ -PIXI.WebGLStencilManager.prototype.pushStencil = function(graphics, webGLData, renderSession) -{ + * Applies the Mask and adds it to the current filter stack. + * + * @param graphics {Graphics} + * @param webGLData {Array} + * @param renderSession {object} + */ +WebGLStencilManager.prototype.pushStencil = function (graphics, webGLData, renderSession) { var gl = this.gl; this.bindGraphics(graphics, webGLData, renderSession); - if(this.stencilStack.length === 0) - { + if (this.stencilStack.length === 0) { gl.enable(gl.STENCIL_TEST); gl.clear(gl.STENCIL_BUFFER_BIT); this.reverse = true; @@ -57,56 +50,46 @@ // draw the triangle strip! - if(webGLData.mode === 1) - { + if (webGLData.mode === 1) { gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0 ); - - if(this.reverse) - { + + if (this.reverse) { gl.stencilFunc(gl.EQUAL, 0xFF - level, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.DECR); } - else - { + else { gl.stencilFunc(gl.EQUAL,level, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.INCR); } // draw a quad to increment.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - - if(this.reverse) - { + + if (this.reverse) { gl.stencilFunc(gl.EQUAL,0xFF-(level+1), 0xFF); } - else - { + else { gl.stencilFunc(gl.EQUAL,level+1, 0xFF); } this.reverse = !this.reverse; } - else - { - if(!this.reverse) - { + else { + if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xFF - level, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.DECR); } - else - { + else { gl.stencilFunc(gl.EQUAL,level, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.INCR); } gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0 ); - if(!this.reverse) - { + if (!this.reverse) { gl.stencilFunc(gl.EQUAL,0xFF-(level+1), 0xFF); } - else - { + else { gl.stencilFunc(gl.EQUAL,level+1, 0xFF); } } @@ -119,15 +102,13 @@ /** * TODO this does not belong here! - * - * @method bindGraphics + * * @param graphics {Graphics} * @param webGLData {Array} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLStencilManager.prototype.bindGraphics = function(graphics, webGLData, renderSession) -{ - //if(this._currentGraphics === graphics)return; +WebGLStencilManager.prototype.bindGraphics = function (graphics, webGLData, renderSession) { + //if (this._currentGraphics === graphics)return; this._currentGraphics = graphics; var gl = this.gl; @@ -137,20 +118,19 @@ offset = renderSession.offset, shader;// = renderSession.shaderManager.primitiveShader; - if(webGLData.mode === 1) - { + if (webGLData.mode === 1) { shader = renderSession.shaderManager.complexPrimitiveShader; renderSession.shaderManager.setShader( shader ); gl.uniform1f(shader.flipY, renderSession.flipY); - + gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform3fv(shader.color, webGLData.color); gl.uniform1f(shader.alpha, graphics.worldAlpha * webGLData.alpha); @@ -164,8 +144,7 @@ // set the index buffer! gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer); } - else - { + else { //renderSession.shaderManager.activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; renderSession.shaderManager.setShader( shader ); @@ -176,10 +155,10 @@ gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); - gl.uniform3fv(shader.tintColor, PIXI.hex2rgb(graphics.tint)); + gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); - + gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, 4 * 6, 0); @@ -191,89 +170,75 @@ }; /** - * @method popStencil * @param graphics {Graphics} * @param webGLData {Array} - * @param renderSession {Object} + * @param renderSession {object} */ -PIXI.WebGLStencilManager.prototype.popStencil = function(graphics, webGLData, renderSession) -{ +WebGLStencilManager.prototype.popStencil = function (graphics, webGLData, renderSession) { var gl = this.gl; this.stencilStack.pop(); - + this.count--; - if(this.stencilStack.length === 0) - { + if (this.stencilStack.length === 0) { // the stack is empty! gl.disable(gl.STENCIL_TEST); } - else - { + else { var level = this.count; this.bindGraphics(graphics, webGLData, renderSession); gl.colorMask(false, false, false, false); - - if(webGLData.mode === 1) - { + + if (webGLData.mode === 1) { this.reverse = !this.reverse; - if(this.reverse) - { + if (this.reverse) { gl.stencilFunc(gl.EQUAL, 0xFF - (level+1), 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.INCR); } - else - { + else { gl.stencilFunc(gl.EQUAL,level+1, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.DECR); } // draw a quad to increment.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, ( webGLData.indices.length - 4 ) * 2 ); - + gl.stencilFunc(gl.ALWAYS,0,0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.INVERT); // draw the triangle strip! gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0 ); - - if(!this.reverse) - { + + if (!this.reverse) { gl.stencilFunc(gl.EQUAL,0xFF-(level), 0xFF); } - else - { + else { gl.stencilFunc(gl.EQUAL,level, 0xFF); } } - else - { + else { // console.log("<<>>") - if(!this.reverse) - { + if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xFF - (level+1), 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.INCR); } - else - { + else { gl.stencilFunc(gl.EQUAL,level+1, 0xFF); gl.stencilOp(gl.KEEP,gl.KEEP,gl.DECR); } gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0 ); - if(!this.reverse) - { + if (!this.reverse) { gl.stencilFunc(gl.EQUAL,0xFF-(level), 0xFF); } - else - { + else { gl.stencilFunc(gl.EQUAL,level, 0xFF); } } @@ -286,12 +251,10 @@ }; /** -* Destroys the mask stack. -* -* @method destroy -*/ -PIXI.WebGLStencilManager.prototype.destroy = function() -{ + * Destroys the mask stack. + * + */ +WebGLStencilManager.prototype.destroy = function () { this.stencilStack = null; this.gl = null; };