Skip to content

Commit

Permalink
add ctrl-click to remove faces
Browse files Browse the repository at this point in the history
  • Loading branch information
memelotsqui committed Oct 26, 2023
1 parent 57797b9 commit 5d823cf
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/components/Scene.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default function Scene({sceneModel, lookatManager}) {
})
}

const restoreCUllIndicesAndColliders = () => {
const restoreCullIndicesAndColliders = () => {
avatarModel.traverse((child)=>{
if (child.isMesh) {
if (child.userData.origIndexBuffer){
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();

};

Expand Down

0 comments on commit 5d823cf

Please sign in to comment.