From 5c6671070cd5964ad4839de6ff09bd0953265f8d Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Sat, 19 Aug 2023 20:05:43 +0200 Subject: [PATCH] fix(tools/mesh/transformEdge): improve edge detection for meshes that are not flat --- src/tools/mesh/connectEdgeTo.ts | 1 + src/tools/mesh/transformEdge.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/tools/mesh/connectEdgeTo.ts b/src/tools/mesh/connectEdgeTo.ts index 35e67850..cbe6a405 100644 --- a/src/tools/mesh/connectEdgeTo.ts +++ b/src/tools/mesh/connectEdgeTo.ts @@ -19,6 +19,7 @@ export const connectEdgeTo = (source: BufferGeometry, target: BufferGeometry) => const edgeIdx = sourceEdgeVertices.findIndex((edgeVertex) => { return edgeVertex.equals(vertex.vector) }) + if (edgeIdx !== -1) { const [edgePoint] = sourceEdgeVertices.splice(edgeIdx, 1) diff --git a/src/tools/mesh/transformEdge.ts b/src/tools/mesh/transformEdge.ts index 2b621470..6cccebd7 100644 --- a/src/tools/mesh/transformEdge.ts +++ b/src/tools/mesh/transformEdge.ts @@ -1,20 +1,20 @@ -import { BufferAttribute, EdgesGeometry, Mesh } from 'three' +import { BufferAttribute, Mesh } from 'three' import { Vector3 } from '@src/Vector3.js' +import { categorizeVertices } from '@tools/mesh/categorizeVertices.js' import { getVertices } from '@tools/mesh/getVertices.js' export const transformEdge = (offset: Vector3, mesh: Mesh) => { - // TODO: instead of EdgesGeometry try using the edge detection code from the Alia's nightmare level - // EdgesGeometry fails if the geometry is already bumped - const edge = new EdgesGeometry(mesh.geometry) - const edgeVertices = getVertices(edge) + const { edges, corners } = categorizeVertices(mesh.geometry) + const edgeVertices = [...edges, ...corners] const vertices = getVertices(mesh.geometry) const coords = mesh.geometry.getAttribute('position') as BufferAttribute vertices.forEach((vertex) => { const edgeIdx = edgeVertices.findIndex((edgeVertex) => { - return edgeVertex.vector.equals(vertex.vector) + return edgeVertex.equals(vertex.vector) }) + if (edgeIdx !== -1) { edgeVertices.splice(edgeIdx, 1) coords.setX(vertex.idx, vertex.vector.x + offset.x)