Three.js - 如何反序列化geometry.toJSON()? (geometry.fromJSON在哪里?) [英] Three.js - How to deserialize geometry.toJSON()? (where is geometry.fromJSON?)

查看:512
本文介绍了Three.js - 如何反序列化geometry.toJSON()? (geometry.fromJSON在哪里?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试卸载一些 Geometry 加载和处理到Web worker。要将其发送回主线程, Geometry 实例需要序列化,似乎 Geometry.prototype.toJSON()就是针对这种类型的东西。

I'm trying to offload some Geometry loading and processing into a web worker. To send it back to the main thread, the Geometry instance needs to be serialized, and it seems that Geometry.prototype.toJSON() was meant for exactly this type of thing.

但我无法弄清楚如何将该对象变回几何主线程中的实例。如何使用 toJSON()输出?

But I can't figure out how to turn that object back into a Geometry instance in the main thread. How is the toJSON() output supposed to be used?

PS:我见过这个相关的问题,但似乎过时了。 toJSON()尚未在API中。接受的答案有点复杂,并且要求我仍然在主线程中做一些原始工作。

PS: I've seen this related question, but it seems dated. toJSON() wasn't in the API yet. The accepted answer is a bit convoluted, and requires me to still do some raw work in the main thread.

推荐答案

如果我理解正确的问题是:

If I understand correctly the issue is:


  • 你有一个文件要加载为几何(obj,stl等)。

  • 您想要在WebWorker中加载此文件。

  • 然后,您希望将几何图形发送回主脚本。

  • 所以你正在考虑将文件作为JSON发送回主线程,因为不支持发送对象。

  • 然后你将json转换为几何上的几何主线程。

  • You have a file which you want to load as a geometry (obj, stl, etc.).
  • You want to load this file in a WebWorker.
  • You then want to send the geometry back to the main script.
  • So you're thinking about sending the file back to the main thread as JSON, since sending objects is not supported.
  • Then you would convert the json to a geometry on the main thread.

这个问题是从JSON字符串转换为几何是另一种加载操作(这就是JSONLoader的原因) ),那么在那一点上你也可以在主线程上完成加载。

The problem with this is that converting from a JSON string to a geometry is another loading operation (which is why there's JSONLoader), so at that point you may as well have just done the loading on the main thread.

我使用的方法是将文件加载到平面阵列中顶点和法线,然后我结束那些回到主线程添加到BufferGeometry。您还可以使用可转移对象来获得更快的速度。

The approach I've used is to load the file into flat arrays of vertices and normals, then I send those back to the main thread to add to a BufferGeometry. You can also use transferable objects to gain some more speed.

// worker.js

var vertices = new Float32Array( faces * 3 * 3 );
var normals = new Float32Array( faces * 3 * 3 );   

// Load your file into the arrays somehow.

var message = {
    status:'complete',
    vertices: vertices,
    normals: normals
};

postMessage(message, [message.vertices.buffer, message.normals.buffer]);







// app.js

onmessage = function (event) {

    var vertices = event.data.vertices;
    var normals = event.data.normals;

    var geometry = new THREE.BufferGeometry();
    geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
    geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );

    var material = new THREE.MeshPhongMaterial();

    var mesh = new THREE.Mesh( geometry, material );

    // Do something with it.

};

这篇关于Three.js - 如何反序列化geometry.toJSON()? (geometry.fromJSON在哪里?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