Engine API Reference - v2.20.0-beta.0
    Preparing search index...

    Class LightComponent

    The LightComponent enables an Entity to light the scene. There are three types of light:

    • directional: A global light that emits light in the direction of the negative y-axis of the owner entity. Emulates light sources that appear to be infinitely far away such as the sun. The owner entity's position is effectively ignored.
    • omni: A local light that emits light in all directions from the owner entity's position. Emulates candles, lamps, bulbs, etc.
    • spot: A local light that emits light similarly to an omni light but is bounded by a cone centered on the owner entity's negative y-axis. Emulates flashlights, spotlights, etc.

    You should never need to use the LightComponent constructor directly. To add a LightComponent to an Entity, use Entity#addComponent:

    const entity = new pc.Entity();
    entity.addComponent('light', {
    type: 'omni',
    color: new pc.Color(1, 0, 0),
    intensity: 2
    });

    Once the LightComponent is added to the entity, you can access it via the Entity#light property:

    entity.light.intensity = 3; // Set the intensity of the light

    console.log(entity.light.intensity); // Get the intensity of the light

    Relevant Engine API examples:

    Hierarchy (View Summary)

    Index

    Properties

    entity: Entity

    The Entity that this Component is attached to.

    The ComponentSystem used to create this Component.

    Accessors

    • get affectDynamic(): boolean

      Gets whether the light will affect non-lightmapped objects.

      Returns boolean

    • set affectDynamic(value: boolean): void

      Sets whether the light will affect non-lightmapped objects. Toggles the MASK_AFFECT_DYNAMIC bit on mask. Defaults to true.

      Parameters

      • value: boolean

      Returns void

    • get affectLightmapped(): boolean

      Gets whether the light will affect lightmapped objects.

      Returns boolean

    • set affectLightmapped(value: boolean): void

      Sets whether the light will affect lightmapped objects. Toggles the MASK_AFFECT_LIGHTMAPPED bit on mask. Mutually exclusive with bake on the mask: enabling one clears the other's mask bit. Defaults to false.

      Parameters

      • value: boolean

      Returns void

    • get affectSpecularity(): boolean

      Gets whether material specularity will be affected by this light.

      Returns boolean

    • set affectSpecularity(value: boolean): void

      Sets whether material specularity will be affected by this light. Only takes effect when type is "directional"; for other types the value is preserved on the component and applied if type later becomes "directional". Defaults to true.

      Parameters

      • value: boolean

      Returns void

    • get bake(): boolean

      Gets whether the light will be rendered into lightmaps.

      Returns boolean

    • set bake(value: boolean): void

      Sets whether the light will be rendered into lightmaps. Toggles the MASK_BAKE bit on mask. Mutually exclusive with affectLightmapped on the mask: enabling one clears the other's mask bit. Defaults to false.

      Parameters

      • value: boolean

      Returns void

    • get bakeArea(): number

      Gets the angular size in degrees of the area used when baking soft shadow boundaries for the directional light into the lightmap.

      Returns number

    • set bakeArea(value: number): void

      Sets the angular size in degrees of the area used when baking soft shadow boundaries for the directional light into the lightmap. Range is 0 to 180. Requires bake to be set to true and type to be "directional". Defaults to 0.

      Parameters

      • value: number

      Returns void

    • get bakeDir(): boolean

      Gets whether the light's direction will contribute to directional lightmaps.

      Returns boolean

    • set bakeDir(value: boolean): void

      Sets whether the light's direction will contribute to directional lightmaps. The light must be enabled and bake set to true. Be aware that the directional lightmap is an approximation and can only hold a single direction per pixel. Intersecting multiple lights with bakeDir set to true may lead to incorrect-looking specular/bump mapping in the area of intersection. The error is not always visible though, and is highly scene-dependent. Defaults to true.

      Parameters

      • value: boolean

      Returns void

    • get bakeNumSamples(): number

      Gets the number of samples used to bake this light into the lightmap.

      Returns number

    • set bakeNumSamples(value: number): void

      Sets the number of samples used to bake this light into the lightmap. Defaults to 1. Maximum value is 255.

      Parameters

      • value: number

      Returns void

    • get cascadeBlend(): number

      Gets the blend factor for cascaded shadow maps.

      Returns number

    • set cascadeBlend(value: number): void

      Sets the blend factor for cascaded shadow maps, defining the fraction of each cascade level used for blending between adjacent cascades. The value should be between 0 and 1. Defaults to 0, which disables blending between cascades.

      Parameters

      • value: number

      Returns void

    • get cascadeDistribution(): number

      Gets the distribution of subdivision of the camera frustum for individual shadow cascades.

      Returns number

    • set cascadeDistribution(value: number): void

      Sets the distribution of subdivision of the camera frustum for individual shadow cascades. Only used if numCascades is larger than 1. Can be a value in range of 0 and 1. Value of 0 represents a linear distribution, value of 1 represents a logarithmic distribution. Defaults to 0.5. Larger value increases the resolution of the shadows in the near distance.

      Parameters

      • value: number

      Returns void

    • get castShadows(): boolean

      Gets whether the light will cast shadows.

      Returns boolean

    • set castShadows(value: boolean): void

      Sets whether the light will cast shadows. Defaults to false.

      Parameters

      • value: boolean

      Returns void

    • get color(): Color

      Gets the color of the light.

      Returns Color

    • set color(value: Color): void

      Sets the color of the light in sRGB space. The alpha component of the color is ignored. Defaults to white ([1, 1, 1]).

      Parameters

      Returns void

    • get cookie(): Texture | null

      Gets the texture to be used as the cookie for this light.

      Returns Texture | null

    • set cookie(value: Texture | null): void

      Sets the texture to be used as the cookie for this light. Only spot and omni lights can have cookies. Spot lights expect a 2D texture; omni lights expect a cubemap. Defaults to null.

      Parameters

      Returns void

    • get cookieAngle(): number

      Gets the angle for spotlight cookie rotation (in degrees).

      Returns number

    • set cookieAngle(value: number): void

      Sets the angle for spotlight cookie rotation in degrees. Defaults to 0.

      Parameters

      • value: number

      Returns void

    • get cookieAsset(): number | null

      Gets the id of the texture asset used as the cookie for this light, or null if none is set.

      Returns number | null

    • set cookieAsset(value: number | null): void

      Sets the id of the texture asset to be used as the cookie for this light. Only spot and omni lights can have cookies. Spot lights expect a 2D texture; omni lights expect a cubemap. Defaults to null.

      Parameters

      • value: number | null

      Returns void

    • get cookieChannel(): string

      Gets the color channels of the cookie texture to use.

      Returns string

    • set cookieChannel(value: string): void

      Sets the color channels of the cookie texture to use. Can be "r", "g", "b", "a" or "rgb". Defaults to "rgb".

      Parameters

      • value: string

      Returns void

    • get cookieFalloff(): boolean

      Gets whether normal spotlight falloff is active when a cookie texture is set.

      Returns boolean

    • set cookieFalloff(value: boolean): void

      Sets whether normal spotlight falloff is active when a cookie texture is set. When set to false, a spotlight will work like a pure texture projector (only fading with distance). Defaults to true.

      Parameters

      • value: boolean

      Returns void

    • get cookieIntensity(): number

      Gets the cookie texture intensity.

      Returns number

    • set cookieIntensity(value: number): void

      Sets the cookie texture intensity. Defaults to 1.

      Parameters

      • value: number

      Returns void

    • get cookieOffset(): Vec2 | null

      Gets the spotlight cookie position offset.

      Returns Vec2 | null

    • set cookieOffset(value: Vec2 | null): void

      Sets the spotlight cookie position offset. Defaults to null.

      Parameters

      Returns void

    • get cookieScale(): Vec2 | null

      Gets the spotlight cookie scale.

      Returns Vec2 | null

    • set cookieScale(value: Vec2 | null): void

      Sets the spotlight cookie scale. Set to null to use no scaling. Defaults to null.

      Parameters

      Returns void

    • get enabled(): boolean

      Gets the enabled state of the component.

      Returns boolean

    • set enabled(arg: boolean): void

      Sets the enabled state of the component.

      Parameters

      • arg: boolean

      Returns void

    • get falloffMode(): number

      Gets the fall off mode for the light.

      Returns number

    • set falloffMode(value: number): void

      Sets the fall off mode for the light. This controls the rate at which a light attenuates from its position. Can be:

      Affects omni and spot lights only. Defaults to LIGHTFALLOFF_LINEAR.

      Parameters

      • value: number

      Returns void

    • get innerConeAngle(): number

      Gets the half-angle (measured in degrees from the light's direction axis to the cone edge) at which the spotlight cone starts to fade off.

      Returns number

    • set innerConeAngle(value: number): void

      Sets the half-angle (measured in degrees from the light's direction axis to the cone edge) at which the spotlight cone starts to fade off. The full inner beam angle is twice this value. Affects spot lights only. Defaults to 40 (i.e. an 80-degree full inner beam).

      Parameters

      • value: number

      Returns void

    • get intensity(): number

      Gets the brightness of the light.

      Returns number

    • set intensity(value: number): void

      Sets the brightness of the light. Defaults to 1.

      Parameters

      • value: number

      Returns void

    • get isStatic(): boolean

      Gets whether the light ever moves.

      Returns boolean

    • set isStatic(value: boolean): void

      Sets whether the light ever moves. This is an optimization hint. Defaults to false.

      Parameters

      • value: boolean

      Returns void

    • get layers(): number[]

      Gets the array of layer IDs (Layer#id) to which this light should belong.

      Returns number[]

    • set layers(value: number[]): void

      Sets the array of layer IDs (Layer#id) to which this light should belong. Don't push/pop/splice or modify this array. If you want to change it, set a new one instead. Defaults to [LAYERID_WORLD].

      Parameters

      • value: number[]

      Returns void

    • get luminance(): number

      Gets the physically-based luminance.

      Returns number

    • set luminance(value: number): void

      Sets the physically-based luminance. Only used if scene.physicalUnits is true. Defaults to 0.

      Parameters

      • value: number

      Returns void

    • get mask(): number

      Gets the mask to determine which MeshInstances are lit by this light.

      Returns number

    • set mask(value: number): void

      Sets the bitmask that determines which MeshInstances are lit by this light. The value is composed from MASK_AFFECT_DYNAMIC, MASK_AFFECT_LIGHTMAPPED and MASK_BAKE. The affectDynamic, affectLightmapped and bake helpers write to the same underlying mask but maintain their own state and are not recomputed from mask, so writing mask directly will not update those helpers (and a subsequent write to a helper may overwrite bits set via mask). Defaults to MASK_AFFECT_DYNAMIC.

      Parameters

      • value: number

      Returns void

    • get normalOffsetBias(): number

      Gets the normal offset depth bias.

      Returns number

    • set normalOffsetBias(value: number): void

      Sets the normal offset depth bias. Valid range is 0 to 1. Defaults to 0.

      Parameters

      • value: number

      Returns void

    • get numCascades(): number

      Gets the number of shadow cascades.

      Returns number

    • set numCascades(value: number): void

      Sets the number of shadow cascades. Can be 1, 2, 3 or 4. Defaults to 1, representing no cascades.

      Parameters

      • value: number

      Returns void

    • get outerConeAngle(): number

      Gets the half-angle (measured in degrees from the light's direction axis to the cone edge) at which the spotlight cone has faded to nothing.

      Returns number

    • set outerConeAngle(value: number): void

      Sets the half-angle (measured in degrees from the light's direction axis to the cone edge) at which the spotlight cone has faded to nothing. The full outer beam angle is twice this value. Affects spot lights only. Defaults to 45 (i.e. a 90-degree full outer beam).

      Parameters

      • value: number

      Returns void

    • get penumbraFalloff(): number

      Gets the falloff rate for shadow penumbra for contact hardening shadows.

      Returns number

    • set penumbraFalloff(value: number): void

      Sets the falloff rate for shadow penumbra for contact hardening shadows. This is a value larger than or equal to 1. This parameter determines how quickly the shadow softens with distance. Higher values result in a faster softening of the shadow, while lower values produce a more gradual transition. Defaults to 1.

      Parameters

      • value: number

      Returns void

    • get penumbraSize(): number

      Gets the size of penumbra for contact hardening shadows.

      Returns number

    • set penumbraSize(value: number): void

      Sets the size of penumbra for contact hardening shadows. For area lights, acts as a multiplier with the dimensions of the area light. For punctual and directional lights it's the area size of the light. Defaults to 1.

      Parameters

      • value: number

      Returns void

    • get range(): number

      Gets the range of the light.

      Returns number

    • set range(value: number): void

      Sets the range of the light. Affects omni and spot lights only. Defaults to 10.

      Parameters

      • value: number

      Returns void

    • get shadowBias(): number

      Get the depth bias for tuning the appearance of the shadow mapping generated by this light.

      Returns number

    • set shadowBias(value: number): void

      Set the depth bias for tuning the appearance of the shadow mapping generated by this light. Valid range is 0 to 1. Defaults to 0.05.

      Parameters

      • value: number

      Returns void

    • get shadowBlockerSamples(): number

      Gets the number of blocker samples used for contact hardening shadows.

      Returns number

    • set shadowBlockerSamples(value: number): void

      Sets the number of blocker samples used for soft shadows when the shadow type is SHADOW_PCSS_32F. These samples are used to estimate the distance between the shadow caster and the shadow receiver, which is then used for the estimation of contact hardening in the shadow. This value should be a non-negative whole number. Higher values improve shadow quality by considering more occlusion points, but can decrease performance. When set to 0, contact hardening is disabled and the shadow has constant softness. Defaults to 16. Note that this value can be lower than shadowSamples to optimize performance, often without large impact on quality.

      Parameters

      • value: number

      Returns void

    • get shadowDistance(): number

      Gets the distance from the viewpoint beyond which shadows are no longer rendered.

      Returns number

    • set shadowDistance(value: number): void

      Sets the distance from the viewpoint beyond which shadows are no longer rendered. Affects directional lights only. Defaults to 40.

      Parameters

      • value: number

      Returns void

    • get shadowIntensity(): number

      Gets the intensity of the shadow darkening.

      Returns number

    • set shadowIntensity(value: number): void

      Sets the intensity of the shadow darkening. 0 having no effect and 1 meaning shadows are entirely black. Defaults to 1.

      Parameters

      • value: number

      Returns void

    • get shadowResolution(): number

      Gets the size of the texture used for the shadow map.

      Returns number

    • set shadowResolution(value: number): void

      Sets the size of the texture used for the shadow map. Valid sizes are 64, 128, 256, 512, 1024, 2048. Defaults to 1024.

      Parameters

      • value: number

      Returns void

    • get shadowSamples(): number

      Gets the number of shadow samples used for soft shadows.

      Returns number

    • set shadowSamples(value: number): void

      Sets the number of shadow samples used for soft shadows when the shadow type is SHADOW_PCSS_32F. This value should be a positive whole number starting at 1. Higher values result in smoother shadows but can significantly decrease performance. Defaults to 16.

      Parameters

      • value: number

      Returns void

    • get shadowUpdateMode(): number

      Gets the shadow update mode.

      Returns number

    • set shadowUpdateMode(value: number): void

      Sets the shadow update mode. This tells the renderer how often shadows must be updated for this light. Can be:

      Defaults to SHADOWUPDATE_REALTIME.

      Parameters

      • value: number

      Returns void

    • get shadowUpdateOverrides(): number[] | null

      Gets an array of SHADOWUPDATE_ settings per shadow cascade.

      Returns number[] | null

    • set shadowUpdateOverrides(values: number[] | null): void

      Sets an array of SHADOWUPDATE_ settings per shadow cascade. Set to null if not used. Defaults to null.

      Parameters

      • values: number[] | null

      Returns void

    • get shape(): number

      Gets the light source shape.

      Returns number

    • set shape(value: number): void

      Sets the light source shape. Can be:

      Defaults to LIGHTSHAPE_PUNCTUAL.

      Parameters

      • value: number

      Returns void

    • get type(): string

      Gets the type of the light.

      Returns string

    • set type(value: string): void

      Sets the type of the light. Can be:

      • "directional": A global light that emits light in the direction of the negative y-axis of the owner entity.
      • "omni": A local light that emits light in all directions from the owner entity's position.
      • "spot": A local light that emits light similarly to an omni light but is bounded by a cone centered on the owner entity's negative y-axis.

      Defaults to "directional".

      Parameters

      • value: string

      Returns void

    • get vsmBias(): number

      Gets the VSM bias value.

      Returns number

    • set vsmBias(value: number): void

      Sets the bias used to fight shadow acne when rendering variance shadow maps. Range is 0 to

      1. Defaults to 0.0025.

      Parameters

      • value: number

      Returns void

    • get vsmBlurMode(): number

      Gets the blurring mode for variance shadow maps.

      Returns number

    • set vsmBlurMode(value: number): void

      Sets the blurring mode for variance shadow maps. Can be:

      • BLUR_BOX: Box filter.
      • BLUR_GAUSSIAN: Gaussian filter. May look smoother than box, but requires more samples.

      Defaults to BLUR_GAUSSIAN.

      Parameters

      • value: number

      Returns void

    • get vsmBlurSize(): number

      Gets the number of samples used for blurring a variance shadow map.

      Returns number

    • set vsmBlurSize(value: number): void

      Sets the number of samples used for blurring a variance shadow map. Only odd values are supported; even values are rounded up to the next odd value. Values should be between 1 and 25. Defaults to 11.

      Parameters

      • value: number

      Returns void

    Methods

    • Fire an event, all additional arguments are passed on to the event listener.

      Parameters

      • name: string

        Name of event to fire.

      • Optionalarg1: any

        First argument that is passed to the event handler.

      • Optionalarg2: any

        Second argument that is passed to the event handler.

      • Optionalarg3: any

        Third argument that is passed to the event handler.

      • Optionalarg4: any

        Fourth argument that is passed to the event handler.

      • Optionalarg5: any

        Fifth argument that is passed to the event handler.

      • Optionalarg6: any

        Sixth argument that is passed to the event handler.

      • Optionalarg7: any

        Seventh argument that is passed to the event handler.

      • Optionalarg8: any

        Eighth argument that is passed to the event handler.

      Returns EventHandler

      Self for chaining.

      obj.fire('test', 'This is the message');
      
    • Test if there are any handlers bound to an event name.

      Parameters

      • name: string

        The name of the event to test.

      Returns boolean

      True if the object has handlers bound to the specified event name.

      obj.on('test', () => {}); // bind an event to 'test'
      obj.hasEvent('test'); // returns true
      obj.hasEvent('hello'); // returns false
    • Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

      Parameters

      • Optionalname: string

        Name of the event to unbind.

      • Optionalcallback: HandleEventCallback

        Function to be unbound.

      • Optionalscope: any

        Scope that was used as the this when the event is fired.

      Returns EventHandler

      Self for chaining.

      const handler = () => {};
      obj.on('test', handler);

      obj.off(); // Removes all events
      obj.off('test'); // Removes all events called 'test'
      obj.off('test', handler); // Removes all handler functions, called 'test'
      obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
    • Attach an event handler to an event.

      Parameters

      • name: string

        Name of the event to bind the callback to.

      • callback: HandleEventCallback

        Function that is called when event is fired. Note the callback is limited to 8 arguments.

      • Optionalscope: any = ...

        Object to use as 'this' when the event is fired, defaults to current this.

      Returns EventHandle

      Can be used for removing event in the future.

      obj.on('test', (a, b) => {
      console.log(a + b);
      });
      obj.fire('test', 1, 2); // prints 3 to the console
      const evt = obj.on('test', (a, b) => {
      console.log(a + b);
      });
      // some time later
      evt.off();
    • Attach an event handler to an event. This handler will be removed after being fired once.

      Parameters

      • name: string

        Name of the event to bind the callback to.

      • callback: HandleEventCallback

        Function that is called when event is fired. Note the callback is limited to 8 arguments.

      • Optionalscope: any = ...

        Object to use as 'this' when the event is fired, defaults to current this.

      Returns EventHandle

      Can be used for removing event in the future.

      obj.once('test', (a, b) => {
      console.log(a + b);
      });
      obj.fire('test', 1, 2); // prints 3 to the console
      obj.fire('test', 1, 2); // not going to get handled