Files
iron-arc/app/lib/src/game/art.dart
T
SoftwareApp fb85915418 art: new ingame renderer (sky, parallax, terrain, tanks, effects)
- lib/src/game/art.dart draws the scene with canvas primitives: dusk sky with a
camera-relative sun, three parallax ridges (far = lighter), soil with rim
light and irregular rocks, tanks with hull/turret/barrel/tracks and a
health bar in team colour, comet trail, impact with fireball/ring/sparks and
a dotted aim hint.
- Flutter tokens come from the generated palette, so app UI and artwork match.
- performance was part of the job: the first version ran at 31-40 % jank. An
A/B against the old renderer (0-6.5 %) proved it was the new scene, then the
causes were removed one by one: full-screen radial vignette (now edge bands),
per-ridge gradient shaders and per-frame paints (flat fills, cached paints),
two clipped gradient rects under the surface, terrain redrawn every frame (now
a ui.Picture per terrain version) and mask-filter blurs.
- result on the Redmi Note 10: 60.0 fps, 0.0-0.8 % jank.
- goldens regenerated and reviewed; store screenshots re-captured and renamed
to match their content (01 gameplay, 02 projectile, 03 crater, 04 settings).
- docs/art-direction.md: phase 2 section with the performance table.
2026-10-25 02:59:20 +01:00

621 lines
18 KiB
Dart

