All files / entities/base Plane.js

0% Statements 0/19
0% Branches 0/8
0% Functions 0/6
0% Lines 0/19

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                                                                                                                         
import Element from "../Element";
import { ENTITY_TYPES } from "../constants";
 
import { PlaneGeometry, DoubleSide, Vector3, MeshBasicMaterial } from "three";
import Color from "../../lib/Color";
 
const UP = new Vector3(0, 1, 0);
const DOWN = new Vector3(0, -1, 0);
 
export default class Plane extends Element {
    constructor(height, width, color = Color.randomColor(true), options = {}) {
        super({
            height,
            width,
            color,
            ...options,
        });
 
        this.height = height;
        this.width = width;
        this.color = color;
 
        const { transparent = false, opacity = 1 } = options;
 
        const material = new MeshBasicMaterial({ color, side: DoubleSide, transparent, opacity });
        const geometry = new PlaneGeometry(width, height);
 
        this.setBody({ geometry, material });
        this.setEntityType(ENTITY_TYPES.MESH.TYPE);
        this.setEntitySubtype(ENTITY_TYPES.MESH.SUBTYPES.PLANE);
    }
 
    static get UP() {
        return UP;
    }
    static get DOWN() {
        return DOWN;
    }
 
    toJSON(parseJSON = false) {
        if (this.isSerializable()) {
            return {
                ...super.toJSON(parseJSON),
                height: this.height,
                width: this.width,
                color: this.color,
            };
        }
    }
 
    face(direction) {
        const vector = new Vector3(direction.x, direction.y, direction.z);
 
        this.body.lookAt(vector);
    }
 
    static create(data = {}) {
        return new Plane(data.height, data.width, data.color, data.options);
    }
}