浏览代码

Merge pull request #18694 from Mugen87/dev41

Raycaster: Use THREE.Layers in intersectObject().
Mr.doob 5 年之前
父节点
当前提交
e04c846ba7

+ 2 - 1
docs/api/en/core/Object3D.html

@@ -61,7 +61,8 @@
 		<h3>[property:Layers layers]</h3>
 		<h3>[property:Layers layers]</h3>
 		<p>
 		<p>
 		The layer membership of the object. The object is only visible if it has at least one
 		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>
 		</p>
 
 
 		<h3>[property:Matrix4 matrix]</h3>
 		<h3>[property:Matrix4 matrix]</h3>

+ 12 - 0
docs/api/en/core/Raycaster.html

@@ -107,6 +107,18 @@
 		Defaults to null.
 		Defaults to null.
 		</p>
 		</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>
 		<h3>[property:Object params]</h3>
 		<p>
 		<p>
 		An object with the following properties:
 		An object with the following properties:

+ 3 - 2
docs/api/zh/core/Object3D.html

@@ -59,7 +59,8 @@
 	<h3>[property:Layers layers]</h3>
 	<h3>[property:Layers layers]</h3>
 	<p>
 	<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>
 	</p>
 
 
 	<h3>[property:Matrix4 matrix]</h3>
 	<h3>[property:Matrix4 matrix]</h3>
@@ -199,7 +200,7 @@
 	<h3>[method:Object3D applyQuaternion]( [param:Quaternion quaternion] )</h3>
 	<h3>[method:Object3D applyQuaternion]( [param:Quaternion quaternion] )</h3>
 	<p>对当前物体应用由四元数所表示的变换。</p>
 	<p>对当前物体应用由四元数所表示的变换。</p>
 
 
-	
+
 	<h3>[method:this attach]( [param:Object3D object] )</h3>
 	<h3>[method:this attach]( [param:Object3D object] )</h3>
 	<p>将*object*作为子级来添加到该对象中,同时保持该object的世界变换。</p>
 	<p>将*object*作为子级来添加到该对象中,同时保持该object的世界变换。</p>
 
 

+ 12 - 0
docs/api/zh/core/Raycaster.html

@@ -105,6 +105,18 @@
 		Defaults to null.
 		Defaults to null.
 		</p>
 		</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>
 		<h3>[property:Object params]</h3>
 		<p>
 		<p>
 			具有以下属性的对象:<code>
 			具有以下属性的对象:<code>

+ 6 - 0
src/core/Raycaster.d.ts

@@ -4,6 +4,7 @@ import { Object3D } from './Object3D';
 import { Vector2 } from './../math/Vector2';
 import { Vector2 } from './../math/Vector2';
 import { Ray } from './../math/Ray';
 import { Ray } from './../math/Ray';
 import { Camera } from './../cameras/Camera';
 import { Camera } from './../cameras/Camera';
+import { Layers } from './Layers';
 
 
 export interface Intersection {
 export interface Intersection {
 	distance: number;
 	distance: number;
@@ -62,6 +63,11 @@ export class Raycaster {
 	 */
 	 */
 	camera: Camera;
 	camera: Camera;
 
 
+	/**
+	 * Used by Raycaster to selectively ignore 3D objects when performing intersection tests.
+	 */
+	layers: Layers;
+
 	params: RaycasterParameters;
 	params: RaycasterParameters;
 
 
 	/**
 	/**

+ 6 - 2
src/core/Raycaster.js

@@ -1,4 +1,5 @@
 import { Ray } from '../math/Ray.js';
 import { Ray } from '../math/Ray.js';
+import { Layers } from './Layers.js';
 
 
 /**
 /**
  * @author mrdoob / http://mrdoob.com/
  * @author mrdoob / http://mrdoob.com/
@@ -14,6 +15,7 @@ function Raycaster( origin, direction, near, far ) {
 	this.near = near || 0;
 	this.near = near || 0;
 	this.far = far || Infinity;
 	this.far = far || Infinity;
 	this.camera = null;
 	this.camera = null;
+	this.layers = new Layers();
 
 
 	this.params = {
 	this.params = {
 		Mesh: {},
 		Mesh: {},
@@ -44,9 +46,11 @@ function ascSort( a, b ) {
 
 
 function intersectObject( object, raycaster, intersects, recursive ) {
 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 ) {
 	if ( recursive === true ) {