参考 loadModel()

loadModel()

加载一个 3D 模型以创建一个 p5.Geometry 对象。

loadModel() 可以从 OBJ 和 STL 文件加载 3D 模型。一旦加载了模型,就可以使用 model() 函数来显示,如 model(shape)

有三种调用 loadModel() 的方式,通过可选参数来帮助处理模型。

第一个参数 path,始终是一个指向文件的 String。本地文件的路径应该是相对的,如 loadModel('/assets/model.obj')。像 ’https://example.com/model.obj'` 这样的 URL 可能会因为浏览器安全问题而被阻止。

第一种调用 loadModel() 的方式,在文件路径之后有三个可选参数。 第一个可选参数是 successCallback,它是一个在模型加载完成后调用的函数。 例如,loadModel('/assets/model.obj', handleModel) 将在模型加载完成后调用 handleModel()。 第二个可选参数是 failureCallback,它是一个在模型加载失败时调用的函数。例如, loadModel('/assets/model.obj', handleModel, handleFailure) 将在加载过程中出现错误时调用 handleFailure() 函数。 第三个可选参数是 fileType,它是模型的文件扩展名字符串。例如, loadModel('/assets/model', handleModel, handleFailure, '.obj') 将尝试加载 model 文件作为 .obj 文件。

第二种调用 loadModel() 的方式,在文件路径之后有四个可选参数。第一个可选参数是一个 Boolean,如果传递 true, 如 loadModel('/assets/model.obj', true),则模型将被调整大小以确保适合画布。 接下来的三个参数分别是 successCallbackfailureCallback,和 fileType,如上所述。

第三种调用 loadModel() 的方式,在文件路径之后有一个可选参数。可选参数 options 是一个包含选项的对象,如 loadModel('/assets/model.obj', options)options对象可以具有以下属性:

let options = { // Enables standardized size scaling during loading if set to true. normalize: true, <p> // Function to call once the model loads. successCallback: handleModel,</p> <p> // Function to call if an error occurs while loading. failureCallback: handleError,</p> <p> // Model's file extension. fileType: '.stl',</p> <p> // Flips the U texture coordinates of the model. flipU: false,</p> <p> // Flips the V texture coordinates of the model. flipV: false };</p> <p>// Pass the options object to loadModel(). loadModel('/assets/model.obj', options); </p>

模型加载可能需要一些时间。在 preload() 中调用 loadModel() 可以确保模型在被用于 setup()draw() 之前加载完成。

注意:不支持带有颜色的 STL 文件。带有颜色的 STL 文件将以无颜色方式呈现。

示例

语法

loadModel(path, normalize, [successCallback], [failureCallback], [fileType])
loadModel(path, [successCallback], [failureCallback], [fileType])
loadModel(path, [options])

参数

path
String:

path of the model to be loaded.

normalize
Boolean:

if true, scale the model to fit the canvas.

successCallback
function(p5.Geometry):

function to call once the model is loaded. Will be passed the p5.Geometry object.

failureCallback
Function(Event):

function to call if the model fails to load. Will be passed an Error event object.

fileType
String:

model’s file extension. Either '.obj' or '.stl'.

options
Object:

loading options.

Notice any errors or typos? Please let us know. Please feel free to edit src/webgl/loading.js and open a pull request!

相关参考