diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index a906f19..1dbaf99 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -19,8 +19,9 @@ * The texture of the Mesh * * @member {Texture} + * @private */ - this.texture = texture; + this._texture = null; /** * The Uvs of the Mesh @@ -76,6 +77,9 @@ * @member {number} */ this.drawMode = drawMode || Mesh.DRAW_MODES.TRIANGLE_MESH; + + // run texture setter; + this.texture = texture; } // constructor @@ -83,6 +87,43 @@ Mesh.prototype.constructor = Mesh; module.exports = Mesh; +Object.defineProperties(Mesh.prototype, { + /** + * The texture that the sprite is using + * + * @member {PIXI.Texture} + * @memberof PIXI.mesh.Mesh# + */ + texture: { + get: function () + { + return this._texture; + }, + set: function (value) + { + if (this._texture === value) + { + return; + } + + this._texture = value; + + if (value) + { + // wait for the texture to load + if (value.baseTexture.hasLoaded) + { + this._onTextureUpdate(); + } + else + { + value.once('update', this._onTextureUpdate, this); + } + } + } + } +}); + /** * Renders the object using the WebGL renderer * @@ -312,8 +353,7 @@ * @param event * @private */ - -Mesh.prototype.onTextureUpdate = function () +Mesh.prototype._onTextureUpdate = function () { this.updateFrame = true; }; diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index a906f19..1dbaf99 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -19,8 +19,9 @@ * The texture of the Mesh * * @member {Texture} + * @private */ - this.texture = texture; + this._texture = null; /** * The Uvs of the Mesh @@ -76,6 +77,9 @@ * @member {number} */ this.drawMode = drawMode || Mesh.DRAW_MODES.TRIANGLE_MESH; + + // run texture setter; + this.texture = texture; } // constructor @@ -83,6 +87,43 @@ Mesh.prototype.constructor = Mesh; module.exports = Mesh; +Object.defineProperties(Mesh.prototype, { + /** + * The texture that the sprite is using + * + * @member {PIXI.Texture} + * @memberof PIXI.mesh.Mesh# + */ + texture: { + get: function () + { + return this._texture; + }, + set: function (value) + { + if (this._texture === value) + { + return; + } + + this._texture = value; + + if (value) + { + // wait for the texture to load + if (value.baseTexture.hasLoaded) + { + this._onTextureUpdate(); + } + else + { + value.once('update', this._onTextureUpdate, this); + } + } + } + } +}); + /** * Renders the object using the WebGL renderer * @@ -312,8 +353,7 @@ * @param event * @private */ - -Mesh.prototype.onTextureUpdate = function () +Mesh.prototype._onTextureUpdate = function () { this.updateFrame = true; }; diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 0ef3573..3111c26 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -47,12 +47,6 @@ */ this.indices = new Uint16Array(points.length * 2); - /* - * @member {TextureUvs} Current texture uvs - * @private - */ - this._textureUvs = null; - this.refresh(); } @@ -70,7 +64,8 @@ { var points = this.points; - if (points.length < 1) + // if too little points, or texture hasn't got UVs set yet just move on. + if (points.length < 1 || !this.texture._uvs) { return; } @@ -80,7 +75,7 @@ var indices = this.indices; var colors = this.colors; - var textureUvs = this._getTextureUvs(); + var textureUvs = this.texture._uvs; var offset = new core.math.Point(textureUvs.x0, textureUvs.y0); var factor = new core.math.Point(textureUvs.x2 - textureUvs.x0, textureUvs.y2 - textureUvs.y0); @@ -119,37 +114,23 @@ indices[index] = index; indices[index + 1] = index + 1; } + + this.dirty = true; }; -/* - * Returns texture UVs - * - * @private - */ - -Rope.prototype._getTextureUvs = function() -{ - if(!this._textureUvs) - { - this._textureUvs = new core.TextureUvs(); - this._textureUvs.set(this.texture.crop, this.texture.baseTexture, this.texture.rotate); - } - return this._textureUvs; -}; - -/* +/** * Clear texture UVs when new texture is set * * @private */ - -Rope.prototype.onTextureUpdate = function () +Rope.prototype._onTextureUpdate = function () { - this._textureUvs = null; - Mesh.prototype.onTextureUpdate.call(this); + Mesh.prototype._onTextureUpdate.call(this); + + this.refresh(); }; -/* +/** * Updates the object transform for rendering * * @private diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index a906f19..1dbaf99 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -19,8 +19,9 @@ * The texture of the Mesh * * @member {Texture} + * @private */ - this.texture = texture; + this._texture = null; /** * The Uvs of the Mesh @@ -76,6 +77,9 @@ * @member {number} */ this.drawMode = drawMode || Mesh.DRAW_MODES.TRIANGLE_MESH; + + // run texture setter; + this.texture = texture; } // constructor @@ -83,6 +87,43 @@ Mesh.prototype.constructor = Mesh; module.exports = Mesh; +Object.defineProperties(Mesh.prototype, { + /** + * The texture that the sprite is using + * + * @member {PIXI.Texture} + * @memberof PIXI.mesh.Mesh# + */ + texture: { + get: function () + { + return this._texture; + }, + set: function (value) + { + if (this._texture === value) + { + return; + } + + this._texture = value; + + if (value) + { + // wait for the texture to load + if (value.baseTexture.hasLoaded) + { + this._onTextureUpdate(); + } + else + { + value.once('update', this._onTextureUpdate, this); + } + } + } + } +}); + /** * Renders the object using the WebGL renderer * @@ -312,8 +353,7 @@ * @param event * @private */ - -Mesh.prototype.onTextureUpdate = function () +Mesh.prototype._onTextureUpdate = function () { this.updateFrame = true; }; diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 0ef3573..3111c26 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -47,12 +47,6 @@ */ this.indices = new Uint16Array(points.length * 2); - /* - * @member {TextureUvs} Current texture uvs - * @private - */ - this._textureUvs = null; - this.refresh(); } @@ -70,7 +64,8 @@ { var points = this.points; - if (points.length < 1) + // if too little points, or texture hasn't got UVs set yet just move on. + if (points.length < 1 || !this.texture._uvs) { return; } @@ -80,7 +75,7 @@ var indices = this.indices; var colors = this.colors; - var textureUvs = this._getTextureUvs(); + var textureUvs = this.texture._uvs; var offset = new core.math.Point(textureUvs.x0, textureUvs.y0); var factor = new core.math.Point(textureUvs.x2 - textureUvs.x0, textureUvs.y2 - textureUvs.y0); @@ -119,37 +114,23 @@ indices[index] = index; indices[index + 1] = index + 1; } + + this.dirty = true; }; -/* - * Returns texture UVs - * - * @private - */ - -Rope.prototype._getTextureUvs = function() -{ - if(!this._textureUvs) - { - this._textureUvs = new core.TextureUvs(); - this._textureUvs.set(this.texture.crop, this.texture.baseTexture, this.texture.rotate); - } - return this._textureUvs; -}; - -/* +/** * Clear texture UVs when new texture is set * * @private */ - -Rope.prototype.onTextureUpdate = function () +Rope.prototype._onTextureUpdate = function () { - this._textureUvs = null; - Mesh.prototype.onTextureUpdate.call(this); + Mesh.prototype._onTextureUpdate.call(this); + + this.refresh(); }; -/* +/** * Updates the object transform for rendering * * @private diff --git a/src/mesh/webgl/MeshRenderer.js b/src/mesh/webgl/MeshRenderer.js index 746ad4b..998c36f 100644 --- a/src/mesh/webgl/MeshRenderer.js +++ b/src/mesh/webgl/MeshRenderer.js @@ -80,7 +80,7 @@ texture = mesh.texture.baseTexture, shader = renderer.shaderManager.plugins.meshShader; - var drawMode = mesh.drawMode === Mesh.DRAW_MODES.TRIANGLE_STRIP ? gl.TRIANGLE_STRIP : gl.TRIANGLES; + var drawMode = mesh.drawMode === Mesh.DRAW_MODES.TRIANGLE_MESH ? gl.TRIANGLE_STRIP : gl.TRIANGLES; renderer.blendModeManager.setBlendMode(mesh.blendMode); @@ -98,7 +98,6 @@ gl.bufferSubData(gl.ARRAY_BUFFER, 0, mesh.vertices); gl.vertexAttribPointer(shader.attributes.aVertexPosition, 2, gl.FLOAT, false, 0, 0); - // update the uvs gl.bindBuffer(gl.ARRAY_BUFFER, mesh._uvBuffer); gl.vertexAttribPointer(shader.attributes.aTextureCoord, 2, gl.FLOAT, false, 0, 0); @@ -115,7 +114,9 @@ // bind the current texture gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); } + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh._indexBuffer); + gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, mesh.indices); } else { @@ -128,11 +129,11 @@ // update the uvs gl.bindBuffer(gl.ARRAY_BUFFER, mesh._uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, mesh.uvs, gl.STATIC_DRAW); - gl.vertexAttribPointer(shader.attributes.aTextureCoord, 2, gl.FLOAT, false, 0, 0); + gl.vertexAttribPointer(shader.attributes.aTextureCoord, 2, gl.FLOAT, false, 0, 0); gl.activeTexture(gl.TEXTURE0); - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[gl.id]) { this.renderer.updateTexture(texture); }