Please help me, i am creating a babylon js extension for turbowarp. i want to make a function to load a .obj file with a .mtl file. the only problem is that i can’t directly give a image as an input due to some limitations of turbowarp and thats why i can only give dataURI of a image as a string.
Here is my plan, suppose that i have a .mtl file that contains the diffuse texture of my 3d model. the .mtl file has a “texture.png” image in the diffuse texture. now i want to make a function to attach the dataURI of that image to “texture.png” so, whenever babylon.js sees a image named “texture.png”, it will go and take the dataURI of that image from my program. and yeah i am a beginner in javascript
Would it be possible to put the data URI in the .mtl file instead of the texture.png reference?
To answer your direct question, you can change any network request made in babylon using web request modifiers. WebRequest.CustomRequestModifiers is an array with functions that will be executed before each network request is done, so you can have a kind of map that maps texture names to data URIs. Something like this:
BABYLON.WebRequest.CustomRequestModifiers.push((request, url) => {
switch(url) {
case "texture1.png": // the full URL of the request from the .mtl file
return "dataURINumber1";
//.. continue
}
});
i am sorry for late reply, i had my online tuition. well yeah if putting the data URI directly in the .mtl file is the only possible way then i will have to do it. but suppose that i replace those with dataURI and then i load it through my babylon loader, how exactly i will tell babylon to use that dataURI there instead of the name of the image? im a starter in javascript so yeah i don’t know much about it, i would really like if u would help me further.
hey and i also asked chatgpt and chatgpt gave me this code which doesn’t work and gives some errors, i wonder if it is possible to make it work. and if it isn’t then no problem. here is the code:
const imageDataMap = {
"texture.png": input.imageURI // e.g. "data:image/png;base64,...."
};
// 4) install preprocess hook
BABYLON.Tools.PreprocessUrl = (url) => {
// if it's not a string, leave it alone
if (typeof url !== "string") {
return url;
}
// extract the file name
const name = url.split("/").pop();
// if we have a data-URI for that name, return it (string)
if (imageDataMap[name]) {
return imageDataMap[name];
}
// otherwise return the original URL (string)
return url;
};
// 5) load both in the same AppendAsync call
await BABYLON.SceneLoader.AppendAsync("", [ objF, mtlF ], scene);