Quellcode durchsuchen

Merge pull request #17118 from gkjohnson/raycaster-camera-suggestion

Suggestion: Expose and document Raycaster.camera
Michael Herzog vor 6 Jahren
Ursprung
Commit
5d68fa63b3
4 geänderte Dateien mit 26 neuen und 5 gelöschten Zeilen
  1. 8 0
      docs/api/en/core/Raycaster.html
  2. 6 0
      src/core/Raycaster.d.ts
  3. 3 2
      src/core/Raycaster.js
  4. 9 3
      src/objects/Sprite.js

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

@@ -103,6 +103,14 @@
 		This value shouldn't be negative and should be smaller than the far property.
 		</p>
 
+		<h3>[property:Camera camera]</h3>
+		<p>
+		The camera to use when raycasting against view-dependent objects such as billboarded objects like [page:Sprites]. This field
+		can be set manually or is set when calling "setFromCamera".
+
+		Defaults to null.
+		</p>
+
 		<h3>[property:Object params]</h3>
 		<p>
 		An object with the following properties:

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

@@ -55,6 +55,12 @@ export class Raycaster {
 	 */
 	far: number;
 
+	/**
+	 * The camera to use when raycasting against view-dependent objects such as billboarded objects like Sprites. This field
+	 * can be set manually or is set when calling "setFromCamera".
+	 */
+	camera: Camera;
+
 	params: RaycasterParameters;
 
 	/**

+ 3 - 2
src/core/Raycaster.js

@@ -13,6 +13,7 @@ function Raycaster( origin, direction, near, far ) {
 
 	this.near = near || 0;
 	this.far = far || Infinity;
+	this.camera = null;
 
 	this.params = {
 		Mesh: {},
@@ -79,13 +80,13 @@ Object.assign( Raycaster.prototype, {
 
 			this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
 			this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
-			this._camera = camera;
+			this.camera = camera;
 
 		} else if ( ( camera && 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.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
-			this._camera = camera;
+			this.camera = camera;
 
 		} else {
 

+ 9 - 3
src/objects/Sprite.js

@@ -60,6 +60,12 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	raycast: function ( raycaster, intersects ) {
 
+		if ( raycaster.camera === null ) {
+
+			console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
+
+		}
+
 		if ( _uvC === undefined ) {
 
 			_intersectPoint = new Vector3();
@@ -82,12 +88,12 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 		_worldScale.setFromMatrixScale( this.matrixWorld );
 
-		_viewWorldMatrix.copy( raycaster._camera.matrixWorld );
-		this.modelViewMatrix.multiplyMatrices( raycaster._camera.matrixWorldInverse, this.matrixWorld );
+		_viewWorldMatrix.copy( raycaster.camera.matrixWorld );
+		this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
 
 		_mvPosition.setFromMatrixPosition( this.modelViewMatrix );
 
-		if ( raycaster._camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
+		if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
 
 			_worldScale.multiplyScalar( - _mvPosition.z );