Skip to content

Conversation

@Szabolcs05
Copy link

Fix sky light and block light calculation for schematics

Summary
This PR fixes broken lighting when loading schematic files into the world. Previously, lighting was not calculated correctly, resulting in areas being too bright or too dark.

Changes

Sky Light Calculation

  • Implemented proper top-down sky light propagation through transparent blocks
  • Sky light now correctly stops at opaque blocks and propagates through transparent ones (glass, leaves, flowers, etc.)

Block Light Calculation

  • Added support for light-emitting blocks (torches, lanterns, glowstone, etc.)
  • Light sources now properly illuminate surrounding areas with correct falloff

Light Propagation

  • Implemented flood-fill algorithm for both sky and block light
  • Light correctly decreases by 1 for each block traveled through transparent materials

Transparent Block Detection

  • Added comprehensive list of transparent block patterns (glass, leaves, flowers, fences, signs, etc.)
  • Added list of light-emitting blocks with their respective light levels

Files Changed

  • crates/pico_structures/src/world.rs - Global light calculation and propagation
  • crates/pico_structures/src/schematic.rs - Transparent/light-emitting block detection
  • crates/pico_structures/src/lib.rs - Export LightSection type
  • crates/minecraft_packets/src/play/data/light_data.rs - Updated to handle both sky and block light
  • crates/minecraft_packets/src/play/chunk_data_and_update_light_packet.rs - Retrieve pre-calculated light data

Result
Schematics now display with natural-looking shadows and proper illumination from light sources.

Quozul added 30 commits May 30, 2025 10:47
Implementation of PaletteContainer, biome and dimension
use tracing::warn;

/// Blocks that allow sky light to pass through (fully or partially transparent)
const TRANSPARENT_BLOCK_PATTERNS: &[&str] = &[
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list of string IDs could be handled at compile time, where a build script would map this to smaller and more optimized InternalIds. Same for the LIGHT_EMITTING_BLOCKS const below.
For now, both have manageable amount of strings, so it is relatively fine. Comparing two strings is slower than two integers.

@Szabolcs05 Szabolcs05 force-pushed the feature/lighting-improvements branch 3 times, most recently from 25ae43a to f87644c Compare December 12, 2025 16:32
Copy link
Owner

@Quozul Quozul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to update the docs to remove the line that says light is not supported.

let mut sky_light = 15u8;
for y in (0..total_height).rev() {
let pos = Coordinates::new(x, y, z);
let idx = get_index(x, y, z);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are computing the index from the x,y,z coordinates, you could iterate over the index directly, to have only one loop. Similar below for the block_light_volume computation and at other places.

You could also probably merge both loops.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sky light loop needs to iterate top-down per column (to track when light gets blocked), so it can't be fully merged with block light. But I can simplify by iterating over columns and merging the inner work.

@Szabolcs05 Szabolcs05 force-pushed the feature/lighting-improvements branch 4 times, most recently from 52619c7 to 7d9d14e Compare December 12, 2025 20:54
@Szabolcs05 Szabolcs05 force-pushed the feature/lighting-improvements branch from 7d9d14e to 8f752f1 Compare December 12, 2025 21:15
Add optimized light calculation using Starlight-inspired algorithm that processes both sky light and block light simultaneously. Uses parallel processing for initialization, pre-computed transparency data for better cache performance, and directional propagation tracking to avoid redundant checks. Includes helper methods for bounds checking and chunk indexing.
@Szabolcs05 Szabolcs05 force-pushed the feature/lighting-improvements branch from 8f752f1 to faba204 Compare December 14, 2025 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants