从blob中加载OBJ文件在three.js中

huangapple go评论67阅读模式
英文:

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);
}

OBJLoader.parse 文档

OBJLoader.parse 源代码

MDN Blob.text()

英文:

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);
}

OBJLoader.parse Documentation

OBJLoader.parse Source Code

MDN Blob.text()

huangapple
  • 本文由 发表于 2023年2月27日 07:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定