Browse Source

PointLight/SpotLight: Deprecate distance parameter.

Mr.doob 4 months ago
parent
commit
a72f57ea13
3 changed files with 73 additions and 27 deletions
  1. 36 15
      src/lights/PointLight.js
  2. 35 10
      src/lights/SpotLight.js
  3. 2 2
      src/loaders/ObjectLoader.js

+ 36 - 15
src/lights/PointLight.js

@@ -9,7 +9,7 @@ import { PointLightShadow } from './PointLightShadow.js';
  * This light can cast shadows - see the {@link PointLightShadow} for details.
  *
  * ```js
- * const light = new THREE.PointLight( 0xff0000, 1, 100 );
+ * const light = new THREE.PointLight( 0xff0000, 1 );
  * light.position.set( 50, 50, 50 );
  * scene.add( light );
  * ```
@@ -23,7 +23,7 @@ class PointLight extends Light {
 	 *
 	 * @param {(number|Color|string)} [color=0xffffff] - The light's color.
 	 * @param {number} [intensity=1] - The light's strength/intensity measured in candela (cd).
-	 * @param {number} [distance=0] - Maximum range of the light. `0` means no limit.
+	 * @param {number} [distance=0] - Deprecated. Distance is now computed from intensity and decay.
 	 * @param {number} [decay=2] - The amount the light dims along the distance of the light.
 	 */
 	constructor( color, intensity, distance = 0, decay = 2 ) {
@@ -41,17 +41,11 @@ class PointLight extends Light {
 
 		this.type = 'PointLight';
 
-		/**
-		 * When distance is zero, light will attenuate according to inverse-square
-		 * law to infinite distance. When distance is non-zero, light will attenuate
-		 * according to inverse-square law until near the distance cutoff, where it
-		 * will then attenuate quickly and smoothly to 0. Inherently, cutoffs are not
-		 * physically correct.
-		 *
-		 * @type {number}
-		 * @default 0
-		 */
-		this.distance = distance;
+		if ( distance > 0 ) {
+
+			console.warn( 'THREE.PointLight: "distance" is now computed from "intensity" and "decay".' );
+
+		}
 
 		/**
 		 * The amount the light dims along the distance of the light. In context of
@@ -92,6 +86,35 @@ class PointLight extends Light {
 
 	}
 
+	/**
+	 * The maximum range of the light, computed from intensity and decay.
+	 *
+	 * @type {number}
+	 */
+	get distance() {
+
+		// Compute effective distance from intensity and decay
+		// Distance where intensity drops to 1% (threshold = 0.01)
+		if ( this.decay > 0 ) {
+
+			return Math.pow( this.intensity / 0.01, 1 / this.decay );
+
+		}
+
+		return 0;
+
+	}
+
+	set distance( value ) {
+
+		if ( value > 0 ) {
+
+			console.warn( 'THREE.PointLight: "distance" is now computed from "intensity" and "decay".' );
+
+		}
+
+	}
+
 	dispose() {
 
 		super.dispose();
@@ -104,7 +127,6 @@ class PointLight extends Light {
 
 		super.copy( source, recursive );
 
-		this.distance = source.distance;
 		this.decay = source.decay;
 
 		this.shadow = source.shadow.clone();
@@ -117,7 +139,6 @@ class PointLight extends Light {
 
 		const data = super.toJSON( meta );
 
-		data.object.distance = this.distance;
 		data.object.decay = this.decay;
 
 		data.object.shadow = this.shadow.toJSON();

+ 35 - 10
src/lights/SpotLight.js

@@ -31,7 +31,7 @@ class SpotLight extends Light {
 	 *
 	 * @param {(number|Color|string)} [color=0xffffff] - The light's color.
 	 * @param {number} [intensity=1] - The light's strength/intensity measured in candela (cd).
-	 * @param {number} [distance=0] - Maximum range of the light. `0` means no limit.
+	 * @param {number} [distance=0] - Deprecated. Distance is now computed from intensity and decay.
 	 * @param {number} [angle=Math.PI/3] - Maximum angle of light dispersion from its direction whose upper bound is `Math.PI/2`.
 	 * @param {number} [penumbra=0] - Percent of the spotlight cone that is attenuated due to penumbra. Value range is `[0,1]`.
 	 * @param {number} [decay=2] - The amount the light dims along the distance of the light.
@@ -68,13 +68,11 @@ class SpotLight extends Light {
 		 */
 		this.target = new Object3D();
 
-		/**
-		 * Maximum range of the light. `0` means no limit.
-		 *
-		 * @type {number}
-		 * @default 0
-		 */
-		this.distance = distance;
+		if ( distance > 0 ) {
+
+			console.warn( 'THREE.SpotLight: "distance" is now computed from "intensity" and "decay".' );
+
+		}
 
 		/**
 		 * Maximum angle of light dispersion from its direction whose upper bound is `Math.PI/2`.
@@ -145,6 +143,35 @@ class SpotLight extends Light {
 
 	}
 
+	/**
+	 * The maximum range of the light, computed from intensity and decay.
+	 *
+	 * @type {number}
+	 */
+	get distance() {
+
+		// Compute effective distance from intensity and decay
+		// Distance where intensity drops to 1% (threshold = 0.01)
+		if ( this.decay > 0 ) {
+
+			return Math.pow( this.intensity / 0.01, 1 / this.decay );
+
+		}
+
+		return 0;
+
+	}
+
+	set distance( value ) {
+
+		if ( value > 0 ) {
+
+			console.warn( 'THREE.SpotLight: "distance" is now computed from "intensity" and "decay".' );
+
+		}
+
+	}
+
 	dispose() {
 
 		super.dispose();
@@ -157,7 +184,6 @@ class SpotLight extends Light {
 
 		super.copy( source, recursive );
 
-		this.distance = source.distance;
 		this.angle = source.angle;
 		this.penumbra = source.penumbra;
 		this.decay = source.decay;
@@ -174,7 +200,6 @@ class SpotLight extends Light {
 
 		const data = super.toJSON( meta );
 
-		data.object.distance = this.distance;
 		data.object.angle = this.angle;
 		data.object.decay = this.decay;
 		data.object.penumbra = this.penumbra;

+ 2 - 2
src/loaders/ObjectLoader.js

@@ -896,7 +896,7 @@ class ObjectLoader extends Loader {
 
 			case 'PointLight':
 
-				object = new PointLight( data.color, data.intensity, data.distance, data.decay );
+				object = new PointLight( data.color, data.intensity, 0, data.decay );
 
 				break;
 
@@ -908,7 +908,7 @@ class ObjectLoader extends Loader {
 
 			case 'SpotLight':
 
-				object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
+				object = new SpotLight( data.color, data.intensity, 0, data.angle, data.penumbra, data.decay );
 				object.target = data.target || '';
 
 				break;

粤ICP备19079148号