Skip to content

Commit 8717452

Browse files
committed
Save/load camera settings
1 parent 8e6cf1d commit 8717452

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/dev-gui.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ class DevGUI {
235235
// });
236236
devHelpers.add(this.webglPreview, 'render').listen();
237237
devHelpers.add(this.webglPreview, 'clear').listen();
238+
239+
240+
devHelpers.add(this.webglPreview, 'saveCamera').listen();
241+
devHelpers.add(this.webglPreview, 'loadCamera').listen();
238242
}
239243
}
240244

src/webgl-preview.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ export class WebGLPreview {
280280
this.resize();
281281

282282
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
283+
284+
this.loadCamera();
285+
283286
this.initScene();
284287
this.animate();
285288

@@ -1023,4 +1026,31 @@ export class WebGLPreview {
10231026
this.initGui();
10241027
}
10251028
}
1029+
1030+
saveCamera() {
1031+
localStorage.setItem('cameraPosition', JSON.stringify(this.camera.position));
1032+
localStorage.setItem('cameraRotation', JSON.stringify(this.camera.rotation));
1033+
localStorage.setItem('cameraZoom', JSON.stringify(this.camera.zoom));
1034+
localStorage.setItem('cameraTarget', JSON.stringify(this.controls.target));
1035+
}
1036+
loadCamera() {
1037+
const position = JSON.parse(localStorage.getItem('cameraPosition'));
1038+
const rotation = JSON.parse(localStorage.getItem('cameraRotation'));
1039+
const zoom = JSON.parse(localStorage.getItem('cameraZoom'));
1040+
const target = JSON.parse(localStorage.getItem('cameraTarget'));
1041+
if (position && rotation && zoom && target) {
1042+
this.camera.position.x = position.x;
1043+
this.camera.position.y = position.y;
1044+
this.camera.position.z = position.z;
1045+
this.camera.rotation.x = rotation.x;
1046+
this.camera.rotation.y = rotation.y;
1047+
this.camera.rotation.z = rotation.z;
1048+
this.camera.zoom = zoom;
1049+
// this.camera.updateProjectionMatrix();
1050+
this.controls.target.x = target.x;
1051+
this.controls.target.y = target.y;
1052+
this.controls.target.z = target.z;
1053+
this.controls.update();
1054+
}
1055+
}
10261056
}

0 commit comments

Comments
 (0)