英文:
load OBJ file from blob in three.js
问题
我试图使用three.js从blob中加载一个OBJ文件。我参考了这里并成功加载了STL文件。但对于OBJ文件却不起作用。我收到的错误是:
TypeError: text.indexOf is not a function
at OBJLoader.parse (OBJLoader.js:482:13)
以下是我的代码:
// 这里出现了错误
async loadObj(blob) {
var loader = new OBJLoader();
const arrayBuffer = await blob.arrayBuffer();
const geometry = loader.parse(arrayBuffer);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
// 这里是有效的
async loadStl(blob) {
const loader = new STLLoader();
const arrayBuffer = await blob.arrayBuffer();
const geometry = loader.parse(arrayBuffer);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
英文:
I'm trying to load an OBJ file from a blob with three js.
I've referred to this and got STLs to load. But it's not working for OBJs.
The error I'm getting is:
TypeError: text.indexOf is not a function
at OBJLoader.parse (OBJLoader.js:482:13)
Here's my code:
// this gives an error
async loadObj(blob) {
var loader = new OBJLoader();
const arrayBuffer = await blob.arrayBuffer();
const geometry = loader.parse(arrayBuffer);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
// this works
async loadStl(blob) {
const loader = new STLLoader();
const arrayBuffer = await blob.arrayBuffer();
const geometry = loader.parse(arrayBuffer);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
</details>
# 答案1
**得分**: 0
```loader.parse``` 期望一个字符串,而你正在传递一个数组缓冲。尝试传递一个字符串而不是数组缓冲:
```javascript
async loadObj(blob) {
var loader = new OBJLoader();
const blob_string = await blob.text();
const geometry = loader.parse(blob_string);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
英文:
loader.parse
expects a string, you are passing an array buffer. Try passing a string instead:
async loadObj(blob) {
var loader = new OBJLoader();
const blog_string = await blob.text();
const geometry = loader.parse(blog_string);
const material = new THREE.MeshPhysicalMaterial({});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论