Class ShadowGenerator
Hierarchy (View Summary)
- ShadowGenerator
Implements
Index
Constructors
Properties
Accessors
Methods
Constructors
constructor
-
new ShadowGenerator(
mapSize: number,
light: IShadowLight,
usefullFloatFirst?: boolean,
camera?: Nullable<Camera>,
useRedTextureType?: boolean,
forceGLSL?: boolean,
): ShadowGeneratorCreates 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: booleanBy 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: booleanForces 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
- mapSize: number
Properties
custom Allow Rendering
Gets or sets a custom function to allow/disallow rendering a sub mesh in the shadow map
custom Shader Options
Gets or sets the custom shader name to use
do Not Serialize
Specifies if the ShadowGenerator should be serialized, true to skip serialization.
Note a ShadowGenerator will not be serialized if its light has doNotSerialize=true
enable Soft Transparent Shadow
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!
force Back Faces Only
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.
frustum Edge Falloff
Controls the extent to which the shadows fade out at the edge of the frustum
id
Gets or set the id of the shadow generator. It will be the one from the light if not defined
on After Shadow Map Render Mesh Observable
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)
on After Shadow Map Render Observable
Observable triggered after the shadow is rendered. Can be used to restore internal effect state
on Before Shadow Map Render Mesh Observable
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)
on Before Shadow Map Render Observable
Observable triggered before the shadow is rendered. Can be used to update internal effect state
use Opacity Texture For Transparent Shadow
If this is true, use the opacity texture's alpha channel for transparent shadows instead of the diffuse one
StaticCLASSNAME
Name of the shadow generator class
StaticDEFAULT_ ALPHA_ CUTOFF
Defines the default alpha cutoff value used for transparent alpha tested materials.
Static ReadonlyFILTER_ BLURCLOSEEXPONENTIALSHADOWMAP
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)
Static ReadonlyFILTER_ BLUREXPONENTIALSHADOWMAP
Shadow generator mode ESM: Blurred Exponential Shadow Mapping. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
Static ReadonlyFILTER_ CLOSEEXPONENTIALSHADOWMAP
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)
Static ReadonlyFILTER_ EXPONENTIALSHADOWMAP
Shadow generator mode ESM: Exponential Shadow Mapping. (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
Static ReadonlyFILTER_ NONE
Shadow generator mode None: no filtering applied.
Static ReadonlyFILTER_ PCF
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)
Static ReadonlyFILTER_ PCSS
Shadow generator mode PCSS: Percentage Closering Soft Shadow. benefits from Webgl 2 shadow samplers. Fallback to Poisson Sampling in Webgl 1 Contact Hardening
Static ReadonlyFILTER_ POISSONSAMPLING
Shadow generator mode Poisson Sampling: Percentage Closer Filtering. (Multiple Tap around evenly distributed around the pixel are used to evaluate the shadow strength)
StaticForce GLSL
Force all the shadow generators to compile to glsl even on WebGPU engines. False by default. This is mostly meant for backward compatibility.
Static ReadonlyQUALITY_ HIGH
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.
Static ReadonlyQUALITY_ LOW
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.
Static ReadonlyQUALITY_ MEDIUM
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.
Accessors
bias
-
get bias(): number
Gets the bias: offset applied on the depth preventing acnea (in light direction).
Returns number
-
set bias(bias: number): void
Sets the bias: offset applied on the depth preventing acnea (in light direction).
Parameters
- bias: number
Returns void
blur Box Offset
-
get blurBoxOffset(): number
Gets the blur box offset: offset applied during the blur pass. Only useful if useKernelBlur = false
Returns number
-
set blurBoxOffset(value: number): void
Sets the blur box offset: offset applied during the blur pass. Only useful if useKernelBlur = false
Parameters
- value: number
Returns void
blur Kernel
-
get blurKernel(): number
Gets the blur kernel: kernel size of the blur pass. Only useful if useKernelBlur = true
Returns number
-
set blurKernel(value: number): void
Sets the blur kernel: kernel size of the blur pass. Only useful if useKernelBlur = true
Parameters
- value: number
Returns void
blur Scale
-
get blurScale(): number
Gets the blur scale: scale of the blurred texture compared to the main shadow map. 2 means half of the size.
Returns number
-
set blurScale(value: number): void
Sets the blur scale: scale of the blurred texture compared to the main shadow map. 2 means half of the size.
Parameters
- value: number
Returns void
camera
contact Hardening Light Size UV Ratio
-
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,
): voidSets 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
darkness
-
get darkness(): number
Gets or sets the actual darkness of a shadow
Returns number
-
set darkness(value: number): void
Parameters
- value: number
Returns void
depth Scale
-
get depthScale(): number
Gets the depth scale used in ESM mode.
Returns number
-
set depthScale(value: number): void
Sets the depth scale used in ESM mode. This can override the scale stored on the light.
Parameters
- value: number
Returns void
filter
-
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
filtering Quality
-
get filteringQuality(): number
Gets the PCF or PCSS Quality. Only valid if usePercentageCloserFiltering or usePercentageCloserFiltering is true.
Returns number
-
set filteringQuality(filteringQuality: number): void
Sets the PCF or PCSS Quality. Only valid if usePercentageCloserFiltering or usePercentageCloserFiltering is true.
Parameters
- filteringQuality: number
Returns void
light
-
Gets or sets the light that is casting the shadows
Returns IShadowLight
-
Parameters
- light: IShadowLight
Returns void
map Size
-
get mapSize(): number
Gets or sets the size of the texture what stores the shadows
Returns number
-
set mapSize(size: number): void
Parameters
- size: number
Returns void
normal Bias
-
get normalBias(): number
Gets the normalBias: offset applied on the depth preventing acnea (along side the normal direction and proportional to the light/normal angle).
Returns number
-
set normalBias(normalBias: number): void
Sets the normalBias: offset applied on the depth preventing acnea (along side the normal direction and proportional to the light/normal angle).
Parameters
- normalBias: number
Returns void
projection Matrix
-
Gets the projection matrix used to render the shadow map.
Returns Matrix
shader Language
-
get shaderLanguage(): ShaderLanguage
Gets the shader language used in this generator.
Returns ShaderLanguage
transparency Shadow
-
get transparencyShadow(): boolean
Gets or sets the ability to have transparent shadow
Returns boolean
-
set transparencyShadow(value: boolean): void
Parameters
- value: boolean
Returns void
use Blur Close Exponential Shadow Map
-
get useBlurCloseExponentialShadowMap(): boolean
Gets if the current filter is set to filtered "close ESM" (using the inverse of the exponential to prevent steep falloff artifacts).
Returns boolean
-
set useBlurCloseExponentialShadowMap(value: boolean): void
Sets the current filter to filtered "close ESM" (using the inverse of the exponential to prevent steep falloff artifacts).
Parameters
- value: boolean
Returns void
use Blur Exponential Shadow Map
-
get useBlurExponentialShadowMap(): boolean
Gets if the current filter is set to filtered ESM.
Returns boolean
-
set useBlurExponentialShadowMap(value: boolean): void
Gets if the current filter is set to filtered ESM.
Parameters
- value: boolean
Returns void
use Close Exponential Shadow Map
-
get useCloseExponentialShadowMap(): boolean
Gets if the current filter is set to "close ESM" (using the inverse of the exponential to prevent steep falloff artifacts).
Returns boolean
-
set useCloseExponentialShadowMap(value: boolean): void
Sets the current filter to "close ESM" (using the inverse of the exponential to prevent steep falloff artifacts).
Parameters
- value: boolean
Returns void
use Contact Hardening Shadow
-
get useContactHardeningShadow(): boolean
Gets if the current filter is set to "PCSS" (contact hardening).
Returns boolean
-
set useContactHardeningShadow(value: boolean): void
Sets the current filter to "PCSS" (contact hardening).
Parameters
- value: boolean
Returns void
use Exponential Shadow Map
-
get useExponentialShadowMap(): boolean
Gets if the current filter is set to ESM.
Returns boolean
-
set useExponentialShadowMap(value: boolean): void
Sets the current filter is to ESM.
Parameters
- value: boolean
Returns void
use Float32 Texture Type
-
get useFloat32TextureType(): boolean
Gets or sets a value indicating whether the shadow map should use full float texture type (instead of half float, which is the default). Use this option when you need more precision (for self shadowing, for instance).
Returns boolean
-
set useFloat32TextureType(useFloat32TextureType: boolean): void
Parameters
- useFloat32TextureType: boolean
Returns void
use Kernel Blur
-
get useKernelBlur(): boolean
Gets whether the blur pass is a kernel blur (if true) or box blur. Only useful in filtered mode (useBlurExponentialShadowMap...)
Returns boolean
-
set useKernelBlur(value: boolean): void
Sets whether the blur pass is a kernel blur (if true) or box blur. Only useful in filtered mode (useBlurExponentialShadowMap...)
Parameters
- value: boolean
Returns void
use Percentage Closer Filtering
-
get usePercentageCloserFiltering(): boolean
Gets if the current filter is set to "PCF" (percentage closer filtering).
Returns boolean
-
set usePercentageCloserFiltering(value: boolean): void
Sets the current filter to "PCF" (percentage closer filtering).
Parameters
- value: boolean
Returns void
use Poisson Sampling
-
get usePoissonSampling(): boolean
Gets if the current filter is set to Poisson Sampling.
Returns boolean
-
set usePoissonSampling(value: boolean): void
Sets the current filter to Poisson Sampling.
Parameters
- value: boolean
Returns void
use Red Texture Format
-
get useRedTextureFormat(): boolean
Gets or sets a value indicating whether the shadow map should use a red-channel-only texture format. Using a single-channel format reduces memory usage when color data is not needed.
Returns boolean
-
set useRedTextureFormat(useRedTextureFormat: boolean): void
Parameters
- useRedTextureFormat: boolean
Returns void
view Matrix
-
Gets the view matrix used to render the shadow map.
Returns Matrix
Methods
add Shadow Caster
-
Helper function to add a mesh and its descendants to the list of shadow casters.
Parameters
- mesh: AbstractMesh
Mesh to add
- includeDescendants: boolean = true
boolean indicating if the descendants should be added. Default to true
Returns ShadowGenerator
the Shadow Generator itself
- mesh: AbstractMesh
bind Shadow Light
-
Binds the shadow related information inside of an effect (information like near, far, darkness... defined in the generator but impacting the effect).
Parameters
- lightIndex: string
Index of the light in the enabled light list of the material owning the effect
- effect: Effect
The effect we are binding the information for
Returns void
- lightIndex: string
dispose
-
Disposes the ShadowGenerator.
Parameters
- clearObservables: boolean = true
Defines whether to clear the observables or not (true by default). Returns nothing.
Returns void
- clearObservables: boolean = true
force Compilation
-
forceCompilation(
onCompiled?: (generator: IShadowGenerator) => void,
options?: Partial<{ useInstances: boolean }>,
): voidForces all the attached effect to compile to enable rendering only once ready vs. lazily compiling effects.
Parameters
OptionalonCompiled: (generator: IShadowGenerator) => voidCallback triggered at the and of the effects compilation
Optionaloptions: Partial<{ useInstances: boolean }>Sets of optional options forcing the compilation with different modes
Returns void
force Compilation Async
-
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
get Class Name
-
Gets the class name of that object
Returns string
"ShadowGenerator"
get Darkness
-
Returns the darkness value (float). This can only decrease the actual darkness of a shadow. 0 means strongest and 1 would means no shadow.
Returns number
the darkness.
get Light
get Shadow Map
-
Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
Returns Nullable<RenderTargetTexture>
The render target texture if present otherwise, null
get Shadow Map For Rendering
-
Gets the RTT used during rendering (can be a blurred version of the shadow map or the shadow map itself).
Returns Nullable<RenderTargetTexture>
The render target texture if the shadow map is present otherwise, null
get Transform Matrix
-
Gets the transformation matrix used to project the meshes into the map from the light point of view. (eq to shadow projection matrix * light transform matrix)
Returns Matrix
The transform matrix used to create the shadow map
is Ready
-
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
- subMesh: SubMesh
prepare Defines
-
Prepare all the defines in a material relying on a shadow map at the specified light index.
Parameters
- defines: any
Defines of the material we want to update
- lightIndex: number
Index of the light in the enabled light list of the material
Returns void
- defines: any
recreate Shadow Map
-
Recreates the shadow map dependencies like RTT and post processes. This can be used during the switch between Cube and 2D textures for instance.
Returns void
remove Shadow Caster
-
Helper function to remove a mesh and its descendants from the list of shadow casters
Parameters
- mesh: AbstractMesh
Mesh to remove
- includeDescendants: boolean = true
boolean indicating if the descendants should be removed. Default to true
Returns ShadowGenerator
the Shadow Generator itself
- mesh: AbstractMesh
serialize
-
Serializes the shadow generator setup to a json object.
Returns any
The serialized JSON object
set Darkness
-
Sets the darkness value (float). This can only decrease the actual darkness of a shadow.
Parameters
- darkness: number
The darkness value 0 means strongest and 1 would means no shadow.
Returns ShadowGenerator
the shadow generator allowing fluent coding.
- darkness: number
set Transparency Shadow
-
Sets the ability to have transparent shadow (boolean).
Parameters
- transparent: boolean
True if transparent else False
Returns ShadowGenerator
the shadow generator allowing fluent coding
- transparent: boolean
StaticParse
-
Parse(
parsedShadowGenerator: any,
scene: Scene,
constr?: (
mapSize: number,
light: IShadowLight,
camera: Nullable<Camera>,
) => ShadowGenerator,
): ShadowGeneratorParses a serialized ShadowGenerator and returns a new ShadowGenerator.
Parameters
- parsedShadowGenerator: any
The JSON object to parse
- scene: Scene
The scene to create the shadow map for
Optionalconstr: (
mapSize: number,
light: IShadowLight,
camera: Nullable<Camera>,
) => ShadowGeneratorA function that builds a shadow generator or undefined to create an instance of the default shadow generator
Returns ShadowGenerator
The parsed shadow generator
- parsedShadowGenerator: any
Default implementation IShadowGenerator. This is the main object responsible of generating shadows in the framework. Documentation: https://doc.babylonjs.com/features/featuresDeepDive/lights/shadows
See