Skip to content

Commit

Permalink
LoaderUtils: Add resolveURL(). (#22707)
Browse files Browse the repository at this point in the history
* Expose resolveURL on the GLTFParser

* Move resolveURL to LoaderUtils

* Use options variable instead of parser
  • Loading branch information
robertlong authored Oct 22, 2021
1 parent a8813be commit cd8c225
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
10 changes: 10 additions & 0 deletions docs/api/en/loaders/LoaderUtils.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ <h3>[method:String extractUrlBase]( [param:String url] )</h3>
</p>


<h3>[method:String resolveURL]( [param:String url], [param:String path] )</h3>
<p>
[page:String url] — The absolute or relative url resolve.
[page:String path] — The base path for relative urls to be resolved against.
</p>
<p>
Resolves relative urls against the given path. Absolute paths, data urls, and blob urls will be returned as is. Invalid urls will return an empty string.
</p>


<h2>Source</h2>

<p>
Expand Down
32 changes: 2 additions & 30 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,34 +1958,6 @@ const ALPHA_MODES = {
BLEND: 'BLEND'
};

/* UTILITY FUNCTIONS */

function resolveURL( url, path ) {

// Invalid URL
if ( typeof url !== 'string' || url === '' ) return '';

// Host Relative URL
if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) {

path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' );

}

// Absolute URL http://,https://,//
if ( /^(https?:)?\/\//i.test( url ) ) return url;

// Data URI
if ( /^data:.*,.*$/i.test( url ) ) return url;

// Blob URL
if ( /^blob:.*$/i.test( url ) ) return url;

// Relative URL
return path + url;

}

/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material
*/
Expand Down Expand Up @@ -2645,7 +2617,7 @@ class GLTFParser {

return new Promise( function ( resolve, reject ) {

loader.load( resolveURL( bufferDef.uri, options.path ), resolve, undefined, function () {
loader.load( LoaderUtils.resolveURL( bufferDef.uri, options.path ), resolve, undefined, function () {

reject( new Error( 'THREE.GLTFLoader: Failed to load buffer "' + bufferDef.uri + '".' ) );

Expand Down Expand Up @@ -2891,7 +2863,7 @@ class GLTFParser {

}

loader.load( resolveURL( sourceURI, options.path ), onLoad, undefined, reject );
loader.load( LoaderUtils.resolveURL( sourceURI, options.path ), onLoad, undefined, reject );

} );

Expand Down
26 changes: 26 additions & 0 deletions src/loaders/LoaderUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ class LoaderUtils {

}

static resolveURL( url, path ) {

// Invalid URL
if ( typeof url !== 'string' || url === '' ) return '';

// Host Relative URL
if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) {

path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' );

}

// Absolute URL http://,https://,//
if ( /^(https?:)?\/\//i.test( url ) ) return url;

// Data URI
if ( /^data:.*,.*$/i.test( url ) ) return url;

// Blob URL
if ( /^blob:.*$/i.test( url ) ) return url;

// Relative URL
return path + url;

}

}

export { LoaderUtils };

0 comments on commit cd8c225

Please sign in to comment.