|
| 1 | +import Events from './Events'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Initialize various cameras, store original one. |
| 5 | + */ |
| 6 | +export function initCameras (inspector) { |
| 7 | + const originalCamera = inspector.currentCameraEl = inspector.sceneEl.camera.el; |
| 8 | + inspector.currentCameraEl.setAttribute( |
| 9 | + 'data-aframe-inspector-original-camera', |
| 10 | + '' |
| 11 | + ); |
| 12 | + |
| 13 | + // If the current camera is the default, we should prevent AFRAME from |
| 14 | + // remove it once when we inject the editor's camera. |
| 15 | + if (inspector.currentCameraEl.hasAttribute('data-aframe-default-camera')) { |
| 16 | + inspector.currentCameraEl.removeAttribute('data-aframe-default-camera'); |
| 17 | + inspector.currentCameraEl.setAttribute( |
| 18 | + 'data-aframe-inspector', |
| 19 | + 'default-camera' |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + inspector.currentCameraEl.setAttribute('camera', 'active', false); |
| 24 | + |
| 25 | + // Create Inspector camera. |
| 26 | + const perspectiveCamera = inspector.camera = new THREE.PerspectiveCamera(); |
| 27 | + perspectiveCamera.far = 10000; |
| 28 | + perspectiveCamera.near = 0.01; |
| 29 | + perspectiveCamera.position.set(0, 1.6, 2); |
| 30 | + perspectiveCamera.lookAt(new THREE.Vector3(0, 1.6, -1)); |
| 31 | + perspectiveCamera.updateMatrixWorld(); |
| 32 | + inspector.sceneEl.object3D.add(perspectiveCamera); |
| 33 | + inspector.sceneEl.camera = perspectiveCamera; |
| 34 | + |
| 35 | + const orthoCamera = new THREE.OrthographicCamera(-10, 10, 10, -10); |
| 36 | + inspector.sceneEl.object3D.add(orthoCamera); |
| 37 | + |
| 38 | + const cameras = inspector.cameras = { |
| 39 | + perspective: perspectiveCamera, |
| 40 | + original: originalCamera, |
| 41 | + ortho: orthoCamera |
| 42 | + }; |
| 43 | + |
| 44 | + Events.on('cameraperspectivetoggle', () => { |
| 45 | + inspector.sceneEl.camera = inspector.camera = cameras.perspective; |
| 46 | + Events.emit('cameratoggle', {camera: inspector.camera, value: 'perspective'}); |
| 47 | + }); |
| 48 | + |
| 49 | + Events.on('cameraorthographictoggle', dir => { |
| 50 | + inspector.sceneEl.camera = inspector.camera = cameras.ortho; |
| 51 | + if (dir === 'left') { |
| 52 | + cameras.ortho.position.set(-10, 0, 0); |
| 53 | + } else if (dir === 'right') { |
| 54 | + cameras.ortho.position.set(10, 0, 0); |
| 55 | + } else if (dir === 'top') { |
| 56 | + cameras.ortho.position.set(0, 10, 0); |
| 57 | + } else if (dir === 'bottom') { |
| 58 | + cameras.ortho.position.set(0, -10, 0); |
| 59 | + } else if (dir === 'back') { |
| 60 | + cameras.ortho.position.set(0, 0, -10); |
| 61 | + } else if (dir === 'front') { |
| 62 | + cameras.ortho.position.set(0, 0, 10); |
| 63 | + } |
| 64 | + cameras.ortho.lookAt(0, 0, 0); |
| 65 | + Events.emit('cameratoggle', {camera: inspector.camera, value: `ortho${dir}`}); |
| 66 | + }); |
| 67 | + |
| 68 | + return inspector.cameras; |
| 69 | +}; |
0 commit comments