Loads a 3D model to create a p5.Geometry object.
loadModel()
can load 3D models from OBJ and STL files. Once the model is loaded, it can be displayed with the model() function, as in model(shape)
.
There are three ways to call loadModel()
with optional parameters to help process the model.
The first parameter, path
, is a String
with the path to the file. Paths to local files should be relative, as in loadModel('/assets/model.obj')
. URLs such as 'https://example.com/model.obj'
may be blocked due to browser security. The path
parameter can also be defined as a Request
object for more advanced usage. Note: When loading a .obj
file that references materials stored in .mtl
files, p5.js will attempt to load and apply those materials. To ensure that the .obj
file reads the .mtl
file correctly include the .mtl
file alongside it.
The first way to call loadModel()
has three optional parameters after the file path. The first optional parameter, successCallback
, is a function to call once the model loads. For example, loadModel('/assets/model.obj', handleModel)
will call the handleModel()
function once the model loads. The second optional parameter, failureCallback
, is a function to call if the model fails to load. For example, loadModel('/assets/model.obj', handleModel, handleFailure)
will call the handleFailure()
function if an error occurs while loading. The third optional parameter, fileType
, is the model’s file extension as a string. For example, loadModel('/assets/model', handleModel, handleFailure, '.obj')
will try to load the file model as a .obj
file.
The second way to call loadModel()
has four optional parameters after the file path. The first optional parameter is a Boolean
value. If true
is passed, as in loadModel('/assets/model.obj', true)
, then the model will be resized to ensure it fits the canvas. The next three parameters are successCallback
, failureCallback
, and fileType
as described above.
The third way to call loadModel()
has one optional parameter after the file path. The optional parameter, options
, is an Object
with options, as in loadModel('/assets/model.obj', options)
. The options
object can have the following properties:
let options = {
// Enables standardized size scaling during loading if set to true.
normalize: true,
// Function to call once the model loads.
successCallback: handleModel,
// Function to call if an error occurs while loading.
failureCallback: handleError,
// Model's file extension.
fileType: '.stl',
// Flips the U texture coordinates of the model.
flipU: false,
// Flips the V texture coordinates of the model.
flipV: false
};
// Pass the options object to loadModel().
loadModel('/assets/model.obj', options);
This function returns a Promise
and should be used in an async
setup with await
. See the examples for the usage syntax.
Note: There’s no support for colored STL files. STL files with color will be rendered without color.
Examples
Syntax
loadModel(path, normalize, [successCallback], [failureCallback], [fileType])
loadModel(path, [successCallback], [failureCallback], [fileType])
loadModel(path, [options], [options.successCallback], [options.failureCallback], [options.fileType], [options.normalize], [options.flipU], [options.flipV])
Parameters
true
, scale the model to fit the canvas.Error
event object.'.obj'
or '.stl'
.Returns
Related References
createModel
Load a 3d model from an OBJ or STL string.
isBinary
This function checks if the file is in ASCII format or in Binary format It is done by searching keyword solid at the start of the file.
loadModel
Loads a 3D model to create a p5.Geometry object.
matchDataViewAt
This function matches the query at the provided offset.