diff --git a/packages/core/src/textures/BaseTexture.js b/packages/core/src/textures/BaseTexture.js index 2c80fca..cbdd18e 100644 --- a/packages/core/src/textures/BaseTexture.js +++ b/packages/core/src/textures/BaseTexture.js @@ -9,6 +9,12 @@ import EventEmitter from 'eventemitter3'; import bitTwiddle from 'bit-twiddle'; +const defaultBufferOptions = { + scaleMode: SCALE_MODES.NEAREST, + format: FORMATS.RGBA, + premultiplyAlpha: false, +}; + /** * A texture stores the information that represents an image. All textures have a base texture. * @@ -547,22 +553,17 @@ * is provided, a new Float32Array is created. * @param {number} width - Width of the resource * @param {number} height - Height of the resource + * @param {object} [options] See {@link PIXI.BaseTexture}'s constructor for options. * @return {PIXI.BaseTexture} The resulting new BaseTexture */ - static fromBuffer(buffer, width, height) + static fromBuffer(buffer, width, height, options) { buffer = buffer || new Float32Array(width * height * 4); const resource = new BufferResource(buffer, { width, height }); const type = buffer instanceof Float32Array ? TYPES.FLOAT : TYPES.UNSIGNED_BYTE; - return BaseTexture(resource, { - scaleMode: SCALE_MODES.NEAREST, - format: FORMATS.RGBA, - type, - width, - height, - }); + return new BaseTexture(resource, Object.assign(defaultBufferOptions, options || { width, height, type })); } /** diff --git a/packages/core/src/textures/BaseTexture.js b/packages/core/src/textures/BaseTexture.js index 2c80fca..cbdd18e 100644 --- a/packages/core/src/textures/BaseTexture.js +++ b/packages/core/src/textures/BaseTexture.js @@ -9,6 +9,12 @@ import EventEmitter from 'eventemitter3'; import bitTwiddle from 'bit-twiddle'; +const defaultBufferOptions = { + scaleMode: SCALE_MODES.NEAREST, + format: FORMATS.RGBA, + premultiplyAlpha: false, +}; + /** * A texture stores the information that represents an image. All textures have a base texture. * @@ -547,22 +553,17 @@ * is provided, a new Float32Array is created. * @param {number} width - Width of the resource * @param {number} height - Height of the resource + * @param {object} [options] See {@link PIXI.BaseTexture}'s constructor for options. * @return {PIXI.BaseTexture} The resulting new BaseTexture */ - static fromBuffer(buffer, width, height) + static fromBuffer(buffer, width, height, options) { buffer = buffer || new Float32Array(width * height * 4); const resource = new BufferResource(buffer, { width, height }); const type = buffer instanceof Float32Array ? TYPES.FLOAT : TYPES.UNSIGNED_BYTE; - return BaseTexture(resource, { - scaleMode: SCALE_MODES.NEAREST, - format: FORMATS.RGBA, - type, - width, - height, - }); + return new BaseTexture(resource, Object.assign(defaultBufferOptions, options || { width, height, type })); } /** diff --git a/packages/core/src/textures/Texture.js b/packages/core/src/textures/Texture.js index b677121..5a22225 100644 --- a/packages/core/src/textures/Texture.js +++ b/packages/core/src/textures/Texture.js @@ -275,9 +275,10 @@ * @static * @param {number|string|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|PIXI.BaseTexture} * source - Source to create texture from + * @param {object} [options] See {@link PIXI.BaseTexture}'s constructor for options. * @return {PIXI.Texture} The newly created texture */ - static from(source, scaleMode) + static from(source, options) { let cacheId = null; @@ -299,7 +300,7 @@ if (!texture) { - texture = new Texture(new BaseTexture(source, scaleMode)); + texture = new Texture(new BaseTexture(source, options)); texture.baseTexture.cacheId = cacheId; BaseTexture.addToCache(texture.baseTexture, cacheId); @@ -311,6 +312,22 @@ } /** + * Create a new Texture with a BufferResource from a Float32Array. + * RGBA values are floats from 0 to 1. + * @static + * @param {Float32Array|UintArray} buffer The optional array to use, if no data + * is provided, a new Float32Array is created. + * @param {number} width - Width of the resource + * @param {number} height - Height of the resource + * @param {object} [options] See {@link PIXI.BaseTexture}'s constructor for options. + * @return {PIXI.Texture} The resulting new BaseTexture + */ + static fromBuffer(buffer, width, height, options) + { + return new Texture(BaseTexture.fromBuffer(buffer, width, height, options)); + } + + /** * Create a texture from a source and add to the cache. * * @static