Jump to content

Sprite2D Change Texture


Recommended Posts

Is it supposed to be possible to change the texture of a Sprite2D after it has been rendered?

Whenever I try to do that I'm getting the following error:


TypeError: this.modelRenderCache is null

If it is supposed to be possible I'll make a PG to demonstrate the error.

If not, is the alternative to create multiple Sprite2D each with its own texture?

Link to comment
Share on other sites


Changed to 3 seconds. I think the problem is with hasAlpha, perhaps because in this PG the second texture is a jpg without alpha. It's worth trying with another transparent png just to make sure that's not the case.


This PG throws this error:


babylon.canvas2d.js:9972 Uncaught TypeError: Cannot read property '_partData' of null
    at GroupInstanceInfo.get [as transparentData] (babylon.canvas2d.js:9972)
    at ScreenSpaceCanvas2D.Group2D._renderTransparentData (babylon.canvas2d.js:12017)
    at ScreenSpaceCanvas2D.Group2D._groupRender (babylon.canvas2d.js:11889)
    at ScreenSpaceCanvas2D.Canvas2D._render (babylon.canvas2d.js:17921)
    at e.callback (babylon.canvas2d.js:16702)
    at e.notifyObservers (babylon.js:2)
    at r.render (babylon.js:13)
    at index.js:223
    at n._renderLoop (babylon.js:5)


Locally I get this error for a different code that is quite complex to reproduce, but same idea:


TypeError: this.modelRenderCache is null[Learn More]  babylon.canvas2d.min.js:6:21059
    t<.get file:///D:/Projects/someProject/client/src/lib/babylon.canvas2d.min.js:6:21059
    n.prototype._renderTransparentData file:///D:/Projects/someProject/client/src/lib/babylon.canvas2d.min.js:7:24988
    n.prototype._groupRender file:///D:/Projects/someProject/client/src/lib/babylon.canvas2d.min.js:7:22514
    n.prototype._render file:///D:/Projects/someProject/client/src/lib/babylon.canvas2d.min.js:11:13599
    n/a._afterRenderObserver< file:///D:/Projects/someProject/client/src/lib/babylon.canvas2d.min.js:10:15698
    Observable.prototype.notifyObservers file:///D:/Projects/someProject/client/src/lib/babylon.max.js:4968:21
    Scene.prototype.render file:///D:/Projects/someProject/client/src/lib/babylon.max.js:22755:13
    Application/< file:///D:/Projects/someProject/client/src/app.js:4471:45
    Engine.prototype._renderLoop file:///D:/Projects/someProject/client/src/lib/babylon.max.js:8827:21


They both seem to be related to transparent data. Locally both of the textures I'm using are transparent pngs.

Link to comment
Share on other sites

        Object.defineProperty(GroupInstanceInfo.prototype, "transparentData", {
            get: function () {
                if (!this._transparentData) {
                    this._transparentData = new Array(this._partCount);
                    for (var i = 0; i < this._partCount; i++) {
                        var zoff = this.modelRenderCache._partData[i]._zBiasOffset;
                        this._transparentData[i] = new TransparentGroupInfoPartData(this._strides[i], zoff);
                return this._transparentData;
            enumerable: true,
            configurable: true

There it is, the zoff line.  Hey @Nockawa, first of all, welcome back!  I hope you had some fun "out west" in the west.  We sure miss you when you're gone.  Someday, you may actually get to play with BJS, instead-of always working on it.  :)

Immutable, huh?  That means... unchangeable.  hmm.

If we had a function... sprite.setInitialTexture(someTexture, hasAlpha, etc)... that function COULD re-initialize the sprite as if it were a new sprite, yes?  The 'Initial' part COULD indicate to users... that the sprite is being re-created to accomplish this.

I think you agree, this issue is going to arise again, because... making a new sprite2d with rect2d and text2d (like the #29 PG demo)... is a substantial amount of code lines.  There will be future others who will try to re-use a sprite2D, instead-of making a new one (esp if they ONLY need to change the texture or atlas). 

So, I'm thinking about ways to make it APPEAR as-if they changed the texture, when actually, they re-created/re-initialized... using the new texture/atlas.

Not sure if that would work. Just thinkin' and inkin'.  :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...