High contrast
Open the live demo · Read the source · View on GitHub
Some players cannot pick a monster out of a busy floor by its colour, or cannot see the edge of a step against the floor in front of it. The high-contrast look is an accommodation for them. It takes the texture out of every surface, drains most of the colour out of the frame, draws a line at every edge the geometry has, and then puts colour back only on the things a game says matter, as a ring around each one.
It is off by default. A game switches it on when a player asks for it, which
flutter3d_game reads from the platform's own high-contrast setting with the
player's switch over it. This page sets it directly.
Step 1: A level with too much going on #
A tiled floor in two close greens and browns, and three crates painted three different colours. This is the detail the look is there to remove: the tiles are texture inside one surface, and the crates' paint says nothing about what they are.
// A busy floor and some crates in colours of their own: the detail the
// look is there to take away.
const CheckerboardTexture tiles = CheckerboardTexture(
size: 32,
cell: 2,
light: 0x9A8F7A,
dark: 0x5E6B48,
);
final Scene scene = Scene()
..ambientIntensity = 0.35 * LightNode.legacyUnit
..add(
MeshNode(
DeviceMesh.upload(
context.device,
const PlaneShape(width: 12, depth: 12).build(),
),
RenderMaterial(
name: 'tiles',
albedo: tiles.upload(context.device),
albedoSampler: SamplerDescriptor.linearRepeat,
roughness: 0.9,
),
name: 'floor',
),
)
..add(
LightNode(name: 'sun', intensity: 2.5 * LightNode.legacyUnit)
..setLocalForward(Vector3(-0.4, -0.8, -0.3)),
);
final DeviceMesh crate = DeviceMesh.upload(
context.device,
CuboidShape(size: Vector3.all(1.0)).build(),
);
final List<Vector4> paint = <Vector4>[
Vector4(0.7, 0.35, 0.2, 1.0),
Vector4(0.25, 0.45, 0.7, 1.0),
Vector4(0.55, 0.6, 0.25, 1.0),
];
for (var i = 0; i < 3; i++) {
scene.add(
MeshNode(
crate,
RenderMaterial(
name: 'crate $i',
baseColor: _fromSrgb(paint[i]),
roughness: 0.7,
),
name: 'crate $i',
)..setPosition(-3.0 + i * 1.2, 0.5, -2.5),
);
}
Step 2: Mark what the player has to find #
MeshNode.outlineColor gives a node a role colour. Here a monster is red, a
pickup is yellow and the way out is green. The three balls are the same grey
underneath, so with the look off nothing tells them apart.
The colour is a display colour, as a swatch gives it, and not linear like a material's tint: the ring is drawn on the finished picture, after the tone map, so it comes out the colour the player picked. A mark is per mesh. A model made of several meshes is marked by walking it.
// Three things the player has to find, each in the colour its role has:
// a monster, a pickup and the way out. The colours are display colours,
// as a swatch in the game's settings gives them.
final DeviceMesh ball = DeviceMesh.upload(
context.device,
SphereShape(radius: 0.5, segments: 24, rings: 12).build(),
);
final List<(String, Vector3, Vector3)> things =
<(String, Vector3, Vector3)>[
('monster', Vector3(1.0, 0.2, 0.2), Vector3(1.8, 0.5, 0.5)),
('pickup', Vector3(1.0, 0.85, 0.1), Vector3(-1.2, 0.5, 1.6)),
('exit', Vector3(0.2, 0.9, 0.4), Vector3(3.2, 0.5, -2.2)),
];
for (final (String role, Vector3 color, Vector3 at) in things) {
final MeshNode node = MeshNode(
ball,
RenderMaterial(
name: role,
baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.6, 1.0),
),
name: role,
)..setPositionFrom(at);
// The role colours are picked by eye, so they are sRGB.
node.outlineColor = LinearColor.fromSrgb(color.x, color.y, color.z);
_marked.add((node, color));
scene.add(node);
}
Step 3: Turn the look on #
RenderSettings.highContrast takes a HighContrastSettings. saturation is
how much colour each pixel keeps, a quarter by default, and contrast pushes
light and dark apart around mid grey. outlineWidth is how far the edge line
reaches, in pixels. roleWidth is the ring around a marked node, and
roleFill lays a share of the same colour over the node itself, so a small
thing far away is still easy to find.
The flattening asks the surface buffer, not the picture, which neighbouring pixels belong to the same surface. The mortar between two tiles is an edge in colour but not in depth or facing, so it is averaged away, while the step between floor and crate stays. The edge line works on how far depth bends, not how far it steps, so a floor seen at a slant is not drawn as one solid block of line.
Switch High contrast off and on. Then switch Role outlines off: the three balls go back to being grey shapes among grey shapes. Drag Saturation up to see how much colour the look normally takes away.
highContrast: HighContrastSettings(
enabled: look,
saturation: saturation,
contrast: contrast,
outlineWidth: 1.0,
roleWidth: 3.0,
roleFill: 0.3,
),
Step 4: What the frame should show #
The look is a pass called high contrast on the finished picture. The rings
come from a mask drawn before it, outline mask, which runs only while the
look is on and some node is marked. With the look off neither pass runs, so
leaving marks set on a game's monsters costs nothing for a player who never
asks for the look.
// The look is one pass on the finished picture, and the rings are a mask
// drawn before it from the marked nodes alone. On, with marks, both run;
// off, neither does, which is what "an exact no-op" means here.
bool ran(String name) => frame.passes.any((FramePass p) => p.name == name);
final bool marked = scene.meshes.any(
(MeshNode m) => m.outlineColor != null,
);
if (ran('high contrast') != look) {
throw StateError(
'the look pass ran: ${ran('high contrast')}, look: $look',
);
}
if (ran('outline mask') != (look && marked)) {
throw StateError(
'the outline mask ran: ${ran('outline mask')}; '
'look: $look, marked nodes: $marked, skipped: ${frame.skipped}',
);
}
Note. The look reads the surface buffer, so the frame loses its multisampling while it is on. A mark behind something the scene drew is dropped against the same buffer, so a monster is never ringed through a wall.