"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PointCloudGeometry = void 0;
/** @packageDocumentation
 * @module WebGL
 */
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const AttributeMap_1 = require("./AttributeMap");
const CachedGeometry_1 = require("./CachedGeometry");
const GL_1 = require("./GL");
const AttributeBuffers_1 = require("./AttributeBuffers");
const System_1 = require("./System");
/** @internal */
class PointCloudGeometry extends CachedGeometry_1.CachedGeometry {
    get isDisposed() { return this.buffers.isDisposed && this._vertices.isDisposed; }
    get asPointCloud() { return this; }
    get supportsThematicDisplay() { return true; }
    get overrideColorMix() { return .5; } // This could be a setting from either the mesh or the override if required.
    dispose() {
        (0, core_bentley_1.dispose)(this.buffers);
        (0, core_bentley_1.dispose)(this._vertices);
    }
    constructor(pointCloud) {
        super();
        this._colorHandle = undefined;
        this.buffers = AttributeBuffers_1.BuffersContainer.create();
        this._vertices = AttributeBuffers_1.QBufferHandle3d.create(pointCloud.qparams, pointCloud.positions);
        const attrPos = AttributeMap_1.AttributeMap.findAttribute("a_pos", 2 /* TechniqueId.PointCloud */, false);
        (0, core_bentley_1.assert)(undefined !== attrPos);
        const vertexDataType = (pointCloud.positions instanceof Float32Array) ? GL_1.GL.DataType.Float : ((pointCloud.positions instanceof Uint8Array) ? GL_1.GL.DataType.UnsignedByte : GL_1.GL.DataType.UnsignedShort);
        this.buffers.addBuffer(this._vertices, [AttributeBuffers_1.BufferParameters.create(attrPos.location, 3, vertexDataType, false, 0, 0, false)]);
        this._vertexCount = pointCloud.positions.length / 3;
        this._hasFeatures = core_common_1.FeatureIndexType.Empty !== pointCloud.features.type;
        this.voxelSize = pointCloud.voxelSize;
        this.colorIsBgr = "bgr" === pointCloud.colorFormat;
        if (undefined !== pointCloud.colors) {
            this._colorHandle = AttributeBuffers_1.BufferHandle.createArrayBuffer(pointCloud.colors);
            const attrColor = AttributeMap_1.AttributeMap.findAttribute("a_color", 2 /* TechniqueId.PointCloud */, false);
            (0, core_bentley_1.assert)(undefined !== attrColor);
            this.buffers.addBuffer(this._colorHandle, [AttributeBuffers_1.BufferParameters.create(attrColor.location, 3, GL_1.GL.DataType.UnsignedByte, true, 0, 0, false)]);
        }
    }
    collectStatistics(stats) {
        const bytesUsed = this._vertices.bytesUsed + (undefined !== this._colorHandle ? this._colorHandle.bytesUsed : 0);
        stats.addPointCloud(bytesUsed);
    }
    _wantWoWReversal(_target) { return false; }
    get techniqueId() { return 2 /* TechniqueId.PointCloud */; }
    getPass(target) {
        // Point clouds don't cast shadows.
        return target.isDrawingShadowMap ? "none" : "point-clouds";
    }
    get renderOrder() { return 5 /* RenderOrder.Linear */; }
    get qOrigin() { return this._vertices.origin; }
    get qScale() { return this._vertices.scale; }
    get colors() { return this._colorHandle; }
    get hasFeatures() { return this._hasFeatures; }
    get hasBakedLighting() { return true; }
    draw() {
        this.buffers.bind();
        System_1.System.instance.context.drawArrays(GL_1.GL.PrimitiveType.Points, 0, this._vertexCount);
        this.buffers.unbind();
    }
    // ###TODO delete this.
    getLineWeight(_params) {
        // If line weight < 0 it is real size in meters (voxel size).
        return (this.voxelSize > 0) ? -this.voxelSize : 1;
    }
}
exports.PointCloudGeometry = PointCloudGeometry;
//# sourceMappingURL=PointCloud.js.map