@mesmotronic/three-mesh-bvh-json
v0.0.1
Published
Tools for converting three-mesh-bvh data to and from JSON for caching
Downloads
8
Maintainers
Readme
JSON tools for three-mesh-bvh
This library contains utility methods for serializing BVH data generated by three-mesh-bvh into JSON or easily JSON-able Objects for caching or embedding:
| Method | Description | |--------------------------|-------------------------------------------------| | serializeToObject | Serialize MeshBVH instance into plain Object | | serializeToJson | Serialize MeshBVH instance into JSON | | deserializeFromObject | Deserialize MeshBVH instance from plain Object | | deserializeFromJson | Deserialize MeshBVH instance from JSON |
In addition to the ability to serialize directly to and from JSON format,
support for plain Objects has been included for use with custom toJSON()
functions, fetch requests and other circumstances where JSON encoding and
decoding is handled by another process
Install
npm i github:mesmotronic/three-mesh-bvh-jsonExample
Save as JSON
import * as THREE from 'three';
import { MeshBVH } from 'three-mesh-bvh';
import { serializeToJson } from 'three-mesh-bvh-json';
const geometry = new THREE.TorusKnotGeometry(10, 3, 100, 16);
const bvh = new MeshBVH(geometry);
const body = serializeToJson(bvh);
fetch('/api/save-bvh', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body
});Load from JSON
import * as THREE from 'three';
import { deserializeFromObject } from 'three-mesh-bvh-json';
async function loadBvh(geometry) {
const response = await fetch('/path/to/bvh.json');
const obj = await response.json();
const bvh = deserializeFromObject(obj, geometry);
geometry.boundsTree = bvh;
}
const geometry = new THREE.TorusKnotGeometry(10, 3, 100, 16);
loadBvh(geometry);