【问题标题】:loading models - threejs加载模型-threejs
【发布时间】:2020-11-01 03:35:03
【问题描述】:

我偶然发现了将对象加载到 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: object not an instance of 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.js


    【解决方案1】:

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

    要加载使用 Blender 等 DCC 工具创作的外部模型,推荐的 3D 格式是 glTF。不幸的是,您没有正确使用加载程序。应该是:

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

    我建议你看看THREE.GLTFLoader 的official documentation page,以便更好地了解onLoad() 回调的gltf 参数的结构。

    【讨论】:

    • 知道了。但我找不到 ObjectLoader.js 的 CDN 路径。你能帮我吗? (如果我只是将 ObjectLoader.js 添加到需要大量其他导入的 js 项目文件夹中)
    • ObjectLoader 是主库的一部分。这意味着它足以包含three.module.js。
    • 好的。如果我使用 THREE.ObjectLoader,它会抛出“three.module.js:39957 THREE.ObjectLoader:不再支持加载“几何”。”。如何改为从 JSON 加载数据?
    • 或者只是如何使用 THREE.ObjectLoader 导入没有错误的模型?
    • 出现Loading "Geometry" is not supported anymore. 消息,表示您正在尝试加载旧版 JSON 文件。在这种情况下,您必须返回发布r98。
    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 2021-09-12
    • 2021-07-02
    • 2022-01-07
    • 2021-08-24
    • 2020-08-20
    • 2018-09-13
    • 2015-03-23
    相关资源
    最近更新 更多