/**
 * Copyright (c) 2018 Bitbloq (BQ)
 *
 * License: MIT
 *
 * long description for the file
 *
 * Derived from THREE.GridHelper
 *
 * @summary short description for the file
 * @author David García <https://github.com/empoalp>, Alberto Valero <https://github.com/avalero>
 *
 * Created at     : 2018-11-06 10:52:42
 * Last modified  : 2018-11-09 09:55:56
 */
import * as THREE from 'three';
import cloneDeep from 'lodash.clonedeep';
var BaseGrid = /** @class */ (function () {
    function BaseGrid(config) {
        this.mesh = new THREE.Group();
        this.gridConfig = cloneDeep(config);
        this.colorBig = new THREE.Color(this.gridConfig.bigGrid.color);
        this.colorSmall = new THREE.Color(this.gridConfig.smallGrid.color);
        this.colorCenter = new THREE.Color(this.gridConfig.centerGrid.color);
        if (this.gridConfig.smallGrid.enabled)
            this.mesh.add(this.smallGrid());
        if (this.gridConfig.bigGrid.enabled)
            this.mesh.add(this.bigGrid());
        if (this.gridConfig.centerGrid.enabled)
            this.mesh.add(this.centerGrid());
        if (this.gridConfig.plane.enabled)
            this.mesh.add(this.plane());
    }
    BaseGrid.prototype.bigGrid = function () {
        var halfSize = this.gridConfig.size / 2;
        var vertices = [];
        var colors = [];
        for (var j = 0, k = -halfSize; k <= halfSize; k += this.gridConfig.bigGrid.step) {
            vertices.push(-halfSize, k, 0, halfSize, k, 0);
            vertices.push(k, -halfSize, 0, k, halfSize, 0);
            var color = this.colorBig;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
        }
        var geometry = new THREE.BufferGeometry();
        geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
        geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
        var material = new THREE.LineBasicMaterial({
            vertexColors: THREE.VertexColors,
            linewidth: this.gridConfig.bigGrid.lineWidth,
            fog: false,
        });
        var mesh = new THREE.LineSegments(geometry, material);
        return mesh;
    };
    BaseGrid.prototype.smallGrid = function () {
        var halfSize = this.gridConfig.size / 2;
        var vertices = [];
        var colors = [];
        for (var j = 0, k = -halfSize; k <= halfSize; k += this.gridConfig.smallGrid.step) {
            vertices.push(-halfSize, k, 0, halfSize, k, 0);
            vertices.push(k, -halfSize, 0, k, halfSize, 0);
            var color = this.colorSmall;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
            color.toArray(colors, j);
            j += 3;
        }
        var geometry = new THREE.BufferGeometry();
        geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
        geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
        var material = new THREE.LineBasicMaterial({
            vertexColors: THREE.VertexColors,
            linewidth: this.gridConfig.smallGrid.lineWidth,
            fog: false,
        });
        var mesh = new THREE.LineSegments(geometry, material);
        return mesh;
    };
    BaseGrid.prototype.centerGrid = function () {
        var halfSize = this.gridConfig.size / 2;
        var vertices = [];
        var colors = [];
        vertices.push(-halfSize, 0, 0, halfSize, 0, 0);
        vertices.push(0, -halfSize, 0, 0, halfSize, 0);
        var color = this.colorCenter;
        color.toArray(colors, 0);
        color.toArray(colors, 3);
        color.toArray(colors, 6);
        color.toArray(colors, 9);
        var geometry = new THREE.BufferGeometry();
        geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
        geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
        var material = new THREE.LineBasicMaterial({
            vertexColors: THREE.VertexColors,
            linewidth: this.gridConfig.centerGrid.lineWidth,
            fog: false,
        });
        var mesh = new THREE.LineSegments(geometry, material);
        return mesh;
    };
    BaseGrid.prototype.plane = function () {
        var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1));
        var helper = new THREE.PlaneHelper(plane, this.gridConfig.size, this.gridConfig.plane.color);
        return helper;
    };
    BaseGrid.prototype.getMesh = function () {
        return this.mesh;
    };
    return BaseGrid;
}());
export default BaseGrid;
//# sourceMappingURL=BaseGrid.js.map