Light

Lighting is essential for creating realistic 3D scenes. Gorilla3D provides TGorillaLight, an enhanced light component that extends FireMonkey's TLight with additional properties for advanced shading, attenuation control, and light scattering effects.

Light Types

Point Light

Omnidirectional light that emanates from a single point in all directions, like a light bulb.

var
  PointLight: TGorillaLight;
begin
  PointLight := TGorillaLight.Create(Viewport);
  PointLight.Parent := Viewport;
  PointLight.LightType := TLightType.Point;
  PointLight.Position.Point := Point3D(0, 5, 0);
  PointLight.Color := TAlphaColors.White;
end;

Use For:

  • Indoor lighting (lamps, candles)
  • Outdoor lights (street lamps, torches)
  • Special effects (explosions, magic spells)

Directional Light

Parallel light rays coming from a specific direction, like sunlight. Position doesn't matter, only rotation.

var
  SunLight: TGorillaLight;
begin
  SunLight := TGorillaLight.Create(Viewport);
  SunLight.Parent := Viewport;
  SunLight.LightType := TLightType.Directional;
  SunLight.RotationAngle.X := -45;  // Angle from above
  SunLight.RotationAngle.Y := 45;   // Angle from side
  SunLight.Color := TAlphaColorRec.Lightyellow;
end;

Use For:

  • Sun and moon lighting
  • General scene illumination
  • Outdoor scenes

Spot Light

Cone-shaped light with position and direction, like a flashlight or stage spotlight.

var
  SpotLight: TGorillaLight;
begin
  SpotLight := TGorillaLight.Create(Viewport);
  SpotLight.Parent := Viewport;
  SpotLight.LightType := TLightType.Spot;
  SpotLight.Position.Point := Point3D(0, 10, 0);
  SpotLight.RotationAngle.X := 90;  // Point downward
  SpotLight.SpotCutOff := 30;       // Cone angle
  SpotLight.Color := TAlphaColors.White;
end;

Use For:

  • Flashlights and headlights
  • Stage lighting
  • Focused dramatic lighting

Creating Lights

Design-Time

  1. Drop TGorillaLight onto your viewport from the tool palette
  2. Set the LightType in the Object Inspector
  3. Position and configure the light
  4. Adjust color, intensity, and attenuation properties

Runtime

function CreateLight(AViewport: TGorillaViewport; AType: TLightType;
  APosition: TPoint3D; AColor: TAlphaColor): TGorillaLight;
begin
  Result := TGorillaLight.Create(AViewport);
  Result.Parent := AViewport;
  Result.LightType := AType;
  Result.Position.Point := APosition;
  Result.Color := AColor;
  Result.Intensity := 1.0;
end;

Light Properties

Color Components

Gorilla3D lights have three color components:

Diffuse Color

The main light color affecting directly lit surfaces.

Light.Color := TAlphaColors.White;

Ambient Color

Indirect environmental light that brightens shadowed areas.

// Subtle ambient light
Light.Ambient := TAlphaColorF.Create(0.1, 0.1, 0.1, 1.0);

// Stronger ambient (reduces contrast)
Light.Ambient := TAlphaColorF.Create(0.3, 0.3, 0.3, 1.0);

// Colored ambient (e.g., blue sky light)
Light.Ambient := TAlphaColorF.Create(0.1, 0.15, 0.25, 1.0);

Specular Color

Color of shiny highlights on reflective surfaces.

// White highlights (most realistic)
Light.Specular := TAlphaColorF.Create(1, 1, 1, 1);

// Colored highlights (stylized look)
Light.Specular := TAlphaColorF.Create(1, 0.9, 0.8, 1);  // Warm highlights

// No highlights
Light.Specular := TAlphaColorF.Create(0, 0, 0, 1);

Intensity

Control light strength without changing color:

// Dim light
Light.Intensity := 0.5;

// Normal light
Light.Intensity := 1.0;

// Bright light
Light.Intensity := 2.0;

// Very bright (maximum)
Light.Intensity := 10.0;

