Skip to content

Lighting and rendering

A place made from the Luduvo template starts with three lighting entities at the top of the place: Sun, a DirectionalLight that sets sunlight; Environment, an Environment that sets the sky, ambient light, fog and exposure; and Clouds, a Clouds entity that sets the cloud layer. Point lights and spotlights light smaller areas, and each part's material, color and transparency decide how it takes the light.

Scripts reach these settings in two ways. The sun's angle and color, the ambient light, the fog, the clouds and the exposure are tables on World: World.Sun, World.Ambient, World.Fog, World.Clouds and World.Exposure. Read a field, write a field, or assign a table to change several fields at once. Write them from a server script so every player sees the same sky. Sun shadows and the sky itself are components on the Sun and Environment entities, which a script changes through component views. Studio's Properties panel shows all of them on the three entities.

Set the sun

World.Sun has Azimuth, the compass direction of the sun, and Elevation, its height above the horizon, both in degrees. Writing either one turns the Sun entity. Tint is the color of the sunlight and Intensity its brightness from 0 to 20; they are the Sun's DirectionalColor and DirectionalEnergy.

-- Server script: the sun circles the sky once every 2 minutes.
function Update(dt: number)
    local sun = World.Sun
    sun.Azimuth = (sun.Azimuth + dt * 3) % 360
end

Sunlight passes through a simulated atmosphere, so a low sun gives redder, dimmer light on its own. When Elevation is below 0, the sun casts no shadows. Keep one DirectionalLight per place: only the first one in the world lights the scene, and World.Sun reads and writes the Sun.

Choose a sky

The Environment draws the sky from whichever of these components it has, in this order of priority:

  1. Atmosphere computes the sky from the sun's position. The Luduvo template starts with it.
  2. PanoramaSky draws one panorama or cube texture, set by Panorama.
  3. SixSidedSky draws six face textures, from SkyboxBk to SkyboxUp.

With none of them, or while a sky texture is missing or loading, the background shows BackgroundColor.

-- Server script: replace the atmosphere with the Starry Night cube.
local env = World.Find("Environment")
if env then
    env:Remove(Component.Atmosphere)
    local sky = env:Add(Component.PanoramaSky)
    sky.Panorama = "72057594037929996"
end

Only the atmosphere sky draws the sun's disk, which the SunDisk component on the Sun sizes.

Light the shadows

World.Ambient sets the light on surfaces the sun does not reach. Source is "FollowBackground" (the sky, blended with Tint by Contribution, from 0 to 1), "Color" (the flat Tint on every surface) or "Disabled", and Intensity runs from 0 to 16.

-- Server script: dim blue night light.
World.Ambient = {
    Source = "Color",
    Tint = Color.new(0.3, 0.35, 0.5),
    Intensity = 0.2,
}

Control sun shadows

The Sun's DirectionalLight component has three shadow fields:

  • DirectionalShadowMode is "Hard" (the default) or "None".
  • DirectionalShadowFill sets the share of diffuse sunlight that still reaches surfaces in shadow (100/255 by default). At 0 a shadow gets ambient light alone; at 1 it is fully lit except for the sun's highlight.
  • MaxDistance sets how far from the camera the sun casts shadows (500 units by default). A larger value puts more parts in the shadow pass and costs frame time.
-- Server script: lighter shadows that end closer to the camera.
local sun = World.Find("Sun")
local light = sun and sun.DirectionalLight
if light then
    light.DirectionalShadowFill = 0.6
    light.MaxDistance = 300
end

A part with ShadowOff casts no shadows but still receives them. Parts with a Transparency above 0, with a "Transparent" material, or with a "Water" material cast no shadows either. These rules apply to the sun and to local lights alike.

Each player's graphics settings draw sun shadows off, with hard edges, or with soft edges. A place whose DirectionalShadowMode is "None" stays shadowless for everyone.

Add fog and clouds

The Luduvo template has no fog; writing any field of World.Fog turns it on. Mode is "Linear", from none at Start to solid at End (in units from the camera), or "Exponential" or "ExponentialSquared", thickened by Density from 0 to 1. Color is the color parts fade into. Fog is lit by the same sun and sky as the ground and leaves the sky clear.

-- Server script: fog that starts at 20 units and hides parts past 300.
World.Fog = {
    Mode = "Linear",
    Start = 20,
    End = 300,
    Color = Color.new(0.7, 0.75, 0.8),
}

A client script can change a fog field for its own player alone with World.Override, without changing what the server sends. World.ClearOverride returns to the place's value.

-- Client script: pull the fog in to 40 units for this player.
World.Override(Component.Fog.End, 40)

World.Clouds sets the volumetric cloud layer of the Clouds entity, drawn in front of every sky. Coverage runs from 0 (clear) to 1 (an unbroken layer), and around 0.3 gives scattered patches. A higher Density turns white clouds gray and then storm dark.

-- Server script: half the sky under cloud.
World.Clouds.Coverage = 0.5

Set the exposure

