Engine API Reference - v2.24.0-beta.28
    Preparing search index...

    Class DynamicSkyEffect

    The dynamic sky effect: renders the sky of the camera - a day sky of an analytic atmosphere, lit by the sun, a night sky of stars and the moon, and optionally clouds drifting with the wind - in place of the sky of the scene. The time of day is set by the position of the sun, see DynamicSkyEffect#azimuth and DynamicSkyEffect#elevation, and the effect can keep a directional light in sync with it, see DynamicSkyEffect#light.

    The sky has no clouds by default, see DynamicSkyEffect#cloudType. They are a single flat layer of noise (CLOUDTYPE_FLAT), or volumetric clouds ray marched through a layer of 3D noise (CLOUDTYPE_VOLUMETRIC). The volumetric clouds are the expensive part of the sky, and render to a texture of their own, at a reduced resolution by default, see DynamicSkyEffect#cloudResolution. Their noise is generated on the CPU when the clouds are first used.

    The effect also generates the lighting of the scene from its sky: the environment atlas lighting the materials, assigned to Scene#envAtlas, see DynamicSkyEffect#lighting. The cameras without the effect, such as the cameras rendering planar reflections, render the sky of the scene from that environment atlas - a still image of this sky.

    The effect can also haze the scene with the sky, see DynamicSkyEffect#haze: with their distance, the lit materials fade into the color of the sky behind them, in place of the fog of the camera.

    In XR, the sky and the flat clouds render for each view, like the rest of the scene. The volumetric clouds render to their texture for the view of the camera alone, so they streak where the view of an eye extends past it - use CLOUDTYPE_FLAT clouds in XR.

    The shaders of the scene's own materials can evaluate the sky too, for example to fade distant ground into the color of the sky behind it. The effect sets the uniforms of its sky on the graphics device every frame, and its shader chunk dynamicSkyAtmospherePS, in GLSL and WGSL, evaluates the sky from them. Include the chunk in a shader, and dynamicSkyBase(dir, ambient) returns the radiance of the sky in a direction, without the sun disc, the stars and the moon, and sets ambient to the light of the sky around it. The direction is in the space of the sky - a world space direction turned by the rotation of the sky of the scene, dynSkyRotation, which the effect sets with the other uniforms, and with X flipped:

    #include "dynamicSkyAtmospherePS"
    uniform mat3 dynSkyRotation;

    vec3 skyColor(vec3 worldDir) {
    vec3 dir = worldDir * dynSkyRotation;
    dir.x = -dir.x;
    vec3 ambient;
    return dynamicSkyBase(dir, ambient);
    }

    The chunk dynamicSkyHazePS applies the haze of the sky, see DynamicSkyEffect#haze, in the shaders that the haze does not reach on its own - dynamicSkyHaze(color, cameraPos, worldPos) returns a linear color seen through it:

    #include "dynamicSkyHazePS"

    vec3 hazed = dynamicSkyHaze(color, view_position, vPositionW);

    With more than one dynamic sky, a material renders with the sky that was updated last. On the CPU, DynamicSkyEffect#getSkyColor evaluates the same sky in a direction.

    The sky does not work together with the volumetric fog of the camera frame, CameraFrame#volumetricFog: the clouds cast no shadows into the fog, and the fog is not lit by the light of the sky.

    The effect is not part of a camera frame by default - construct it and add it to the camera frame of a camera:

    const sky = new DynamicSkyEffect(app.graphicsDevice);
    sky.light = sunEntity.light;
    sky.cloudType = CLOUDTYPE_VOLUMETRIC;
    cameraFrame.addEffect(sky);
    cameraFrame.update();

    Like all effects, its settings apply on the next CameraFrame#update, which a day and night cycle moving the sun calls every frame. The clouds drift with the time of the application.

    Hierarchy (View Summary)

    Index
    atlasSize: number = 512

    The size of the environment atlas the lighting is generated to. Higher values give sharper reflections on glossy surfaces, and a sharper sky of the scene for the cameras without the effect, such as the cameras rendering planar reflections. Defaults to 512.

    azimuth: number = 0

    The azimuth of the sun, in degrees - its compass direction. Defaults to 0.

    cameraFrame: CameraFrame | null = null

    The camera frame the effect is registered with, or null.

    cloudBrightness: number = 0.6

    The brightness of the sunlight falling on the clouds. Defaults to 0.6.

    cloudCoverage: number = 0.45

    The fraction of the sky covered by clouds, 0-1 range, from a clear sky to overcast. Defaults to 0.45.

    cloudHeight: number = 2000

    The altitude of the cloud layer, in meters. Defaults to 2000.

    cloudNightBrightness: number = 0.05

    The brightness of the moonlight on the clouds at night. The moonlight of the DynamicSkyEffect#light is usually much brighter than the moon, to keep the night scene readable - lit by it, the clouds would glow against the dark night sky. Defaults to 0.05.

    cloudOpacity: number = 1

    The opacity of the clouds, 0-1 range. Defaults to 1.

    cloudQuality: number = 0.5

    The quality of the volumetric clouds, 0-1 range - the number of their steps through the cloud layer, from 24 to 64, and toward the light, from 1 to 4, and the fineness of the steps through a cloud. The cost grows about linearly, doubling from 0.5 to 1. Lower values give less defined edges of the clouds and a flatter shading. Defaults to 0.5.

    cloudResolution: number = 0.5

    The resolution the volumetric clouds render at, relative to the resolution of the scene, 0.25-1 range. The clouds are soft, so a reduced resolution saves most of their cost with little loss of detail. The sky blurs them over 3x3 of their texels as it reads them, smoothing the grain of their steps, so lower values also soften the clouds. Defaults to 0.5.

    cloudSize: number = 8000

    The distance in meters over which the cloud pattern repeats. Larger values give larger clouds. Defaults to 8000.

    cloudSoftness: number = 0.7

    The softness of the clouds, 0.01-1 range. Lower values give dense clouds with crisp edges and deep shadows, higher values thin them into translucent, wispy clouds with softer shading. Defaults to 0.7.

    cloudSunDimming: number = 0.6

    The fraction of the sunlight the clouds block at the full coverage, dimming the DynamicSkyEffect#light. Defaults to 0.6.

    cloudThickness: number = 2500

    The thickness of the layer of the volumetric clouds, in meters, above their DynamicSkyEffect#cloudHeight. Defaults to 2500.

    cloudType: string = CLOUDTYPE_NONE

    The type of the clouds of the sky, CLOUDTYPE_NONE, CLOUDTYPE_FLAT or CLOUDTYPE_VOLUMETRIC. Defaults to CLOUDTYPE_NONE.

    The graphics device.

    elevation: number = 25

    The elevation of the sun above the horizon, in degrees. Negative values place the sun below the horizon, which gives the night sky. Defaults to 25.

    enabled: boolean = true

    Whether the effect is enabled. An effect can additionally disable itself from its own parameters, see CameraFrameEffect#active.

    haze: boolean = false

    Whether the sky hazes the scene the camera renders - the aerial perspective. With their distance, surfaces fade into the color of the sky behind them: warm toward a low sun, blue away from it, and the gradient of the night sky after the sunset. The haze is densest at DynamicSkyEffect#hazeBaseHeight and thins out with the height above it, see DynamicSkyEffect#hazeHeight, so the valleys stay hazy while the view from high up is clear.

    The haze takes the place of the fog of the camera, Scene#fog or CameraComponent#fog: while the haze is enabled, the effect gives its camera a fog of its own, and gives the camera its fog back once the haze is disabled. The lit materials the camera renders, such as StandardMaterial, fade into the sky, while the other shaders, such as those of the particles and the gaussian splats, render without fog - custom shaders can apply the haze with the chunk dynamicSkyHazePS, see the description of the class. A material opts out of the haze, rendering without fog, with StandardMaterial#useFog, or by setting the define DYNAMIC_SKY_NO_HAZE:

    material.setDefine('DYNAMIC_SKY_NO_HAZE', true);
    material.update();

    A material replacing the chunk fogPS with its own is not hazed, and finds the define FOG of its shaders set to DYNAMICSKYHAZE on the camera. Enabling or disabling the haze compiles the shaders of the lit materials the camera renders again. Defaults to false.

    hazeBaseHeight: number = 0

    The height the haze is densest at, in world units - usually the lowest ground of the scene, such as the level of the sea. Below it, the haze grows denser still. Defaults to 0.

    hazeDensity: number = 0.0005

    The density of the haze at its DynamicSkyEffect#hazeBaseHeight, per world unit - the fraction of the light it scatters over a unit of distance. A level view at that height is hidden by 95% of the haze at about 3 / density. Defaults to 0.0005.

    hazeHeight: number = 1000

    The height over which the density of the haze falls to about a third, in world units. Lower values keep the haze closer to the ground. Defaults to 1000.

    light: LightComponent | null = null

    The directional light kept in sync with the sun - its direction, color and intensity. By day it shines from the sun, at DynamicSkyEffect#sunIntensity, dimmed by the clouds, and at night it fades to the moonlight, see DynamicSkyEffect#moonIntensity. Defaults to null.

    lighting: boolean = true

    Whether the effect generates the lighting of the scene from its sky - the environment atlas lighting the materials, assigned to Scene#envAtlas. Only one effect should do so, the one of the main camera. The lighting is generated when the effect is first used, and again when the sun moves by DynamicSkyEffect#lightingThreshold, when a setting of the sky changes by more than a percent, when the clouds drift by DynamicSkyEffect#lightingCloudThreshold, and when DynamicSkyEffect#bakeLighting is called. The clouds are a still image in the lighting, and the sun disc is left out of it, as the DynamicSkyEffect#light carries the light of the sun. Defaults to true.

    lightingCloudThreshold: number = 0

    The distance the clouds drift by, as a fraction of the DynamicSkyEffect#cloudSize, before the lighting is generated again, so the clouds in it follow the clouds of the sky. A value of 0 disables it, and the drift of the clouds does not generate the lighting again. Defaults to 0.

    lightingInterpolation: boolean = false

    Whether the lighting blends from its previous update to its latest one, instead of changing at once. While the sky changes gradually, such as with an animated time of day, each update blends in over the time since the previous one, up to a second, so the lighting follows the sky smoothly, about one update behind it. Without it, the lighting steps at each update, which shows as flicker in the reflections of a quickly changing sky on glossy surfaces, such as water. Sudden changes, such as a jump of the time of day, and DynamicSkyEffect#bakeLighting, still apply at once. It costs two more environment atlases of memory, and a small pass each frame while the lighting blends, so enable it only when the sky changes continuously. Defaults to false.

    lightingResolution: number = 256

    The resolution of the sky the lighting is generated from, the size of the faces of a cubemap, in texels. The sky is filtered down from it to the environment atlas, so it should be well above the resolution of the atlas: at lower values, the detail of the clouds aliases, and their reflections flicker on each update of the lighting as the clouds drift. Defaults to 256.

    lightingThreshold: number = 0.5

    The angle the sun moves by, in degrees, before the lighting is generated again - keeping the cost of a day and night cycle down. Defaults to 0.5.

    luminance: number = 1

    The brightness of the sky, relative to the light of the sun. At 1, the sky lights the scene with about a tenth of the light of the sun, as a clear sky does, at any elevation of the sun: the blue sky and the ground lit by the sun are then of a similar brightness, so a single exposure suits both. Lower values give a deeper sky, higher values a brighter one, and a brighter ambient light of the scene. Defaults to 1.

    mieCoefficient: number = 0.005

    The amount of the Mie (aerosol) scattering, 0-0.1 range, setting the brightness of the glow around the sun. Defaults to 0.005.

    mieDirectionalG: number = 0.8

    The directional anisotropy of the Mie scattering, 0-1 range, setting how tightly the glow hugs the sun. Defaults to 0.8.

    moonColor: Color = ...

    The color of the moonlight, and of the moon disc. Defaults to a pale blue.

    moonDirection: Vec3 = ...

    The direction toward the moon, in the world space, turned by the DynamicSkyEffect#rotation of the sky. It does not need to be normalized. Defaults to a direction high in the sky.

    moonGlow: number = 3

    The brightness of the moon disc in the night sky. Defaults to 3.

    moonIntensity: number = 1

    The intensity of the DynamicSkyEffect#light at night - the moonlight, a dim cool light from the direction of the moon. Defaults to 1.

    moonSize: number = 0.6

    The angular radius of the moon disc, in degrees. Defaults to 0.6.

    nightBrightness: number = 0.052

    The brightness of the night sky. Defaults to 0.052.

    nightColor: Color = ...

    The color of the night sky. Defaults to a deep blue.

    rayleigh: number = 1.5

    The amount of the Rayleigh scattering, 0-4 range, setting the blueness of the sky. Defaults to 1.5.

    rotation: number = 0

    The rotation of the whole sky around the vertical axis, in degrees. It is added to the azimuth of the sun, so the sky, the sun and the moon turn together, along with the light and the shadows it casts. Defaults to 0.

    starBrightness: number = 0.05

    The brightness of the stars. A value of 0 disables them. Defaults to 0.05.

    starDensity: number = 0.8

    The density of the stars, 0-1 range - the fraction of the cells of the sky holding a star. Defaults to 0.8.

    starSize: number = 0.5

    The scale of the size of the stars. Defaults to 0.5.

    sunIntensity: number = 2

    The intensity of the light of the sun: of the DynamicSkyEffect#light with the sun above the horizon, and of the sunlit clouds. The sky scales with it too, see DynamicSkyEffect#luminance, so it sets the brightness of the whole day, to match the exposure of the camera. Defaults to 2, a sunny day at an exposure of 1.

    turbidity: number = 2

    The haziness of the atmosphere, 1-20 range. Higher values give a milkier sky and a larger glow around the sun. Defaults to 2.

    twilightGlow: number = 0.52

    The strength of the warm glow at the horizon while the sun is just below it. Defaults to 0.52.

    windDirection: number = 45

    The direction the clouds drift toward, in degrees around the vertical axis. Defaults to 45.

    windSpeed: number = 10

    The speed the clouds drift with, in meters per second. Defaults to 10.

    • get cameraExposureScale(): number

      Gets the scale the effect applies to the exposure of the camera this frame, multiplying the exposure of the scene, or of the physical camera with physical units - for effects adapting the exposure to what the camera sees. Read every frame while the effect takes part, after CameraFrameEffect#frameUpdate; the camera frame applies the product of the scales of the effects taking part, and the camera renders with its own exposure again once none does. Defaults to 1.

      // an effect brightening the image by one stop
      get cameraExposureScale() {
      return 2;
      }

      Returns number

    • get chunkName(): string

      Gets the name the compose chunk can be overridden under in ShaderChunks, the same way as the built-in chunks. Derived from the id as compose<Id>PS unless given.

      Returns string

    • get defineName(): string

      Gets the define set on the compose shader while the effect is active: the id in upper snake case, colorEnhance giving COLOR_ENHANCE.

      Returns string

    • get entryPoint(): string

      Gets the name of the function the composition calls at the effect's slot. Derived from the id as apply<Id> unless given. The signature depends on the slot: colour slots take (vec3 color, vec2 uv) and return the colour, COMPOSESLOT_SCENE takes and returns a vec4.

      Returns string

    • get requires(): string[]

      Gets the frame resources the effect needs, the FRAMERESOURCE_* constants - for its passes, or for its compose chunk, such as the scene depth. The camera frame provides them while the effect is active. Requirements which depend on the device are computed when constructing the effect; override this as a getter when they depend on a parameter, and the camera frame provides what it returns from the next CameraFrame#update.

      Returns string[]

    • get slot(): string | null

      Gets the compose slot the entry function is called at, or null for an effect that does not take part in the composition.

      Returns string | null

    • Destroys the effect, removing it from its camera frame if it is still registered and releasing everything it owns, the passes it created included. Subclasses which create resources in their constructor release them in an override, calling super.destroy(). The removal takes effect for the frames rendered after the next CameraFrame#update, which is due before the next frame renders.

      Returns void

    • Gets the color of the sky in a direction - its linear radiance, without the sun disc, the stars and the moon, as dynamicSkyBase of the shader chunk dynamicSkyAtmospherePS evaluates it. Use it for lighting worked out on the CPU, such as the light the sky gives to particles. It evaluates the sky as of the last CameraFrame#update.

      Parameters

      • direction: Vec3

        The direction, in the world space. It does not need to be normalized.

      • Optionalcolor: Color = ...

        The color to receive the result. A new one is created if not given.

      Returns Color

      The color of the sky.

      // the light of the sky from straight above
      const zenith = sky.getSkyColor(Vec3.UP);
    • Resolves the scene, for an effect whose passes run at the resolve stage - see CameraFrameEffectPasses. Called every frame while the effect's passes take part in the frame, before any pass renders and before CameraFrameEffect#frameUpdate is called on any effect, in the order the effects are registered. The effect hands its passes the scene color the effects before it resolved, and replaces it with the texture they render to this frame, which the effects after it, the half resolution copy of the scene and the composition then read:

      resolveScene(scene) {
      this._pass.setSourceTexture(scene.texture);
      scene.texture = this._pass.outputTexture;
      }

      An effect resolving the scene to another resolution also sets the width and the height. The default implementation leaves the scene unchanged.

      Parameters

      Returns void

    • Sets a define on the compose shader. The define persists until changed, so fixed defines can be set once in the constructor and those depending on the parameters from CameraFrameEffect#update. A value of false, null or undefined removes it. Changing a define rebuilds the compose shader.

      Parameters

      • name: string

        The define name.

      • value: any

        The value.

      Returns void