@pmndrs/pointer-events
Advanced tools
Comparing version 6.2.3 to 6.2.4
@@ -15,2 +15,5 @@ import { Object3D } from 'three'; | ||
private unregister; | ||
/** | ||
* @returns true if any pointer is captured | ||
*/ | ||
private startIntersection; | ||
@@ -17,0 +20,0 @@ /** |
@@ -25,4 +25,8 @@ import { intersectPointerEventTargets } from './intersections/utils.js'; | ||
} | ||
/** | ||
* @returns true if any pointer is captured | ||
*/ | ||
startIntersection(nonCapturedPointers, nativeEvent) { | ||
const length = this.pointers.length; | ||
let anyPointerIsCaptured = false; | ||
for (let i = 0; i < length; i++) { | ||
@@ -36,2 +40,3 @@ const pointer = this.pointers[i]; | ||
if (pointerCapture != null) { | ||
anyPointerIsCaptured = true; | ||
pointer.setIntersection(pointer.intersector.intersectPointerCapture(pointerCapture, nativeEvent)); | ||
@@ -43,2 +48,3 @@ continue; | ||
} | ||
return anyPointerIsCaptured; | ||
} | ||
@@ -101,17 +107,15 @@ /** | ||
} | ||
/* | ||
slow version that stays in here for benchmarking | ||
for (let i = 0; i < this.pointers.length; i++) { | ||
this.pointers[i].move(scene, nativeEvent) | ||
}*/ | ||
//start intersection, build nonCapturedPointers list, and compute the intersection for all captured pointers | ||
this.nonCapturedPointers.length = 0; | ||
this.startIntersection(this.nonCapturedPointers, nativeEvent); | ||
//intersect scene using the non captured pointers | ||
intersectPointerEventTargets(scene, this.nonCapturedPointers); | ||
//finalize the intersection for the non captured pointers | ||
const nonCapturedPointerLength = this.nonCapturedPointers.length; | ||
for (let i = 0; i < nonCapturedPointerLength; i++) { | ||
const pointer = this.nonCapturedPointers[i]; | ||
pointer.setIntersection(pointer.intersector.finalizeIntersection()); | ||
const anyPointerIsCaptured = this.startIntersection(this.nonCapturedPointers, nativeEvent); | ||
//we only need to intersect the scene if no pointer is captured or (in case one or more pointers are captured) if mulitple pointers can be enabled | ||
if (!anyPointerIsCaptured || this.enableMultiplePointers) { | ||
//intersect scene using the non captured pointers | ||
intersectPointerEventTargets(scene, this.nonCapturedPointers); | ||
//finalize the intersection for the non captured pointers | ||
const nonCapturedPointerLength = this.nonCapturedPointers.length; | ||
for (let i = 0; i < nonCapturedPointerLength; i++) { | ||
const pointer = this.nonCapturedPointers[i]; | ||
pointer.setIntersection(pointer.intersector.finalizeIntersection()); | ||
} | ||
} | ||
@@ -118,0 +122,0 @@ //commit the intersection, compute active pointers, and enabling/disabling pointers |
@@ -0,1 +1,2 @@ | ||
import { Mesh } from 'three'; | ||
import { Pointer } from './pointer.js'; | ||
@@ -5,2 +6,3 @@ import { PointerEvent } from './event.js'; | ||
import { generateUniquePointerId } from './pointer/index.js'; | ||
import { getClosestUV } from './utils.js'; | ||
function htmlEventToCoords(element, e, target) { | ||
@@ -28,6 +30,7 @@ if (!(e instanceof globalThis.MouseEvent)) { | ||
} | ||
if (e.uv == null) { | ||
if (!(e.object instanceof Mesh)) { | ||
return target.set(0, 0); | ||
} | ||
target.copy(e.uv).multiplyScalar(2).addScalar(-1); | ||
getClosestUV(target, e.point, e.object); | ||
target.multiplyScalar(2).addScalar(-1); | ||
return target; | ||
@@ -34,0 +37,0 @@ } |
@@ -7,1 +7,2 @@ export { Pointer } from './pointer.js'; | ||
export * from './combine.js'; | ||
export { getClosestUV } from './utils.js'; |
@@ -7,1 +7,2 @@ export { Pointer } from './pointer.js'; | ||
export * from './combine.js'; | ||
export { getClosestUV } from './utils.js'; |
@@ -1,2 +0,2 @@ | ||
import { Object3D } from 'three'; | ||
import { Mesh, Object3D, Vector2, Vector3 } from 'three'; | ||
import { PointerEventsMap } from './event.js'; | ||
@@ -13,1 +13,2 @@ declare module 'three' { | ||
export declare function getObjectListeners<E>({ _listeners, __r3f }: Object3D, forEvent: keyof PointerEventsMap): Array<(event: E) => void> | undefined; | ||
export declare function getClosestUV(target: Vector2, point: Vector3, mesh: Mesh): void; |
@@ -0,1 +1,2 @@ | ||
import { BufferAttribute, Matrix4, Triangle, Vector2, Vector3 } from 'three'; | ||
export function hasObjectListeners({ _listeners, __r3f }) { | ||
@@ -38,1 +39,57 @@ if (_listeners != null && Object.keys(_listeners).length > 0) { | ||
}; | ||
const triangleHelper1 = new Triangle(); | ||
const triangleHelper2 = new Triangle(); | ||
const aVec2Helper = new Vector2(); | ||
const bVec2Helper = new Vector2(); | ||
const cVec2Helper = new Vector2(); | ||
const pointHelper = new Vector3(); | ||
const inverseMatrix = new Matrix4(); | ||
const localPointHelper = new Vector3(); | ||
export function getClosestUV(target, point, mesh) { | ||
localPointHelper.copy(point).applyMatrix4(inverseMatrix.copy(mesh.matrixWorld).invert()); | ||
const uv = mesh.geometry.attributes.uv; | ||
if (uv == null || !(uv instanceof BufferAttribute)) { | ||
return void target.set(0, 0); | ||
} | ||
let clostestDistance; | ||
loopThroughTriangles(mesh, (i1, i2, i3) => { | ||
mesh.getVertexPosition(i1, triangleHelper1.a); | ||
mesh.getVertexPosition(i2, triangleHelper1.b); | ||
mesh.getVertexPosition(i3, triangleHelper1.c); | ||
const distance = triangleHelper1.closestPointToPoint(localPointHelper, pointHelper).distanceTo(localPointHelper); | ||
if (clostestDistance != null && distance >= clostestDistance) { | ||
return void target.set(0, 0); | ||
} | ||
clostestDistance = distance; | ||
triangleHelper2.copy(triangleHelper1); | ||
aVec2Helper.fromBufferAttribute(uv, i1); | ||
bVec2Helper.fromBufferAttribute(uv, i2); | ||
cVec2Helper.fromBufferAttribute(uv, i3); | ||
}); | ||
if (clostestDistance == null) { | ||
return void target.set(0, 0); | ||
} | ||
triangleHelper2.closestPointToPoint(localPointHelper, pointHelper); | ||
triangleHelper2.getInterpolation(pointHelper, aVec2Helper, bVec2Helper, cVec2Helper, target); | ||
} | ||
function loopThroughTriangles(mesh, fn) { | ||
const drawRange = mesh.geometry.drawRange; | ||
if (mesh.geometry.index != null) { | ||
const index = mesh.geometry.index; | ||
const start = Math.max(0, drawRange.start); | ||
const end = Math.min(index.count, drawRange.start + drawRange.count); | ||
for (let i = start; i < end; i += 3) { | ||
fn(index.getX(i), index.getX(i + 1), index.getX(i + 2)); | ||
} | ||
return; | ||
} | ||
const position = mesh.geometry.attributes.position; | ||
if (position == null) { | ||
return; | ||
} | ||
const start = Math.max(0, drawRange.start); | ||
const end = Math.min(position.count, drawRange.start + drawRange.count); | ||
for (let i = start; i < end; i += 3) { | ||
fn(i, i + 1, i + 2); | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"license": "SEE LICENSE IN LICENSE", | ||
"version": "6.2.3", | ||
"version": "6.2.4", | ||
"homepage": "https://github.com/pmndrs/xr", | ||
@@ -8,0 +8,0 @@ "author": "Bela Bohlender", |
83500
1924