Fix fence UV generation before geometry merge
This commit is contained in:
@@ -20,26 +20,13 @@ function applyFenceUVs(geometry: THREE.BufferGeometry) {
|
|||||||
let minX = Number.POSITIVE_INFINITY
|
let minX = Number.POSITIVE_INFINITY
|
||||||
let minY = Number.POSITIVE_INFINITY
|
let minY = Number.POSITIVE_INFINITY
|
||||||
let minZ = Number.POSITIVE_INFINITY
|
let minZ = Number.POSITIVE_INFINITY
|
||||||
let maxX = Number.NEGATIVE_INFINITY
|
|
||||||
let maxY = Number.NEGATIVE_INFINITY
|
|
||||||
let maxZ = Number.NEGATIVE_INFINITY
|
|
||||||
|
|
||||||
for (let index = 0; index < position.count; index += 1) {
|
for (let index = 0; index < position.count; index += 1) {
|
||||||
const px = position.getX(index)
|
minX = Math.min(minX, position.getX(index))
|
||||||
const py = position.getY(index)
|
minY = Math.min(minY, position.getY(index))
|
||||||
const pz = position.getZ(index)
|
minZ = Math.min(minZ, position.getZ(index))
|
||||||
minX = Math.min(minX, px)
|
|
||||||
minY = Math.min(minY, py)
|
|
||||||
minZ = Math.min(minZ, pz)
|
|
||||||
maxX = Math.max(maxX, px)
|
|
||||||
maxY = Math.max(maxY, py)
|
|
||||||
maxZ = Math.max(maxZ, pz)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const width = Math.max(maxX - minX, 0.001)
|
|
||||||
const height = Math.max(maxY - minY, 0.001)
|
|
||||||
const depth = Math.max(maxZ - minZ, 0.001)
|
|
||||||
|
|
||||||
for (let index = 0; index < position.count; index += 1) {
|
for (let index = 0; index < position.count; index += 1) {
|
||||||
const px = position.getX(index)
|
const px = position.getX(index)
|
||||||
const py = position.getY(index)
|
const py = position.getY(index)
|
||||||
@@ -52,14 +39,14 @@ function applyFenceUVs(geometry: THREE.BufferGeometry) {
|
|||||||
let v = 0
|
let v = 0
|
||||||
|
|
||||||
if (ny >= nx && ny >= nz) {
|
if (ny >= nx && ny >= nz) {
|
||||||
u = (px - minX) / width
|
u = px - minX
|
||||||
v = (pz - minZ) / depth
|
v = pz - minZ
|
||||||
} else if (nx >= nz) {
|
} else if (nx >= nz) {
|
||||||
u = (pz - minZ) / depth
|
u = pz - minZ
|
||||||
v = (py - minY) / height
|
v = py - minY
|
||||||
} else {
|
} else {
|
||||||
u = (px - minX) / width
|
u = px - minX
|
||||||
v = (py - minY) / height
|
v = py - minY
|
||||||
}
|
}
|
||||||
|
|
||||||
uvs[index * 2] = u
|
uvs[index * 2] = u
|
||||||
@@ -157,13 +144,17 @@ function generateFenceGeometry(fence: FenceNode) {
|
|||||||
const geometries = parts.map((part) => {
|
const geometries = parts.map((part) => {
|
||||||
const geometry = new THREE.BoxGeometry(1, 1, 1)
|
const geometry = new THREE.BoxGeometry(1, 1, 1)
|
||||||
geometry.scale(part.scale[0], part.scale[1], part.scale[2])
|
geometry.scale(part.scale[0], part.scale[1], part.scale[2])
|
||||||
|
applyFenceUVs(geometry)
|
||||||
geometry.translate(part.position[0], part.position[1], part.position[2])
|
geometry.translate(part.position[0], part.position[1], part.position[2])
|
||||||
return geometry
|
return geometry
|
||||||
})
|
})
|
||||||
|
|
||||||
const merged = mergeGeometries(geometries, false) ?? new THREE.BufferGeometry()
|
const merged = mergeGeometries(geometries, false) ?? new THREE.BufferGeometry()
|
||||||
geometries.forEach((geometry) => geometry.dispose())
|
geometries.forEach((geometry) => geometry.dispose())
|
||||||
applyFenceUVs(merged)
|
const mergedUv = merged.getAttribute('uv')
|
||||||
|
if (mergedUv) {
|
||||||
|
merged.setAttribute('uv2', new THREE.Float32BufferAttribute(Array.from(mergedUv.array), 2))
|
||||||
|
}
|
||||||
merged.computeVertexNormals()
|
merged.computeVertexNormals()
|
||||||
return merged
|
return merged
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user