Files
vibemake/js/robot-materials.js
2026-10-30 18:43:45 +08:00

80 lines
4.6 KiB
JavaScript

import * as THREE from 'three';
const DEFAULT_COLOR = new THREE.Color('#9a9ea6');
const PRESETS = {
// Common metallic finish for imported geometry that does not carry authored PBR material data.
showcase: { roughness: 0.26, metalness: 0.72, clearcoat: 0.16, clearcoatRoughness: 0.14, envMapIntensity: 1.35 },
plastic: { roughness: 0.46, metalness: 0, clearcoat: 0, clearcoatRoughness: 0.1 },
// 金属预设使用较低粗糙度和轻微 clearcoat,让环境反射形成连续的高光带。
metal: { roughness: 0.24, metalness: 0.88, clearcoat: 0.18, clearcoatRoughness: 0.14 },
rubber: { roughness: 0.86, metalness: 0, clearcoat: 0, clearcoatRoughness: 0.1 },
};
function hasAuthoredPbr(src) {
if (!src?.isMeshStandardMaterial) return false;
const hasTexture = ['map', 'normalMap', 'roughnessMap', 'metalnessMap', 'aoMap', 'emissiveMap', 'bumpMap', 'alphaMap'].some((key) => src[key]);
return src.isMeshPhysicalMaterial || hasTexture || src.roughness !== 1 || src.metalness !== 0;
}
// Phong enhancement adapted from fan-ziqi/robot_viewer, Copyright 2025 Ziqi Fan, Apache-2.0.
// Modified to preserve authored PBR materials, imported texture channels and named overrides.
// See vendor/robot-viewer/LICENSE and vendor/robot-viewer/NOTICE.
function enhancePhongMaterial(mat, envMap) {
mat.shininess = Math.max(mat.shininess, 50);
if (Math.min(mat.specular.r, mat.specular.g, mat.specular.b) < 0.2) mat.specular.setRGB(0.3, 0.3, 0.3);
mat.envMap = envMap || mat.envMap;
mat.reflectivity = 0.3;
}
// PBR 模型保留贴图、物理参数和多材质分组;只有 URDF / STL 的简单材质需要补齐。
export function prepareRobotMaterial(original, paint = {}, options = {}) {
if (Array.isArray(original)) return original.map((mat) => prepareRobotMaterial(mat, paint, options));
const src = original;
const usePhong = options.renderPreset === 'studio' && !hasAuthoredPbr(src);
let mat;
if (usePhong && src?.isMeshPhongMaterial) {
mat = src.clone();
} else if (!usePhong && src?.isMeshStandardMaterial) {
mat = src.clone();
// STL-to-GLB conversion creates a default white StandardMaterial; treat that placeholder like URDF/STL fallback.
if (!hasAuthoredPbr(src)) Object.assign(mat, PRESETS.showcase);
} else {
const hasColor = src?.color && !(src.color.r === 1 && src.color.g === 1 && src.color.b === 1 && src.isMeshPhongMaterial && !src.name);
const Material = usePhong ? THREE.MeshPhongMaterial : THREE.MeshPhysicalMaterial;
mat = new Material({
name: src?.name || '',
color: hasColor ? src.color : DEFAULT_COLOR,
...(usePhong ? {} : PRESETS.showcase),
});
// 贴图共享原资源,颜色 / 向量独立复制,避免悬停高亮影响别的零件。
for (const key of ['map', 'normalMap', 'normalMapType', 'bumpMap', 'bumpScale', 'alphaMap', 'aoMap', 'aoMapIntensity', 'lightMap', 'lightMapIntensity', 'emissiveMap', 'emissiveIntensity', 'displacementMap', 'displacementScale', 'displacementBias', 'transparent', 'opacity', 'alphaTest', 'side', 'depthWrite', 'depthTest', 'vertexColors', 'flatShading']) {
if (src?.[key] !== undefined) mat[key] = src[key];
}
if (src?.normalScale) mat.normalScale.copy(src.normalScale);
if (src?.emissive) mat.emissive.copy(src.emissive);
}
if (usePhong) enhancePhongMaterial(mat, options.envMap);
for (const [name, value] of Object.entries(paint)) {
if (!mat.name.includes(name)) continue;
const override = typeof value === 'string' ? { color: value } : value;
const finish = { ...PRESETS[override.preset], ...override };
if (override.color !== undefined) mat.color.set(override.color);
if (override.emissive !== undefined) mat.emissive.set(override.emissive);
if (mat.isMeshPhongMaterial) {
// Existing roughness overrides still control highlight width in the legacy shader.
if (finish.roughness !== undefined) mat.shininess = THREE.MathUtils.clamp(2 / Math.max(finish.roughness, 0.04) ** 2 - 2, 1, 1000);
if (finish.metalness !== undefined) mat.specular.setScalar(THREE.MathUtils.lerp(0.05, 0.3, finish.metalness));
if (finish.emissiveIntensity !== undefined) mat.emissiveIntensity = finish.emissiveIntensity;
continue;
}
if (PRESETS[override.preset]) Object.assign(mat, PRESETS[override.preset]);
for (const key of ['roughness', 'metalness', 'envMapIntensity', 'emissiveIntensity', 'clearcoat', 'clearcoatRoughness']) {
if (override[key] !== undefined) mat[key] = override[key];
}
}
// 只保留模型或配置明确指定的自发光,不根据绿色外壳的颜色猜测。
mat.userData.baseEmissive = { color: mat.emissive.clone(), intensity: mat.emissiveIntensity };
return mat;
}