Jump to content

Refresh diffuseTexture on mesh and CreateScreenshot ?


mauri
 Share

Recommended Posts

Hi,

I am new to this forum and I really love this. I have been playing with babylon.js now for a year and really enjoying the framework. Mostly I have found answers to my problems here or on the playground.

Now I just can't solve this issue. I have a model which you can upload new picture (.jpq) through web site. I upload the picture to server ( new picture replaces the old one e.g. table1.jpg)

What I have been doing is reloading the page after upload. But now I would like to do this without upload. I kind of managed to do this:

 

var scene = engine.scenes[0];

scene.materials.forEach(function (thematerial) {

if (typeof (thematerial.diffuseTexture) != 'undefined') {

var newfilelocation = thematerial.diffuseTexture.url;

thematerial.diffuseTexture.hasAlpha = true;

thematerial.diffuseTexture = new BABYLON.Texture(newfilelocation, scene, true, true, 3, null, null, null, true);

}

});

This reloads materials and I can see result as I want to. But after this I also would like to take a snapshot and upload that picture to server.

 

BABYLON.Tools.CreateScreenshot(engine, scene.cameras[0], { width: cw, height: ch });

I have got this to work put after updating the mesh (materials) the picture materials are all black. Is there a way to avoid this?

I guess this is to do with the last buffer parameter = true, but this is the only way I have managed to reload the mesh materials?

Screenshot works fine if I hook it up to button and use it there right after upload and without loading the page...

 

I hope this makes sence :D

 

Mauri

Link to comment
Share on other sites

Hi Mauri,

you will need to wait at least one frame for the textures to actually load. this is why they are black. you could do that in a callback running after the rendering was done, something like this:

scene.registerAfterRender(function screenshot() {
  //TAKE SCREENSHOT
  scene.unregisterAfterRender(screenshot);
});

So it will be executed after the scene rendered, and only one time.

And sorry, just have to ask - why are you checking if the type of the diffuse texture is != undefined? It will be enough to simply check if it exists, using if(material.diffuseTexture) { //do something }

And just a small note - you might want to dispose the old texture before creating a new one. before creating the new texture, call thematerial.diffuseTexture.dispose() to clear up the resources.

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.

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

  • Recently Browsing   0 members

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