diff --git a/src/tools/mesh/index.ts b/src/tools/mesh/index.ts index 67fc7a41..89903100 100644 --- a/src/tools/mesh/index.ts +++ b/src/tools/mesh/index.ts @@ -1,7 +1,7 @@ export { getVertices, getNonIndexedVertices } from '@tools/mesh/getVertices.js' export { loadOBJ } from '@tools/mesh/loadOBJ.js' export { makeBumpy } from '@tools/mesh/makeBumpy.js' -export { scaleUV, flipUVHorizontally, flipUVVertically } from '@tools/mesh/scaleUV.js' +export { scaleUV, flipUVHorizontally, flipUVVertically, normalizeUV } from '@tools/mesh/scaleUV.js' export { toArxCoordinateSystem } from '@tools/mesh/toArxCoordinateSystem.js' export { transformEdge } from '@tools/mesh/transformEdge.js' export { translateUV } from '@tools/mesh/translateUV.js' diff --git a/src/tools/mesh/scaleUV.ts b/src/tools/mesh/scaleUV.ts index c872196a..a0d2c176 100644 --- a/src/tools/mesh/scaleUV.ts +++ b/src/tools/mesh/scaleUV.ts @@ -25,6 +25,29 @@ export const scaleUV = (scale: Vector2, geometry: BufferGeometry) => { } } +export const normalizeUV = (geometry: BufferGeometry) => { + const uv = geometry.getAttribute('uv') as BufferAttribute + + for (let idx = 0; idx < uv.count; idx++) { + let u = uv.getX(idx) + let v = uv.getY(idx) + + if (u < 0) { + u = 1 - (u % 1) + } else if (u > 1) { + u = u % 1 + } + + if (v < 0) { + v = 1 - (v % 1) + } else if (v > 1) { + v = v % 1 + } + + uv.setXY(idx, u, v) + } +} + export const flipUVHorizontally = (geometry: BufferGeometry) => { return scaleUV(new Vector2(-1, 1), geometry) }