-
Notifications
You must be signed in to change notification settings - Fork 40
Fix for raycasting inaccuracy #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "react-three-map": patch | ||
| --- | ||
|
|
||
| Fix hover/raycast inaccuracy by deriving the pointer-event ray origin from the cursor position. The ray origin was being unprojected from a fixed NDC point that ignored the cursor, producing a skewed ray whose error grew towards the edges of the map. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { Matrix4, Ray } from "three"; | ||
|
|
||
| /** | ||
| * Builds a raycaster ray from the pointer position by unprojecting NDC points | ||
| * through the inverted projection*view matrix. | ||
| * | ||
| * Both the origin (near plane) and direction (towards the far plane) are derived | ||
| * from the pointer, so the resulting ray is the geometrically correct line | ||
| * through the camera eye for any cursor position — the same near→far scheme | ||
| * three.js uses in `Raycaster.setFromCamera`. | ||
| */ | ||
| export function computeRay(ray: Ray, pointerX: number, pointerY: number, projViewInv: Matrix4) { | ||
| ray.origin.set(pointerX, pointerY, -1).applyMatrix4(projViewInv); | ||
| ray.direction | ||
| .set(pointerX, pointerY, 1) | ||
| .applyMatrix4(projViewInv) | ||
| .sub(ray.origin) | ||
| .normalize(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Matrix4, PerspectiveCamera, Ray, Vector3 } from "three"; | ||
| import { expect, it } from "vitest"; | ||
| import { computeRay } from "../core/compute-ray"; | ||
|
|
||
| /** | ||
| * For a perspective camera every pointer ray must pass through the camera eye. | ||
| * `computeRay` satisfies this for any cursor position by deriving the origin | ||
| * from the pointer; the previous fixed origin (`setScalar(0)`) only did so at | ||
| * the dead centre of the view, which is the hover/raycast offset bug. | ||
| */ | ||
| it("computeRay builds a ray that passes through the perspective camera eye", () => { | ||
| const camera = new PerspectiveCamera(60, 1.5, 0.1, 100); | ||
| camera.position.set(3, 4, 5); | ||
| camera.lookAt(0, 0, 0); | ||
| camera.updateMatrixWorld(true); | ||
| camera.updateProjectionMatrix(); | ||
|
|
||
| // projection * view, inverted — the matrix events.ts unprojects through | ||
| const projViewInv = new Matrix4() | ||
| .multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse) | ||
| .invert(); | ||
|
|
||
| const ray = new Ray(); | ||
| // an off-centre pointer, where the old fixed origin was wrong | ||
| computeRay(ray, 0.6, -0.3, projViewInv); | ||
|
|
||
| // direction is unit length | ||
| expect(ray.direction.length()).toBeCloseTo(1, 6); | ||
|
|
||
| // eye is collinear with the ray: (eye - origin) is parallel to direction | ||
| const eye = camera.position.clone(); | ||
| const toEye = eye.clone().sub(ray.origin).normalize(); | ||
| expect(Math.abs(toEye.dot(ray.direction))).toBeCloseTo(1, 5); | ||
|
|
||
| // regression guard: the old origin (unprojected NDC centre) is NOT on the | ||
| // ray, so it produced a skewed ray that missed the eye. | ||
| const oldOrigin = new Vector3().setScalar(0).applyMatrix4(projViewInv); | ||
| const toEyeOld = eye.clone().sub(oldOrigin).normalize(); | ||
| expect(Math.abs(toEyeOld.dot(ray.direction))).toBeLessThan(0.999); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.