UNPKG

arx-level-generator

Version:
62 lines 2.21 kB
import { Vector3 } from '../../Vector3.js'; import { isBetween } from '../../helpers.js'; const getMaterialIndex = (idx, geometry) => { if (geometry.groups.length === 0) { return undefined; } const group = geometry.groups.find(({ start, count }) => { return isBetween(start, start + count - 1, idx); }); return group?.materialIndex; }; /** * Gets the vertices of a geometry. * Should be used for stuff, like terrain manipulation as indexed geometries will not be unpacked * into individual vertices. */ export const getVertices = (geometry) => { const vertices = []; const coords = geometry.getAttribute('position'); for (let idx = 0; idx < coords.count; idx++) { vertices.push({ idx, vector: new Vector3(coords.getX(idx), coords.getY(idx), coords.getZ(idx)), materialIndex: getMaterialIndex(idx, geometry), }); } return vertices; }; /** * Gets the non-indexed version of vertices of a geometry. * Should be used when converting it to Arx polygon data as Arx uses non-indexed geometry. */ export const getNonIndexedVertices = (geometry) => { const vertices = []; const index = geometry.getIndex(); const coords = geometry.getAttribute('position'); if (index === null) { // non-indexed geometry, all vertices are unique for (let idx = 0; idx < coords.count; idx++) { vertices.push({ idx, vector: new Vector3(coords.getX(idx), coords.getY(idx), coords.getZ(idx)), materialIndex: getMaterialIndex(idx, geometry), }); } } else { // indexed geometry, has shared vertices for (let i = 0; i < index.count; i++) { const idx = index.getX(i); vertices.push({ idx, vector: new Vector3(coords.getX(idx), coords.getY(idx), coords.getZ(idx)), // TODO: if the following line produces weird things // then replace i with idx materialIndex: getMaterialIndex(i, geometry), }); } } return vertices; }; //# sourceMappingURL=getVertices.js.map