From 5d823cfe92dcc12ad722b961fb9dd0c93b594152 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Thu, 26 Oct 2023 17:58:10 -0600 Subject: [PATCH] add ctrl-click to remove faces --- src/components/Scene.jsx | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/components/Scene.jsx b/src/components/Scene.jsx index 7dd87cd6..20a87528 100644 --- a/src/components/Scene.jsx +++ b/src/components/Scene.jsx @@ -153,7 +153,7 @@ export default function Scene({sceneModel, lookatManager}) { }) } - const restoreCUllIndicesAndColliders = () => { + const restoreCullIndicesAndColliders = () => { avatarModel.traverse((child)=>{ if (child.isMesh) { if (child.userData.origIndexBuffer){ @@ -164,7 +164,7 @@ export default function Scene({sceneModel, lookatManager}) { }) } - const checkIndicesExist = (array, indices) =>{ + const checkIndicesIndex = (array, indices) =>{ for (let i =0; i < array.length; i+=3){ if (indices[0] != array[i]){ continue @@ -175,28 +175,37 @@ export default function Scene({sceneModel, lookatManager}) { if (indices[2] != array[i+2]){ continue } - return true; + return i; } - return false; + return -1; } - const updateCullIndices = (intersection) => { + const updateCullIndices = (intersection, removeFace) => { const intersectedObject = intersection.object; const face = intersection.face; const newIndices = [face.a,face.b,face.c]; const clipIndices = intersectedObject.userData?.clippedIndexGeometry?.array + + if (clipIndices != null){ - if (!checkIndicesExist(clipIndices,newIndices)){ - const uint32ArrayAsArray = Array.from(clipIndices); + const hitIndex = checkIndicesIndex(clipIndices,newIndices) + const uint32ArrayAsArray = Array.from(clipIndices); + if (hitIndex == -1 && !removeFace){ const mergedIndices = [...uint32ArrayAsArray, ...newIndices]; intersectedObject.userData.clippedIndexGeometry = new THREE.BufferAttribute(new Uint32Array(mergedIndices),1,false); } + if (hitIndex != 1 && removeFace){ + uint32ArrayAsArray.splice(hitIndex, 3); + intersectedObject.userData.clippedIndexGeometry = new THREE.BufferAttribute(new Uint32Array(uint32ArrayAsArray), 1, false); + } } } const handleMouseClick = (event) => { + const isCtrlPressed = event.ctrlKey; + setOriginalInidicesAndColliders(); // Calculate mouse position in normalized device coordinates @@ -213,10 +222,10 @@ export default function Scene({sceneModel, lookatManager}) { if (intersects.length > 0) { const intersection = intersects[0]; - updateCullIndices(intersection) + updateCullIndices(intersection, isCtrlPressed) } - restoreCUllIndicesAndColliders(); + restoreCullIndicesAndColliders(); };