API

Class ShadowGenerator

Default implementation IShadowGenerator. This is the main object responsible of generating shadows in the framework. Documentation: https://doc.babylonjs.com/features/featuresDeepDive/lights/shadows

Hierarchy (View Summary)

Implements

Index
  • Creates a ShadowGenerator object. A ShadowGenerator is the required tool to use the shadows. Each light casting shadows needs to use its own ShadowGenerator. Documentation : https://doc.babylonjs.com/features/featuresDeepDive/lights/shadows

    Parameters

    • mapSize: number

      The size of the texture what stores the shadows. Example : 1024.

    • light: IShadowLight

      The light object generating the shadows.

    • OptionalusefullFloatFirst: boolean

      By default the generator will try to use half float textures but if you need precision (for self shadowing for instance), you can use this option to enforce full float texture.

    • Optionalcamera: Nullable<Camera>

      Camera associated with this shadow generator (default: null). If null, takes the scene active camera at the time we need to access it

    • OptionaluseRedTextureType: boolean

      Forces the generator to use a Red instead of a RGBA type for the shadow map texture format (default: false)

    • forceGLSL: boolean = false

      defines a boolean indicating if the shader must be compiled in GLSL even if we are using WebGPU

    Returns ShadowGenerator

customAllowRendering: (subMesh: SubMesh) => boolean

Gets or sets a custom function to allow/disallow rendering a sub mesh in the shadow map

customShaderOptions: ICustomShaderOptions

Gets or sets the custom shader name to use

doNotSerialize: boolean = false

Specifies if the ShadowGenerator should be serialized, true to skip serialization. Note a ShadowGenerator will not be serialized if its light has doNotSerialize=true

enableSoftTransparentShadow: boolean = false

Enables or disables shadows with varying strength based on the transparency When it is enabled, the strength of the shadow is taken equal to mesh.visibility If you enabled an alpha texture on your material, the alpha value red from the texture is also combined to compute the strength: mesh.visibility * alphaTexture.a The texture used is the diffuse by default, but it can be set to the opacity by setting useOpacityTextureForTransparentShadow Note that by definition transparencyShadow must be set to true for enableSoftTransparentShadow to work!

forceBackFacesOnly: boolean = false

If true the shadow map is generated by rendering the back face of the mesh instead of the front face. This can help with self-shadowing as the geometry making up the back of objects is slightly offset. It might on the other hand introduce peter panning.

frustumEdgeFalloff: number = 0

Controls the extent to which the shadows fade out at the edge of the frustum

id: string

Gets or set the id of the shadow generator. It will be the one from the light if not defined

onAfterShadowMapRenderMeshObservable: Observable<Mesh> = ...

Observable triggered after a mesh is rendered in the shadow map. Can be used to update internal effect state (that you can get from the onAfterShadowMapRenderObservable)

onAfterShadowMapRenderObservable: Observable<Effect> = ...

Observable triggered after the shadow is rendered. Can be used to restore internal effect state

onBeforeShadowMapRenderMeshObservable: Observable<Mesh> = ...

Observable triggered before a mesh is rendered in the shadow map. Can be used to update internal effect state (that you can get from the onBeforeShadowMapRenderObservable)

onBeforeShadowMapRenderObservable: Observable<Effect> = ...

Observable triggered before the shadow is rendered. Can be used to update internal effect state

useOpacityTextureForTransparentShadow: boolean = false

If this is true, use the opacity texture's alpha channel for transparent shadows instead of the diffuse one

CLASSNAME: string = "ShadowGenerator"

Name of the shadow generator class

DEFAULT_ALPHA_CUTOFF: number = 0.5

Defines the default alpha cutoff value used for transparent alpha tested materials.

FILTER_BLURCLOSEEXPONENTIALSHADOWMAP: 5

Shadow generator mode ESM: Blurred Exponential Shadow Mapping using the inverse of the exponential preventing edge artifacts on steep falloff. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)

FILTER_BLUREXPONENTIALSHADOWMAP: 3

Shadow generator mode ESM: Blurred Exponential Shadow Mapping. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)

