Skip to content

Commit

Permalink
refactor(EntityModel): extract textureContainer padding function to a…
Browse files Browse the repository at this point in the history
… function in helpers
  • Loading branch information
meszaros-lajos-gyorgy committed Mar 20, 2024
1 parent 4411124 commit 7dc5b27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/EntityModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 ?? '<missing material>',
})
}
})
}

Expand Down
13 changes: 12 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = <T>(length: number, paddingValue: T, array: T[]) => {
return [...array, ...repeat(paddingValue, length)].slice(0, length)
}

0 comments on commit 7dc5b27

Please sign in to comment.