Skip to content

Tools for converting three-mesh-bvh data to and from JSON for caching

License

Notifications You must be signed in to change notification settings

mesmotronic/three-mesh-bvh-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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-json

Example

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

About

Tools for converting three-mesh-bvh data to and from JSON for caching

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published