javascript - 将加载的模型分配给对象三个 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);
});
});```
解决方案
首先:线路this.instance;
没有效果。此外,“我以后不能在其他功能中使用 this.instance”是什么意思。访问时是否出现运行时错误instance
?
如果您想安全起见,请将其作为第一行放在构造函数中:
const scope = this;
然后scope.instance = object
在你的onLoad()
回调中使用。
推荐阅读
- flutter - Flutter:按时间戳查询 Firestore 文档
- prolog - 始终显示在序言中的“显示”
- c++ - 在用户在 Visual Studio 中输入我对“不正确”答案的限制后,有没有办法让控制台自动关闭?
- c# - 如何使用 process.OutputDataReceived 处理多行消息?
- python - Dockerfile 发布步骤
- vue.js - 如何在 vuejs 中为通知添加声音?
- python - 如何向此数据框添加趋势线(Python)
- javascript - Typescript 装饰器替换函数体或返回
- android - 导航抽屉活动:在按钮单击时从片段移动到片段
- hibernate - Hibernate Search - 索引多对一关系