Skip to content

Commit

Permalink
fix: group should recursive search for child elements (#728) (#731)
Browse files Browse the repository at this point in the history
* fix: group should recursive search for child elements (#728)

* fix: rename variable

---------

Co-authored-by: hexianWeb <[email protected]>
Co-authored-by: Alvaro Saburido <[email protected]>
Co-authored-by: Garrett Walker <[email protected]>
  • Loading branch information
4 people authored Jul 3, 2024
1 parent d09bab4 commit f09367b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/composables/useRaycaster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import type { DomEvent, TresCamera, TresEvent } from 'src/types'
import type { TresContext } from '../useTresContextProvider'

export const useRaycaster = (
objects: Ref<Object3D[]>,
objectsWithEvents: Ref<Object3D[]>,
ctx: TresContext,
) => {
// having a separate computed makes useElementBounding work
const canvas = computed(() => ctx.renderer.value.domElement as HTMLCanvasElement)
const intersects: ShallowRef<Intersection[]> = shallowRef([])
const { x, y } = usePointer({ target: canvas })
const objectWihtEvents = computed(() => objects.value.filter(obj => obj.__tres?.eventCount > 0))
let delta = 0

const { width, height, top, left } = useElementBounding(canvas)
Expand All @@ -35,7 +34,7 @@ export const useRaycaster = (

ctx.raycaster.value.setFromCamera(new Vector2(x, y), ctx.camera.value)

intersects.value = ctx.raycaster.value.intersectObjects(objectWihtEvents.value, true)
intersects.value = ctx.raycaster.value.intersectObjects(objectsWithEvents.value, true)
return intersects.value
}

Expand Down
8 changes: 4 additions & 4 deletions src/composables/useTresEventManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export function useTresEventManager(
if (scene) { _scene.value = scene }
if (context) { _context.value = context }

const hasEvents = object => object.__tres?.eventCount > 0
const hasChildrenWithEvents = object => object.children?.some(child => hasChildrenWithEvents(child)) || hasEvents(object)
// TODO: Optimize to not hit test on the whole scene
const sceneChildren = computed(() =>
_scene.value ? _scene.value.children : [],
)
const objectsWithEvents = computed(() => _scene.value?.children?.filter(hasChildrenWithEvents) || [])

function executeEventListeners(
listeners: Function | Function[],
Expand Down Expand Up @@ -111,7 +111,7 @@ export function useTresEventManager(
onPointerMissed,
onWheel,
forceUpdate,
} = useRaycaster(sceneChildren, context)
} = useRaycaster(objectsWithEvents, context)

onPointerUp(event => propogateEvent('onPointerUp', event))
onPointerDown(event => propogateEvent('onPointerDown', event))
Expand Down

0 comments on commit f09367b

Please sign in to comment.