--- /dev/null
+/*
+ * Aukio 3D engine demos. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.aukio.e3d.examples.gi_radius_demo;
+
+import eu.svjatoslav.aukio.e3d.geometry.Camera;
+import eu.svjatoslav.aukio.e3d.geometry.Point3D;
+import eu.svjatoslav.aukio.e3d.gui.ViewFrame;
+import eu.svjatoslav.aukio.e3d.gui.ViewPanel;
+import eu.svjatoslav.aukio.e3d.renderer.raster.Color;
+import eu.svjatoslav.aukio.e3d.renderer.raster.ShapeCollection;
+import eu.svjatoslav.aukio.e3d.renderer.raster.gi.GlobalIllumination;
+import eu.svjatoslav.aukio.e3d.renderer.raster.lighting.LightSource;
+import eu.svjatoslav.aukio.e3d.renderer.raster.lighting.LightingManager;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.Billboard;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.texturedpolygon.TriangleMeshBlock;
+import eu.svjatoslav.aukio.e3d.renderer.raster.texture.Texture;
+
+import java.util.Random;
+
+/**
+ * Radius-gated GI showcase: a 48 000-unit procedural tower district
+ * stored as lean {@link TriangleMeshBlock}s (the same geometry kind
+ * streaming game worlds use). Without radius gating the bake would
+ * grind the whole district forever; with {@code e3d.giStage2Radius /
+ * giStage3Radius / giStage4Radius} set (config.yaml or -D), the light
+ * bubble around the camera gets centroid bounce, then coarse lightmaps,
+ * then fine lightmaps — and the bubble follows as you fly (WASD + mouse
+ * drag; the catch-up watcher re-lights what you approach).
+ *
+ * <p>GI uses district-scale per-instance radius overrides (stage 2/3/4
+ * = 24 000 / 10 000 / 5 000 units, mesh texel = 50 units) — the shared
+ * config.yaml values are FO4-scale (meters), far too small for this
+ * 48 000-unit district. An explicit -De3d.gi.stage*Radius /
+ * -De3d.gi.meshUnitsPerTexel flag still wins over the demo defaults.</p>
+ *
+ * <p>The golden path ({@link #buildScene}) is GI-free and fully
+ * deterministic (seeded heights and textures).</p>
+ */
+public class GiRadiusDemo {
+
+ private static final int GROUND_QUADS = 24; // per axis
+ private static final double GROUND_SIZE = 2000; // units per ground quad
+ private static final int TOWERS = 30; // per axis
+ private static final double PITCH = 1600; // tower spacing
+ private static final double FOOT = 600; // tower footprint
+
+ /** Sun position, shared by the light source and the visible disc. */
+ private static final double SUN_X = 100000;
+ private static final double SUN_Y = -150000;
+ private static final double SUN_Z = 50000;
+ /** Sun disc width as a fraction of screen width (FO4 default: 0.10). */
+ private static final double SUN_DISC_SCREEN_FRACTION = 0.10;
+
+ /** Sky blue, the same color the Fallout 4 viewer uses
+ * (Fo4Lighting.skyBackground). */
+ private static final String SKY_HEX = "7AA3D8";
+ /** Sky blue as opaque ARGB; the golden harness reads this for its
+ * background fill so the golden matches the live view. */
+ public static final int SKY_BACKGROUND_ARGB =
+ 0xFF000000 | Integer.parseInt(SKY_HEX, 16);
+
+ public static void main(final String[] args) {
+ final ViewFrame viewFrame = new ViewFrame("GI radius bubble demo");
+ final ViewPanel viewPanel = viewFrame.getViewPanel();
+ // FO4-style blue sky behind the district.
+ viewPanel.backgroundColor = Color.hex(SKY_HEX);
+ buildScene(viewPanel.getRootShapeCollection(),
+ viewPanel.getLightingManager(), viewPanel.getCamera());
+ System.out.println("GI radius demo: district-scale GI defaults "
+ + "(radii 24000/10000/5000, mesh texel 50 units); override "
+ + "with -De3d.gi.stage2Radius=... / -De3d.gi.meshUnitsPerTexel=... "
+ + "Fly with WASD + mouse drag.");
+ // Per-instance overrides, NOT System.setProperty: the launcher
+ // runs every demo in this one JVM, so a global property would
+ // leak district-scale radii into the next demo (or the FO4
+ // environment) launched afterwards. An explicit -D flag still
+ // wins (null override = EngineConfig resolution).
+ // 4 workers: this district's eligible set per bubble is ~10x the
+ // FO4-scale default (2 workers), and the bake must converge while
+ // the render loop keeps the machine busy.
+ viewPanel.enableGlobalIllumination(4, gi -> {
+ gi.setRadiusOverrides(
+ giDefault("e3d.gi.stage2Radius", 24000),
+ giDefault("e3d.gi.stage3Radius", 10000),
+ giDefault("e3d.gi.stage4Radius", 5000));
+ gi.setMeshUnitsPerTexelOverride(
+ giDefault("e3d.gi.meshUnitsPerTexel", 50));
+ });
+ // The engine renders on demand (0 fps when idle), but a baking
+ // GI scene changes continuously — request frames until the bake
+ // converges, plus while post-bake catch-up drains after camera
+ // jumps. Idle afterwards (power save).
+ // Capped at 20 fps while GI works: an uncapped render loop eats
+ // ~80% of the machine and starves the GI workers (Java thread
+ // priority is a no-op on Linux by default), stretching the bake
+ // from seconds to minutes.
+ final GlobalIllumination giSystem = viewPanel.getGlobalIllumination();
+ viewPanel.addFrameListener((panel, deltaMs) -> {
+ final GlobalIllumination.Status s = giSystem.getStatus();
+ final boolean giActive = !s.baked || s.catchUpPending > 0;
+ panel.setFrameRate(giActive ? 20 : 60);
+ return giActive;
+ });
+ viewPanel.repaintDuringNextViewUpdate();
+ }
+
+ /** Demo-scaled GI default; null when the user set the -D flag
+ * explicitly (null override = EngineConfig reads the flag). */
+ private static Double giDefault(final String property, final double demoValue) {
+ return System.getProperty(property) == null ? demoValue : null;
+ }
+
+ /** Golden-harness entry: deterministic scene, no GI. */
+ public static void buildScene(final ShapeCollection shapes,
+ final LightingManager lighting,
+ final Camera camera) {
+ final Texture groundTex = groundTexture();
+ final Texture wallTex = wallTexture();
+ final Texture roofTex = roofTexture();
+
+ // Ground plane (single block): 24x24 quads, spanning
+ // 0..48000 on X and Z.
+ {
+ final int quads = GROUND_QUADS;
+ final double[] world = new double[quads * quads * 2 * 9];
+ final double[] uv = new double[quads * quads * 2 * 6];
+ final Texture[] textures = new Texture[quads * quads * 2];
+ int t = 0;
+ for (int i = 0; i < quads; i++)
+ for (int j = 0; j < quads; j++) {
+ final double x0 = i * GROUND_SIZE, x1 = x0 + GROUND_SIZE;
+ final double z0 = j * GROUND_SIZE, z1 = z0 + GROUND_SIZE;
+ t = putTri(world, uv, textures, t, groundTex,
+ x0, 0, z0, x1, 0, z0, x0, 0, z1,
+ x0 / 100, z0 / 100, x1 / 100, z0 / 100,
+ x0 / 100, z1 / 100);
+ t = putTri(world, uv, textures, t, groundTex,
+ x1, 0, z0, x1, 0, z1, x0, 0, z1,
+ x1 / 100, z0 / 100, x1 / 100, z1 / 100,
+ x0 / 100, z1 / 100);
+ }
+ shapes.addShape(new TriangleMeshBlock(world, uv, textures, false),
+ "ground");
+ }
+
+ // Towers (single block): deterministic heights, 10 tris each.
+ {
+ final Random random = new Random(42);
+ final double[] world = new double[TOWERS * TOWERS * 10 * 9];
+ final double[] uv = new double[TOWERS * TOWERS * 10 * 6];
+ final Texture[] textures = new Texture[TOWERS * TOWERS * 10];
+ int t = 0;
+ for (int i = 0; i < TOWERS; i++)
+ for (int j = 0; j < TOWERS; j++) {
+ final double x0 = i * PITCH + (PITCH - FOOT) / 2;
+ final double z0 = j * PITCH + (PITCH - FOOT) / 2;
+ final double h = 800 + random.nextDouble() * 3200;
+ t = putTower(world, uv, textures, t, wallTex, roofTex,
+ x0, z0, x0 + FOOT, z0 + FOOT, h);
+ }
+ shapes.addShape(new TriangleMeshBlock(world, uv, textures, true),
+ "towers");
+ }
+
+ lighting.setAmbientLight(Color.hex("141418"));
+ // Sky dome illumination: GI bounce rays that escape the district
+ // sample the sky color, so shadowed faces pick up a cool blue
+ // bounce instead of going black (bugreport-20261005-145449).
+ lighting.setSkyColor(Color.hex(SKY_HEX));
+ // Distant attenuation-compensated sun, ~110 delivered units.
+ lighting.addLight(new LightSource(
+ new Point3D(SUN_X, SUN_Y, SUN_Z), Color.hex("FFF4E0"), 3.9e7));
+ // Three colored street lamps near the spawn for visible bleeding.
+ lighting.addLight(new LightSource(new Point3D(-600, -350, 800),
+ Color.hex("FF4020"), 30));
+ lighting.addLight(new LightSource(new Point3D(2400, -350, 800),
+ Color.hex("2060FF"), 30));
+ lighting.addLight(new LightSource(new Point3D(800, -350, -600),
+ Color.hex("30FF60"), 30));
+
+ // Visible sun disc in the sky, same approach as the Fallout 4
+ // loader (Fo4Lighting.addSkyMarkers): one glowing alpha billboard
+ // at the sun position. It depth-tests against the world, so
+ // towers occlude it and it only shows in open sky; being a
+ // billboard (not a triangle), GI never traces it and it casts
+ // no shadow.
+ {
+ final Texture glow = sunGlowTexture();
+ final double distance = Math.sqrt(SUN_X * SUN_X + SUN_Y * SUN_Y
+ + SUN_Z * SUN_Z);
+ // Billboard.setScale folds in an internal SCALE_MULTIPLIER of
+ // 0.005; the on-screen half-width comes out as
+ // width * scale * 0.005 * texWidth / z. Solve for the scale
+ // that gives the wanted screen-width fraction at the sun
+ // distance.
+ final double scale = SUN_DISC_SCREEN_FRACTION * distance
+ / (0.005 * glow.primaryBitmap.width);
+ shapes.addShape(new Billboard(
+ new Point3D(SUN_X, SUN_Y, SUN_Z), scale, glow), "sky");
+ }
+
+ // Elevated mid-district spawn looking up toward the sun
+ // (user-tuned pose, 2026-10-05).
+ camera.getTransform().set(7316.66, -1366.66, 9372.93, -0.97, 0.30, 0.00);
+ }
+
+ /** Emits one tower (4 walls + roof, 10 triangles), normals outward.
+ * Wall UVs: u = horizontal extent, v = height (y); roof: x/z. */
+ private static int putTower(final double[] world, final double[] uv,
+ final Texture[] textures, int t,
+ final Texture wall, final Texture roof,
+ final double x0, final double z0,
+ final double x1, final double z1,
+ final double h) {
+ final double yTop = -h; // engine up is -Y
+ // +X wall (u along z, v along y)
+ t = putTri(world, uv, textures, t, wall, x1, yTop, z0, x1, 0, z0, x1, yTop, z1,
+ z0 / 100, yTop / 100, z0 / 100, 0, z1 / 100, yTop / 100);
+ t = putTri(world, uv, textures, t, wall, x1, 0, z0, x1, 0, z1, x1, yTop, z1,
+ z0 / 100, 0, z1 / 100, 0, z1 / 100, yTop / 100);
+ // -X wall
+ t = putTri(world, uv, textures, t, wall, x0, yTop, z1, x0, 0, z1, x0, yTop, z0,
+ z1 / 100, yTop / 100, z1 / 100, 0, z0 / 100, yTop / 100);
+ t = putTri(world, uv, textures, t, wall, x0, 0, z1, x0, 0, z0, x0, yTop, z0,
+ z1 / 100, 0, z0 / 100, 0, z0 / 100, yTop / 100);
+ // +Z wall (u along x)
+ t = putTri(world, uv, textures, t, wall, x0, yTop, z1, x1, yTop, z1, x0, 0, z1,
+ x0 / 100, yTop / 100, x1 / 100, yTop / 100, x0 / 100, 0);
+ t = putTri(world, uv, textures, t, wall, x1, yTop, z1, x1, 0, z1, x0, 0, z1,
+ x1 / 100, yTop / 100, x1 / 100, 0, x0 / 100, 0);
+ // -Z wall
+ t = putTri(world, uv, textures, t, wall, x1, yTop, z0, x0, yTop, z0, x1, 0, z0,
+ x1 / 100, yTop / 100, x0 / 100, yTop / 100, x1 / 100, 0);
+ t = putTri(world, uv, textures, t, wall, x0, yTop, z0, x0, 0, z0, x1, 0, z0,
+ x0 / 100, yTop / 100, x0 / 100, 0, x1 / 100, 0);
+ // roof (normal -Y, up)
+ t = putTri(world, uv, textures, t, roof, x0, yTop, z0, x1, yTop, z0, x0, yTop, z1,
+ x0 / 100, z0 / 100, x1 / 100, z0 / 100, x0 / 100, z1 / 100);
+ t = putTri(world, uv, textures, t, roof, x1, yTop, z0, x1, yTop, z1, x0, yTop, z1,
+ x1 / 100, z0 / 100, x1 / 100, z1 / 100, x0 / 100, z1 / 100);
+ return t;
+ }
+
+ /** One textured triangle with explicit UVs (primary-texture pixels). */
+ private static int putTri(final double[] world, final double[] uv,
+ final Texture[] textures, final int t,
+ final Texture tex,
+ final double ax, final double ay, final double az,
+ final double bx, final double by, final double bz,
+ final double cx, final double cy, final double cz,
+ final double ua, final double va,
+ final double ub, final double vb,
+ final double uc, final double vc) {
+ final int w = t * 9;
+ world[w] = ax; world[w + 1] = ay; world[w + 2] = az;
+ world[w + 3] = bx; world[w + 4] = by; world[w + 5] = bz;
+ world[w + 6] = cx; world[w + 7] = cy; world[w + 8] = cz;
+ final int u = t * 6;
+ uv[u] = ua; uv[u + 1] = va;
+ uv[u + 2] = ub; uv[u + 3] = vb;
+ uv[u + 4] = uc; uv[u + 5] = vc;
+ textures[t] = tex;
+ return t + 1;
+ }
+
+ /** Dark asphalt with a subtle seeded checker. */
+ private static Texture groundTexture() {
+ final Texture t = new Texture(32, 32, 0);
+ t.wrap = true;
+ final Random random = new Random(7);
+ for (int j = 0; j < 32; j++)
+ for (int i = 0; i < 32; i++) {
+ final boolean odd = ((i / 8) + (j / 8)) % 2 == 0;
+ final int v = (odd ? 70 : 62) + random.nextInt(8);
+ t.primaryBitmap.pixels[j * 32 + i] = 0xFF000000
+ | (v << 16) | (v << 8) | v;
+ }
+ return t;
+ }
+
+ /** Light brick: horizontal mortar bands + seeded value noise. */
+ private static Texture wallTexture() {
+ final Texture t = new Texture(32, 32, 0);
+ t.wrap = true;
+ final Random random = new Random(13);
+ for (int j = 0; j < 32; j++)
+ for (int i = 0; i < 32; i++) {
+ final boolean mortar = j % 8 == 0
+ || (i + (j / 8) % 2 * 4) % 8 == 0;
+ final int r = mortar ? 150 : 172 + random.nextInt(16);
+ final int g = mortar ? 140 : 152 + random.nextInt(12);
+ final int b = mortar ? 130 : 132 + random.nextInt(10);
+ t.primaryBitmap.pixels[j * 32 + i] = 0xFF000000
+ | (r << 16) | (g << 8) | b;
+ }
+ return t;
+ }
+
+ /** Flat dark roof. */
+ private static Texture roofTexture() {
+ final Texture t = new Texture(16, 16, 0);
+ t.wrap = true;
+ java.util.Arrays.fill(t.primaryBitmap.pixels, 0xFF3A3A40);
+ return t;
+ }
+
+ /** 128px radial glow texture: hot warm core, smooth falloff to
+ * transparent. Ported from the Fallout 4 loader
+ * (Fo4Lighting.sunGlowTexture) so the sun looks the same. */
+ private static Texture sunGlowTexture() {
+ final int size = 128;
+ final Texture texture = new Texture(size, size, 1);
+ texture.hasAlpha = true;
+ final int[] px = texture.primaryBitmap.pixels;
+ final double half = size / 2.0;
+ for (int y = 0; y < size; y++)
+ for (int x = 0; x < size; x++) {
+ final double dx = (x + 0.5 - half) / half;
+ final double dy = (y + 0.5 - half) / half;
+ final double r = Math.sqrt(dx * dx + dy * dy);
+ final int a;
+ if (r >= 1.0)
+ a = 0;
+ else {
+ // hot core to ~35% radius, then smooth falloff
+ final double fall = r < 0.35
+ ? 1.0 : Math.pow((1.0 - r) / 0.65, 1.8);
+ a = Math.min(255, (int) (255 * fall));
+ }
+ px[y * size + x] = (a << 24) | (255 << 16) | (240 << 8) | 205;
+ }
+ return texture;
+ }
+}
import eu.svjatoslav.aukio.e3d.headless.PixelAssertions;
import eu.svjatoslav.aukio.e3d.headless.Snapshot;
import eu.svjatoslav.aukio.e3d.renderer.raster.Color;
+import eu.svjatoslav.aukio.e3d.renderer.raster.RenderingContext;
import eu.svjatoslav.aukio.e3d.renderer.raster.ShapeCollection;
import eu.svjatoslav.aukio.e3d.renderer.raster.lighting.LightingManager;
import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.LightmappedCompositeShape;
/**
* @param poseOverride when non-null, render from this pose string instead
* of the camera the builder positioned
+ * @param backgroundArgb background fill before painting (0 = black,
+ * the historical default)
*/
private record Entry(String name, SceneBuilder builder, String poseOverride,
- int channelTolerance) {
+ int channelTolerance, int backgroundArgb) {
Entry(final String name, final SceneBuilder builder) {
- this(name, builder, null, CHANNEL_TOLERANCE);
+ this(name, builder, null, CHANNEL_TOLERANCE, 0);
}
Entry(final String name, final SceneBuilder builder, final String poseOverride) {
- this(name, builder, poseOverride, CHANNEL_TOLERANCE);
+ this(name, builder, poseOverride, CHANNEL_TOLERANCE, 0);
}
Entry(final String name, final SceneBuilder builder, final int channelTolerance) {
- this(name, builder, null, channelTolerance);
+ this(name, builder, null, channelTolerance, 0);
+ }
+
+ Entry(final String name, final SceneBuilder builder, final int channelTolerance,
+ final int backgroundArgb) {
+ this(name, builder, null, channelTolerance, backgroundArgb);
}
}
new Entry("light-sources", LightSourceDemo::buildScene),
new Entry("house-flat", DemoGoldens::buildHouseScene, HOUSE_DEFAULT_POSE),
new Entry("house-straddle", DemoGoldens::buildHouseScene, HOUSE_STRADDLE_POSE),
+ new Entry("village", eu.svjatoslav.aukio.e3d.examples.village_demo.VillageDemo::buildScene),
+ new Entry("gi-radius", eu.svjatoslav.aukio.e3d.examples.gi_radius_demo.GiRadiusDemo::buildScene,
+ CHANNEL_TOLERANCE,
+ eu.svjatoslav.aukio.e3d.examples.gi_radius_demo.GiRadiusDemo.SKY_BACKGROUND_ARGB),
new Entry("fullscreen", FullscreenDemo::buildScene),
new Entry("stereo", StereoDemo::buildScene),
new Entry("head-tracking", HeadTrackingDemo::buildScene),
final Camera camera = new Camera();
entry.builder().build(scene, lighting, camera);
- if (entry.poseOverride() != null)
- return Snapshot.render(scene, lighting, entry.poseOverride(), WIDTH, HEIGHT);
- return Snapshot.render(scene, lighting, camera, WIDTH, HEIGHT);
+ final Camera shotCamera = entry.poseOverride() != null
+ ? Snapshot.cameraFromPose(entry.poseOverride()) : camera;
+ final RenderingContext ctx = new RenderingContext(WIDTH, HEIGHT, 1);
+ ctx.lightingManager = lighting;
+ Snapshot.renderInto(scene, shotCamera, ctx, entry.backgroundArgb());
+ return ctx.getImage();
}
/** Runs an external tool, discarding its output; true on exit 0. */
*/
public class HouseDemo {
- /** Room width along X in world units. */
- private static final double ROOM_WIDTH = 800;
-
- /** Room depth along Z in world units. */
- private static final double ROOM_DEPTH = 700;
-
/** Wall height in world units. */
private static final double WALL_HEIGHT = 320;
- /** Grid cell size for wall/floor/ceiling subdivision. */
- private static final double CELL = 100;
-
/** Thickness of partition walls between rooms (two surfaces this far apart). */
private static final double PARTITION_THICKNESS = 10;
/** Doorway height in world units (aligns with 80-unit vertical grid). */
private static final double DOOR_HEIGHT = 240;
- /** Vertical grid step for walls. */
- private static final double WALL_VCELL = 80;
-
- /** Eye height of the starting camera (above floor, up is -Y). */
- private static final double EYE_HEIGHT = 170;
-
// House layout: three rooms side by side along X.
// X from -1200 to +1200, partitions at -400 and +400.
// Z from -350 to +350. Floor at Y=0, ceiling at Y=-320 (up is -Y).
import eu.svjatoslav.aukio.e3d.examples.galaxy_demo.PointCloudDemo;
import eu.svjatoslav.aukio.e3d.examples.graph_demo.MathGraphsDemo;
import eu.svjatoslav.aukio.e3d.examples.house_demo.HouseDemo;
+import eu.svjatoslav.aukio.e3d.examples.village_demo.VillageDemo;
import eu.svjatoslav.aukio.e3d.examples.obj_loader_demo.ObjLoaderDemo;
import eu.svjatoslav.aukio.e3d.examples.terrain_demo.TerrainDemo;
"Walkable 3-room house, each room lit by a different color",
new ShowHouseDemo()),
+ new DemoEntry("Village (GI stress)",
+ "Multi-house, multi-floor night village — GI bake scaling test bed",
+ new ShowVillageDemo()),
+
+ new DemoEntry("GI radius bubble",
+ "48 000-unit tower district of lean mesh blocks — lightmaps only near the camera",
+ new ShowGiRadiusDemo()),
+
new DemoEntry("Fullscreen demo",
"Warp starfield in borderless fullscreen — press F11 to toggle",
new ShowFullscreenDemo()),
}
}
+ private static class ShowVillageDemo extends AbstractAction {
+ ShowVillageDemo() {
+ putValue(NAME, "Village (GI stress)");
+ }
+
+ @Override
+ public void actionPerformed(final ActionEvent e) {
+ VillageDemo.main(null);
+ }
+ }
+
+ private static class ShowGiRadiusDemo extends AbstractAction {
+ ShowGiRadiusDemo() {
+ putValue(NAME, "GI radius bubble");
+ }
+
+ @Override
+ public void actionPerformed(final ActionEvent e) {
+ eu.svjatoslav.aukio.e3d.examples.gi_radius_demo.GiRadiusDemo.main(null);
+ }
+ }
+
private static class ShowWindingOrder extends AbstractAction {
ShowWindingOrder() {
putValue(NAME, "Winding order");
--- /dev/null
+/*
+ * Aukio 3D engine demos. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.aukio.e3d.examples.village_demo;
+
+import eu.svjatoslav.aukio.e3d.geometry.Camera;
+import eu.svjatoslav.aukio.e3d.geometry.Point3D;
+import eu.svjatoslav.aukio.e3d.gui.ViewFrame;
+import eu.svjatoslav.aukio.e3d.gui.ViewPanel;
+import eu.svjatoslav.aukio.e3d.renderer.raster.Color;
+import eu.svjatoslav.aukio.e3d.renderer.raster.ShapeCollection;
+import eu.svjatoslav.aukio.e3d.renderer.raster.lighting.LightSource;
+import eu.svjatoslav.aukio.e3d.renderer.raster.lighting.LightingManager;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.LightmappedCompositeShape;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.LightSourceMarker;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.solid.SolidPolygonCube;
+import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.solid.SolidPolygonRectangularBox;
+
+import static eu.svjatoslav.aukio.e3d.geometry.Point3D.point;
+import static eu.svjatoslav.aukio.e3d.renderer.raster.Color.hex;
+
+/**
+ * GI scaling test bed: a night-time village of multiple houses, each
+ * one to three floors, on an open ground plane with streets, street
+ * lamps and a distant moon.
+ *
+ * <p>Purpose: the House demo bakes in seconds, so it cannot stress the
+ * GI bake scheduling. This scene is big enough (~10k lightmapped
+ * quads, ~45 lamps) that generation times are measurable, which makes
+ * it the playground for ideas about staying fast and incremental on
+ * big worlds (distance-based lightmap resolution, incremental rebakes,
+ * work ordering).</p>
+ *
+ * <p>Structure: every floor of every house is the House-demo layout
+ * (three rooms, two partition walls with doorways), with window
+ * openings in the exterior walls so interior lamps spill onto the
+ * street. One colored lamp per floor. The whole scene takes the GI
+ * path: interiors AND exteriors (two-sided walls get lightmapped
+ * backs from the engine wrap), and the ground plane bakes in its own
+ * composite at coarse units-per-texel so street lamps pool on the
+ * asphalt and moonlight grades the grass. Only far houses (via
+ * {@code --lm=N}) take the plain per-polygon path. Street lamps and
+ * house lamps together exceed the engine's default tracked-light
+ * cap, so this demo sizes {@code e3d.gi.maxLights} to its lamp count
+ * before the bake starts.</p>
+ *
+ * <p><b>Run:</b></p>
+ * <pre>{@code
+ * java -cp target/aukio-3d-demos.jar eu.svjatoslav.aukio.e3d.examples.village_demo.VillageDemo \
+ * [--cols=4] [--rows=3] [--floors=3] [--units=6.0] [--threads=4] [--lm=all|N]
+ * }</pre>
+ *
+ * <p>{@code --lm=N} lightmaps only the N houses nearest the village
+ * center (the rest get plain per-polygon GI) — the manual knob for
+ * distance-based GI resolution experiments.</p>
+ */
+public class VillageDemo {
+
+ // House layout (same as HouseDemo): three rooms along X.
+ private static final double WALL_HEIGHT = 320;
+ private static final double SLAB = 20;
+ private static final double STOREY = WALL_HEIGHT + SLAB;
+ private static final double PARTITION_THICKNESS = 10;
+ private static final double DOOR_HALF_WIDTH = 100;
+ private static final double DOOR_HEIGHT = 240;
+ private static final double HX = 1200; // house half width along X
+ private static final double HZ = 350; // house half depth along Z
+
+ // Village layout.
+ private static final double PITCH_X = 3200;
+ private static final double PITCH_Z = 2200;
+ private static final double GROUND_MARGIN = 4000;
+ private static final double GROUND_Y = 5; // just below house floors (Y=0)
+ private static final double GROUND_CELL = 800;
+ private static final double STREET_HALF_WIDTH = 700;
+
+ /** Street centerline distance south of each house row. */
+ private static final double STREET_OFFSET = 1100;
+
+ private static final Color WALL_COLOR = hex("D8D0C0");
+ private static final Color FLOOR_COLOR = hex("989080");
+ private static final Color CEILING_COLOR = hex("E8E8E8");
+ private static final Color GRASS_COLOR = hex("3E4A34");
+ private static final Color ASPHALT_COLOR = hex("383840");
+
+ /** Interior lamp palette, cycled by (house, floor). */
+ private static final Color[] LAMP_PALETTE = {
+ hex("FFD890"), hex("D8E4FF"), hex("FFB060"),
+ hex("90FFB0"), hex("E0A0FF"), hex("FFF0A0"),
+ };
+
+ /** Decides whether a grid cell is left out (door/window opening). */
+ private interface CellSkipper {
+ boolean skip(int i, int j, int uSegs, int vSegs);
+ }
+
+ private static final CellSkipper NO_SKIP = (i, j, u, v) -> false;
+
+ /** Partition doorway: centered, from the floor up to DOOR_HEIGHT. */
+ private static final CellSkipper DOOR_SKIP = (i, j, u, v) -> {
+ final double cellCenterU = (i + 0.5) * (2 * HZ) / u;
+ final double cellBaseV = j * WALL_HEIGHT / v;
+ return Math.abs(cellCenterU - HZ) < DOOR_HALF_WIDTH && cellBaseV < DOOR_HEIGHT;
+ };
+
+ /** Window band in exterior walls: every 3rd cell, 80..240 above the floor. */
+ private static final CellSkipper WINDOW_SKIP = (i, j, u, v) ->
+ i % 3 == 1 && j >= 1 && j <= 2;
+
+ /** Ground-floor front wall: windows plus a centered street door. */
+ private static final CellSkipper WINDOW_DOOR_SKIP = (i, j, u, v) -> {
+ if (WINDOW_SKIP.skip(i, j, u, v))
+ return true;
+ final double cellCenterU = (i + 0.5) * (2 * HX) / u;
+ final double cellBaseV = j * WALL_HEIGHT / v;
+ return Math.abs(cellCenterU - HX) < DOOR_HALF_WIDTH && cellBaseV < DOOR_HEIGHT;
+ };
+
+ /** Village scale knobs, parsed from command line. */
+ public static final class Spec {
+ public int cols = 4;
+ public int rows = 3;
+ public int maxFloors = 3;
+ public double unitsPerTexel = 6.0;
+ /** Ground lightmap resolution (cells are 800 units — coarse suffices). */
+ public double groundUnitsPerTexel = 12.5;
+ public int threads = 4;
+ /** Lightmapped houses nearest the center; -1 = all. */
+ public int lightmappedHouses = -1;
+ public boolean streetLights = true;
+
+ public int floorsOf(final int col, final int row) {
+ return 1 + ((col * 2 + row) % maxFloors);
+ }
+
+ public int totalLamps() {
+ int lamps = 1; // moon
+ for (int row = 0; row < rows; row++)
+ for (int col = 0; col < cols; col++)
+ lamps += floorsOf(col, row);
+ if (streetLights)
+ lamps += (rows + 1) * (cols + 1);
+ return lamps;
+ }
+ }
+
+ /** Entry point. See class javadoc for the accepted --flags. */
+ public static void main(final String[] args) {
+ final Spec spec = parseArgs(args);
+
+ final ViewFrame viewFrame = new ViewFrame("Village Demo (GI stress)");
+ final ViewPanel viewPanel = viewFrame.getViewPanel();
+
+ // The lamp count exceeds the engine's default tracked-light
+ // cap (16); size it to this scene before the first bake.
+ System.setProperty("e3d.gi.maxLights", Integer.toString(spec.totalLamps()));
+
+ buildVillage(viewPanel.getRootShapeCollection(),
+ viewPanel.getLightingManager(), spec);
+
+ // Street-level view down the first street, houses on the left.
+ viewPanel.getCamera().getTransform().set(
+ -2600, -220, -1100,
+ -1.52, -0.04, 0.00);
+
+ viewPanel.getLightingManager().setAmbientLight(hex("101018"));
+
+ System.out.println("Village: " + spec.cols + "x" + spec.rows + " houses, up to "
+ + spec.maxFloors + " floors, " + spec.totalLamps() + " lamps, "
+ + spec.unitsPerTexel + " units/texel, "
+ + (spec.lightmappedHouses < 0 ? "all" : Integer.toString(spec.lightmappedHouses))
+ + " lightmapped houses, GI threads " + spec.threads);
+
+ viewPanel.enableGlobalIllumination(spec.threads);
+ viewPanel.repaintDuringNextViewUpdate();
+ }
+
+ private static Spec parseArgs(final String[] args) {
+ final Spec spec = new Spec();
+ if (args == null)
+ return spec;
+ for (final String arg : args) {
+ if (arg.startsWith("--cols="))
+ spec.cols = Integer.parseInt(arg.substring(7));
+ else if (arg.startsWith("--rows="))
+ spec.rows = Integer.parseInt(arg.substring(7));
+ else if (arg.startsWith("--floors="))
+ spec.maxFloors = Integer.parseInt(arg.substring(9));
+ else if (arg.startsWith("--units="))
+ spec.unitsPerTexel = Double.parseDouble(arg.substring(8));
+ else if (arg.startsWith("--ground-units="))
+ spec.groundUnitsPerTexel = Double.parseDouble(arg.substring(15));
+ else if (arg.startsWith("--threads="))
+ spec.threads = Integer.parseInt(arg.substring(10));
+ else if (arg.startsWith("--lm="))
+ spec.lightmappedHouses = "all".equals(arg.substring(5))
+ ? -1 : Integer.parseInt(arg.substring(5));
+ else if ("--no-street-lights".equals(arg))
+ spec.streetLights = false;
+ }
+ return spec;
+ }
+
+ /** Golden-harness entry: deterministic default village. */
+ public static void buildScene(final ShapeCollection shapes,
+ final LightingManager lighting,
+ final Camera camera) {
+ buildVillage(shapes, lighting, new Spec());
+ lighting.setAmbientLight(hex("101018"));
+ camera.getTransform().set(
+ -2600, -220, -1100,
+ -1.52, -0.04, 0.00);
+ }
+
+ /**
+ * Builds the whole village: ground, houses, street lamps, moon.
+ * Public so headless GI harnesses can construct the scene without
+ * a window.
+ */
+ public static void buildVillage(final ShapeCollection shapes,
+ final LightingManager lighting,
+ final Spec spec) {
+ final LightmappedCompositeShape lightmapped = new LightmappedCompositeShape();
+ lightmapped.setLightmappingEnabled(true);
+ lightmapped.setLightmapUnitsPerTexel(spec.unitsPerTexel);
+
+ // The ground bakes too: its own lightmapped composite at a
+ // much coarser units-per-texel (cells are 800 units; street
+ // lamp pools and moonlight gradients need no wall-level
+ // detail). Two-sided wall sources produce lightmapped exterior
+ // backs automatically (engine wrap), so facades bake as well.
+ final LightmappedCompositeShape ground = new LightmappedCompositeShape();
+ ground.setLightmappingEnabled(true);
+ ground.setLightmapUnitsPerTexel(spec.groundUnitsPerTexel);
+ addGround(ground, spec);
+
+ // Far houses (--lm=N): plain per-polygon GI (centroid
+ // resolution), no lightmap texel arrays at all.
+ final LightmappedCompositeShape plain = new LightmappedCompositeShape();
+
+ final double centerX = (spec.cols - 1) * PITCH_X / 2 + (spec.rows % 2) * 400;
+ final double centerZ = (spec.rows - 1) * PITCH_Z / 2;
+
+ for (int row = 0; row < spec.rows; row++) {
+ for (int col = 0; col < spec.cols; col++) {
+ final double ox = col * PITCH_X + (row % 2) * 800;
+ final double oz = row * PITCH_Z;
+ final int floors = spec.floorsOf(col, row);
+ final boolean lm = spec.lightmappedHouses < 0
+ || rankFromCenter(col, row, spec) < spec.lightmappedHouses;
+ final AbstractCompositeShape target = lm ? lightmapped : plain;
+ for (int floor = 0; floor < floors; floor++)
+ buildStorey(target, ox, oz, floor, col, row);
+ }
+ }
+
+ shapes.addShape(lightmapped);
+ shapes.addShape(ground);
+ shapes.addShape(plain);
+
+ addInteriorLamps(lighting, shapes, spec);
+ if (spec.streetLights)
+ addStreetLights(lighting, shapes, plain, spec);
+ addMoon(lighting, shapes, centerX, centerZ);
+ }
+
+ /** House rank by distance from the village center (for --lm=N). */
+ private static int rankFromCenter(final int col, final int row, final Spec spec) {
+ int rank = 0;
+ final double myX = col * PITCH_X + (row % 2) * 800;
+ final double myZ = row * PITCH_Z;
+ final double cx = (spec.cols - 1) * PITCH_X / 2 + (spec.rows % 2) * 400;
+ final double cz = (spec.rows - 1) * PITCH_Z / 2;
+ final double myD2 = (myX - cx) * (myX - cx) + (myZ - cz) * (myZ - cz);
+ for (int r = 0; r < spec.rows; r++)
+ for (int c = 0; c < spec.cols; c++) {
+ final double x = c * PITCH_X + (r % 2) * 800;
+ final double z = r * PITCH_Z;
+ final double d2 = (x - cx) * (x - cx) + (z - cz) * (z - cz);
+ if (d2 < myD2 || (d2 == myD2 && (r < row || (r == row && c < col))))
+ rank++;
+ }
+ return rank;
+ }
+
+ /**
+ * Builds one storey of one house: the House-demo three-room layout
+ * at (ox, oz), floor index {@code floor} (0 = ground floor).
+ */
+ private static void buildStorey(final AbstractCompositeShape shapes,
+ final double ox, final double oz, final int floor,
+ final int col, final int row) {
+ final double yBase = -floor * STOREY;
+ final double yTop = yBase - WALL_HEIGHT;
+
+ final double x0 = ox - HX, x1 = ox + HX;
+ final double z0 = oz - HZ, z1 = oz + HZ;
+
+ // Floor slab (normal up, into the room) and ceiling (normal down).
+ addGridSurface(shapes, point(x0, yBase, z0),
+ point(2 * HX, 0, 0), point(0, 0, 2 * HZ),
+ 24, 7, FLOOR_COLOR, point(0, -1, 0), NO_SKIP);
+ addGridSurface(shapes, point(x0, yTop, z0),
+ point(2 * HX, 0, 0), point(0, 0, 2 * HZ),
+ 24, 7, CEILING_COLOR, point(0, 1, 0), NO_SKIP);
+
+ // Front wall (z0, normal +Z) and back wall (z1, normal -Z),
+ // windows on every floor; street door on the ground floor front.
+ addGridSurface(shapes, point(x0, yBase, z0),
+ point(2 * HX, 0, 0), point(0, -WALL_HEIGHT, 0),
+ 24, 4, WALL_COLOR, point(0, 0, 1),
+ floor == 0 ? WINDOW_DOOR_SKIP : WINDOW_SKIP);
+ addGridSurface(shapes, point(x0, yBase, z1),
+ point(2 * HX, 0, 0), point(0, -WALL_HEIGHT, 0),
+ 24, 4, WALL_COLOR, point(0, 0, -1), WINDOW_SKIP);
+
+ // Side walls (normals into the house), windows.
+ addGridSurface(shapes, point(x0, yBase, z0),
+ point(0, 0, 2 * HZ), point(0, -WALL_HEIGHT, 0),
+ 7, 4, WALL_COLOR, point(1, 0, 0), WINDOW_SKIP);
+ addGridSurface(shapes, point(x1, yBase, z0),
+ point(0, 0, 2 * HZ), point(0, -WALL_HEIGHT, 0),
+ 7, 4, WALL_COLOR, point(-1, 0, 0), WINDOW_SKIP);
+
+ // Partition walls with doorways, two surfaces back to back.
+ addPartition(shapes, ox - 400, yBase, oz);
+ addPartition(shapes, ox + 400, yBase, oz);
+
+ // Furniture: one big cube per floor, position/color varied.
+ final int variant = (col + row * 2 + floor) % 3;
+ addCube(shapes, point(ox - 800 + variant * 600, yBase - 70, oz + 150),
+ 70, LAMP_PALETTE[(col * 5 + row * 3 + floor + 2) % LAMP_PALETTE.length]);
+ }
+
+ private static void addPartition(final AbstractCompositeShape shapes,
+ final double x, final double yBase, final double oz) {
+ final double half = PARTITION_THICKNESS / 2;
+ addGridSurface(shapes, point(x - half, yBase, oz - HZ),
+ point(0, 0, 2 * HZ), point(0, -WALL_HEIGHT, 0),
+ 7, 4, WALL_COLOR, point(-1, 0, 0), DOOR_SKIP);
+ addGridSurface(shapes, point(x + half, yBase, oz - HZ),
+ point(0, 0, 2 * HZ), point(0, -WALL_HEIGHT, 0),
+ 7, 4, WALL_COLOR, point(1, 0, 0), DOOR_SKIP);
+ }
+
+ /** Ground plane, plain-shaded: asphalt streets on a grass field. */
+ private static void addGround(final AbstractCompositeShape shapes, final Spec spec) {
+ final double x0 = -GROUND_MARGIN;
+ final double x1 = (spec.cols - 1) * PITCH_X + 800 + GROUND_MARGIN;
+ final double z0 = -GROUND_MARGIN;
+ final double z1 = (spec.rows - 1) * PITCH_Z + GROUND_MARGIN;
+ final int uSegs = (int) ((x1 - x0) / GROUND_CELL);
+ final int vSegs = (int) ((z1 - z0) / GROUND_CELL);
+
+ for (int i = 0; i < uSegs; i++) {
+ for (int j = 0; j < vSegs; j++) {
+ final double qx0 = x0 + i * GROUND_CELL;
+ final double qz0 = z0 + j * GROUND_CELL;
+ final double cx = qx0 + GROUND_CELL / 2;
+ final double cz = qz0 + GROUND_CELL / 2;
+ final Color color = onStreet(cx, cz, spec) ? ASPHALT_COLOR : GRASS_COLOR;
+ final SolidPolygon quad = new SolidPolygon(new Point3D[]{
+ point(qx0, GROUND_Y, qz0),
+ point(qx0 + GROUND_CELL, GROUND_Y, qz0),
+ point(qx0 + GROUND_CELL, GROUND_Y, qz0 + GROUND_CELL),
+ point(qx0, GROUND_Y, qz0 + GROUND_CELL),
+ }, color);
+ quad.setShadingEnabled(true);
+ shapes.addShape(quad);
+ }
+ }
+ }
+
+ /** Streets run along X, one south of every house row plus one north of row 0. */
+ private static boolean onStreet(final double cx, final double cz, final Spec spec) {
+ for (int s = -1; s < spec.rows; s++)
+ if (Math.abs(cz - (s * PITCH_Z + STREET_OFFSET)) < STREET_HALF_WIDTH)
+ return true;
+ return false;
+ }
+
+ private static void addInteriorLamps(final LightingManager lighting,
+ final ShapeCollection shapes, final Spec spec) {
+ for (int row = 0; row < spec.rows; row++)
+ for (int col = 0; col < spec.cols; col++) {
+ final double ox = col * PITCH_X + (row % 2) * 800;
+ final double oz = row * PITCH_Z;
+ for (int floor = 0; floor < spec.floorsOf(col, row); floor++) {
+ final double yTop = -floor * STOREY - WALL_HEIGHT;
+ final double lampX = ox + (new double[]{-800, 0, 800})[floor % 3];
+ final Color color =
+ LAMP_PALETTE[(col * 5 + row * 3 + floor) % LAMP_PALETTE.length];
+ addLight(lighting, shapes, point(lampX, yTop + 80, oz), color, 6.0);
+ }
+ }
+ }
+
+ /** Poles with cool white heads along every street, alternating sides. */
+ private static void addStreetLights(final LightingManager lighting,
+ final ShapeCollection shapes,
+ final AbstractCompositeShape plain, final Spec spec) {
+ final double xMax = (spec.cols - 1) * PITCH_X + 800;
+ for (int s = -1; s < spec.rows; s++) {
+ final double streetZ = s * PITCH_Z + STREET_OFFSET;
+ int index = 0;
+ for (double x = -1600; x <= xMax + 1600; x += PITCH_X) {
+ final double z = streetZ + (index % 2 == 0 ? -500 : 500);
+ // Pole: thin box from the ground up to Y=-430.
+ final SolidPolygonRectangularBox pole = new SolidPolygonRectangularBox(
+ point(x - 15, GROUND_Y, z - 15),
+ point(x + 15, -430, z + 15),
+ hex("282828"));
+ pole.setShadingEnabled(true);
+ plain.addShape(pole);
+ addLight(lighting, shapes, point(x, -460, z), hex("C8D8FF"), 7.0);
+ index++;
+ }
+ }
+ }
+
+ /** Distant cool moon: intensity boosted to defeat attenuation. */
+ private static void addMoon(final LightingManager lighting, final ShapeCollection shapes,
+ final double cx, final double cz) {
+ final double mx = cx + 20000, my = -30000, mz = cz + 12000;
+ final double d2 = 20000 * 20000 + 30000 * 30000 + 12000 * 12000;
+ final double intensity = 1.2 * (1 + 0.0001 * d2);
+ addLight(lighting, shapes, point(mx, my, mz), hex("A0B8FF"), intensity);
+ }
+
+ private static void addLight(final LightingManager lighting, final ShapeCollection shapes,
+ final Point3D position, final Color color,
+ final double intensity) {
+ lighting.addLight(new LightSource(position, color, intensity));
+ shapes.addShape(new LightSourceMarker(position, color));
+ }
+
+ private static void addCube(final AbstractCompositeShape shapes, final Point3D center,
+ final double size, final Color color) {
+ final SolidPolygonCube cube = new SolidPolygonCube(center, size, color);
+ cube.setShadingEnabled(true);
+ cube.setBackfaceCulling(true);
+ shapes.addShape(cube);
+ }
+
+ /**
+ * Adds a flat surface subdivided into a grid of quads, skipping
+ * cells the skipper rejects (door/window openings). Same winding
+ * convention as HouseDemo: the normal always points along
+ * {@code inward}.
+ */
+ private static void addGridSurface(final AbstractCompositeShape shapes,
+ final Point3D corner,
+ final Point3D edgeU, final Point3D edgeV,
+ final int uSegs, final int vSegs,
+ final Color color, final Point3D inward,
+ final CellSkipper skipper) {
+ final double du = 1.0 / uSegs;
+ final double dv = 1.0 / vSegs;
+
+ for (int i = 0; i < uSegs; i++) {
+ for (int j = 0; j < vSegs; j++) {
+ if (skipper.skip(i, j, uSegs, vSegs))
+ continue;
+
+ final Point3D p00 = lerp(corner, edgeU, edgeV, i * du, j * dv);
+ final Point3D p10 = lerp(corner, edgeU, edgeV, (i + 1) * du, j * dv);
+ final Point3D p11 = lerp(corner, edgeU, edgeV, (i + 1) * du, (j + 1) * dv);
+ final Point3D p01 = lerp(corner, edgeU, edgeV, i * du, (j + 1) * dv);
+
+ final Point3D eu = p10.withSubtracted(p00);
+ final Point3D ev = p01.withSubtracted(p00);
+ final Point3D normal = eu.cross(ev);
+
+ final Point3D[] vertices = normal.dot(inward) > 0
+ ? new Point3D[]{p00, p10, p11, p01}
+ : new Point3D[]{p00, p01, p11, p10};
+
+ final SolidPolygon quad = new SolidPolygon(vertices, color);
+ quad.setShadingEnabled(true);
+ shapes.addShape(quad);
+ }
+ }
+ }
+
+ /** Returns corner + fu*edgeU + fv*edgeV as a new point. */
+ private static Point3D lerp(final Point3D corner, final Point3D edgeU,
+ final Point3D edgeV, final double fu, final double fv) {
+ return point(
+ corner.x + edgeU.x * fu + edgeV.x * fv,
+ corner.y + edgeU.y * fu + edgeV.y * fv,
+ corner.z + edgeU.z * fu + edgeV.z * fv);
+ }
+}