Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/lights/AmbientLight.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import { Light } from './Light.js';

/**
* This light globally illuminates all objects in the scene equally.
*
* It cannot be used to cast shadows as it does not have a direction.
*
* ```js
* const light = new THREE.AmbientLight( 0x404040 ); // soft white light
* scene.add( light );
* ```
*
* @augments Light
*/
class AmbientLight extends Light {

/**
* Constructs a new ambient light.
*
* @param {(number|Color|string)} [color=0xffffff] - The light's color.
* @param {number} [intensity=1] - The light's strength/intensity.
*/
constructor( color, intensity ) {

super( color, intensity );

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isAmbientLight = true;

this.type = 'AmbientLight';
Expand Down
57 changes: 57 additions & 0 deletions src/lights/DirectionalLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,78 @@ import { Light } from './Light.js';
import { DirectionalLightShadow } from './DirectionalLightShadow.js';
import { Object3D } from '../core/Object3D.js';

/**
* A light that gets emitted in a specific direction. This light will behave
* as though it is infinitely far away and the rays produced from it are all
* parallel. The common use case for this is to simulate daylight; the sun is
* far enough away that its position can be considered to be infinite, and
* all light rays coming from it are parallel.
*
* A common point of confusion for directional lights is that setting the
* rotation has no effect. This is because three.js's DirectionalLight is the
* equivalent to what is often called a 'Target Direct Light' in other
* applications.
*
* This means that its direction is calculated as pointing from the light's
* {@link Object3D#position} to the {@link DirectionalLight#target} position
* (as opposed to a 'Free Direct Light' that just has a rotation
* component).
*
* This light can cast shadows - see the {@link DirectionalLightShadow} for details.
*
* ```js
* // White directional light at half intensity shining from the top.
* const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
* scene.add( directionalLight );
* ```
*
* @augments Light
*/
class DirectionalLight extends Light {

/**
* Constructs a new directional light.
*
* @param {(number|Color|string)} [color=0xffffff] - The light's color.
* @param {number} [intensity=1] - The light's strength/intensity.
*/
constructor( color, intensity ) {

super( color, intensity );

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isDirectionalLight = true;

this.type = 'DirectionalLight';

this.position.copy( Object3D.DEFAULT_UP );
this.updateMatrix();

/**
* The directional light points from its position to the
* target's position.
*
* For the target's position to be changed to anything other
* than the default, it must be added to the scene.
*
* It is also possible to set the target to be another 3D object
* in the scene. The light will now track the target object.
*
* @type {Object3D}
*/
this.target = new Object3D();

/**
* This property holds the light's shadow configuration.
*
* @type {DirectionalLightShadow}
*/
this.shadow = new DirectionalLightShadow();

}
Expand Down
15 changes: 15 additions & 0 deletions src/lights/DirectionalLightShadow.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { LightShadow } from './LightShadow.js';
import { OrthographicCamera } from '../cameras/OrthographicCamera.js';

/**
* Represents the shadow configuration of directional lights.
*
* @augments LightShadow
*/
class DirectionalLightShadow extends LightShadow {

/**
* Constructs a new directional light shadow.
*/
constructor() {

super( new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isDirectionalLightShadow = true;

}
Expand Down
32 changes: 32 additions & 0 deletions src/lights/HemisphereLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,51 @@ import { Light } from './Light.js';
import { Color } from '../math/Color.js';
import { Object3D } from '../core/Object3D.js';

/**
* A light source positioned directly above the scene, with color fading from
* the sky color to the ground color.
*
* This light cannot be used to cast shadows.
*
* ```js
* const light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
* scene.add( light );
* ```
*
* @augments Light
*/
class HemisphereLight extends Light {

/**
* Constructs a new hemisphere light.
*
* @param {(number|Color|string)} [skyColor=0xffffff] - The light's sky color.
* @param {(number|Color|string)} [groundColor=0xffffff] - The light's ground color.
* @param {number} [intensity=1] - The light's strength/intensity.
*/
constructor( skyColor, groundColor, intensity ) {

super( skyColor, intensity );

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isHemisphereLight = true;

this.type = 'HemisphereLight';

this.position.copy( Object3D.DEFAULT_UP );
this.updateMatrix();

/**
* The light's ground color.
*
* @type {Color}
*/
this.groundColor = new Color( groundColor );

}
Expand Down
36 changes: 36 additions & 0 deletions src/lights/Light.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
import { Object3D } from '../core/Object3D.js';
import { Color } from '../math/Color.js';

/**
* Abstract base class for lights - all other light types inherit the
* properties and methods described here.
*
* @abstract
* @augments Object3D
*/
class Light extends Object3D {

/**
* Constructs a new light.
*
* @param {(number|Color|string)} [color=0xffffff] - The light's color.
* @param {number} [intensity=1] - The light's strength/intensity.
*/
constructor( color, intensity = 1 ) {

super();

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isLight = true;

this.type = 'Light';

/**
* The light's color.
*
* @type {Color}
*/
this.color = new Color( color );

/**
* The light's intensity.
*
* @type {number}
* @default 1
*/
this.intensity = intensity;

}

/**
* Frees the GPU-related resources allocated by this instance. Call this
* method whenever this instance is no longer used in your app.
*/
dispose() {

// Empty here in base class; some subclasses override.
Expand Down
43 changes: 43 additions & 0 deletions src/lights/LightProbe.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
import { SphericalHarmonics3 } from '../math/SphericalHarmonics3.js';
import { Light } from './Light.js';

/**
* Light probes are an alternative way of adding light to a 3D scene. Unlike
* classical light sources (e.g. directional, point or spot lights), light
* probes do not emit light. Instead they store information about light
* passing through 3D space. During rendering, the light that hits a 3D
* object is approximated by using the data from the light probe.
*
* Light probes are usually created from (radiance) environment maps. The
* class {@link LightProbeGenerator} can be used to create light probes from
* cube textures or render targets. However, light estimation data could also
* be provided in other forms e.g. by WebXR. This enables the rendering of
* augmented reality content that reacts to real world lighting.
*
* The current probe implementation in three.js supports so-called diffuse
* light probes. This type of light probe is functionally equivalent to an
* irradiance environment map.
*
* @augments Light
*/
class LightProbe extends Light {

/**
* Constructs a new light probe.
*
* @param {SphericalHarmonics3} sh - The spherical harmonics which represents encoded lighting information.
* @param {number} [intensity=1] - The light's strength/intensity.
*/
constructor( sh = new SphericalHarmonics3(), intensity = 1 ) {

super( undefined, intensity );

/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isLightProbe = true;

/**
* A light probe uses spherical harmonics to encode lighting information.
*
* @type {SphericalHarmonics3}
*/
this.sh = sh;

}
Expand All @@ -23,6 +60,12 @@ class LightProbe extends Light {

}

/**
* Deserializes the light prove from the given JSON.
*
* @param {Object} json - The JSON holding the serialized light probe.
* @return {LightProbe} A reference to this light probe.
*/
fromJSON( json ) {

this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON();
Expand Down
Loading
Loading