在三个JS中序列化相机状态

问题描述 投票:4回答:1

在ThreeJS场景中序列化(或保存或编组)摄像机状态,然后在以后取消序列化(或加载或解组)摄像机的最佳方法是什么?

现在我正在保存摄像机位置,向上和(欧拉角)旋转场的x,y,z坐标。后来我尝试通过调用position.set()up.set()rotation.set()来恢复这个摄像机,然后跟进调用updateProjectionMatrix()。我假设序列化和非序列化时默认的欧拉角旋转顺序是相同的。

它是否正确?

serialization 3d three.js
1个回答
7
投票

我建议改为存放相机的矩阵。这包括相机的整个转换,包括位置,旋转和缩放。

连载:

const cameraState = JSON.stringify(camera.matrix.toArray());
// ... store cameraState somehow ...

反序列化:

// ... read cameraState somehow ...
camera.matrix.fromArray(JSON.parse(cameraState));
// Get back position/rotation/scale attributes
camera.matrix.decompose(camera.position, camera.quaternion, camera.scale); 
© www.soinside.com 2019 - 2024. All rights reserved.