diff --git a/src/EntityModel.ts b/src/EntityModel.ts index e538b0e3..19572468 100644 --- a/src/EntityModel.ts +++ b/src/EntityModel.ts @@ -8,7 +8,7 @@ import { Polygons } from '@src/Polygons.js' import { Settings } from '@src/Settings.js' import { Texture } from '@src/Texture.js' import { Vector3 } from '@src/Vector3.js' -import { fileExists, roundToNDecimals } from '@src/helpers.js' +import { arrayPadRight, fileExists, roundToNDecimals } from '@src/helpers.js' import { createCacheFolderIfNotExists } from '@services/cache.js' import { getNonIndexedVertices } from '@tools/mesh/getVertices.js' import { repeat } from './faux-ramda.js' @@ -275,15 +275,15 @@ export class EntityModel { const numberOfGroups = geometry.groups.length === 0 ? 1 : geometry.groups.length - let texture: (Texture | undefined)[] = [] + let textures: (Texture | undefined)[] = [] if (material instanceof MeshBasicMaterial) { if (material.map instanceof Texture) { - texture = repeat(material.map, numberOfGroups) + textures = repeat(material.map, numberOfGroups) } else { console.warn('[warning] EntityModel: Unsupported texture map in material when adding threejs mesh') } } else if (Array.isArray(material)) { - texture = material.map((material) => { + textures = material.map((material) => { if (material instanceof MeshBasicMaterial) { if (material.map instanceof Texture) { return material.map @@ -300,10 +300,10 @@ export class EntityModel { console.warn('[warning] EntityModel: Unsupported material found when adding threejs mesh') } - ;[...texture, ...repeat(undefined, numberOfGroups)].slice(0, numberOfGroups).forEach((t) => { - ftlData.textureContainers.push({ + ftlData.textureContainers = arrayPadRight(numberOfGroups, undefined, textures).map((t) => { + return { filename: t?.filename ?? '', - }) + } }) } diff --git a/src/helpers.ts b/src/helpers.ts index 4e8f1560..adb57739 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -3,7 +3,7 @@ import path from 'node:path' import { fileURLToPath } from 'node:url' import { Box3, BufferGeometry, Euler, Mesh, Object3D, Vector3 as ThreeJsVector3 } from 'three' import { Vector3 } from '@src/Vector3.js' -import { mean } from '@src/faux-ramda.js' +import { mean, repeat } from '@src/faux-ramda.js' export type PackageJsonProps = { name: string @@ -171,3 +171,14 @@ export const pointToBox = (point: Vector3, size: number | Vector3) => { const max = point.clone().add(size) return new Box3(min, max) } + +/** + * This function also cuts the array to the given size! + * + * `arrayPadRight(4, undefined, [1, 2]) -> [1, 2, undefined, undefined]` + * + * `arrayPadRight(4, undefined, [1, 2, 3, 4, 5, 6]) -> [1, 2, 3, 4]` + */ +export const arrayPadRight = (length: number, paddingValue: T, array: T[]) => { + return [...array, ...repeat(paddingValue, length)].slice(0, length) +}