three.js - 加载模型-threejs
问题描述
我偶然发现了将对象加载到three.js 视口中的问题。教程表明需要使用THREE.ObjectLoader()。就我而言,ObjectLoader 在几个版本前已被删除。正确加载模型的方式是什么,或者我应该使用什么加载器(和文件格式)?我试过 GLTFLoader
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.114/build/three.module.js";
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/three@0.114/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.114.0/examples/jsm/loaders/GLTFLoader.js';
...
let loader = new GLTFLoader();
loader.load('./models/object.gltf',
(obj) => {
scene.add(obj);
}
);
它抛出了三个.module.js:5562 THREE.Object3D.add:对象不是 THREE.Object3D 的实例。 CDN 加载器可以在这里找到 - https://cdn.jsdelivr.net/npm/three@0.114.0/examples/jsm/loaders/
更新:如何使用 ObjectLoader 导入数据?
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.114/build/three.module.js";
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/three@0.114/examples/jsm/controls/OrbitControls.js;
...
let loader = new THREE.ObjectLoader();
loader.load('./models/object.json',
(obj) => {
scene.add(obj);
}
);
/* throws
three.module.js:39957 THREE.ObjectLoader: Loading "Geometry"
is not supported anymore
*/
解决方案
THREE.ObjectLoader尚未从存储库中删除。您可以使用它来加载three.js
自定义 JSON 格式。
要加载在 Blender 等 DCC 工具中创作的外部模型,推荐的 3D 格式是 glTF。不幸的是,您没有正确使用加载程序。它应该是:
loader.load('./models/object.gltf',
(gltf) => {
scene.add(gltf.scene);
}
);
我建议您查看官方文档页面,THREE.GLTFLoader
以便更好地了解回调gltf
参数的onLoad()
结构。
推荐阅读
- rust - 为什么我的生命周期函数调用代码正常或失败?
- c# - 整个项目 C# 中可用的功能
- r - data.table:在 i 中使用 .SD
- arrays - 如何更新laravel中的主数组
- vba - 我可以对word中的整个文档使用收缩方法吗
- javascript - NodeJS:始终在文件夹中保留 30 个最新文件,删除其余文件?
- java - android spinner,如果某个项目选择了其他微调器,则从衍生微调器 1 中出现
- security - 具有 Domain 与 SameSite 严格的 cookie 之间的安全差异是什么?
- android - 我们可以在不使用模型的情况下为列表视图创建搜索过滤器吗?
- f# - 如何在 af# 构造函数中声明类型别名?