Pixi.js: Texture: Using different wrapMode's with the same source

Created on 21 Sep 2020  路  6Comments  路  Source: pixijs/pixi.js

Expected Behavior

When calling Texture.from(source, options) would expend the the options would be obeyed.

Current Behavior

Obeys the options when creating the initial baseTexture, but does not for future calls.

Possible Solution

Move wrapMode up to the Texture (from BaseTexture) so the same source can support multiple wrapModes

Steps to Reproduce

var canvas = document.createElement('canvas')
var a = PIXI.Texture.from(canvas, {wrapMode: PIXI.WRAP_MODES.CLAMP})
var b = PIXI.Texture.from(canvas, {wrapMode: PIXI.WRAP_MODES.REPEAT})

console.log(
    a.baseTexture.wrapMode === PIXI.WRAP_MODES.CLAMP, 
    b.baseTexture.wrapMode === PIXI.WRAP_MODES.REPEAT
)

Environment

Stale 馃ぉ Good First PR

Most helpful comment

This is because of the TextureCache:

https://github.com/pixijs/pixi.js/blob/9ff49a23670bdb0ed864d4e53fb91d524eab59f2/packages/core/src/textures/Texture.ts#L383

For now, the better option would not to use the factory method and create the base-texture yourself:

const baseTexture = new BaseTexture(canvas as HTMLCanvasElement, { wrapMode: WRAP_MODES.REPEAT });
const texture = new Texture(baseTexture);

This will create two separate, independent textures. I don't know if there is a better solution that can use the same WebGL texture, although I doubt it.

All 6 comments

webgl2 + texture samplers can solve that, but nobody tried to add it in pixi yet.

Its strange that we obey that option if baseTexture is already created.

This is because of the TextureCache:

https://github.com/pixijs/pixi.js/blob/9ff49a23670bdb0ed864d4e53fb91d524eab59f2/packages/core/src/textures/Texture.ts#L383

For now, the better option would not to use the factory method and create the base-texture yourself:

const baseTexture = new BaseTexture(canvas as HTMLCanvasElement, { wrapMode: WRAP_MODES.REPEAT });
const texture = new Texture(baseTexture);

This will create two separate, independent textures. I don't know if there is a better solution that can use the same WebGL texture, although I doubt it.

@bigtimebuddy I don't think we need to fix this, because the normative usage of Texture.from assumes you want to use the texture cache. Documenting this behavior should be fine.

I agree @SukantPal.

Also, it's important to note that wrapMode is a _BaseTexture property_ not a Texture property. The Texture.from options are a pass-thru to BaseTexture (this is already noted in the docs for Texture.from), so what @mudcube wants here isn't actually possible without having two BaseTexture objects.

PR is welcome if someone would like add more context about how Texture.from automatically adds things to the TextureCache. The caching behavior is already mentioned in BaseTexture.from.

@mudcube: I would suggest the above and not use Texture.from and manually create a new BaseTexture OR clone your canvas and pass each canvas into Texture.from and that should also work.

Thank you for these great recommendations, works like a charm :)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lunabunn picture lunabunn  路  3Comments

madroneropaulo picture madroneropaulo  路  3Comments

sntiagomoreno picture sntiagomoreno  路  3Comments

zcr1 picture zcr1  路  3Comments

courtneyvigo picture courtneyvigo  路  3Comments