THREE.js Model Loader for delegating to the appropriate and normalizing the result of different geometry loaders. Uses the file's extension to an appropriate loader function.
import PLYLoader from 'three/examples/loaders/PLYLoader';
import ModelLoader from 'threejs-model-loader';
var loader = new ModelLoader();
// register the function to load ply files
loader.loaderCallbacks[ 'ply' ] = function( url, manager, onLoad, onProgress, onError ) {
new PLYLoader( manager ).load( url, onLoad, onProgress, onError );
} );
loader.load( '.../model.ply', res => {
// res.model
// res.originalResult
// res.extension
} );
A map of file extensions to THREE Loader names that are provided with the THREE project. When using imports or require the loaders are not available so these loader functions must be registered manually.
{
'dae': 'ColladaLoader',
'fbx': 'FBXLoader',
'gcode': 'GCodeLoader',
'gltf': 'GLTFLoader',
// ...
}
An object that maps file extension to loader function callback. Defaults to empty. Add functions to the map to register load callbacks.
{
ply: function( url, manager, onLoad, onProgress, onError ) {
new PLYLoader( manager ).load( url, onLoad, onProgress, onError );
}
}
Instantiate the ModelLoader
with a THREE.LoadingManager
.
A function signature that mirrors all the THREE.js geometry loaders. An appropriate loader is selected from the loaderCallbacks based on the file extension.
onLoad
is passed an object with following values.
{
model, // THREE.js Object3D, Group, or Mesh that was loaded
extension, // The extension of the model that was loaded
originalResult // The original result that the loader returned
}
An override to the detected file extension.
Takes the data
to parse into geometry and the associated file extension.
The model is returned asynchronously in onLoad
.
See load
for documentation on what the onLoad
function is passed.
Function used to get the function used to load the geometry.
By default this looks the given extension up in the loaderCallbacks
object.
Returns whether or not the ModelLoader can load a file with the provided extension.
By default this checks if the given extension is in the loaderCallbacks
object.
<!-- Register the Element -->
<script href="https://github.com/gkjohnson/threejs-model-loader/blob/master/../model-viewer-element.js" />
<script>customElements.define('model-viewer', ModelViewer)</script>
<body>
<model-viewer src="https://github.com/gkjohnson/threejs-model-loader/raw/master/../path/to/model.ply" display-shadow ambient-color="red"></model-viewer>
</body>
The url of the model to display.
Whether or not the render the shadow under the robot.
The color of the ambient light specified with css colors.
Show a grid underneath the model.
Automatically redraw the model every frame instead of waiting to be dirtied.
Dirty the renderer so the element will redraw next frame.
Fires when a model is going to load.
Fires when all the geometry has been fully loaded.
Fires when there's a problem loading the model.
Install Node.js and NPM.
Run npm install
.
Run npm run server
.
Visit localhost:9080/example/
to view the page.