Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 129x 129x 129x 129x 129x 129x 129x 5x 1x 1x 1x 1x 1x 4x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 116x 8x 8x 8x 2x 6x 2x 4x 4x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 4x | import { ThreeCameraService } from "./threeCamera.service" import { Injectable } from "@angular/core" import { Box3, Mesh, MeshNormalMaterial, PerspectiveCamera, Vector3, Sphere, BoxGeometry, MOUSE } from "three" import { ThreeSceneService } from "./threeSceneService" import { MapControls } from "three/examples/jsm/controls/MapControls" import { ThreeRendererService } from "./threeRenderer.service" import { EventEmitter } from "../../../util/EventEmitter" import { BehaviorSubject } from "rxjs" type CameraChangeEvents = { onCameraChanged: (data: { camera: PerspectiveCamera }) => void } @Injectable({ providedIn: "root" }) export class ThreeMapControlsService { static CAMERA_CHANGED_EVENT_NAME = "camera-changed" MAX_ZOOM = 200 MIN_ZOOM = 10 controls: MapControls private eventEmitter = new EventEmitter<CameraChangeEvents>() zoomPercentage$ = new BehaviorSubject<number>(100) constructor( private threeCameraService: ThreeCameraService, private threeSceneService: ThreeSceneService, private threeRendererService: ThreeRendererService ) {} setControlTarget(cameraTarget: Vector3) { this.controls.target.set(cameraTarget.x, cameraTarget.y, cameraTarget.z) } rotateCameraInVectorDirection(x: number, y: number, z: number) { const zoom = this.getZoom() this.lookAtDirectionFromTarget(x, y, z) this.applyOldZoom(zoom) this.threeRendererService.render() this.onInput(this.threeCameraService.camera) } autoFitTo() { setTimeout(() => { const boundingSphere = this.getBoundingSphere() if (boundingSphere.radius === -1) { return } const length = this.cameraPerspectiveLengthCalculation(boundingSphere) const cameraReference = this.threeCameraService.camera cameraReference.position.set(length, length, boundingSphere.center.z) this.controls.update() this.focusCameraViewToCenter(boundingSphere) this.threeRendererService.render() this.onInput(this.threeCameraService.camera) const scale = 1.3 // object size / display size this.controls.maxDistance = length * 4 this.controls.minDistance = boundingSphere.radius / (10 * scale) this.setZoomPercentage(140) }) } private cameraPerspectiveLengthCalculation(boundingSphere: Sphere) { const cameraReference = this.threeCameraService.camera //TODO: Scale Factor for object to camera ratio const scale = 1.3 // object size / display size const objectAngularSize = ((cameraReference.fov * Math.PI) / 180) * scale const distanceToCamera = boundingSphere.radius / Math.tan(objectAngularSize / 2) return Math.sqrt(Math.pow(distanceToCamera, 2) + Math.pow(distanceToCamera, 2)) } private focusCameraViewToCenter(boundingSphere: Sphere) { const boundingSphereCenter: Vector3 = boundingSphere.center.clone() boundingSphereCenter.setY(0) this.controls.target.set(boundingSphereCenter.x, boundingSphereCenter.y, boundingSphereCenter.z) this.threeCameraService.camera.lookAt(boundingSphereCenter) this.threeCameraService.camera.updateProjectionMatrix() } getBoundingSphere() { return new Box3().setFromObject(this.threeSceneService.mapGeometry).getBoundingSphere(new Sphere()) } private lookAtDirectionFromTarget(x: number, y: number, z: number) { this.threeCameraService.camera.position.set(this.controls.target.x, this.controls.target.y, this.controls.target.z) const alignmentCube = new Mesh(new BoxGeometry(20, 20, 20), new MeshNormalMaterial()) this.threeSceneService.scene.add(alignmentCube) alignmentCube.position.set(this.controls.target.x, this.controls.target.y, this.controls.target.z) alignmentCube.translateX(x) alignmentCube.translateY(y) alignmentCube.translateZ(z) this.threeCameraService.camera.lookAt(alignmentCube.getWorldPosition(alignmentCube.position)) this.threeSceneService.scene.remove(alignmentCube) } private getZoom() { return this.threeCameraService.camera.position.distanceTo(this.controls.target) } private applyOldZoom(oldZoom: number) { this.threeCameraService.camera.translateZ(oldZoom) } init(domElement: HTMLCanvasElement) { this.controls = new MapControls(this.threeCameraService.camera, domElement) this.controls.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN } this.controls.zoomToCursor = true const updateZoomToCursor = (event: KeyboardEvent | MouseEvent) => { this.controls.zoomToCursor = !event.altKey } window.addEventListener("keydown", updateZoomToCursor) window.addEventListener("keyup", updateZoomToCursor) window.addEventListener("mousemove", updateZoomToCursor) this.controls.minPolarAngle = 0 this.controls.maxPolarAngle = Math.PI / 2 this.controls.listenToKeyEvents(window) this.controls.addEventListener("change", () => { this.onInput(this.threeCameraService.camera) this.updateZoomPercentage() this.threeRendererService.render() }) this.updateZoomPercentage() } onInput(camera: PerspectiveCamera) { this.setControlTarget(this.controls.target) this.eventEmitter.emit("onCameraChanged", { camera }) } subscribe<Key extends keyof CameraChangeEvents>(key: Key, callback: CameraChangeEvents[Key]) { this.eventEmitter.on(key, data => { callback(data) }) } getZoomPercentage(distance: number): number { const min = this.controls.minDistance const max = this.controls.maxDistance if (distance <= min) { return this.MAX_ZOOM } if (distance >= max) { return this.MIN_ZOOM } const range = max - min return this.MAX_ZOOM - ((distance - min) / range) * (this.MAX_ZOOM - this.MIN_ZOOM) } getDistanceFromZoomPercentage(percentage: number): number { const min = this.controls.minDistance const max = this.controls.maxDistance const range = max - min return min + ((this.MAX_ZOOM - percentage) / (this.MAX_ZOOM - this.MIN_ZOOM)) * range } updateZoomPercentage() { const distance = this.threeCameraService.camera.position.distanceTo(this.controls.target) const zoomFactor = this.getZoomPercentage(distance) this.zoomPercentage$.next(zoomFactor) } setZoomPercentage(zoom: number) { const newDistance = this.getDistanceFromZoomPercentage(zoom) const direction = new Vector3().subVectors(this.threeCameraService.camera.position, this.controls.target).normalize() this.threeCameraService.camera.position.copy(this.controls.target).add(direction.multiplyScalar(newDistance)) this.controls.update() this.zoomPercentage$.next(zoom) } } |