fix(models): tracked and emergency tints mix into aircraft paint instead of multiplying it

Aircraft models multiplied their tint into the shaded paint (HIGHLIGHT).
A saturated tint zeroes every channel it lacks: the tracked cyan (no red)
drew a red helicopter solid black, day or night, and a white Cessna one
flat cyan. Tinted models now mix the colour in at 30%
(AIRCRAFT_MODEL_TINT_MIX), so the paint stays readable and the colour
still marks them; untinted models still draw their own colours, and the
IR boost stays flat white.

Neither the multi-part models nor the part shader were involved: old and
new GLBs, with and without the shader, render the same pixels, and an
untinted model at night is lit by the default ambient term.

The new conformance check draw.models.paint draws a model painted with
COLOR_0 under a fixed sun and reads its colour by day, at night, and
under a tint mixed in. The parts test model can now carry vertex paint.
This commit is contained in:
Sameh Khamis committed 2026-11-17 16:50:52 -08:00
1 parent 953af60e1d
commit e83c1672dd
9 files changed
+221 -12

No files matched your search

+1 -1
View File
@@ -405,7 +405,7 @@ export function createAircraftModels({
tint: record.tint ?? WHITE,
// The mesh's own colours, tinted while tracked or in an emergency;
// the IR boost draws it flat white.
colorBlend: aircraftModelBlend(state._irBoost === true),
colorBlend: aircraftModelBlend(state._irBoost === true, record.tint),
environmentMap: AIRCRAFT_ENVIRONMENT_MAP_OPTIONS.enabled,
...(record.parts ? { parts: record.parts } : {}),
show: record.show,
+21 -6
View File
@@ -17,15 +17,30 @@ export const AIRCRAFT_ENVIRONMENT_MAP_OPTIONS = Object.freeze({
const HIGHLIGHT = Object.freeze({ mode: 'highlight' });
const FLAT = Object.freeze({ mode: 'mix', amount: 1 });
/**
* How much of a tracked or emergency colour a model takes over its paint.
* Multiplying by a saturated tint zeroes every channel the tint lacks: the
* tracked cyan (no red) draws a red helicopter black and a white Cessna flat
* cyan. Mixed in at this share, the paint stays readable and the colour still
* marks the model.
*/
export const AIRCRAFT_MODEL_TINT_MIX = 0.3;
const TINTED = Object.freeze({ mode: 'mix', amount: AIRCRAFT_MODEL_TINT_MIX });
/**
* How an aircraft model's colour meets its mesh, as a draw item's
* `colorBlend`. `highlight` multiplies, so white draws the mesh's own
* colours as built, and the tracked or emergency colour tints the mesh
* without hiding its paint. The IR boost draws the model flat white instead
* (`mix` at 1, unlit).
* `colorBlend`. White (no tint) multiplies (`highlight`), which draws the
* mesh's own colours as built. A tracked or emergency colour is mixed in at
* `AIRCRAFT_MODEL_TINT_MIX`, so it tints the mesh without hiding its paint.
* The IR boost draws the model flat white instead (`mix` at 1, unlit).
* @param {boolean} irBoost
* @param {ArrayLike<number>|null} [tint] The model's RGBA tint, 0..1; white
* or none draws the mesh's own colours.
* @returns {{mode: 'highlight'|'mix', amount?: number}}
*/
export function aircraftModelBlend(irBoost) {
return irBoost ? FLAT : HIGHLIGHT;
export function aircraftModelBlend(irBoost, tint = null) {
if (irBoost) return FLAT;
const white = !tint || (tint[0] >= 1 && tint[1] >= 1 && tint[2] >= 1);
return white ? HIGHLIGHT : TINTED;
}
+66 -1
View File
@@ -2,7 +2,12 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { readLayerSource } from '../testSupport/readLayerSource.mjs';
import { AIRCRAFT_ENVIRONMENT_MAP_OPTIONS } from './aircraftModelLighting.js';
import {
AIRCRAFT_ENVIRONMENT_MAP_OPTIONS,
aircraftModelBlend,
} from './aircraftModelLighting.js';
import * as flightsPolicy from '../layers/flights/policy.js';
import * as militaryPolicy from '../layers/military/policy.js';
/**
* Every aircraft model drawn in the scene skips the renderer's dynamic
@@ -48,3 +53,63 @@ test('localAdsb: its draw-port models skip the dynamic environment map', () => {
/\benvironmentMap:\s*AIRCRAFT_ENVIRONMENT_MAP_OPTIONS\.enabled\b/,
);
});
/**
* The colour a model draws in, as the renderer blends its tint over the
* shaded paint (`ModelItem.colorBlend`, Cesium's model colour stage): `mix`
* moves the paint toward the tint by `amount`; `highlight` multiplies.
*/
function blended(paint, tint, blend) {
if (blend.mode === 'mix')
return paint.map((c, i) => c + (tint[i] - c) * blend.amount);
if (blend.mode === 'highlight') return paint.map((c, i) => c * tint[i]);
return tint.slice(0, 3);
}
/** Paints the fleet models carry: a red helicopter, a white airliner, a dark blue tail. */
const PAINTS = {
red: [0.8, 0.05, 0.05],
white: [0.9, 0.9, 0.9],
blue: [0.05, 0.1, 0.5],
};
test('an untinted model draws its mesh in its own colours', () => {
for (const tint of [null, [1, 1, 1, 1], flightsPolicy.MODEL_WHITE]) {
const blend = aircraftModelBlend(false, tint);
for (const paint of Object.values(PAINTS))
assert.deepEqual(blended(paint, [1, 1, 1], blend), paint);
}
});
test('the tracked and emergency tints keep every paint, never black or one flat colour', () => {
const tints = {
'tracked cyan': flightsPolicy.TRACKED_CYAN,
emergency: flightsPolicy.EMERGENCY_TINT,
'military tracked': militaryPolicy.TRACKED_ICON_COLOR,
'military emergency': militaryPolicy.EMERGENCY_ICON_COLOR,
};
for (const [tintName, tint] of Object.entries(tints)) {
const blend = aircraftModelBlend(false, tint);
const drawn = {};
for (const [paintName, paint] of Object.entries(PAINTS)) {
const out = blended(paint, tint, blend);
drawn[paintName] = out;
// Multiplying by the tracked cyan (no red) drew a red helicopter black.
paint.forEach((c, i) =>
assert.ok(
out[i] >= c * 0.6,
`${tintName} over ${paintName}: channel ${i} ${out[i].toFixed(2)} keeps most of ${c}`,
),
);
}
// Paints that differ still differ: the tint is not one flat colour.
const spread = (a, b) => Math.max(...a.map((c, i) => Math.abs(c - b[i])));
assert.ok(spread(drawn.red, drawn.white) > 0.4, `${tintName}: red and white apart`);
assert.ok(spread(drawn.blue, drawn.white) > 0.4, `${tintName}: blue and white apart`);
}
});
test('the IR boost draws every model flat white whatever its tint', () => {
for (const tint of [null, flightsPolicy.TRACKED_CYAN])
assert.deepEqual(aircraftModelBlend(true, tint), { mode: 'mix', amount: 1 });
});
+1
View File
@@ -84,6 +84,7 @@ export const CESIUM_CHECKS = new Set([
'draw.models.loadFailed',
'draw.models.tint',
'draw.models.blend',
'draw.models.paint',
'draw.models.update',
'draw.models.parts',
'draw.motion',
+87
View File
@@ -4049,6 +4049,93 @@ export function portChecks() {
},
{ capability: 'models', bareSky: true },
),
check(
'draw.models.paint',
'a lit model draws its vertex paint (`COLOR_0`) under the sun, stays readable with the sun below the horizon, and a tint mixed in keeps the paint',
async (h, r) => {
if (h.pixels === false)
return { skip: 'this harness reads no drawn pixels' };
// Austin at 13:00 local (sun about 80° up) and at 03:00 (about 32° below).
const DAY = Date.parse('2026-06-21T18:00:00Z');
const NIGHT = Date.parse('2026-06-21T08:00:00Z');
r.frame.setTime(DAY);
await overAustin(h, r, 200);
const set = r.draw.models({ id: 'conf:models.paint' });
const ready = [];
set.on('ready', (id) => ready.push(id));
const pos = h.toEcef({ ...AUSTIN, height: 0 });
const model = (extra = {}) => ({
id: 'm',
pos,
url: 'conformance:painted-model.glb',
// Red paint, as a fleet model's livery: a white material, its
// colour per vertex.
gltf: partsModelGlb({ paint: [0.8, 0.05, 0.05] }),
orientation: { heading: 0, pitch: 0, roll: 0 },
environmentMap: false,
tint: '#ffffff',
colorBlend: { mode: 'highlight' },
...extra,
});
try {
set.set([model()]);
for (let i = 0; i < 600 && !ready.length; i++) await drawn(h, r, 1);
deepEqual(ready, ['m'], 'ready');
const s = r.query.project(pos);
ok(s, 'the model projects');
// The mean colour of a patch on the model's top, seen from above.
const mean = async (time) => {
r.frame.setTime(time);
await drawn(h, r, 3);
await settled(r);
r.frame.setTime(time);
r.frame.renderNow();
const image = r.readPixels({
crop: {
x: Math.round(s.x) - 4,
y: Math.round(s.y) - 4,
width: 9,
height: 9,
},
});
const sum = [0, 0, 0];
for (let i = 0; i < image.data.length; i += 4)
for (let c = 0; c < 3; c++) sum[c] += image.data[i + c];
return sum.map((v) => Math.round(v / (image.data.length / 4)));
};
const day = await mean(DAY);
ok(
day[0] > 100 && day[0] > 2 * day[1] && day[0] > 2 * day[2],
`in daylight, its red paint: ${day}`,
);
const night = await mean(NIGHT);
ok(
night[0] >= 30 && night[0] > 2 * night[1],
`with the sun below the horizon, still its paint, not black: ${night}`,
);
ok(
night[0] < day[0],
`darker at night than by day: ${night}, ${day}`,
);
// A tint with no red, as a tracked cyan: mixed in, the paint shows.
set.upsert([
model({
tint: '#00ffff',
colorBlend: { mode: 'mix', amount: 0.3 },
}),
]);
const tinted = await mean(DAY);
ok(
tinted[0] > 60 && tinted[1] > day[1] + 30,
`a cyan tint mixed in at 0.3 keeps the red paint and adds its colour: ${tinted}`,
);
deepEqual(ready, ['m'], 'never loaded again');
} finally {
set.dispose();
}
},
{ capability: 'models', bareSky: true },
),
check(
'draw.models.bytes',
'a model given its bytes is drawn from them, its url never fetched',
+40 -3
View File
@@ -108,15 +108,19 @@ const CUBES = [
* The model's GLB bytes. Its mesh node is scaled by `scale`, turned
* `turnDeg` about its vertical and moved by `translation`; the mesh is
* built in that node's own frame, so the model it draws is the same
* whatever the node's transform.
* whatever the node's transform. `paint` (RGB, 0..1) is every vertex's
* colour (`COLOR_0`) over a white material, as the fleet models carry
* their paint; without it the model has no vertex colours.
* @param {{scale?: number, turnDeg?: number,
* translation?: [number, number, number]}} [node]
* translation?: [number, number, number],
* paint?: [number, number, number]}} [node]
* @returns {Uint8Array}
*/
export function partsModelGlb({
scale = 10,
turnDeg = 0,
translation = [0, 0, 0],
paint,
} = {}) {
const a = (turnDeg * Math.PI) / 180;
const c = Math.cos(a);
@@ -139,7 +143,13 @@ export function partsModelGlb({
const parts = new Uint8Array(count * 4);
mesh.parts.forEach((part, i) => (parts[i * 4] = part));
const indices = new Uint16Array(mesh.indices);
// Normalised bytes, RGBA: four to a vertex, on its 4-byte boundary.
const colors = paint ? new Uint8Array(count * 4) : null;
if (colors)
for (let i = 0; i < count; i++)
colors.set([...paint.map((c) => Math.round(c * 255)), 255], i * 4);
const chunks = [positions, normals, parts, indices];
if (colors) chunks.push(colors);
const offsets = [];
let length = 0;
for (const chunk of chunks) {
@@ -177,7 +187,12 @@ export function partsModelGlb({
{
primitives: [
{
attributes: { POSITION: 0, NORMAL: 1, _PART: 2 },
attributes: {
POSITION: 0,
NORMAL: 1,
_PART: 2,
...(colors ? { COLOR_0: 4 } : {}),
},
indices: 3,
material: 0,
},
@@ -220,6 +235,17 @@ export function partsModelGlb({
byteLength: indices.byteLength,
target: 34963,
},
...(colors
? [
{
buffer: 0,
byteOffset: offsets[4],
byteLength: colors.byteLength,
byteStride: 4,
target: 34962,
},
]
: []),
],
accessors: [
{
@@ -238,6 +264,17 @@ export function partsModelGlb({
count: indices.length,
type: 'SCALAR',
},
...(colors
? [
{
bufferView: 4,
componentType: 5121,
normalized: true,
count,
type: 'VEC4',
},
]
: []),
],
};
let text = new TextEncoder().encode(JSON.stringify(json));
+1
View File
@@ -30,6 +30,7 @@ test('the stub passes the conformance suite', async () => {
'draw.lines.split',
'draw.lines.outlineDraped',
'draw.models.blend',
'draw.models.paint',
'globe.imagery.video',
'globe.weather.drawn',
'globe.weather.sceneTime',