create3000 / x_ite

X_ITE X3D Browser, view and manipulate X3D and VRML scenes in HTML.
https://create3000.github.io/x_ite/
Other
67 stars 15 forks source link

How are relative internal URLs resolved when x3d content is supplied by string? #148

Closed gwhitney closed 1 year ago

gwhitney commented 1 year ago

I have a VRML97 file that internally refers to a png via a relative URL. When I use a URL to that VRML97 file as the src attribute, the png loads fine. When I fetch the text and feed it to the browser via createX3DFromString, it can no longer find the png. Is there any way to control the resolution of relative URLs when I load content this way, or do I just have to be sure to replace any relative URLs in the string with absolute URLs before I pass it to createX3DFromString? (The point of fetching and then parsing is to do some preprocessing on the VRML97 code before rendering it.) Thanks for any guidance.

create3000 commented 1 year ago

Relative URLs are always resolved against the URL of the current execution context, i.e. the surrounding scene. If you have a Script node, and within this Script node you load a scene via createX3DFromString, all relative URLs are resolved against the URL of the scene to which the Script node belongs.

If you are using the external browser, i.e. within a HTML script element, you can use the baseURL property to control this behavior:

<script>
async function load (x3dSyntax)
{
   const canvas = X3D.createBrowser();
   const browser = canvas .browser;

   browser .baseURL = "https://example.com";  
   const scene = await browser .createX3DFromString (x3dSyntax)
}

load (...)
</script>

See also https://create3000.github.io/x_ite/reference/browser-services#baseurl.