Range: 0.0 to 10.0 (automatically clamped)

Attenuation

Attenuation controls how light intensity decreases with distance. Only affects Point and Spot lights.

Formula

Attenuation = 1 / (Constant + Linear × Distance + Quadratic × Distance²)

Attenuation Components

// Constant: Base attenuation (usually 1.0)
Light.ConstantAttenuation := 1.0;

// Linear: Proportional falloff
Light.LinearAttenuation := 0.35;

// Quadratic: Rapid falloff at distance
Light.QuadraticAttenuation := 0.44;

Common Presets

procedure SetLightRange(ALight: TGorillaLight; ARange: Integer);
begin
  ALight.ConstantAttenuation := 1.0;

  case ARange of
    7:  // Short range
      begin
        ALight.LinearAttenuation := 0.7;
        ALight.QuadraticAttenuation := 1.8;
      end;
    13: // Medium range
      begin
        ALight.LinearAttenuation := 0.35;
        ALight.QuadraticAttenuation := 0.44;
      end;
    20: // Long range
      begin
        ALight.LinearAttenuation := 0.22;
        ALight.QuadraticAttenuation := 0.20;
      end;
    50: // Very long range
      begin
        ALight.LinearAttenuation := 0.07;
        ALight.QuadraticAttenuation := 0.017;
      end;
    100: // Extremely long range
      begin
        ALight.LinearAttenuation := 0.045;
        ALight.QuadraticAttenuation := 0.0075;
      end;
  end;
end;

Visual Effect

// Sharp falloff (dramatic, close range)
Light.LinearAttenuation := 1.0;
Light.QuadraticAttenuation := 2.0;

// Gentle falloff (realistic, medium range)
Light.LinearAttenuation := 0.35;
Light.QuadraticAttenuation := 0.44;

// Very gentle (unrealistic, fills large areas)
Light.LinearAttenuation := 0.01;
Light.QuadraticAttenuation := 0.001;

Light Scattering

Light scattering creates volumetric "god rays" effects visible in global illumination rendering.

Availability: Requires Global Illumination enabled (GAME_PACKAGE only)

Basic Setup

// Enable global illumination first
Viewport.GlobalIllumDetail := 3;

// Configure light scattering
Light.ScatteringIntensity := 0.5;   // How visible rays are
Light.ScatteringExposure := 0.01;   // Ray brightness
Light.ScatteringDecay := 1.0;       // Ray fadeout
Light.ScatteringDensity := 0.84;    // Medium density

Parameters

Scattering Intensity (0.0-10.0)

Controls how strongly scattering is mixed into the final image.

Light.ScatteringIntensity := 0.1;   // Subtle effect
Light.ScatteringIntensity := 0.5;   // Noticeable rays
Light.ScatteringIntensity := 2.0;   // Strong dramatic rays

Scattering Exposure (0.0-1.0)

Controls the visibility/brightness of individual light rays.

Light.ScatteringExposure := 0.001;  // Faint rays
Light.ScatteringExposure := 0.01;   // Visible rays
Light.ScatteringExposure := 0.1;    // Very bright rays

Scattering Decay (0.0-2.0)

How quickly rays fade along their path.

Light.ScatteringDecay := 0.5;       // Slow decay, long rays
Light.ScatteringDecay := 1.0;       // Normal decay
Light.ScatteringDecay := 2.0;       // Fast decay, short rays

Scattering Density (0.0-10.0)

Density of the scattering medium.

Light.ScatteringDensity := 0.3;     // Thin atmosphere
Light.ScatteringDensity := 0.84;    // Normal atmosphere
Light.ScatteringDensity := 2.0;     // Dense fog/haze

Scattering Effects

Sunbeams Through Window

procedure CreateSunbeams(ALight: TGorillaLight);
begin
  ALight.LightType := TLightType.Directional;
  ALight.RotationAngle.X := -60;
  ALight.Color := TAlphaColorRec.Lightyellow;
  ALight.Intensity := 1.5;

  // Scattering
  ALight.ScatteringIntensity := 0.8;
  ALight.ScatteringExposure := 0.015;
  ALight.ScatteringDecay := 1.2;
  ALight.ScatteringDensity := 1.2;
