From e4de97b842c7091173f016db24560cea47850fd2 Mon Sep 17 00:00:00 2001 From: Bela Bohlender Date: Tue, 25 Jun 2024 19:31:34 +0200 Subject: [PATCH] fix: sphere&raycast with singular matrix --- .../uikit/src/panel/interaction-panel-mesh.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/uikit/src/panel/interaction-panel-mesh.ts b/packages/uikit/src/panel/interaction-panel-mesh.ts index d1a7f53..98c6a19 100644 --- a/packages/uikit/src/panel/interaction-panel-mesh.ts +++ b/packages/uikit/src/panel/interaction-panel-mesh.ts @@ -1,4 +1,4 @@ -import { Intersection, Mesh, Object3D, Object3DEventMap, Plane, Raycaster, Sphere, Vector2, Vector3 } from 'three' +import { Intersection, Matrix4, Mesh, Object3D, Plane, Sphere, Vector2, Vector3 } from 'three' import { ClippingRect } from '../clipping.js' import { Signal } from '@preact/signals-core' import { OrderInfo } from '../order.js' @@ -26,9 +26,19 @@ declare module 'three' { } } +const scaleHelper = new Vector3() + +function isSingularMatrix(matrix: Matrix4) { + scaleHelper.setFromMatrixScale(matrix) + return scaleHelper.x === 0 || scaleHelper.y === 0 || scaleHelper.z === 0 +} + export function makePanelSpherecast(mesh: Mesh): Exclude { return (sphere, intersects) => { const matrixWorld = mesh.matrixWorld + if (isSingularMatrix(matrixWorld)) { + return + } planeHelper.constant = 0 planeHelper.normal.set(0, 0, 1) planeHelper.applyMatrix4(matrixWorld) @@ -81,6 +91,9 @@ export function makePanelSpherecast(mesh: Mesh): Exclude { const matrixWorld = mesh.matrixWorld + if (isSingularMatrix(matrixWorld)) { + return + } planeHelper.constant = 0 planeHelper.normal.set(0, 0, 1) planeHelper.applyMatrix4(matrixWorld)