UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

91 lines (78 loc) 2.45 kB
import { UintArrayForCount } from "../../../../../../core/collection/array/typed/uint_array_for_count.js"; import { BufferAttribute, BufferGeometry } from "three"; /** * Helper class, allowing us to treat geometry build process as a stream without having to create intermediate arrays and performing unnecessary copying */ export class StreamGeometryBuilder { constructor() { /** * * @type {number} */ this.cursor_vertices = 0; /** * * @type {number} */ this.cursor_indices = 0; /** * * @type {Float32Array|number[]|null} * @private */ this.__vertex_positions = null; /** * * @type {Float32Array|number[]|null} * @private */ this.__vertex_normals = null; /** * * @type {Float32Array|number[]|null} * @private */ this.__vertex_uvs = null; /** * * @type {Uint8Array|Uint16Array|Uint32Array|number[]|null} * @private */ this.__indices = null; } get positions() { return this.__vertex_positions; } get normals() { return this.__vertex_normals; } get uvs() { return this.__vertex_uvs; } get indices() { return this.__indices; } /** * * @param {number} vertex_count * @param {number} polygon_count */ allocate(vertex_count, polygon_count) { this.__vertex_positions = new Float32Array(vertex_count * 3); this.__vertex_normals = new Float32Array(vertex_count * 3); this.__vertex_uvs = new Float32Array(vertex_count * 2); const INDEX_BUFFER_CLASS = UintArrayForCount(vertex_count); this.__indices = new INDEX_BUFFER_CLASS(polygon_count * 3); } /** * @returns {THREE.BufferGeometry} */ build() { const geometry = new BufferGeometry(); geometry.setIndex(new BufferAttribute(this.__indices, 1)); geometry.setAttribute('position', new BufferAttribute(this.__vertex_positions, 3)); geometry.setAttribute('normal', new BufferAttribute(this.__vertex_normals, 3)); geometry.setAttribute('uv', new BufferAttribute(this.__vertex_uvs, 2)); return geometry; } }