'Three.js mesh position toJSON()
I have an issue, using .toJSON()
and .ObjectLoader()
.
I'm creating a mesh object (Object3D) with a position and converting it into JSON.
mesh.position.set(path.x, path.y, path.z);
Mesh {
uuid: 'AAA36A25-18C6-43CD-AF14-4D12826A8C06',
name: '',
type: 'Mesh',
parent: null,
children: [],
up: Vector3 { x: 0, y: 1, z: 0 },
position: Vector3 { x: 358.04999999981374, y: -4499.83, z:
2521.2299999999814 },
var mesh_tojson = mesh.toJSON();
{ metadata: { version: 4.5, type: 'Object', generator: 'Object3D.toJSON' },
geometries:
[ { uuid: '0E18D023-CC64-4D83-AB28-C731BB1E5B1B',
type: 'OctahedronGeometry',
radius: 100,
detail: 0 } ],
materials:
[ { uuid: '055D52FD-0767-44D8-A62F-C1514FE38111',
type: 'MeshLambertMaterial',
color: 16776960,
emissive: 0,
depthFunc: 3,
depthTest: true,
depthWrite: true,
rotation: undefined,
linewidth: undefined } ],
object:
{ uuid: 'AAA36A25-18C6-43CD-AF14-4D12826A8C06',
type: 'Mesh',
matrix: [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
geometry: '0E18D023-CC64-4D83-AB28-C731BB1E5B1B',
material: '055D52FD-0767-44D8-A62F-C1514FE38111' } }
But when I try to load the JSON object, everything is good except I loose the position attribute of my mesh object.
var mesh = loader.parse(mesh.toJSON())
Mesh {
uuid: 'AAA36A25-18C6-43CD-AF14-4D12826A8C06',
name: '',
type: 'Mesh',
parent: null,
children: [],
up: Vector3 { x: 0, y: 1, z: 0 },
position: Vector3 { x: 0, y: 0, z: 0 },
How can I save the position of my object within a JSON object and load it for a scene ?
Solution 1:[1]
In the code for .toJSON()
, position and rotation are not included. You can manually add them to the JSON:
mesh.matrix.toArray( json.object.matrix )
You may also need to manually re-apply them after loading, I'm not sure if THREE.ObjectLoader will do so.
Solution 2:[2]
Try adding mesh.updateMatrix()
before mesh.toJSON()
... it worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Don McCurdy |
Solution 2 | Jacob Philpott |