Referencing to this: Another way of getting picture for new BABYLON.Texture(path, scene) - Questions & Answers - HTML5 Game Devs Forum I think Iāve almost got it working by overwritting the LoadImage function to force a proper xhr request:
Notes:
-I canāt seem to find WebRequest.onload with babylonjsās xhr wrapper and response.onload isnāt the right thing (see update)
-The LoadImage function used by the scene loader isnāt accessible through FileTools.LoadImage
nor Tools.LoadImage
, hence the need to go through ThinEngine._FileToolsLoadImage
. This doesnāt seem right.
ThinEngine._FileToolsLoadImage = (url, onload, onerror, database): HTMLImageElement => {
console.log('xhr image request : ' + url);
const img = new Image();
const imageRequest = new XMLHttpRequest();
imageRequest.open('GET', (url as string));
Object.entries(Cirrus.headersValues).forEach((header) => {
imageRequest.setRequestHeader(header[0], header[1]);
});
imageRequest.responseType = 'blob';
imageRequest.onload = () => {
img.src = URL.createObjectURL(imageRequest.response);
console.log(img);
};
imageRequest.send();
// Babylonjs Webrequest wrapper version (would profit from headers/settings set in global config)
// const img = new Image();
// const imageRequest = new WebRequest();
// imageRequest.open('GET', (url as string));
// imageRequest.responseType = 'blob';
// imageRequest.response.onload = () => {
// img.src = URL.createObjectURL(imageRequest.response);
// };
// imageRequest.send();
return img;
};
Using this, a request is sent and it is actually successful (200). But Something isnt working and the scene isnāt loading afterwards, Iāll keep looking into it.
Update:
Hereās how it looks now:
ThinEngine._FileToolsLoadImage = (url, onload, onerror, offlineProvider, mimeType): HTMLImageElement => {
const img = new Image();
const imageRequest = new WebRequest();
imageRequest.open('GET', (url as string));
imageRequest.responseType = 'blob';
imageRequest.onprogress = function(this: XMLHttpRequest, e: ProgressEvent<EventTarget>) {
this.onload = () => {
img.src = URL.createObjectURL(imageRequest.response);
console.log(img);
onload(img);
};
};
imageRequest.send();
return img;
};
Although I think the LoadImage function would need to be async (since it is returning an empty image at first)? As is, it leads to this:
WebGL: INVALID_VALUE: texImage2D: no image
[.WebGL-00004BAC00144980] GL_INVALID_OPERATION:
Texture format does not support mipmap generation.