import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import '../ui/palette.dart';
/// Scene art for the match: dusk sky, parallax ridges, layered terrain, tanks,
/// projectile trail and impact effects.
///
/// Everything is drawn with canvas primitives on purpose - the terrain is
/// destructible and the camera zooms, so bitmaps would have to be regenerated
/// anyway. Geometry that never changes (ridges) is built once and cached; the
/// per-frame work stays at a few paths and gradients.
///
/// Look rules: docs/art-direction.md - light from the horizon, a warm rim on
/// every silhouette, one accent colour per side, vignette over the frame.
const double kArtWorldWidth = 2048;
const double kArtWorldHeight = 1152;
/// Heightmap sampling step, must match the game (`kSamplePx`).
const int kArtWorldSample = 2;
// ---------------------------------------------------------------------------
// geometry (ported from tools/art/artlib.py so both stay in sync)
// ---------------------------------------------------------------------------
/// Sum of two sines plus seeded jitter - reproducible ridges.
List<Offset> ridgePoints({
required double width,
required double baseY,
required double amplitude,
required int seed,
int steps = 7,
double phase = 0,
double footY = kArtWorldHeight,
}) {
final rnd = math.Random(seed);
final pts = <Offset>[];
for (var i = 0; i <= steps; i++) {
final t = i / steps;
var wave = math.sin((t * 2.2 + phase) * math.pi * 2) * 0.55;
wave += math.sin((t * 5.1 + phase * 1.7) * math.pi * 2) * 0.22;
final jitter = (i > 0 && i < steps) ? (rnd.nextDouble() - 0.5) * 0.30 : 0.0;
pts.add(Offset(t * width, baseY - amplitude * (0.5 + 0.5 * wave + jitter)));
}
pts.add(Offset(width, footY));
pts.add(Offset(0, footY));
return pts;
}
/// Catmull-Rom through the points, emitted as cubic beziers.
Path smoothPath(List<Offset> points, {bool closed = false}) {
final path = Path();
if (points.length < 2) return path;
final pts = <Offset>[
if (closed) points.last,
points.first,
...points,
points.last,
if (closed) points.first,
];
path.moveTo(pts[1].dx, pts[1].dy);
for (var i = 1; i < pts.length - 2; i++) {
final p0 = pts[i - 1], p1 = pts[i], p2 = pts[i + 1], p3 = pts[i + 2];
final c1 = Offset(p1.dx + (p2.dx - p0.dx) / 6, p1.dy + (p2.dy - p0.dy) / 6);
final c2 = Offset(p2.dx - (p3.dx - p1.dx) / 6, p2.dy - (p3.dy - p1.dy) / 6);
path.cubicTo(c1.dx, c1.dy, c2.dx, c2.dy, p2.dx, p2.dy);
}
if (closed) path.close();
return path;
}
// ---------------------------------------------------------------------------
// sky + parallax backdrop
// ---------------------------------------------------------------------------
class SkyArt {
SkyArt({this.seed = 7});
final int seed;
final List<_Ridge> _ridges = [];
final List<Offset> _stars = [];
bool _built = false;
/// Long-lived paints: re-creating shaders every frame caused shader churn and
/// dropped frames on device.
final Paint _skyPaint = Paint();
final Paint _starPaint = Paint();
final Paint _coronaPaint = Paint();
final Paint _sunPaint = Paint();
/// Sun radius in world units. The disc itself is camera-anchored (it is
/// infinitely far away), so every camera position has a visible light source.
static const double sunRadius = 74;
void _build() {
final rnd = math.Random(seed);
for (var i = 0; i < 16; i++) {
_stars.add(Offset(rnd.nextDouble() * kArtWorldWidth, rnd.nextDouble() * 430));
}
_ridges.addAll([
_Ridge(
smoothPath(ridgePoints(
width: kArtWorldWidth, baseY: 545, amplitude: 105, seed: seed + 1, steps: 6)),
parallax: 0.12,
fill: const Color(0xFF20334F),
rim: Palette.terrainRim.withValues(alpha: 0.08),
),
_Ridge(
smoothPath(ridgePoints(
width: kArtWorldWidth,
baseY: 625,
amplitude: 95,
seed: seed + 2,
steps: 7,
phase: 0.31)),
parallax: 0.24,
fill: const Color(0xFF152238),
rim: Palette.terrainRim.withValues(alpha: 0.12),
),
_Ridge(
smoothPath(ridgePoints(
width: kArtWorldWidth,
baseY: 700,
amplitude: 80,
seed: seed + 3,
steps: 5,
phase: 0.62)),
parallax: 0.38,
fill: const Color(0xFF080E19),
rim: Palette.solarLight.withValues(alpha: 0.14),
),
]);
_skyPaint.shader = ui.Gradient.linear(
const Offset(0, 0),
const Offset(0, kArtWorldHeight),
const [
Palette.ink0,
Color(0xFF0B1424),
Color(0xFF17243A),
Color(0xFF3A3026),
],
const [0.0, 0.42, 0.62, 0.78],
);
_built = true;
}
/// `camX` shifts each layer by (1 - parallax): the ridges crawl slower than
/// the playfield. `viewWidth` places the sun relative to the viewport.
void paint(Canvas canvas, {required double camX, required double viewWidth}) {
if (!_built) _build();
canvas.drawRect(
const Rect.fromLTWH(0, 0, kArtWorldWidth, kArtWorldHeight),
_skyPaint,
);
_starPaint.color = const Color(0xFFDCE6F5).withValues(alpha: 0.22);
for (final s in _stars) {
final dx = s.dx + camX * 0.85;
if (dx < -40 || dx > kArtWorldWidth + 40) continue;
canvas.drawCircle(Offset(dx, s.dy), 2.2, _starPaint);
}
_paintSun(canvas, Offset(camX + viewWidth * 0.66, 395));
for (final ridge in _ridges) {
canvas.save();
canvas.translate(camX * (1 - ridge.parallax), 0);
canvas.drawPath(ridge.path, ridge.fillPaint);
if (ridge.rim.a > 0.01) {
canvas.drawPath(ridge.path, ridge.rimPaint);
}
canvas.restore();
}
}
void _paintSun(Canvas canvas, Offset center) {
_coronaPaint.shader = ui.Gradient.radial(center, sunRadius * 2.2, [
Palette.solarDeep.withValues(alpha: 0.22),
Palette.solarDeep.withValues(alpha: 0.0),
]);
canvas.drawCircle(center, sunRadius * 2.2, _coronaPaint);
_sunPaint.shader = ui.Gradient.radial(
center.translate(-sunRadius * 0.25, -sunRadius * 0.3),
sunRadius * 1.35,
[
const Color(0xFFFFF3DC),
Palette.solarLight,
Palette.solarBase,
Palette.solarDeep,
],
const [0.0, 0.45, 0.78, 1.0],
);
canvas.drawCircle(center, sunRadius, _sunPaint);
}
}
class _Ridge {
_Ridge(this.path, {required this.parallax, required this.fill, required this.rim}) {
// Flat fill: a gradient per ridge cost more than it added at this size.
fillPaint.color = fill;
rimPaint
..style = PaintingStyle.stroke
..strokeWidth = 2.5
..color = rim;
}
final Path path;
final double parallax;
final Color fill;
final Color rim;
final Paint fillPaint = Paint();
final Paint rimPaint = Paint();
}
// ---------------------------------------------------------------------------
// terrain
// ---------------------------------------------------------------------------
class TerrainArt {
int _version = -1;
ui.Picture? _picture;
/// The soil never changes between two craters, so the whole layer is recorded
/// into a Picture once. Redrawing it every frame cost ~30 % of the frame
/// budget on the test device (mask filters are not free).
void paint(
Canvas canvas,
Path fill,
Path line, {
List<int>? heights,
int version = -1,
}) {
if (_picture == null || version != _version) {
_version = version;
final recorder = ui.PictureRecorder();
_draw(Canvas(recorder), fill, line, heights);
_picture = recorder.endRecording();
}
canvas.drawPicture(_picture!);
}
void _draw(Canvas canvas, Path fill, Path line, List<int>? heights) {
canvas.drawPath(
fill,
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 520),
const Offset(0, kArtWorldHeight),
[const Color(0xFF54402C), Palette.terrainSoil, Palette.terrainDeep],
const [0.0, 0.35, 1.0],
),
);
canvas.save();
canvas.clipPath(fill);
// Shade under the lip without a mask filter: two soft strokes.
canvas.drawPath(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 26
..strokeJoin = StrokeJoin.round
..color = const Color(0xFF0B0805).withValues(alpha: 0.10),
);
canvas.drawPath(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 12
..strokeJoin = StrokeJoin.round
..color = const Color(0xFF0B0805).withValues(alpha: 0.12),
);
canvas.restore();
// Surface: wide translucent edge, warm line, thin highlight.
canvas.drawPath(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 12
..strokeJoin = StrokeJoin.round
..color = Palette.solarLight.withValues(alpha: 0.07),
);
canvas.drawPath(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 3.5
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..color = Palette.terrainRim.withValues(alpha: 0.55),
);
canvas.drawPath(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.2
..strokeCap = StrokeCap.round
..color = Palette.solarLight.withValues(alpha: 0.35),
);
if (heights != null) _paintSurfaceRocks(canvas, heights);
}
/// Sparse rocks poking out of the surface. Evenly spaced ticks read as a
/// fence, so the spacing is seeded and irregular.
void _paintSurfaceRocks(Canvas canvas, List<int> heights) {
if (heights.length < 8) return;
final rnd = math.Random(91);
var i = 3;
while (i < heights.length - 4) {
i += 12 + rnd.nextInt(38);
if (i >= heights.length - 4) break;
final x = i * kArtWorldSample.toDouble();
final y = heights[i].toDouble();
final w = 4.0 + rnd.nextDouble() * 9;
final h = 3.0 + rnd.nextDouble() * 6;
final rock = Path()
..moveTo(x - w, y + 3)
..lineTo(x - w * 0.5, y - h)
..lineTo(x + w * 0.35, y - h * 0.75)
..lineTo(x + w, y + 3)
..close();
canvas.drawPath(rock, Paint()..color = const Color(0xFF4A3A26));
canvas.drawPath(
rock,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.2
..color = Palette.terrainRim.withValues(alpha: 0.45),
);
}
}
}
// ---------------------------------------------------------------------------
// tanks
// ---------------------------------------------------------------------------
/// Tanks are authored in a local space (origin = ground contact, up = -y) and
/// scaled up, so the silhouette still reads at the camera's ~0.7 zoom.
const double kTankScale = 1.5;
const double _barrelLength = 42;
const double _turretPivotY = 32;
/// World position of the barrel pivot.
Offset tankPivot(Offset pos) => Offset(pos.dx, pos.dy - _turretPivotY * kTankScale);
/// World position of the muzzle - shared by the tank painter and the aim hint.
Offset tankMuzzle(Offset pos, double barrelDeg, double dir) {
final rad = barrelDeg * math.pi / 180;
return Offset(
pos.dx + dir * math.cos(rad) * _barrelLength * kTankScale,
pos.dy - (_turretPivotY + math.sin(rad) * _barrelLength) * kTankScale,
);
}
void paintTank(
Canvas canvas, {
required Offset pos,
required double barrelDeg,
required bool alive,
required bool isPlayer0,
required double hpFrac,
required bool isCurrent,
}) {
final dir = isPlayer0 ? 1.0 : -1.0;
final base = alive
? (isPlayer0 ? Palette.solarBase : Palette.steelBase)
: const Color(0xFF3A4450);
final light = isPlayer0 ? Palette.solarLight : Palette.steelLight;
final dark = isPlayer0 ? Palette.solarDeep : Palette.steelDeep;
final a = alive ? 1.0 : 0.45;
// Contact shadow stays in world space so it hugs the ground line.
canvas.drawOval(
Rect.fromCenter(center: Offset(pos.dx, pos.dy + 3), width: 96, height: 18),
Paint()..color = Colors.black.withValues(alpha: 0.45 * a),
);
canvas.save();
canvas.translate(pos.dx, pos.dy);
canvas.scale(kTankScale);
// Tracks.
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromCenter(center: const Offset(0, -4), width: 66, height: 15),
const Radius.circular(7),
),
Paint()..color = const Color(0xFF090E15).withValues(alpha: a),
);
for (var i = 0; i < 4; i++) {
canvas.drawCircle(
Offset(-21 + i * 14, -4),
4.2,
Paint()..color = const Color(0xFF2B3543).withValues(alpha: a),
);
}
// Hull.
final hull = Path()
..moveTo(-29, -10)
..lineTo(29, -10)
..lineTo(24, -22)
..lineTo(15, -26)
..lineTo(-15, -26)
..lineTo(-24, -22)
..close();
canvas.drawPath(
hull,
Paint()
..shader = ui.Gradient.linear(
const Offset(0, -27),
const Offset(0, -9),
[light.withValues(alpha: a), base.withValues(alpha: a), dark.withValues(alpha: a)],
const [0.0, 0.45, 1.0],
),
);
// Turret.
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromCenter(center: const Offset(0, -31), width: 34, height: 17),
const Radius.circular(8),
),
Paint()
..shader = ui.Gradient.linear(
const Offset(0, -40),
const Offset(0, -23),
[light.withValues(alpha: a), base.withValues(alpha: a), dark.withValues(alpha: a)],
const [0.0, 0.5, 1.0],
),
);
// Barrel: dark core, light top edge, muzzle brake.
final rad = barrelDeg * math.pi / 180;
const pivot = Offset(0, -_turretPivotY);
final muzzle = Offset(
dir * math.cos(rad) * _barrelLength,
-_turretPivotY - math.sin(rad) * _barrelLength,
);
canvas.drawLine(
pivot,
muzzle,
Paint()
..color = const Color(0xFF141A24).withValues(alpha: a)
..strokeWidth = 8
..strokeCap = StrokeCap.round,
);
canvas.drawLine(
pivot.translate(0, -2),
muzzle.translate(0, -2),
Paint()
..color = light.withValues(alpha: 0.55 * a)
..strokeWidth = 2.6
..strokeCap = StrokeCap.round,
);
canvas.drawCircle(muzzle, 5.2, Paint()..color = const Color(0xFF0C1118).withValues(alpha: a));
// Rim light on the hull top edge - the rule that keeps silhouettes readable.
canvas.drawLine(
const Offset(-15, -26.4),
const Offset(15, -26.4),
Paint()
..color = light.withValues(alpha: 0.8 * a)
..strokeWidth = 1.8
..strokeCap = StrokeCap.round,
);
canvas.restore();
if (isCurrent && alive) {
canvas.drawCircle(
Offset(pos.dx, pos.dy - 82),
4.5,
Paint()..color = Palette.solarLight.withValues(alpha: 0.9),
);
}
// Health bar, world space so it keeps a constant on-screen weight.
const barW = 64.0;
final barTop = pos.dy - 84;
final bar = Rect.fromLTWH(pos.dx - barW / 2, barTop, barW, 6);
canvas.drawRRect(
RRect.fromRectAndRadius(bar, const Radius.circular(3.5)),
Paint()..color = Colors.black.withValues(alpha: 0.55),
);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(bar.left, bar.top, barW * hpFrac.clamp(0.0, 1.0), 6),
const Radius.circular(3.5),
),
Paint()
..color = hpFrac > 0.5
? base
: hpFrac > 0.25
? Palette.solarBase
: Palette.stateDanger,
);
canvas.drawRRect(
RRect.fromRectAndRadius(bar, const Radius.circular(3.5)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = Colors.white.withValues(alpha: 0.25),
);
}
// ---------------------------------------------------------------------------
// effects
// ---------------------------------------------------------------------------
void paintProjectile(Canvas canvas, Offset p, List<Offset> trail) {
for (var i = 0; i < trail.length; i++) {
final t = i / trail.length;
canvas.drawCircle(
trail[i],
3 + 5 * t,
Paint()..color = Palette.solarBase.withValues(alpha: 0.06 + 0.18 * t),
);
}
canvas.drawCircle(
p,
18,
Paint()
..color = Palette.solarBase.withValues(alpha: 0.35)
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 8),
);
canvas.drawCircle(p, 6.5, Paint()..color = Palette.solarLight);
canvas.drawCircle(p + const Offset(-1.5, -1.5), 2.6, Paint()..color = Colors.white);
}
/// `t` runs from 1 (impact) down to 0.
void paintImpact(Canvas canvas, Offset p, double t) {
final grow = 1 - t;
final fireRadius = 26 + 54 * grow;
canvas.drawCircle(
p,
fireRadius,
Paint()
..shader = ui.Gradient.radial(p, fireRadius, [
Colors.white.withValues(alpha: 0.85 * t),
Palette.solarBase.withValues(alpha: 0.55 * t),
Palette.solarDeep.withValues(alpha: 0.0),
]),
);
canvas.drawCircle(
p,
18 + 78 * grow,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 3 * t
..color = Palette.solarLight.withValues(alpha: 0.45 * t),
);
// Sparks: deterministic fan, gravity bends them down as they age.
for (var i = 0; i < 9; i++) {
final angle = (i / 9) * math.pi * 2 + 0.4;
final speed = 60 + (i % 3) * 34;
final r = speed * grow * 1.5;
canvas.drawCircle(
Offset(
p.dx + math.cos(angle) * r,
p.dy + math.sin(angle) * r * 0.7 + 90 * grow * grow,
),
3.2 * t,
Paint()..color = Palette.solarLight.withValues(alpha: 0.8 * t),
);
}
}
void paintAim(
Canvas canvas, {
required Offset muzzle,
required double angleDeg,
required double dir,
required double power,
}) {
final rad = angleDeg * math.pi / 180;
for (var i = 1; i <= 7; i++) {
final r = 52.0 + i * 30;
// Approximate drop so the hint curves like the real shot.
final p = Offset(
muzzle.dx + dir * math.cos(rad) * r,
muzzle.dy - math.sin(rad) * r + 1.28 * i * i,
);
canvas.drawCircle(
p,
2.2 + 2.4 * power,
Paint()..color = Colors.white.withValues(alpha: ((0.55 - i * 0.05) * (0.6 + 0.4 * power)).clamp(0.0, 1.0)),
);
}
}
/// Edge darkening without a full-screen radial gradient: four cheap bands.
/// A single radial fill over 2048x1152 tripled the jank rate on the test device.
void paintVignette(Canvas canvas) {
const w = kArtWorldWidth;
const h = kArtWorldHeight;
const band = 300.0;
const edge = Color(0x66000000);
const clear = Color(0x00000000);
canvas.drawRect(
const Rect.fromLTWH(0, 0, w, band),
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 0), const Offset(0, band), [edge, clear]),
);
canvas.drawRect(
const Rect.fromLTWH(0, h - band, w, band),
Paint()
..shader = ui.Gradient.linear(
const Offset(0, h - band), const Offset(0, h), [clear, edge]),
);
}