te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Three.js - How to deserialize geometry.toJSON()? (where is geometry.fromJSON?) - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Three.js - How to deserialize geometry.toJSON()? (where is geometry.fromJSON?) - Stack Overflow

programmeradmin2浏览0评论

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.

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: 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.

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.

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: 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.

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Feb 26, 2015 at 6:44 mhelvensmhelvens 4,3234 gold badges34 silver badges56 bronze badges 4
  • You should look around the loader classes in the documentation: threejs/docs/#Reference/Loaders/ObjectLoader – nemesv Commented Feb 28, 2015 at 16:28
  • 1 @nemesv: I've looked, and looked. Those classes cannot be used to turn toJSON() output back into a Geometry. In theory, I could try to prehend all of their source-code, and write a full solution from scratch, but that would take a lot of time. Mostly, I can't believe that a serialization method toJSON() exists, without a way to deserialize. :-/ – mhelvens Commented Mar 1, 2015 at 9:02
  • See if this helps: stackoverflow./questions/27992147/… – WestLangley Commented Mar 1, 2015 at 16:55
  • @WestLangley: Indeed, that may be the best I can do. That is, download the JSON file in the worker thread, then turn it into geometry in the main thread. Still, it doesn't use toJSON() at all. That's something, I guess, but then I don't understand the point of that method. – mhelvens Commented Mar 1, 2015 at 19:19
Add a ment  | 

3 Answers 3

Reset to default 6

If I understand correctly the issue is:

  • 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.

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.

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:'plete',
    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.

};

You can use JSONLoader unserialize geometry like so:

var geometry = new THREE.Geometry();
var serializedGeometry = geometry.toJSON();
var jsonLoader = new THREE.JSONLoader();

var result = jsonLoader.parse(serializedGeometry.data);

var unserializedGeometry = result.geometry;

Why don't you just use the JSONLoader?

myloader = new THREE.JSONLoader()
myloader.load("path/to/json", function(geometry,material){
    mesh = new THREE.Mesh(geometry,material)
    scene.add(mesh)
})

or loading a JSON file the same way

发布评论

评论列表(0)

  1. 暂无评论