The camera adjusts its exposure to the brightness of the parts in view, the way an eye adapts, and it adapts faster to a brighter view than to a darker one. World.Exposure keeps it between MinEV and MaxEV, from -6 to 20 (defaults 0 and 16). A lower MinEV lets dark rooms brighten more, a higher MaxEV lets bright scenes darken more, and equal values fix the exposure. Because exposure follows the scene, the ratio between light sources matters more than their absolute values.

-- Server script: let a night scene stay dark.
World.Exposure = { MinEV = 2, MaxEV = 16 }

Add a light

Create a PointLight or SpotLight, or add the Light component, and SpotCone for a spotlight, to a part so the light moves with it.

-- Server script: a warm lamp 8 units above the origin.
local lamp = World.Spawn({
    Position = Vector3.new(0, 8, 0),
    Light = { LightColor = Color.new(1, 0.8, 0.6), Range = 40 },
})
lamp.Name = "Lamp"
  • LightColor sets the sRGB color.
  • Intensity sets the brightness from 0 to 1,000 (75 by default).
  • Range sets the reach in units from 0 to 1,000 (100 by default). Light falls off with the square of the distance and fades to nothing at Range.
  • InnerAngle and OuterAngle set the spotlight half-angles in radians from 0 to 1.57 (0.35 and 0.5 by default). The cone is fully lit inside InnerAngle, fades to nothing at OuterAngle, and points along the entity's -Y axis.
  • LightShadowOff stops the light from casting shadows when true.

A light's cost follows its Range, not its Intensity. Particles can light the scene too; see Particle effects.

Set a part's material and color

A part's Material is a material name or asset ID; see Entity properties and components. Color is an sRGB tint, and Transparency runs from 0 (solid) to 1 (invisible).

-- Server script in a part: gold metal.
self.Material = "Metal"
self.Color = Color.new(0.9, 0.7, 0.2)

The built-in materials are SmoothPlastic, Plastic, Brick, Wood, Grass, Ice, Sand, Metal, Aluminum, Rust, Neon, WoodPlanks, Marble, Slate, Concrete, Granite, Pebble, Cobblestone, DiamondPlate, Fabric and Water. Materials.DefineMaterial defines more on the server from a MaterialSpec: textures, roughness, metalness, and a Pipeline of "Opaque", "Cutout", "Transparent", "Water" or "Decal". For a look the settings cannot give, see Writing a material shader.

A mesh takes its own textures from its PbrMaterial component: Albedo, Normal, MetallicRoughness, Occlusion, Emissive and Underlay, each an asset ID string where "0" means none. To put an image on one face of a part, add a Decal as its child and set its Texture and Side.

Make a surface glow

The Neon material draws the part's color as light. Lights and shadows do not change it, and it looks equally bright by day and at night. It does not light the parts around it; add a Light for that.

-- Server script in a part: a pink neon sign.
self.Material = "Neon"
self.Color = Color.new(1, 0.2, 0.6)

Bright Neon colors are well past the brightness where bloom starts, so they spread a soft glow for players who have bloom on. A custom material glows the same way with Neon in its surface, or adds a glow on top of normal lighting with an Emission table. A mesh glows in places with an Emissive texture.

Control the player camera

On a client, World.Viewport.Camera is the camera the player sees through. Two of its properties decide how input drives it: InputSteer lets the mouse, the camera keys, the right stick and touch steer the camera rig, and InputMove lets the movement keys walk the local character.

-- Client script: steer the camera rig from this script alone.
local cam = World.Viewport.Camera
if cam then
    cam.InputSteer = false
end

The player's Field of View setting is Settings.FieldOfView, the vertical field of view in degrees (70 by default).

Graphics settings

Each player picks a graphics preset, and it decides sun shadow edges (off, hard or soft), ambient occlusion, bloom, anti-aliasing and cloud detail. The Low preset turns off sun shadows, ambient occlusion and bloom, so check that a place is still clear to see at Low.

How it works

The renderer collects lights every frame, with these limits:

  • At most 512 point lights and 512 spotlights draw. Lights past that are dropped in the order the engine finds them, not by distance. Particle lights use the point light slots that other lights leave free.
  • At most 4 point lights and 8 spotlights cast shadows: the casting lights nearest the camera, measured to the edge of each light's Range. The rest light without shadows, so LightShadowOff on lamps that do not need a shadow leaves the slots to the ones that do.

The renderer sorts each light into the parts of the view that its Range sphere touches, and a pixel shades only the lights sorted into its part of the view. That is why Range decides a light's cost.

Sun shadows test each pixel on screen against the triangles between it and the sun, so hard shadow edges stay sharp however close the camera comes, and surfaces do not shadow themselves in speckled bands. The test walks a limited number of pixels per area of the shadow; in a very dense scene, pixels past that limit go unshadowed and the client logs a warning.

Transparent parts draw after solid ones, sorted from far to near by the center of each part's bounding box. Two transparent parts that overlap, or one inside another, can draw in the wrong order and swap as the camera moves.