首页 > 解决方案 > 将加载的模型分配给对象三个 Js 中的值

问题描述

我对 Three.js 很陌生。我想将一个 3D 模型加载到 Three.js 中,并将加载的模型作为一个值保存在我的对象中,但是当我这样做时,我以后无法在对象的其他函数中使用 this.instance。

    constructor(name,chords,scene){
        this.instance;
        var mtlLoader = new THREE.MTLLoader();
        mtlLoader.setTexturePath(`/assets/${name}/`);
        mtlLoader.setPath(`/assets/${name}/`);
        mtlLoader.load(`${name}.mtl`, (materials) => {
            materials.preload();
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath(`/assets/${name}/`);
            objLoader.load(`${name}.obj`, (object) => {
                this.instance=object;
                
                scene.add(this.instance);
                
            });
    });```

标签: javascriptthree.js

解决方案


首先:线路this.instance;没有效果。此外,“我以后不能在其他功能中使用 this.instance”是什么意思。访问时是否出现运行时错误instance

如果您想安全起见,请将其作为第一行放在构造函数中:

const scope = this;

然后scope.instance = object在你的onLoad()回调中使用。


推荐阅读