1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

add intersectRecursive for raycasting #55

This commit is contained in:
Kevin Levron 2021-05-05 00:27:00 +02:00
parent 4d30f03130
commit 9e828b01b2
3 changed files with 8 additions and 4 deletions

View File

@ -20,6 +20,7 @@ export default defineComponent({
onPointerLeave: { type: Function as PropType<PointerIntersectCallbackType>, default: emptyCallBack }, onPointerLeave: { type: Function as PropType<PointerIntersectCallbackType>, default: emptyCallBack },
onClick: { type: Function as PropType<PointerIntersectCallbackType>, default: emptyCallBack }, onClick: { type: Function as PropType<PointerIntersectCallbackType>, default: emptyCallBack },
intersectMode: { type: String, default: 'move' }, intersectMode: { type: String, default: 'move' },
intersectRecursive: { type: Boolean, default: false },
}, },
setup(): RaycasterSetupInterface { setup(): RaycasterSetupInterface {
const renderer = inject(RendererInjectionKey) const renderer = inject(RendererInjectionKey)
@ -39,6 +40,7 @@ export default defineComponent({
camera: renderer.camera, camera: renderer.camera,
domElement: renderer.canvas, domElement: renderer.canvas,
intersectObjects: this.getIntersectObjects(), intersectObjects: this.getIntersectObjects(),
intersectRecursive: this.intersectRecursive,
onIntersectEnter: this.onPointerEnter, onIntersectEnter: this.onPointerEnter,
onIntersectOver: this.onPointerOver, onIntersectOver: this.onPointerOver,
onIntersectMove: this.onPointerMove, onIntersectMove: this.onPointerMove,

View File

@ -22,6 +22,7 @@ export type IntersectObject = Mesh | InstancedMesh
export interface PointerPublicConfigInterface { export interface PointerPublicConfigInterface {
intersectMode?: 'frame' intersectMode?: 'frame'
intersectRecursive?: boolean
touch?: boolean touch?: boolean
resetOnEnd?: boolean resetOnEnd?: boolean
resetPosition?: Vector2 resetPosition?: Vector2
@ -59,6 +60,7 @@ export default function usePointer(options: PointerConfigInterface): PointerInte
camera, camera,
domElement, domElement,
intersectObjects, intersectObjects,
intersectRecursive = false,
touch = true, touch = true,
resetOnEnd = false, resetOnEnd = false,
resetPosition = new Vector2(0, 0), resetPosition = new Vector2(0, 0),
@ -119,7 +121,7 @@ export default function usePointer(options: PointerConfigInterface): PointerInte
function intersect() { function intersect() {
if (intersectObjects.length) { if (intersectObjects.length) {
const intersects = raycaster.intersect(positionN, intersectObjects) const intersects = raycaster.intersect(positionN, intersectObjects, intersectRecursive)
const offObjects: IntersectObject[] = [...intersectObjects] const offObjects: IntersectObject[] = [...intersectObjects]
const iMeshes: InstancedMesh[] = [] const iMeshes: InstancedMesh[] = []

View File

@ -4,7 +4,7 @@ import { IntersectObject } from './usePointer'
export interface RaycasterInterface { export interface RaycasterInterface {
position: Vector3 position: Vector3
updatePosition(coords: Vector2): void updatePosition(coords: Vector2): void
intersect(coords: Vector2, objects: IntersectObject[]): Intersection[], intersect(coords: Vector2, objects: IntersectObject[], recursive: boolean): Intersection[],
} }
export interface RaycasterConfigInterface { export interface RaycasterConfigInterface {
@ -28,9 +28,9 @@ export default function useRaycaster(options: RaycasterConfigInterface): Raycast
raycaster.ray.intersectPlane(plane, position) raycaster.ray.intersectPlane(plane, position)
} }
const intersect = (coords: Vector2, objects: IntersectObject[]) => { const intersect = (coords: Vector2, objects: IntersectObject[], recursive = false) => {
raycaster.setFromCamera(coords, camera) raycaster.setFromCamera(coords, camera)
return raycaster.intersectObjects(objects) return raycaster.intersectObjects(objects, recursive)
} }
return { return {