All files / app/codeCharta/ui/viewCube viewCube.component.ts

60.71% Statements 68/112
100% Branches 17/17
42.85% Functions 6/14
60.36% Lines 67/111

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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 2183x                         3x 3x 3x 3x 3x 3x                 3x           20x 20x 20x   20x   20x           20x 20x 20x                                                                                 20x 1x 1x 1x                   1x 1x 1x                                           20x 1x       1x 1x     20x 1x 1x 1x     20x 17x   1x 1x   1x 1x   1x 1x     1x 1x   1x 1x   1x 1x     1x 1x   1x 1x   1x 1x     1x 1x   1x 1x   1x 1x     1x 1x   1x 1x     1x 1x   1x 1x   1x 1x                                        
import {
    AmbientLight,
    AxesHelper,
    BoxHelper,
    Color,
    DirectionalLight,
    Group,
    Mesh,
    PerspectiveCamera,
    Scene,
    Vector3,
    WebGLRenderer
} from "three"
import { ViewCubemeshGenerator } from "./viewCube.meshGenerator"
import { ThreeMapControlsService } from "../codeMap/threeViewer/threeMapControls.service"
import { ViewCubeMouseEventsService } from "./viewCube.mouseEvents.service"
import { Component, ElementRef, OnInit } from "@angular/core"
import { CenterMapButtonComponent } from "./centerMapButton/centerMapButton.component"
import { ZoomSliderComponent } from "./zoomSlider/zoomSlider.component"
 
@Component({
    selector: "cc-view-cube",
    templateUrl: "./viewCube.component.html",
    styleUrls: ["./viewCube.component.scss"],
    standalone: true,
    imports: [CenterMapButtonComponent, ZoomSliderComponent]
})
export class ViewCubeComponent implements OnInit {
    private lights: Group
    private cubeGroup: Group
    private camera: PerspectiveCamera
    private renderer: WebGLRenderer
    private scene: Scene
    private WIDTH = 200
    private HEIGHT = 200
    private LENGTH_VIEWCUBE = 1
 
    private hoverInfo = { cube: null, originalMaterial: null }
 
    private cubeDefinition = {
        top: null,
        sides: null
    }
 
    constructor(
        private elementReference: ElementRef,
        private threeMapControlsService: ThreeMapControlsService,
        private viewCubeMouseEvents: ViewCubeMouseEventsService
    ) {}
 
    ngOnInit() {
        this.initScene()
        this.initLights()
        this.initRenderer(this.elementReference.nativeElement)
        this.initCube()
        this.initAxesHelper()
        this.initCamera()
        this.viewCubeMouseEvents.init(this.cubeGroup, this.camera, this.renderer)
 
        this.threeMapControlsService.subscribe("onCameraChanged", this.onCameraChanged)
        this.viewCubeMouseEvents.subscribe("viewCubeHoveredEvent", this.onCubeHovered)
        this.viewCubeMouseEvents.subscribe("viewCubeUnHoveredEvent", this.onCubeUnhovered)
        this.viewCubeMouseEvents.subscribe("viewCubeClicked", this.onCubeClicked)
    }
 
    private initAxesHelper() {
        const axesHelper = new AxesHelper(1.3)
        const centerOffset = -(this.LENGTH_VIEWCUBE / 2) + 0.01
        axesHelper.position.x += centerOffset
        axesHelper.position.y += centerOffset
        axesHelper.position.z += centerOffset
 
        this.scene.add(axesHelper)
    }
 
    private initCube() {
        const { group, top, sides } = ViewCubemeshGenerator.buildCube(1.9)
 
        this.cubeGroup = group
        this.cubeDefinition.top = top
        this.cubeDefinition.sides = sides
 
        const cubeBoundingBox = new BoxHelper(this.cubeGroup, new Color(0x00_00_00))
 
        this.scene.add(this.cubeGroup)
        this.scene.add(cubeBoundingBox)
    }
 
    onCameraChanged = (data: { camera: PerspectiveCamera }) => {
        const newCameraPosition = this.calculateCameraPosition(data.camera)
        this.setCameraPosition(newCameraPosition)
        this.renderer.render(this.scene, this.camera)
    }
 
