Docs / Rendering
Graphics settings
GraphicsSettings holds the rows every 3D game's graphics menu has, and the world renderer applies every one of them to what it draws. A game with its own renderer applies them itself. The defaults are the High preset, the game as made with 4× anti-aliasing, so nothing looks worse unless a player asks. SceneScaling, under the render scale, draws the 3D scene smaller than the window and scales it up.
The settings
| Setting | Values (default first) | What it changes |
|---|---|---|
shadows | .high, .low, .off | High: every sun cascade at 2048 texels a side. Low: 1024, and nothing is drawn into the farthest cascade. Off: no sun shadows |
antiAliasing | 4, or 1, 2 | samples per pixel in the scene pass (FrameRenderer.sampleCount); 1 is none |
renderScale | .automatic, .full, .threeQuarters, .half | the scene's size against the window; the interface is always drawn sharp |
frameRate | 60, or 120, 30 | what the display link is asked for |
textureQuality | .full, .half, .quarter | each texture side halved reduction times as it is loaded |
clouds | .high, .medium, .flat | volumetric at half the screen's width and height, at a quarter of its pixels, or a flat layer |
fog | .high, .medium, .low | volumetric in cells of 16 pixels and 64 slices, 24 and 48, or distance fog alone |
snowDepth | true | whether lying snow raises the ground (Weather.snowDepth) or is only a white layer |
reflections | true | whether water and the like reflect the world, read from the solid scene |
drawDistance | 1, range 0.5...1 | how far scattered things are drawn, as a share of each kind's own distance |
plantDensity | 1, range 0.25...1 | the share of thin ground cover drawn: grass, flowers, crops |
Low shadows save where the time goes: in a dense wood, the smaller map alone saved one millisecond and leaving the far cascade's casters out saved seven. Texture quality is mostly memory: a 4096² texture held uncompressed with its mips is 85 MB, and 21 MB at half. Images of 128 pixels or less (TextureStore.smallestReduced) always load whole, since a halved icon is a blur. Reflections cost about 2 ms at 1960×1490 with a pond filling the view.
Plant density thins only ground cover. Thinning trees and bushes would change the world, not its cost. GraphicsSettings.keeps(_:atDensity:) says whether a copy is drawn. It hashes the index rather than drawing at random, so the same copies are kept every frame and every run, and lowering the density only removes copies. A game with no plants leaves that row and draw distance out of its menu.
Presets
GraphicsSettings.Preset has .low, .medium and .high; settings gives each one's values. High is the defaults. Medium has low shadows, 2× anti-aliasing, 80% draw distance, 75% ground cover, and medium clouds and fog. Low turns shadows, anti-aliasing, reflections and snow depth off, with half the ground cover, 60% draw distance, the scene at 75%, half-size textures, flat clouds and distance fog.
preset reads settings back as the preset they match, or nil once a player has changed anything: a menu's "Custom". summary is the settings in one line (High, 60 fps, or every value), for a report that has to say what it measured, such as a flythrough.
Applying them with WorldRenderer
The world renderer applies most of them. Start it with the saved settings, so textures load at the right size from the first prop, and set graphicsSettings when a player changes something:
let world = try WorldRenderer(engine: engine, graphics: saved) world.onFrameRateChange = { displayLink.preferredFrameRateRange = $0.frameRateRange } // your game owns the link world.graphicsSettings = GraphicsSettings.Preset.medium.settings world.graphicsSettings.shadows = .high // now a player's own: preset is nil
A change is applied at the start of the next frame, and what it replaces is kept until no frame in flight can be using it.
- Anti-aliasing rebuilds every scene-pass pipeline the world draws with.
- Shadows off gives the memory back; a 16-texel map stands in.
- Texture quality reloads every prop, because changing
TextureStore.qualityforgets every texture loaded from a file.onPropsDroppedtells you, to load what the scene is missing. - Frame rate goes to
onFrameRateChange, because the display link is yours. - Render scale
.automaticmeans full size here.
- Draw distance brings the scattered copies in. Each kind is drawn to where it shrinks to a few pixels across — its own distance, set by its size — and the setting is a share of that, so every kind comes in by the same share and grass, being smallest, goes first in meters.
- Plant density thins the copies of ground cover: the same copies every frame, and only fewer as it falls. Ground cover is what a person decided in the world file (
WorldFile.groundCover,WorldScene.groundCover), else anything drawn all in cut-out cards that is under 1.6 m tall or spread three times wider than it is tall (GroundCover.byShape(size:cutOut:)). Trees and bushes stay whole. - Reflections turn on the water's mirror of the scene (
waterReflects): the solid pass's color is kept for the water and streams to march across. Off, or in a scene with no water, they reflect the sky alone and the color is never copied.
A game with its own renderer applies the three itself: GraphicsSettings.keeps(_:atDensity:) picks the same copies of a kind at a density, and WorldScene.isGroundCover(_:size:cutOut:) tells ground cover.
Applying them with your own renderer
FrameRenderer.apply(_:automaticScaling:) sets sampleCount, as far as the device supports it, and sceneScaling, with automaticScaling standing for .automatic. Call it in encodePrePasses or before a frame; the targets follow in that frame. Every scene-pass pipeline is built with a rasterSampleCount, so rebuild yours when effectiveSampleCount changes:
let before = engine.effectiveSampleCount engine.apply(settings, automaticScaling: .capped(width: 1920, beyond: 2600)) if engine.effectiveSampleCount != before { rebuildScenePipelines() } // your game
A FrameRenderer draws one sample per pixel until you apply settings that ask for four. That is on purpose: every scene pipeline is built for the renderer's count, so it starts where they do, and the settings' 4 is where a graphics menu starts. Shadows are yours: settings.shadows.cascades(splits:lightDepthRange:) makes the ShadowCascades for your splits at the setting's resolution, nil when off, and castingCascades(of:) goes to ShadowMap.encode(into:frameSlot:batches:castingCascades:), which clears the cascades past it so they read as lit. Pass frameRateRange to makeDisplayLink(for:frameRateRange:), whose own default asks for up to 120: that is a game which has said nothing, where the settings start at 60 so a world dressed for 60 is not pushed to 120 unasked. See shadows.
Saving them
GraphicsSettings is Codable, so a SaveStore keeps it as it is:
let graphicsStore = SaveStore<GraphicsSettings>(container: class="p">"MyGame", name: class="p">"graphics") // your game let saved = (try? graphicsStore.load()) ?? GraphicsSettings() try? graphicsStore.save(world.graphicsSettings)
A set saved by an older build still loads: a setting that is missing or unreadable takes its default, and the rest keep what they said. clamped brings stale values back: the two fractions into range, a frame rate not in frameRates to 60, an anti-aliasing count not in antiAliasingChoices to 1. apply and frameRateRange use it, so a hand-edited file can't ask for what the screen never offers.
Scene scaling
A base M2 filling a 4K display draws 8.3 million pixels a frame, and alpha-tested foliage makes each one expensive. FrameRenderer.sceneScaling draws the scene smaller, and the tone-map pass scales it up with linear filtering. The interface is drawn after, at the drawable's size, so it stays sharp.
| Case | The scene is drawn |
|---|---|
.native | at the drawable's size (the default) |
.capped(width:beyond:) | width pixels across, keeping the drawable's shape, once the drawable is wider than beyond |
.fraction(_:) | at this fraction of the drawable each way, at any size; 1 or more is full size, and never under one pixel |
engine.sceneScaling = .capped(width: 1920, beyond: 2600)
capped takes two numbers on purpose. At 4K with a cap of 1920 the scale is exactly 2×, which looks the same. A window 1,960 wide drawn at 1,920 and stretched is softer than either for nothing, so only a drawable well past the cap is scaled. .fraction is a player's trade instead; the .threeQuarters and .half render scales become it.
Change it whenever; the targets follow on the next frame, and sceneSize says what the scene is drawn at. viewportSizeChanged(width:height:) still gets the drawable's size, not the scene's, so aspect and layout don't move. Size anything that matches the scene pixel for pixel to sceneSize.
What it does not do
There is no auto-detected preset and no settings screen. The scale doesn't follow frame time by itself, and the upscale is linear filtering, not a temporal upscaler.