|
1 | | -import { Viewer } from './Viewer.js'; |
| 1 | +import * as THREE from 'three'; |
| 2 | +import { getCurrentTime } from './Util.js'; |
2 | 3 |
|
3 | | -export class Editor extends Viewer { |
4 | | - constructor(params = {}) { |
5 | | - super(params); |
| 4 | +export class Editor { |
| 5 | + constructor(viewer, options = {}) { |
| 6 | + this.viewer = viewer; |
6 | 7 |
|
7 | 8 | this.editPanel = null; |
8 | | - this.editPanelCells = {}; |
9 | 9 |
|
10 | | - this.initializedEditor = false; |
11 | | - this.initEditor(); |
| 10 | + this.pointerUpHandlerPlaneFinder = this.onPointerUpPlaneFinder.bind(this); |
| 11 | + |
| 12 | + this.initialized = false; |
| 13 | + this.init(); |
12 | 14 | } |
13 | 15 |
|
14 | | - initEditor() { |
15 | | - if (this.initializedEditor) return; |
| 16 | + init() { |
| 17 | + if (this.initialized) return; |
| 18 | + |
| 19 | + this.setupEditPanel(); |
| 20 | + |
| 21 | + this.initialized = true; |
| 22 | + } |
| 23 | + |
| 24 | + setupEditPanel() { |
| 25 | + this.editPanel = document.createElement('div'); |
| 26 | + this.editPanel.style.position = 'absolute'; |
| 27 | + this.editPanel.style.padding = '10px'; |
| 28 | + this.editPanel.style.backgroundColor = '#cccccc'; |
| 29 | + this.editPanel.style.border = '#aaaaaa 1px solid'; |
| 30 | + this.editPanel.style.zIndex = 90; |
| 31 | + this.editPanel.style.width = '375px'; |
| 32 | + this.editPanel.style.fontFamily = 'arial'; |
| 33 | + this.editPanel.style.fontSize = '10pt'; |
| 34 | + this.editPanel.style.textAlign = 'left'; |
| 35 | + |
| 36 | + |
| 37 | + const editTable = document.createElement('div'); |
| 38 | + editTable.style.width = '100%'; |
16 | 39 |
|
17 | | - if (this.useBuiltInControls) { |
18 | | - this.rootElement.addEventListener('pointerup', this.onMouseUpEdit.bind(this), false); |
| 40 | + |
| 41 | + // Plane Finder |
| 42 | + const planeFinder = document.createElement('div'); |
| 43 | + planeFinder.style.width = '100%'; |
| 44 | + planeFinder.style.display = 'flex'; |
| 45 | + planeFinder.style.flexDirection = 'row'; |
| 46 | + planeFinder.style.justifyContent = 'space-between'; |
| 47 | + |
| 48 | + const planeFinderLabel = document.createElement('p'); |
| 49 | + planeFinderLabel.id = 'planeFinderLabel'; |
| 50 | + planeFinderLabel.innerHTML = |
| 51 | + `Up: ${this.viewer.camera.up.x.toFixed(3)}, ${this.viewer.camera.up.y.toFixed(3)}, ${this.viewer.camera.up.z.toFixed(3)}`; |
| 52 | + |
| 53 | + const planeFinderButton = document.createElement('button'); |
| 54 | + planeFinderButton.innerHTML = 'Find ground plane'; |
| 55 | + planeFinderButton.addEventListener('click', () => { |
| 56 | + this.setupPlaneFinder(); |
| 57 | + }); |
| 58 | + |
| 59 | + planeFinder.appendChild(planeFinderLabel); |
| 60 | + planeFinder.appendChild(planeFinderButton); |
| 61 | + |
| 62 | + editTable.appendChild(planeFinder); |
| 63 | + |
| 64 | + this.editPanel.appendChild(editTable); |
| 65 | + this.editPanel.style.display = 'block'; |
| 66 | + this.viewer.renderer.domElement.parentElement.prepend(this.editPanel); |
| 67 | + } |
| 68 | + |
| 69 | + setupPlaneFinder() { |
| 70 | + if (this.viewer.useBuiltInControls) { |
| 71 | + this.viewer.rootElement.removeEventListener('pointerup', this.viewer.pointerUpHandler); |
| 72 | + this.viewer.rootElement.addEventListener('pointerup', this.pointerUpHandlerPlaneFinder); |
19 | 73 | } |
| 74 | + } |
20 | 75 |
|
21 | | - this.initializedEditor = true; |
| 76 | + teardownPlaneFinder() { |
| 77 | + if (this.viewer.useBuiltInControls) { |
| 78 | + this.viewer.rootElement.removeEventListener('pointerup', this.pointerUpHandlerPlaneFinder); |
| 79 | + this.viewer.rootElement.addEventListener('pointerup', this.viewer.pointerUpHandler); |
| 80 | + } |
22 | 81 | } |
23 | 82 |
|
24 | | - onMouseUpEdit = function() { |
| 83 | + onPointerUpPlaneFinder = function() { |
| 84 | + const renderDimensions = new THREE.Vector2(); |
| 85 | + const clickOffset = new THREE.Vector2(); |
| 86 | + // const toNewFocalPoint = new THREE.Vector3(); |
| 87 | + const outHits = []; |
| 88 | + let points = []; |
| 89 | + |
25 | 90 | return function(mouse) { |
26 | | - console.log('Editor.onMouseUpEdit', mouse); |
27 | | - }; |
28 | | - }; |
| 91 | + clickOffset.copy(this.viewer.mousePosition).sub(this.viewer.mouseDownPosition); |
| 92 | + const mouseUpTime = getCurrentTime(); |
| 93 | + const wasClick = mouseUpTime - this.viewer.mouseDownTime < 0.5 && clickOffset.length() < 2; |
| 94 | + |
| 95 | + if (!this.transitioningCameraTarget && wasClick) { |
| 96 | + this.viewer.getRenderDimensions(renderDimensions); |
| 97 | + outHits.length = 0; |
| 98 | + this.viewer.raycaster.setFromCameraAndScreenPosition(this.viewer.camera, this.viewer.mousePosition, renderDimensions); |
| 99 | + this.viewer.mousePosition.set(mouse.offsetX, mouse.offsetY); |
| 100 | + this.viewer.raycaster.intersectSplatMesh(this.viewer.splatMesh, outHits); |
| 101 | + |
| 102 | + if (outHits.length > 0) { |
| 103 | + const intersectionPoint = outHits[0].origin; |
29 | 104 |
|
30 | | - // onMouseUp = function() { |
31 | | - // return function(mouse) { |
32 | | - // console.log('Editor.onMouseUp', mouse); |
33 | | - // }; |
34 | | - // }(); |
| 105 | + points.push(intersectionPoint); |
| 106 | + |
| 107 | + if (points.length === 3) { |
| 108 | + const plane = new THREE.Plane(); |
| 109 | + |
| 110 | + plane.setFromCoplanarPoints(points[0], points[1], points[2]); |
| 111 | + |
| 112 | + this.viewer.camera.up = plane.normal; |
| 113 | + this.viewer.invalidate(); |
| 114 | + |
| 115 | + // Update label |
| 116 | + const planeFinderLabel = document.getElementById('planeFinderLabel'); |
| 117 | + planeFinderLabel.innerHTML = |
| 118 | + `${this.viewer.camera.up.x.toFixed(3)},${this.viewer.camera.up.y.toFixed(3)},${this.viewer.camera.up.z.toFixed(3)}`; |
| 119 | + |
| 120 | + points = []; |
| 121 | + this.teardownPlaneFinder(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | + }(); |
35 | 127 | } |
0 commit comments