end;

Volumetric Spotlight

procedure CreateVolumetricSpotlight(ALight: TGorillaLight);
begin
  ALight.LightType := TLightType.Spot;
  ALight.SpotCutOff := 25;
  ALight.Color := TAlphaColors.White;
  ALight.Intensity := 3.0;

  // Strong visible cone
  ALight.ScatteringIntensity := 1.5;
  ALight.ScatteringExposure := 0.02;
  ALight.ScatteringDecay := 0.8;
  ALight.ScatteringDensity := 2.0;
end;

Atmospheric Haze

procedure CreateAtmosphericLight(ALight: TGorillaLight);
begin
  ALight.LightType := TLightType.Directional;
  ALight.Color := TAlphaColorRec.Lightblue;
  ALight.Intensity := 0.8;

  // Subtle atmosphere
  ALight.ScatteringIntensity := 0.3;
  ALight.ScatteringExposure := 0.005;
  ALight.ScatteringDecay := 1.0;
  ALight.ScatteringDensity := 0.5;
end;

Shadow Casting

TGorillaLight can cast shadows through Gorilla3D's Cascaded Shadow Maps (CSM) system. Shadows are enabled per-viewport via Viewport.Shadows; each light then opts in with CastShadow.

// 1. Turn the shadow system on (once, on the viewport)
GorillaViewport1.Shadows.Enabled       := True;
GorillaViewport1.Shadows.CascadeCount  := 4;
GorillaViewport1.Shadows.MapResolution := 2048;
GorillaViewport1.Shadows.MaxDirLights  := 1;

// 2. Flag the lights that should cast shadows
SunLight.CastShadow := True;

Per-Light Shadow Properties

Property Type Default Purpose
CastShadow Boolean False Enables shadow casting for this light.
ShadowBias Single 0.0005 Constant depth bias during the shadow comparison.
ShadowSlopeBias Single 0.001 Slope-scaled bias against acne on grazing surfaces.
ShadowNormalOffset Single 0.02 Sample offset along the surface normal (world units).
ShadowStrength Single 1.0 Per-light shadow opacity [0..1] — 1.0 opaque, 0.0 disabled. Composes with the global Viewport.Shadows.Strength.
ShadowNearFar TPointF (0, 0) Near/far projection override (X = near, Y = far); only components > 0 apply. Spot/point: (0, 0) auto-derives from attenuation. Directional: clamps the cascade depth band (overrides the automatic fit); (0, 0) keeps auto-fit.

Shadow darkness: the scene-wide dial is Viewport.Shadows.Strength — not Viewport.ShadowStrength, which only affects the Global Illumination pass.

Small / close-up casters: directional cascades auto-fit to the scene geometry (Viewport.Shadows.AutoFitCascades, on by default). For one small object, set Light.ShadowNearFar := PointF(0.1, 8) for the crispest result.

See Cascaded Shadow Maps for the full system configuration (filters, cascades, point/spot budgets, strength and cascade fitting).

Lighting Scenarios

Three-Point Lighting

Classic setup for object presentation:

procedure SetupThreePointLighting(AViewport: TGorillaViewport);
var
  KeyLight, FillLight, BackLight: TGorillaLight;
begin
  // Key Light (main light, 45° front-side)
  KeyLight := TGorillaLight.Create(AViewport);
  KeyLight.Parent := AViewport;
  KeyLight.LightType := TLightType.Directional;
  KeyLight.RotationAngle.Point := Point3D(-30, 45, 0);
  KeyLight.Color := TAlphaColors.White;
  KeyLight.Intensity := 1.5;

  // Fill Light (softer, opposite side)
  FillLight := TGorillaLight.Create(AViewport);
  FillLight.Parent := AViewport;
  FillLight.LightType := TLightType.Directional;
  FillLight.RotationAngle.Point := Point3D(-20, -45, 0);
  FillLight.Color := TAlphaColors.White;
  FillLight.Intensity := 0.5;
  FillLight.Ambient := TAlphaColorF.Create(0.2, 0.2, 0.2, 1);

  // Back Light (rim lighting from behind)
  BackLight := TGorillaLight.Create(AViewport);
  BackLight.Parent := AViewport;
  BackLight.LightType := TLightType.Directional;
  BackLight.RotationAngle.Point := Point3D(20, 180, 0);
  BackLight.Color := TAlphaColors.White;
  BackLight.Intensity := 1.0;
