// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import * as THREE from "three";
import { RenderableMarker } from "./RenderableMarker";
import { makeStandardMaterial } from "./materials";
import { rgbToThreeColor } from "../../color";
export class RenderableCube extends RenderableMarker {
    #mesh;
    #outline;
    constructor(topic, marker, receiveTime, renderer) {
        super(topic, marker, receiveTime, renderer);
        // Cube mesh
        const cubeGeometry = this.renderer.sharedGeometry.getGeometry(`${this.constructor.name}-cube`, createGeometry);
        const cubeEdgesGeometry = this.renderer.sharedGeometry.getGeometry(`${this.constructor.name}-cube-edges`, () => createEdgesGeometry(cubeGeometry));
        this.#mesh = new THREE.Mesh(cubeGeometry, makeStandardMaterial(marker.color));
        this.#mesh.castShadow = true;
        this.#mesh.receiveShadow = true;
        this.add(this.#mesh);
        // Cube outline
        this.#outline = new THREE.LineSegments(cubeEdgesGeometry, renderer.outlineMaterial);
        this.#outline.userData.picking = false;
        this.#mesh.add(this.#outline);
        this.update(marker, receiveTime);
    }
    dispose() {
        this.#mesh.material.dispose();
    }
    update(newMarker, receiveTime) {
        super.update(newMarker, receiveTime);
        const marker = this.userData.marker;
        const transparent = marker.color.a < 1;
        if (transparent !== this.#mesh.material.transparent) {
            this.#mesh.material.transparent = transparent;
            this.#mesh.material.depthWrite = !transparent;
            this.#mesh.material.needsUpdate = true;
        }
        this.#outline.visible = this.getSettings()?.showOutlines ?? true;
        rgbToThreeColor(this.#mesh.material.color, marker.color);
        this.#mesh.material.opacity = marker.color.a;
        this.scale.set(marker.scale.x, marker.scale.y, marker.scale.z);
    }
}
export function createGeometry() {
    const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
    cubeGeometry.computeBoundingSphere();
    return cubeGeometry;
}
function createEdgesGeometry(cubeGeometry) {
    const cubeEdgesGeometry = new THREE.EdgesGeometry(cubeGeometry, 40);
    cubeEdgesGeometry.computeBoundingSphere();
    return cubeEdgesGeometry;
}
//# sourceMappingURL=RenderableCube.js.map