Skip to content
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

Raycaster.setFromCamera: Position the ray origin such that it is on the camera near plane #28027

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/Raycaster.js
Expand Up @@ -38,13 +38,13 @@ class Raycaster {

if ( camera.isPerspectiveCamera ) {

this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
this.ray.origin.set( coords.x, coords.y, - 1 ).unproject( camera );
this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
this.camera = camera;

} else if ( camera.isOrthographicCamera ) {

this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
this.ray.origin.set( coords.x, coords.y, - 1 ).unproject( camera );
this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
this.camera = camera;

Expand Down
10 changes: 7 additions & 3 deletions test/unit/src/core/Raycaster.tests.js
Expand Up @@ -12,8 +12,12 @@ import { OrthographicCamera } from '../../../../src/cameras/OrthographicCamera.j

function checkRayDirectionAgainstReferenceVector( rayDirection, refVector, assert ) {

assert.ok( refVector.x - rayDirection.x <= Number.EPSILON && refVector.y - rayDirection.y <= Number.EPSILON && refVector.z - rayDirection.z <= Number.EPSILON, 'camera is pointing to' +
' the same direction as expected' );
assert.ok(
refVector.x - rayDirection.x <= Number.EPSILON &&
refVector.y - rayDirection.y <= Number.EPSILON &&
refVector.z - rayDirection.z <= Number.EPSILON,
'camera is pointing to the same direction as expected'
);

}

Expand Down Expand Up @@ -141,7 +145,7 @@ export default QUnit.module( 'Core', () => {

const raycaster = new Raycaster();
const rayDirection = raycaster.ray.direction;
const camera = new PerspectiveCamera( 90, 1, 1, 1000 );
const camera = new PerspectiveCamera( 90, 1, 0.1, 1000 );
Comment on lines -144 to +148
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the ray direction was being calculated with the "origin" being calculated as "0, 0, 0" which is used to calculate a delta vector in the setFromCamera function. With the change in this PR the origin is at the near plane which is 1 unit from the camera origin resulting in a higher epsilon which Number.EPSILON was not high enough to test for. Lowering the near value lowers the values used in the origin vector and therefore the error in the calculations. From the MDN page on Number.EPSILON:

The Number.EPSILON static data property represents the difference between 1 and the smallest floating point number greater than 1.


raycaster.setFromCamera( {
x: 0,
Expand Down