Skip to content

Commit

Permalink
feat(scaleUV): add normalizeUV function
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed May 23, 2024
1 parent f152a85 commit 68434a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tools/mesh/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
23 changes: 23 additions & 0 deletions src/tools/mesh/scaleUV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 68434a0

Please sign in to comment.