end;

Outdoor Daylight

procedure SetupOutdoorLighting(AViewport: TGorillaViewport);
var
  Sun, Sky: TGorillaLight;
begin
  // Sun (directional, warm)
  Sun := TGorillaLight.Create(AViewport);
  Sun.Parent := AViewport;
  Sun.LightType := TLightType.Directional;
  Sun.RotationAngle.Point := Point3D(-50, 30, 0);
  Sun.Color := TAlphaColorRec.Lightyellow;
  Sun.Intensity := 2.0;

  // Sky light (ambient, cool)
  Sky := TGorillaLight.Create(AViewport);
  Sky.Parent := AViewport;
  Sky.LightType := TLightType.Directional;
  Sky.RotationAngle.Point := Point3D(0, 0, 0);
  Sky.Color := TAlphaColorRec.Lightblue;
  Sky.Intensity := 0.3;
  Sky.Ambient := TAlphaColorF.Create(0.2, 0.25, 0.3, 1);
end;

Indoor Room

procedure SetupIndoorLighting(AViewport: TGorillaViewport);
var
  CeilingLight: TGorillaLight;
  i: Integer;
begin
  // Multiple ceiling lights
  for i := 0 to 3 do
  begin
    CeilingLight := TGorillaLight.Create(AViewport);
    CeilingLight.Parent := AViewport;
    CeilingLight.LightType := TLightType.Point;
    CeilingLight.Position.Y := 8;
    CeilingLight.Position.X := (i mod 2) * 10 - 5;
    CeilingLight.Position.Z := (i div 2) * 10 - 5;
    CeilingLight.Color := TAlphaColorRec.Lightgoldenrodyellow;
    CeilingLight.Intensity := 1.5;

    // Medium range attenuation
    CeilingLight.ConstantAttenuation := 1.0;
    CeilingLight.LinearAttenuation := 0.35;
    CeilingLight.QuadraticAttenuation := 0.44;

    // Soft ambient
    CeilingLight.Ambient := TAlphaColorF.Create(0.15, 0.15, 0.12, 1);
  end;
end;

Campfire Scene

procedure SetupCampfireScene(AViewport: TGorillaViewport);
var
  Fire: TGorillaLight;
  Moon: TGorillaLight;
begin
  // Flickering fire (animate intensity in timer)
  Fire := TGorillaLight.Create(AViewport);
  Fire.Parent := AViewport;
  Fire.LightType := TLightType.Point;
  Fire.Position.Point := Point3D(0, 0.5, 0);
  Fire.Color := TAlphaColorRec.Orange;
  Fire.Intensity := 2.0;
  Fire.LinearAttenuation := 0.22;
  Fire.QuadraticAttenuation := 0.20;

  // Dim moonlight
  Moon := TGorillaLight.Create(AViewport);
  Moon.Parent := AViewport;
  Moon.LightType := TLightType.Directional;
  Moon.RotationAngle.X := -60;
  Moon.Color := TAlphaColorRec.Lightsteelblue;
  Moon.Intensity := 0.2;
  Moon.Ambient := TAlphaColorF.Create(0.05, 0.05, 0.08, 1);
end;

Animated Lighting

Flickering Light

procedure TForm1.TimerFlickerTimer(Sender: TObject);
var
  BaseIntensity: Single;
begin
  BaseIntensity := 1.5;
  TorchLight.Intensity := BaseIntensity + Random * 0.3 - 0.15;
end;

Rotating Light (Lighthouse, Alarm)

