diff --git a/src/components/three/XRControllerMeasure.tsx b/src/components/three/XRControllerMeasure.tsx index bda9008..63b9af9 100644 --- a/src/components/three/XRControllerMeasure.tsx +++ b/src/components/three/XRControllerMeasure.tsx @@ -239,21 +239,33 @@ export function XRControllerMeasure() { if (!trigState.current && trigVal > TRIG_ON) { trigState.current = true; const st = useModelStore.getState(); - if (st.selectionMode && hit && hit.object instanceof THREE.Mesh) { - // Walk up to find ifcElement + modelId - let cur: THREE.Object3D | null = hit.object; - let element: THREE.Object3D | null = null; - let modelId: string | null = null; - while (cur) { - if (!element && cur.userData?.ifcElement) element = cur; - if (!modelId && cur.userData?.modelId) modelId = cur.userData.modelId as string; - cur = cur.parent; - } - if (!element) element = hit.object; - if (!modelId) modelId = st.activeModelId; - if (modelId && element) { - const id = element.userData?.ifcId ?? element.name ?? element.uuid; - st.toggleElementSelection(`${modelId}:${id}`); + if (st.selectionMode) { + // Fresh raycast on the press itself (not every frame) for selection + const triggerHits = raycaster.current.intersectObjects(scene.children, true); + const triggerHit = triggerHits.find((h) => { + const o = h.object; + if (!(o instanceof THREE.Mesh)) return false; + if (o.userData.__edgeLine) return false; + if (o.geometry instanceof THREE.SphereGeometry) return false; + if (o.geometry instanceof THREE.RingGeometry) return false; + if (o.geometry instanceof THREE.PlaneGeometry) return false; + return true; + }); + if (triggerHit && triggerHit.object instanceof THREE.Mesh) { + let cur: THREE.Object3D | null = triggerHit.object; + let element: THREE.Object3D | null = null; + let modelId: string | null = null; + while (cur) { + if (!element && cur.userData?.ifcElement) element = cur; + if (!modelId && cur.userData?.modelId) modelId = cur.userData.modelId as string; + cur = cur.parent; + } + if (!element) element = triggerHit.object; + if (!modelId) modelId = st.activeModelId; + if (modelId && element) { + const id = element.userData?.ifcId ?? element.name ?? element.uuid; + st.toggleElementSelection(`${modelId}:${id}`); + } } } else if (snappedPoint) { st.addMeasurePoint({