    private setCameraPosition(cameraPosition: Vector3) {
        this.camera.position.set(cameraPosition.x, cameraPosition.y, cameraPosition.z)
        this.camera.lookAt(0, 0, 0)
        this.camera.updateProjectionMatrix()
    }
 
    private calculateCameraPosition(camera: PerspectiveCamera) {
        const codeMapTargetVector = this.threeMapControlsService.controls.target.clone()
        const codeMapCameraPosition = camera.position.clone()
        return codeMapCameraPosition.sub(codeMapTargetVector).normalize().multiplyScalar(3)
    }
 
    private initScene() {
        this.scene = new Scene()
    }
 
    private initRenderer(element: HTMLElement) {
        this.renderer = new WebGLRenderer({
            alpha: true,
            antialias: true
        })
        this.renderer.setSize(this.WIDTH, this.HEIGHT)
        this.renderer.setPixelRatio(window.devicePixelRatio) // geometry is low poly, no noticeable performance hit even with higher device pixel ratio
        element.appendChild(this.renderer.domElement)
    }
 
    private initCamera() {
        this.camera = new PerspectiveCamera(45, this.WIDTH / this.HEIGHT, 0.1, 1000)
        this.camera.position.z = 4
    }
 
    onCubeHovered = (data: { cube: Mesh }) => {
        this.hoverInfo = {
            cube: data.cube,
            originalMaterial: data.cube.material
        }
        this.hoverInfo.cube.material.emissive = new Color(0xff_ff_ff)
        this.renderer.render(this.scene, this.camera)
    }
 
    onCubeUnhovered = () => {
        this.hoverInfo.cube.material.emissive = new Color(0x00_00_00) //? NOTE why is this needed
        this.hoverInfo.cube = null
        this.renderer.render(this.scene, this.camera)
    }
 
    onCubeClicked = (data: { cube: Mesh }) => {
        switch (data.cube) {
            case this.cubeDefinition.top.front.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, -1, 1)
                break
            case this.cubeDefinition.top.front.center:
                this.threeMapControlsService.rotateCameraInVectorDirection(0, -1, 1)
                break
            case this.cubeDefinition.top.front.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, -1, 1)
                break
 
            case this.cubeDefinition.top.middle.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, -1, 0)
                break
            case this.cubeDefinition.top.middle.center:
                this.threeMapControlsService.rotateCameraInVectorDirection(0, -1, 0)
                break
            case this.cubeDefinition.top.middle.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, -1, 0)
                break
 
            case this.cubeDefinition.top.back.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, -1, -1)
                break
            case this.cubeDefinition.top.back.center:
                this.threeMapControlsService.rotateCameraInVectorDirection(0, -1, -1)
                break
            case this.cubeDefinition.top.back.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, -1, -1)
                break
 
            case this.cubeDefinition.sides.front.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, 0, -1)
                break
            case this.cubeDefinition.sides.front.center:
                this.threeMapControlsService.rotateCameraInVectorDirection(0, 0, 0)
                break
            case this.cubeDefinition.sides.front.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, 0, -1)
                break
 
            case this.cubeDefinition.sides.middle.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, 0, 0)
                break
            case this.cubeDefinition.sides.middle.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, 0, 0)
                break
 
            case this.cubeDefinition.sides.back.left:
                this.threeMapControlsService.rotateCameraInVectorDirection(1, 0, 1)
                break
            case this.cubeDefinition.sides.back.center:
                this.threeMapControlsService.rotateCameraInVectorDirection(0, 0, 1)
                break
            case this.cubeDefinition.sides.back.right:
                this.threeMapControlsService.rotateCameraInVectorDirection(-1, 0, 1)
                break
        }
    }
 
    private initLights() {
        this.lights = new Group()
        const ambilight = new AmbientLight(0x70_70_70, 2.8) // soft white light
        const light1 = new DirectionalLight(0xe0_e0_e0, 1.8)
        light1.position.set(50, 10, 8).normalize()
 
        const light2 = new DirectionalLight(0xe0_e0_e0, 1.8)
        light2.position.set(-50, 10, -8).normalize()
 
        this.lights.add(ambilight)
        this.lights.add(light1)
        this.lights.add(light2)
 
        this.scene.add(this.lights)
    }
}