Fog

Play Demo

Fog Demo — Distance-based fog via WorldEnvironment.

Demonstrates:

  • Distance fog with adjustable density/start/end

  • Fog colour control

  • Height fog toggle

  • Fog mode switching (linear / exponential / exponential_squared)

  • Bloom + tonemap combined with fog

Controls: A / D - Orbit camera left / right W / S - Zoom in / out Q / E - Raise / lower camera 1 - Toggle fog 2 - Toggle bloom 3 - Cycle fog mode Up / Down - Adjust fog density Left / Right - Adjust tonemap exposure Escape - Quit

Run: uv run python packages/graphics/examples/3d_fog.py

Source Code

  1"""Fog Demo — Distance-based fog via WorldEnvironment.
  2
  3Demonstrates:
  4  - Distance fog with adjustable density/start/end
  5  - Fog colour control
  6  - Height fog toggle
  7  - Fog mode switching (linear / exponential / exponential_squared)
  8  - Bloom + tonemap combined with fog
  9
 10Controls:
 11    A / D       - Orbit camera left / right
 12    W / S       - Zoom in / out
 13    Q / E       - Raise / lower camera
 14    1           - Toggle fog
 15    2           - Toggle bloom
 16    3           - Cycle fog mode
 17    Up / Down   - Adjust fog density
 18    Left / Right - Adjust tonemap exposure
 19    Escape      - Quit
 20
 21Run: uv run python packages/graphics/examples/3d_fog.py
 22"""
 23
 24
 25import math
 26
 27import numpy as np
 28
 29from simvx.core import (
 30    Camera3D,
 31    DirectionalLight3D,
 32    Input,
 33    InputMap,
 34    Key,
 35    Material,
 36    Mesh,
 37    MeshInstance3D,
 38    Node,
 39    Text2D,
 40    WorldEnvironment,
 41)
 42from simvx.graphics import App
 43
 44FOG_MODES = ["linear", "exponential", "exponential_squared"]
 45
 46
 47class FogDemo(Node):
 48    def ready(self):
 49        InputMap.add_action("orbit_left", [Key.A])
 50        InputMap.add_action("orbit_right", [Key.D])
 51        InputMap.add_action("pitch_up", [Key.W])
 52        InputMap.add_action("pitch_down", [Key.S])
 53        InputMap.add_action("zoom_in", [Key.Q])
 54        InputMap.add_action("zoom_out", [Key.E])
 55        InputMap.add_action("toggle_fog", [Key.KEY_1])
 56        InputMap.add_action("toggle_bloom", [Key.KEY_2])
 57        InputMap.add_action("cycle_fog_mode", [Key.KEY_3])
 58        InputMap.add_action("density_up", [Key.UP])
 59        InputMap.add_action("density_down", [Key.DOWN])
 60        InputMap.add_action("exposure_up", [Key.LEFT])
 61        InputMap.add_action("exposure_down", [Key.RIGHT])
 62        InputMap.add_action("quit", [Key.ESCAPE])
 63
 64        self._yaw = 30.0
 65        self._pitch = 25.0
 66        self._distance = 25.0
 67        self._target = (0.0, 2.0, 0.0)
 68        self._fog_mode_idx = 1  # exponential
 69
 70        self._cam = Camera3D(name="Camera", fov=60, near=0.1, far=200.0)
 71        self.add_child(self._cam)
 72
 73        # WorldEnvironment — fog + bloom + tonemap. Warm orange fog contrasts the
 74        # blue gradient sky so distance fog is obvious when toggled, and bloom
 75        # threshold is low enough that the strongly-emissive balls clearly halo.
 76        self._env = self.add_child(WorldEnvironment())
 77        self._env.fog_enabled = True
 78        self._env.fog_colour = (0.95, 0.55, 0.25, 1.0)
 79        self._env.fog_density = 0.12
 80        self._env.fog_start = 2.0
 81        self._env.fog_end = 50.0
 82        self._env.fog_mode = "exponential"
 83        self._env.bloom_enabled = True
 84        self._env.bloom_threshold = 0.8
 85        self._env.bloom_intensity = 1.2
 86        self._env.bloom_soft_knee = 0.7
 87        self._env.tonemap_exposure = 0.9
 88
 89        # Lighting
 90        key = DirectionalLight3D(name="KeyLight", intensity=1.5)
 91        key.look_at((-1.0, -2.0, -1.0))
 92        self.add_child(key)
 93
 94        fill = DirectionalLight3D(name="FillLight", intensity=0.3, colour=(0.6, 0.7, 1.0))
 95        fill.look_at((1.0, -1.0, 2.0))
 96        self.add_child(fill)
 97
 98        # Ground plane
 99        ground = MeshInstance3D(name="Ground", mesh=Mesh.cube())