FILTER_CLOSEEXPONENTIALSHADOWMAP: 4

Shadow generator mode ESM: Exponential Shadow Mapping using the inverse of the exponential preventing edge artifacts on steep falloff. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)

FILTER_EXPONENTIALSHADOWMAP: 1

Shadow generator mode ESM: Exponential Shadow Mapping. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)

FILTER_NONE: 0

Shadow generator mode None: no filtering applied.

FILTER_PCF: 6

Shadow generator mode PCF: Percentage Closer Filtering benefits from Webgl 2 shadow samplers. Fallback to Poisson Sampling in Webgl 1 (https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch11.html)

FILTER_PCSS: 7

Shadow generator mode PCSS: Percentage Closering Soft Shadow. benefits from Webgl 2 shadow samplers. Fallback to Poisson Sampling in Webgl 1 Contact Hardening

FILTER_POISSONSAMPLING: 2

Shadow generator mode Poisson Sampling: Percentage Closer Filtering. (Multiple Tap around evenly distributed around the pixel are used to evaluate the shadow strength)

ForceGLSL: boolean = false

Force all the shadow generators to compile to glsl even on WebGPU engines. False by default. This is mostly meant for backward compatibility.

QUALITY_HIGH: 0

Reserved for PCF and PCSS Highest Quality.

Execute PCF on a 5*5 kernel improving a lot the shadow aliasing artifacts.

Execute PCSS with 32 taps blocker search and 64 taps PCF.

QUALITY_LOW: 2

Reserved for PCF and PCSS The lowest quality but the fastest.

Execute PCF on a 1*1 kernel.

Execute PCSS with 16 taps blocker search and 16 taps PCF.

QUALITY_MEDIUM: 1

Reserved for PCF and PCSS Good tradeoff for quality/perf cross devices

Execute PCF on a 3*3 kernel.

Execute PCSS with 16 taps blocker search and 32 taps PCF.

  • get contactHardeningLightSizeUVRatio(): number

    Gets the Light Size (in shadow map uv unit) used in PCSS to determine the blocker search area and the penumbra size. Using a ratio helps keeping shape stability independently of the map size.

    It does not account for the light projection as it was having too much instability during the light setup or during light position changes.

    Only valid if useContactHardeningShadow is true.

    Returns number

  • set contactHardeningLightSizeUVRatio(
        contactHardeningLightSizeUVRatio: number,
    ): void

    Sets the Light Size (in shadow map uv unit) used in PCSS to determine the blocker search area and the penumbra size. Using a ratio helps keeping shape stability independently of the map size.

    It does not account for the light projection as it was having too much instability during the light setup or during light position changes.

    Only valid if useContactHardeningShadow is true.

    Parameters

    • contactHardeningLightSizeUVRatio: number

    Returns void

  • get filter(): number

    Gets the current mode of the shadow generator (normal, PCF, ESM...). The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE

    Returns number

  • set filter(value: number): void

    Sets the current mode of the shadow generator (normal, PCF, ESM...). The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE

    Parameters

    • value: number

    Returns void

  • Forces all the attached effect to compile to enable rendering only once ready vs. lazily compiling effects.

    Parameters

    • OptionalonCompiled: (generator: IShadowGenerator) => void

      Callback triggered at the and of the effects compilation

    • Optionaloptions: Partial<{ useInstances: boolean }>

      Sets of optional options forcing the compilation with different modes

    Returns void

  • Forces all the attached effect to compile to enable rendering only once ready vs. lazily compiling effects.

    Parameters

    • Optionaloptions: Partial<{ useInstances: boolean }>

      Sets of optional options forcing the compilation with different modes

    Returns Promise<void>

    A promise that resolves when the compilation completes

  • Determine whether the shadow generator is ready or not (mainly all effects and related post processes needs to be ready).

    Parameters

    • subMesh: SubMesh

      The submesh we want to render in the shadow map

    • useInstances: boolean

      Defines whether will draw in the map using instances

    • isTransparent: boolean

      Indicates that isReady is called for a transparent subMesh

    Returns boolean

    true if ready otherwise, false