In my search to support multiple texture types/buffers,
I found the texture.readPixels() function,
refactored my code & got it working, passing pixels to a canvas, then converting to base64 without need for async functions or preloading textures from cache.
BABYLON.Texture.prototype.toBase64 = function(){
if(!this._texture || this.readPixels === void 0) return null;
const pixels = this.readPixels();
const width = this.getSize().width;
const height = this.getSize().height;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(0, 0, 0)';
ctx.fillRect(0, 0, width, height);
let wCount = 0;
let hCount = 0;
for(let i = 0; i < pixels.length; i+=4){
if(wCount > width-1){
wCount = 0;
hCount++;
}
ctx.fillStyle = `rgba( ${pixels[i]}, ${pixels[i+1]}, ${pixels[i+2]}, ${pixels[i+3]/255})`;
ctx.fillRect(wCount, height - 1 - hCount, 1, 1);
wCount++;
}
return canvas.toDataURL('image/png');
};
Now if i export a GLB mesh to .babylon with a base64 texture, it turns white when importing it again.
I’m having a tough time figuring out what causes it, the image data is present & it should work…
Edit 1; PG
https://playground.babylonjs.com/#GDGCSP#2
There is also a slight color/light difference from the standardMaterial to PBRMaterial convertion by GLB exporter. (@Drigax is this behaviour expected? )
Edit 2;
It looks related to PBRMaterial, rather than the texture…
Applying the b64 texture from PBRMaterial as diffuseTexture on a StandardMaterial works fine.
Loading & applying a new texture as albedoTexture on the PBRMaterial is still white.
Edit 3; …Success?
It appears the problem was related to energy conservation,
more specificly the ‘useSmithVisibilityHeightCorrelated’ option
https://playground.babylonjs.com/#GDGCSP#3
(texture is also flipped, but this is mostlikely a bug on my end)
Now this opens a new question,
Why would this work on a .glb mesh, but not a .babylon mesh…
the materials are identical, useSmithVisibilityHeightCorrelated is also turned on, on the .glb material.
Furthermore… attaching .glb material to .babylon mesh without changing anything in the material, works too… this is too weird
Maybe something happens in the PBRMaterial serializing?