100        ground.material = Material(colour=(0.3, 0.35, 0.3), roughness=0.9, metallic=0.0)
101        ground.scale = (50.0, 0.1, 50.0)
102        ground.position = (0.0, -0.05, 0.0)
103        self.add_child(ground)
104
105        # Strongly-emissive metallic orbs to show bloom. ``emissive_colour`` is
106        # (r, g, b, intensity) — the intensity multiplier pushes the fragment
107        # HDR value well above the bloom threshold so the halo is unmistakable
108        # when bloom is on and disappears entirely when toggled off.
109        emissive_specs = [
110            ((1.0, 0.15, 0.05), 6.0),   # fiery red
111            ((0.05, 1.0, 0.25), 5.0),   # emerald
112            ((0.2, 0.3, 1.0),   5.0),   # electric blue
113            ((1.0, 0.8, 0.1),   6.0),   # amber
114            ((1.0, 0.1, 0.9),   5.0),   # magenta
115            ((0.1, 0.9, 1.0),   5.0),   # cyan
116        ]
117        for i, (rgb, intensity) in enumerate(emissive_specs):
118            angle = i * math.pi * 2 / len(emissive_specs)
119            mat = Material(
120                colour=(rgb[0] * 0.2, rgb[1] * 0.2, rgb[2] * 0.2),
121                roughness=0.25, metallic=0.9,
122                emissive_colour=(*rgb, intensity),
123            )
124            obj = MeshInstance3D(name=f"Emissive{i}", mesh=Mesh.sphere(radius=0.6), material=mat)
125            obj.position = (math.cos(angle) * 6.0, 1.0, math.sin(angle) * 6.0)
126            self.add_child(obj)
127
128        # Scattered objects at various distances — fog fades distant ones
129        colours = [
130            (0.9, 0.2, 0.2), (0.2, 0.9, 0.2), (0.2, 0.2, 0.9), (0.9, 0.9, 0.2),
131            (0.9, 0.2, 0.9), (0.2, 0.9, 0.9), (1.0, 0.5, 0.0), (0.5, 0.0, 1.0),
132        ]
133        rng = np.random.default_rng(42)
134        for i in range(30):
135            colour = colours[i % len(colours)]
136            mat = Material(colour=colour, roughness=0.4, metallic=0.3)
137            if i % 3 == 0:
138                mesh = Mesh.sphere(radius=0.8)
139            elif i % 3 == 1:
140                mesh = Mesh.cube()
141            else:
142                mesh = Mesh.cylinder(radius=0.5, height=2.0)
143            obj = MeshInstance3D(name=f"Obj{i}", mesh=mesh, material=mat)
144            obj.position = (rng.uniform(-20, 20), 0.8 if i % 3 != 2 else 1.0, rng.uniform(-20, 20))
145            self.add_child(obj)
146
147        # Tall pillars (visible at distance, good for fog depth testing)
148        pillar_mat = Material(colour=(0.6, 0.6, 0.65), roughness=0.5, metallic=0.1)
149        for i in range(8):
150            angle = i * math.pi * 2 / 8
151            pillar = MeshInstance3D(name=f"Pillar{i}", mesh=Mesh.cube(), material=pillar_mat)
152            pillar.scale = (0.8, 6.0, 0.8)
153            pillar.position = (math.cos(angle) * 15.0, 3.0, math.sin(angle) * 15.0)
154            self.add_child(pillar)
155
156        self._hud = self.add_child(Text2D(name="HUD", text="", font_scale=1.2, x=10.0, y=10.0))
157        self._update_camera()
158
159    def process(self, dt):
160        if Input.is_action_pressed("orbit_left"):
161            self._yaw += 60.0 * dt
162        if Input.is_action_pressed("orbit_right"):
163            self._yaw -= 60.0 * dt
164        if Input.is_action_pressed("zoom_in"):
165            self._distance = max(5.0, self._distance - 10.0 * dt)
166        if Input.is_action_pressed("zoom_out"):
167            self._distance = min(60.0, self._distance + 10.0 * dt)
168        if Input.is_action_pressed("pitch_up"):
169            self._pitch = min(80.0, self._pitch + 30.0 * dt)
170        if Input.is_action_pressed("pitch_down"):
171            self._pitch = max(-10.0, self._pitch - 30.0 * dt)
172
173        if Input.is_action_just_pressed("quit"):
174            self.app.quit()
175            return
176
177        env = self._env
178
179        if Input.is_action_just_pressed("toggle_fog"):
180            env.fog_enabled = not env.fog_enabled
181        if Input.is_action_just_pressed("toggle_bloom"):
182            env.bloom_enabled = not env.bloom_enabled
183        if Input.is_action_just_pressed("cycle_fog_mode"):
184            self._fog_mode_idx = (self._fog_mode_idx + 1) % len(FOG_MODES)
185            env.fog_mode = FOG_MODES[self._fog_mode_idx]
186
187        if Input.is_action_pressed("density_up"):
188            env.fog_density = min(0.2, env.fog_density + 0.02 * dt)
189        if Input.is_action_pressed("density_down"):
190            env.fog_density = max(0.001, env.fog_density - 0.02 * dt)
191
192        if Input.is_action_pressed("exposure_up"):
193            env.tonemap_exposure = min(5.0, env.tonemap_exposure + 1.0 * dt)
194        if Input.is_action_pressed("exposure_down"):
195            env.tonemap_exposure = max(0.1, env.tonemap_exposure - 1.0 * dt)
196
197        self._update_camera()
198        self._update_hud()
199
200    def _update_camera(self):
201        yaw_rad = math.radians(self._yaw)
202        pitch_rad = math.radians(self._pitch)
203        cp = math.cos(pitch_rad)
204        x = self._target[0] + self._distance * cp * math.sin(yaw_rad)
205        y = self._target[1] + self._distance * math.sin(pitch_rad)
206        z = self._target[2] + self._distance * cp * math.cos(yaw_rad)
207        self._cam.position = (x, y, z)
208        self._cam.look_at(self._target)
209
210    def _update_hud(self):
211        env = self._env
212        lines = [
213            "Fog Demo (WorldEnvironment)",
214            f"[1] Fog: {'ON' if env.fog_enabled else 'OFF'}  Density: {env.fog_density:.3f} (Up/Down)",
215            f"[2] Bloom: {'ON' if env.bloom_enabled else 'OFF'}",
216            f"[3] Mode: {env.fog_mode}",
217            f"    Exposure: {env.tonemap_exposure:.2f} (Left/Right)",
218            "A/D orbit  W/S pitch  Q/E zoom  Esc quit",
219        ]
220        self._hud.text = "\n".join(lines)
221
222
223if __name__ == "__main__":
224    app = App(title="Fog Demo", width=1280, height=720)
225    app.run(FogDemo())