diff --git a/test/goods-label.test.js b/test/goods-label.test.js new file mode 100644 index 0000000..162ca8f --- /dev/null +++ b/test/goods-label.test.js @@ -0,0 +1,75 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import * as THREE from "three"; +import { + createGoodsLabel, + GOODS_CUBE, + GOODS_FOOTPRINT_Y, +} from "../viewer/three/goods-label.js"; + +function assertUpward(label) { + const normal = new THREE.Vector3(0, 0, 1).applyQuaternion(label.quaternion); + assert.ok(normal.distanceTo(new THREE.Vector3(0, 1, 0)) < 1e-9); +} + +test("colour-blind letters fit entirely above the cube's top face", () => { + const texture = new THREE.Texture(); + const label = createGoodsLabel(texture, true); + assert.equal(label.isMesh, true); + assertUpward(label); + const bounds = new THREE.Box3().setFromObject(label); + assert.ok(bounds.min.y > GOODS_CUBE.height / 2); + assert.ok(bounds.max.y < GOODS_CUBE.height / 2 + 0.005); + assert.ok(bounds.min.x > -GOODS_CUBE.width / 2); + assert.ok(bounds.max.x < GOODS_CUBE.width / 2); + assert.ok(bounds.min.z > -GOODS_CUBE.depth / 2); + assert.ok(bounds.max.z < GOODS_CUBE.depth / 2); + assert.equal(label.material.map, texture); + assert.equal(label.material.depthTest, true); + assert.equal(label.material.depthWrite, false); + assert.equal(label.castShadow, false); + assert.equal(label.receiveShadow, false); +}); + +test("the cube cannot clip any part of its letter at supported camera tilts", () => { + const slot = new THREE.Group(); + slot.rotation.y = 0.12; + const cube = new THREE.Mesh( + new THREE.BoxGeometry( + GOODS_CUBE.width, + GOODS_CUBE.height, + GOODS_CUBE.depth, + ), + new THREE.MeshBasicMaterial(), + ); + const label = createGoodsLabel(new THREE.Texture(), true); + slot.add(cube, label); + slot.updateMatrixWorld(true); + const ray = new THREE.Raycaster(); + for (const tilt of [0.02, 0.6, 1, Math.PI * 0.41]) { + for (const azimuth of [0, Math.PI / 2, Math.PI, -Math.PI / 2]) { + const camera = new THREE.Vector3().setFromSpherical( + new THREE.Spherical(4, tilt, azimuth), + ); + for (const x of [-0.085, 0, 0.085]) { + for (const z of [-0.085, 0, 0.085]) { + const point = new THREE.Vector3(x, label.position.y, z).applyMatrix4( + slot.matrixWorld, + ); + ray.set(camera, point.sub(camera).normalize()); + assert.equal(ray.intersectObjects([cube, label])[0]?.object, label); + } + } + } + } +}); + +test("empty resource slots keep their flat colour-blind labels", () => { + const label = createGoodsLabel(new THREE.Texture(), false); + assertUpward(label); + assert.ok(Math.abs(label.position.y - (GOODS_FOOTPRINT_Y + 0.003)) < 1e-9); + assert.equal(label.geometry.parameters.width, 0.16); + assert.equal(label.geometry.parameters.height, 0.16); + assert.equal(label.material.transparent, true); + assert.equal(label.castShadow, false); +}); diff --git a/test/three-viewer-browser.mjs b/test/three-viewer-browser.mjs index a0902ff..676d680 100644 --- a/test/three-viewer-browser.mjs +++ b/test/three-viewer-browser.mjs @@ -16,6 +16,15 @@ try { 0, "Receiving preferences never echoes them", ); + // Rebuilding the 3D board must retain/reuse the resource-letter textures. + for (const enabled of [true, false, true]) { + await page.locator("[data-color-blind]").click(); + assert.equal((await state()).preferences.colorBlind, enabled); + assert.equal( + await page.locator(".export-reference .resource-symbol").count(), + enabled ? 3 : 0, + ); + } await page.locator('[data-map-view="2d"]').click(); assert.equal( await page.locator(".vanuatu").getAttribute("data-map-view"), diff --git a/viewer/three/board.js b/viewer/three/board.js index 16f2a0e..2cc95bd 100644 --- a/viewer/three/board.js +++ b/viewer/three/board.js @@ -1,6 +1,11 @@ import * as THREE from "three"; import { translateText } from "../localization/index.js"; import { goodsSlots } from "../goods-slots.js"; +import { + createGoodsLabel, + GOODS_CUBE, + GOODS_FOOTPRINT_Y, +} from "./goods-label.js"; import { OrbitControls } from "three/addons/controls/OrbitControls.js"; import { TILES } from "../../engine/catalog.js"; import { icon, token as renderToken, esc } from "../art.js"; @@ -440,7 +445,17 @@ export function mountBoard3D(target, { onSelect } = {}) { const color = { kava: "#9aca49", copra: "#fff7dc", beef: "#e77557" }[ good ]; - if (filled) box(slot, color, 0.23, 0.24, 0.23, 0, 0, 0); + if (filled) + box( + slot, + color, + GOODS_CUBE.width, + GOODS_CUBE.height, + GOODS_CUBE.depth, + 0, + 0, + 0, + ); else { // A solid, flat colour patch marks the missing cube's footprint. const footprint = mesh( @@ -448,7 +463,7 @@ export function mountBoard3D(target, { onSelect } = {}) { new THREE.PlaneGeometry(0.26, 0.26), new THREE.MeshBasicMaterial({ color, side: THREE.DoubleSide }), 0, - -0.097, + GOODS_FOOTPRINT_Y, 0, ); footprint.rotation.x = -Math.PI / 2; @@ -468,33 +483,9 @@ export function mountBoard3D(target, { onSelect } = {}) { ctx.fillText({ kava: "K", copra: "C", beef: "B" }[good], 32, 34); const texture = new THREE.CanvasTexture(canvas); texture.colorSpace = THREE.SRGBColorSpace; - goodsLabels.set( - good, - new THREE.SpriteMaterial({ map: texture, depthTest: true }), - ); - } - if (filled) { - const letter = new THREE.Sprite(goodsLabels.get(good)); - letter.position.set(0, 0.02, 0.13); - letter.scale.set(0.18, 0.18, 1); - slot.add(letter); - } else { - const letter = mesh( - slot, - new THREE.PlaneGeometry(0.16, 0.16), - new THREE.MeshBasicMaterial({ - map: goodsLabels.get(good).map, - transparent: true, - depthWrite: false, - }), - 0, - -0.094, - 0, - ); - letter.rotation.x = -Math.PI / 2; - letter.castShadow = false; - letter.receiveShadow = false; + goodsLabels.set(good, texture); } + slot.add(createGoodsLabel(goodsLabels.get(good), filled)); } }); for (let slot = 0; slot < t.drawings; slot++) { @@ -1851,10 +1842,7 @@ export function mountBoard3D(target, { onSelect } = {}) { controls.dispose(); disposeBoard(); for (const m of palette.values()) m.dispose(); - for (const m of goodsLabels.values()) { - m.map.dispose(); - m.dispose(); - } + for (const texture of goodsLabels.values()) texture.dispose(); scene.traverse((o) => { o.geometry?.dispose(); if (!Array.isArray(o.material)) o.material?.dispose(); diff --git a/viewer/three/goods-label.js b/viewer/three/goods-label.js new file mode 100644 index 0000000..bae8060 --- /dev/null +++ b/viewer/three/goods-label.js @@ -0,0 +1,22 @@ +import * as THREE from "three"; + +export const GOODS_CUBE = { width: 0.23, height: 0.24, depth: 0.23 }; +export const GOODS_FOOTPRINT_Y = -0.097; + +export function createGoodsLabel(texture, filled) { + const size = filled ? 0.18 : 0.16; + const letter = new THREE.Mesh( + new THREE.PlaneGeometry(size, size), + new THREE.MeshBasicMaterial({ + map: texture, + transparent: true, + depthWrite: false, + }), + ); + // Print on the top face, rather than billboarding through the cube as the + // camera tilts. The small lift avoids z-fighting with the face/empty slot. + letter.position.y = + (filled ? GOODS_CUBE.height / 2 : GOODS_FOOTPRINT_Y) + 0.003; + letter.rotation.x = -Math.PI / 2; + return letter; +}