Files
vibemake/tests/robot-materials.test.js
T

105 lines
5.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { build } from 'esbuild';
import * as THREE from '../vendor/three/build/three.module.min.js';
// 浏览器 import map 的 three 别名在 Node 中指向同一份随站点发布的库。
const { outputFiles } = await build({
entryPoints: ['js/robot-materials.js'],
absWorkingDir: new URL('..', import.meta.url).pathname,
bundle: true, write: false, format: 'esm', platform: 'node',
alias: { three: './vendor/three/build/three.module.min.js' },
});
const { prepareRobotMaterial } = await import(`data:text/javascript;base64,${Buffer.from(outputFiles[0].contents).toString('base64')}`);
test('preserves physical materials, texture channels and authored values without mutating the source', () => {
const normalMap = new THREE.Texture();
const roughnessMap = new THREE.Texture();
const source = new THREE.MeshPhysicalMaterial({
name: 'painted-metal', color: '#315cb0', roughness: 0.23, metalness: 0.85,
clearcoat: 0.6, normalMap, roughnessMap, envMapIntensity: 1.3,
side: THREE.DoubleSide,
});
const material = prepareRobotMaterial(source);
assert.equal(material.isMeshPhysicalMaterial, true);
for (const key of ['roughness', 'metalness', 'clearcoat', 'envMapIntensity', 'side']) assert.equal(material[key], source[key]);
assert.equal(material.normalMap, normalMap);
assert.equal(material.roughnessMap, roughnessMap);
assert.equal(material.color.getHex(), source.color.getHex());
material.color.set('#ff0000');
assert.equal(source.color.getHexString(), '315cb0');
});
test('metal preset adds a controlled clearcoat highlight without changing the authored gold color', () => {
const material = prepareRobotMaterial(new THREE.MeshPhongMaterial({ name: '3d_printed', color: '#9b7600' }), {
'3d_printed': { preset: 'metal', clearcoat: 0.2, clearcoatRoughness: 0.12 },
});
assert.equal(material.isMeshPhysicalMaterial, true);
assert.equal(material.metalness, 0.88);
assert.equal(material.roughness, 0.24);
assert.equal(material.clearcoat, 0.2);
assert.equal(material.clearcoatRoughness, 0.12);
assert.equal(material.color.getHexString(), '9b7600');
});
test('preserves every material slot and applies named overrides independently', () => {
const body = new THREE.MeshPhongMaterial({ name: 'body', color: '#ffffff' });
const tyre = new THREE.MeshPhongMaterial({ name: 'wheel-tyre', color: '#17191c' });
const materials = prepareRobotMaterial([body, tyre], { tyre: { preset: 'rubber', roughness: 0.9 } });
assert.equal(materials.length, 2);
assert.equal(materials[0].name, 'body');
assert.equal(materials[0].metalness, 0.72);
assert.equal(materials[1].name, 'wheel-tyre');
assert.equal(materials[1].roughness, 0.9);
assert.equal(materials[1].metalness, 0);
assert.equal(tyre.roughness, undefined);
});
test('applies the common metallic showcase finish to unconfigured URDF materials while retaining their colors', () => {
const source = new THREE.MeshPhongMaterial({ name: '3d_printed', color: '#ffe000' });
const material = prepareRobotMaterial(source);
assert.equal(material.isMeshPhysicalMaterial, true);
assert.equal(material.color.getHexString(), 'ffe000');
assert.equal(material.metalness, 0.72);
assert.equal(material.roughness, 0.26);
assert.equal(material.envMapIntensity, 1.35);
assert.equal(material.clearcoat, 0.16);
});
test('applies the same finish to default STL-to-GLB materials but preserves authored standard PBR settings', () => {
const imported = prepareRobotMaterial(new THREE.MeshStandardMaterial({ name: 'converted-stl', color: '#9b7600' }));
assert.equal(imported.metalness, 0.72);
assert.equal(imported.roughness, 0.26);
assert.equal(imported.color.getHexString(), '9b7600');
const authored = new THREE.MeshStandardMaterial({ name: 'authored-pbr', color: '#315cb0', roughness: 0.62, metalness: 0.2 });
const preserved = prepareRobotMaterial(authored);
assert.equal(preserved.roughness, 0.62);
assert.equal(preserved.metalness, 0.2);
});
test('green body paint stays non-emissive while authored emission can be restored after hover', () => {
const body = prepareRobotMaterial(new THREE.MeshPhongMaterial({ name: 'body', color: '#c8ff3e' }));
assert.equal(body.emissive.getHex(), 0);
const led = prepareRobotMaterial(new THREE.MeshStandardMaterial({
name: 'led', emissive: '#00ff00', emissiveIntensity: 0.4,
}));
led.emissive.set('#ffffff');
assert.equal(led.userData.baseEmissive.color.getHexString(), '00ff00');
assert.equal(led.userData.baseEmissive.intensity, 0.4);
});
test('keeps transparent legacy material settings and accepts a material preset without repainting', () => {
const source = new THREE.MeshPhongMaterial({ name: 'cover', color: '#335577', transparent: true, opacity: 0.4, depthWrite: false });
source.normalMap = new THREE.Texture();
source.normalScale.set(0.5, 0.7);
const material = prepareRobotMaterial(source, { cover: { preset: 'plastic' } });
assert.equal(material.color.getHex(), source.color.getHex());
assert.equal(material.opacity, 0.4);
assert.equal(material.transparent, true);
assert.equal(material.depthWrite, false);
assert.equal(material.normalMap, source.normalMap);
assert.equal(material.normalScale.x, 0.5);
assert.equal(material.normalScale.y, 0.7);
});