Skip to content

Commit

Permalink
fix: sphere&raycast with singular matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohlender committed Jun 25, 2024
1 parent ef77cf6 commit e4de97b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/uikit/src/panel/interaction-panel-mesh.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<Mesh['spherecast'], undefined> {
return (sphere, intersects) => {
const matrixWorld = mesh.matrixWorld
if (isSingularMatrix(matrixWorld)) {
return
}
planeHelper.constant = 0
planeHelper.normal.set(0, 0, 1)
planeHelper.applyMatrix4(matrixWorld)
Expand Down Expand Up @@ -81,6 +91,9 @@ export function makePanelSpherecast(mesh: Mesh): Exclude<Mesh['spherecast'], und
export function makePanelRaycast(mesh: Mesh): Mesh['raycast'] {
return (raycaster, intersects) => {
const matrixWorld = mesh.matrixWorld
if (isSingularMatrix(matrixWorld)) {
return
}
planeHelper.constant = 0
planeHelper.normal.set(0, 0, 1)
planeHelper.applyMatrix4(matrixWorld)
Expand Down

0 comments on commit e4de97b

Please sign in to comment.