Possible to load geometry instead of mesh?

geometry

#1

Hi, I wondered if it is recommended (or possible at all) to load three.js geometry from a JSON file containing only that. I ask this as there’s a .toJSON function for both Geometry and BufferGeometry, but no .fromJSON function. I would like to minimize the size of exchanged JSON data by transferring geometry JSON instead of mesh JSON, as I plan to exchange the matrix separately (and I don’t care for materials, lights, etc.).

I would be happy if I could find something to easily load more generic Geometry JSON and BufferGeometry JSON, but I have not found a way to do this. Some things I’ve tested briefly but without results (dataParsed is the parsed JSON using the FileLoader containing either Geometry or BufferGeometry):
var geom = dataParsed; //does not work: no center

var geom = new THREE.Geometry(); // or new THREE.BufferGeometry() depending on input
geom.setFromPoints(dataParsed.data.vertices); //does not work: faceless geometry (direct geometry)

I’ve found a way to ‘load’ a BoxGeometry and I might easily expand this for other primitives. I basically load the JSON using the THREE.FileLoader and parse it. Then I create a new BoxGeometry based on the width/height/depth properties of the loaded JSON file, so this is more like a workaround instead of loading the geometry directly.

I’ve added two files and one snippet (below) containing different types of three.js geometry: Geometry, BufferGeometry, BoxGeometry (all derived from the same simple box).
pureGeometry.json (2.1 KB)
pureGeometryBuf.json (6.5 KB)

BoxGeometry JSON:
{
“metadata”: {
“version”: 4.5,
“type”: “Geometry”,
“generator”: “Geometry.toJSON”
},
“uuid”: “C24662A0-2E9B-4BBE-BD8E-B94C61DB2AD8”,
“type”: “BoxGeometry”,
“width”: 2,
“height”: 1,
“depth”: 1
}


#2

In general, three.js recommends to use glTF as the primary 3D format. Nevertheless, it is valid for special purposes to directly load pure geometry data. This is also demonstrated in various examples like in the following which uses BufferGeometryLoader:

https://threejs.org/examples/#webgl_performance

You normally create the content of the respective JSON file by calling BufferGeometry.toJSON(). The problem is that all default geometries of three.js (e.g. BoxGeometry or PlaneGeometry) are serialized in a special way. BufferGeometry.toJSON() does no serialize the actual buffer data but the original constructor parameters of the object. This is done in order to save space and time and since ObjectLoader expects this kind of serialization. You can bypass this logic via a simple hack: Just remove the .parameters property like shown in this fiddle. You can then use BufferGeometryLoader in order to load the data.

BTW: JSONLoader is somewhat deprecated so it makes more sense to focus on BufferGeometryLoader.