UNPKG

@itwin/core-frontend

Version:
61 lines 2.41 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Rendering */ import { assert } from "@itwin/core-bentley"; /** * Holds an array of indices into a VertexTable. Each index is a 24-bit unsigned integer. * The order of the indices specifies the order in which vertices are drawn. * @internal */ export class VertexIndices { data; /** * Directly construct from an array of bytes in which each index occupies 3 contiguous bytes. * The length of the array must be a multiple of 3. This object takes ownership of the array. */ constructor(data) { this.data = data; assert(0 === this.data.length % 3); } /** Get the number of 24-bit indices. */ get length() { return this.data.length / 3; } /** Convert an array of 24-bit unsigned integer values into a VertexIndices object. */ static fromArray(indices) { const bytes = new Uint8Array(indices.length * 3); for (let i = 0; i < indices.length; i++) this.encodeIndex(indices[i], bytes, i * 3); return new VertexIndices(bytes); } static encodeIndex(index, bytes, byteIndex) { assert(byteIndex + 2 < bytes.length); bytes[byteIndex + 0] = index & 0x000000ff; bytes[byteIndex + 1] = (index & 0x0000ff00) >> 8; bytes[byteIndex + 2] = (index & 0x00ff0000) >> 16; } setNthIndex(n, value) { VertexIndices.encodeIndex(value, this.data, n * 3); } decodeIndex(index) { assert(index < this.length); const byteIndex = index * 3; return this.data[byteIndex] | (this.data[byteIndex + 1] << 8) | (this.data[byteIndex + 2] << 16); } decodeIndices() { const indices = []; for (let i = 0; i < this.length; i++) indices.push(this.decodeIndex(i)); return indices; } [Symbol.iterator]() { function* iterator(indices) { for (let i = 0; i < indices.length; i++) yield indices.decodeIndex(i); } return iterator(this); } } //# sourceMappingURL=VertexIndices.js.map