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: Use THREE.Layers in intersectObject(). #18694

Merged
merged 1 commit into from Feb 20, 2020
Merged
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
3 changes: 2 additions & 1 deletion docs/api/en/core/Object3D.html
Expand Up @@ -61,7 +61,8 @@ <h3>[property:Integer id]</h3>
<h3>[property:Layers layers]</h3>
<p>
The layer membership of the object. The object is only visible if it has at least one
layer in common with the [page:Camera] in use.
layer in common with the [page:Camera] in use. This property can also be used to filter out
unwanted objects in ray-intersection tests when using [page:Raycaster].
</p>

<h3>[property:Matrix4 matrix]</h3>
Expand Down
12 changes: 12 additions & 0 deletions docs/api/en/core/Raycaster.html
Expand Up @@ -107,6 +107,18 @@ <h3>[property:Camera camera]</h3>
Defaults to null.
</p>

<h3>[property:Layers layers]</h3>
<p>
Used by [name] to selectively ignore 3D objects when performing intersection tests. The following code example ensures that
only 3D objects on layer *1* will be honored by the instance of [name].

<code>
raycaster.layers.set( 1 );
object.layers.enable( 1 );
</code>

</p>

<h3>[property:Object params]</h3>
<p>
An object with the following properties:
Expand Down
5 changes: 3 additions & 2 deletions docs/api/zh/core/Object3D.html
Expand Up @@ -59,7 +59,8 @@ <h3>[property:Integer id]</h3>
<h3>[property:Layers layers]</h3>
<p>
物体的层级关系。
物体只有和一个正在使用的[page:Camera]至少在同一个层时才可见。
物体只有和一个正在使用的[page:Camera]至少在同一个层时才可见。This property can also be used to filter out
unwanted objects in ray-intersection tests when using [page:Raycaster].
</p>

<h3>[property:Matrix4 matrix]</h3>
Expand Down Expand Up @@ -199,7 +200,7 @@ <h3>[method:null applyMatrix4]( [param:Matrix4 matrix] )</h3>
<h3>[method:Object3D applyQuaternion]( [param:Quaternion quaternion] )</h3>
<p>对当前物体应用由四元数所表示的变换。</p>


<h3>[method:this attach]( [param:Object3D object] )</h3>
<p>将*object*作为子级来添加到该对象中,同时保持该object的世界变换。</p>

Expand Down
12 changes: 12 additions & 0 deletions docs/api/zh/core/Raycaster.html
Expand Up @@ -105,6 +105,18 @@ <h3>[property:Camera camera]</h3>
Defaults to null.
</p>

<h3>[property:Layers layers]</h3>
<p>
Used by [name] to selectively ignore 3D objects when performing intersection tests. The following code example ensures that
only 3D objects on layer *1* will be honored by the instance of [name].

<code>
raycaster.layers.set( 1 );
object.layers.enable( 1 );
</code>

</p>

<h3>[property:Object params]</h3>
<p>
具有以下属性的对象:<code>
Expand Down
6 changes: 6 additions & 0 deletions src/core/Raycaster.d.ts
Expand Up @@ -4,6 +4,7 @@ import { Object3D } from './Object3D';
import { Vector2 } from './../math/Vector2';
import { Ray } from './../math/Ray';
import { Camera } from './../cameras/Camera';
import { Layers } from './Layers';

export interface Intersection {
distance: number;
Expand Down Expand Up @@ -62,6 +63,11 @@ export class Raycaster {
*/
camera: Camera;

/**
* Used by Raycaster to selectively ignore 3D objects when performing intersection tests.
*/
layers: Layers;

params: RaycasterParameters;

/**
Expand Down
8 changes: 6 additions & 2 deletions src/core/Raycaster.js
@@ -1,4 +1,5 @@
import { Ray } from '../math/Ray.js';
import { Layers } from './Layers.js';

/**
* @author mrdoob / http://mrdoob.com/
Expand All @@ -14,6 +15,7 @@ function Raycaster( origin, direction, near, far ) {
this.near = near || 0;
this.far = far || Infinity;
this.camera = null;
this.layers = new Layers();

this.params = {
Mesh: {},
Expand Down Expand Up @@ -44,9 +46,11 @@ function ascSort( a, b ) {

function intersectObject( object, raycaster, intersects, recursive ) {

if ( object.visible === false ) return;
if ( object.layers.test( raycaster.layers ) ) {

object.raycast( raycaster, intersects );
object.raycast( raycaster, intersects );

}

if ( recursive === true ) {

Expand Down