Hello sir,
scene.executeWhenReady function worked this helped me to get a filled canvas image, earlier I was getting blank images because the CreateScreenshotUsingRenderTarget function was hitting meanwhile the scene was taking time to load.
But my problem is not resolved yet
I’m explaining my code steps
- user selects the textures he/she wants to apply to the mesh
and clicks on the button & expects to get a number of screenshots with all textures applied on the mesh one by one.
For example, if the user selects 3 textures he expects 3 downloads with each texture.
to do so, I created an engine, arch rotate camera, lights, and scene, and rendered it.
first time I create all and on the next iteration of the loop I check if the engine is already created I update the texture to the pbrmaterial by using
following
mymaterial.albedoTexture = new BABYLON.Texture(newfabric, this.scene);
after this, I wrote the code to take a screenshot
material.albedoTexture.onLoadObservable.addOnce(this.CreateScreenshotfun('filename);
this don’t work immediately or don’t stop iteration of loop till it download the screenshot with just applied texture.
CreateScreenshotfun function(){
scene.executeWhenReady(()=>{
BABYLON.Tools.CreateScreenshotUsingRenderTarget(
this.engine, this.camera, {
width: width,
height: heigth,
precision: percision
},data => this.downloadImage(data, filename), "image/png",
sharpness
);
});
}
downloadImage function
if ("download" in document.createElement("a")) {
var a = window.document.createElement("a");
a.href = data;
a.setAttribute("download", filename);
a.click();
} else {
var newWindow = window.open("");
newWindow.document.title = filename;
var img = newWindow.document.createElement("img");
img.src = data;
img.alt = filename;
newWindow.document.body.appendChild(img);
}
}
this also is not working it is not giving a pause on the fabrics loop while one screenshot gets downloaded, each loop over user-selected fabrics keeps going on without waiting for the download functionality to complete.
the expectation is while the loop runs it applies the first fabric then downloads the screenshot and then applies the next fabric and downloads screenshots again and this chain will go until the selected fabrics are done with the loop.
Any help will be appreciated
thanks