加载模型-threejs

时间:2020-07-11 14:32:40

标签: three.js

我偶然发现了一个将对象加载到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);
    }
);

它使我 three.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
*/

1 个答案:

答案 0 :(得分:1)

THREE.ObjectLoader尚未从存储库中删除。您可以使用它来加载three.js自定义JSON格式。

要加载使用DCC工具(如Blender)创作的外部模型,建议的3D格式为glTF。不幸的是,您没有正确使用装载机。应该是:

loader.load('./models/object.gltf',
    (gltf) => {
        scene.add(gltf.scene);
    }
);

我建议您看看THREE.GLTFLoader的{​​{3}},以便更好地了解gltf回调的onLoad()参数的结构。