procedure TForm1.TimerRotateTimer(Sender: TObject);
begin
  SpotLight.RotationAngle.Y := SpotLight.RotationAngle.Y + 2;
end;

Pulsing Light

var
  PulseTime: Single = 0;

procedure TForm1.TimerPulseTimer(Sender: TObject);
begin
  PulseTime := PulseTime + 0.05;
  Light.Intensity := 1.0 + Sin(PulseTime) * 0.5;
end;

Day/Night Cycle

var
  TimeOfDay: Single = 0;  // 0-24 hours

procedure TForm1.UpdateDayNightCycle;
var
  SunAngle: Single;
  SunIntensity: Single;
begin
  // Sun angle based on time
  SunAngle := (TimeOfDay / 24) * 360 - 90;
  SunLight.RotationAngle.X := SunAngle;

  // Intensity based on sun height
  SunIntensity := Max(0, Sin(DegToRad(SunAngle + 90)));
  SunLight.Intensity := SunIntensity * 2;

  // Color shift (warm at sunrise/sunset)
  if (TimeOfDay > 5) and (TimeOfDay < 7) then
    SunLight.Color := TAlphaColorRec.Orange  // Sunrise
  else if (TimeOfDay > 18) and (TimeOfDay < 20) then
    SunLight.Color := TAlphaColorRec.Orangered  // Sunset
  else
    SunLight.Color := TAlphaColors.White;  // Day

  TimeOfDay := TimeOfDay + 0.01;  // Advance time
  if TimeOfDay >= 24 then TimeOfDay := 0;
end;

Performance Considerations

Light Count

Each light increases rendering cost:

// Good: Few lights, well-placed
procedure OptimalLighting;
begin
  // 1 directional (sun) + 2-3 points = good performance
end;

// Problematic: Many lights
procedure TooManyLights;
var
  i: Integer;
begin
  for i := 0 to 100 do  // Performance issues!
    CreatePointLight(...);
end;

Dynamic vs Static

// Enable/disable lights based on visibility
procedure UpdateLights;
begin
  if Distance(Camera.Position, Light.Position) > 50 then
    Light.Enabled := False
  else
    Light.Enabled := True;
end;

Light Types Performance

Performance Ranking (best to worst):

  1. Directional Light - No attenuation calculations
  2. Point Light - Simple attenuation
  3. Spot Light - Attenuation + cone calculations

Best Practices

  1. Start Simple: Begin with one directional light, add more as needed

  2. Use Realistic Values: Real-world lighting is often dimmer than you think:

    Sun.Intensity := 1.5;  // Not 10.0
  3. Balance Color Components:

    • Diffuse = main light color
    • Ambient = 10-30% of diffuse brightness
    • Specular = usually white or slightly tinted
  4. Match Attenuation to Scene Scale: If your scene is in meters, use real-world attenuation ranges

  5. Limit Light Count:

    • Mobile: 1-3 lights
    • Desktop: 3-8 lights
    • High-end: 8-16 lights
  6. Use Global Illumination Wisely: Enable only when the scene benefits from it

  7. Test on Target Hardware: Lighting performance varies greatly between devices

Troubleshooting

Lights Not Affecting Objects

// Check material type - must use Gorilla3D materials
if MyMesh.MaterialSource is TGorillaDefaultMaterialSource then
  ShowMessage('Using Gorilla3D material - OK')
else
  ShowMessage('Standard FMX material - limited lighting!');

Scene Too Dark

// Add ambient light
Light.Ambient := TAlphaColorF.Create(0.2, 0.2, 0.2, 1);

// Or increase intensity
Light.Intensity := 2.0;

// Or add fill light

Harsh Shadows/High Contrast

// Increase ambient to fill shadows
Light.Ambient := TAlphaColorF.Create(0.3, 0.3, 0.3, 1);

// Add fill light from opposite direction

Light Too Dim at Distance

// Reduce attenuation for longer range
Light.LinearAttenuation := 0.07;
Light.QuadraticAttenuation := 0.017;

// Or increase intensity
Light.Intensity := 3.0;

See Also