Quach90

Save/load scene with custom material/texture

Recommended Posts

Hi everyone,

I have now been stuck with a problem for a while and figured I would ask here to see if anyone knew how to fix it.

I have a scene where you can dynamically add your own images to it, that will than be transformed into a material with the images as the diffusetexture. The transforming happens by using the raw image data to create the texture with the function: BABYLON.Texture.LoadFromDataString - it looks something like this:

material = new BABYLON.StandardMaterial("imageMaterial", scene);
var imgTexture = new BABYLON.Texture.LoadFromDataString("data:" + Math.random(), RAW IMG DATA, scene);
material.diffuseTexture = imgTexture;

This all works fine, the problem that I am having is that I want to be able to save the scene and load it again later. But it seems the the SceneSerializer doesn't save the texture/material information (the raw image data), so that when I load the scene I get the error: 

Materials:
		Name: groundMaterial
babylon.custom.js:5 Uncaught TypeError: Cannot read property 'width' of null
    at E (babylon.custom.js:5)
    at n.createTexture (babylon.custom.js:5)
    at new i (babylon.custom.js:11)
    at babylon.custom.js:11
    at Function.t.Parse (babylon.custom.js:2)
    at Function.i.Parse (babylon.custom.js:11)
    at Function.t.Parse (babylon.custom.js:2)
    at Function.r.Parse (babylon.custom.js:17)
    at Function.t.Parse (babylon.custom.js:14)
    at Object.load (babylon.custom.js:28)

Looking at the saved serialized scene, I can also see that it is not saving the raw image data, so I have customized the serialize() function to find and save the data when the scene is saved, then the idea was to unpack it when it loads somehow. But I still can't stop the above error from happening. Is there anyone that knows a good way to do this or a fix to how I'm trying to do it?

In short: I am trying to save and load a scenes that have material/texture that I created from my own images. But the loader and saver, doesn't seem to recognize/save the custom material properly.

Thank you!

Share this post


Link to post
Share on other sites

Hi @Luaacro,

Not sure I understand exactly what you mean, but the string "base64String" is no where in the serialized object.

Below is a snippet of how the material looks:

"materials": [..., 
      {
        "tags": null,
        "id": "imageMaterial",
        "name": "imageMaterial",
        "checkReadyOnEveryCall": false,
        "checkReadyOnlyOnce": false,
        "state": "",
        "alpha": 1,
        "backFaceCulling": true,
        "sideOrientation": 1,
        "alphaMode": 2,
        "disableDepthWrite": false,
        "fogEnabled": true,
        "pointSize": 1,
        "zOffset": 0,
        "wireframe": false,
        "pointsCloud": false,
        "fillMode": 0,
        "diffuseTexture": {
            "tags": null,
            "name": "data:0.8948863414411619",
            "hasAlpha": true,
            "getAlphaFromRGB": false,
            "level": 1,
            "coordinatesIndex": 0,
            "coordinatesMode": 0,
            "wrapU": 1,
            "wrapV": 1,
            "anisotropicFilteringLevel": 4,
            "isCube": false,
            "gammaSpace": true,
            "invertZ": false,
            "lodLevelInAlpha": false,
            "lodGenerationOffset": 0,
            "lodGenerationScale": 0.8,
            "isRenderTarget": false,
            "url": "data:0.8948863414411619",
            "uOffset": 0,
            "vOffset": 0,
            "uScale": 1,
            "vScale": 1,
            "uAng": 0,
            "vAng": 0,
            "wAng": 0,
            "isBlocking": true,
            "animations": []
        },
        ...
    }]

 

Should the serializer be able to include images/materials/textures created at run time?

Share this post


Link to post
Share on other sites

Basically it seems that the image data is saved in the "_buffer" on the texture, which is left out when it is serialized, which in terms gives trouble to the material when it is being loaded again. The texture looks something like this, before and after serialized:

Before

before.thumb.png.10c6b909b665d83d9caa66080c3d83e5.png

After serializing the above:

after.png.5af87a7a70b8d7b34ab6e8234345aed9.png

Share this post


Link to post
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.

Guest
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.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.