Import a 3D Model and automatically assign and export animations
TypeScript
Add material information to model analysis
Scott Petrovice9963c2b5fe6
diff --git a/src/dialog.css b/src/dialog.cssindex dab3584..0a197ce 100644--- a/src/dialog.css+++ b/src/dialog.css@@ -329,6 +329,27 @@opacity: 0.7;}+.model-analysis-dialog .model-analysis-material-list {+ margin: 0.4rem 0 0;+ padding: 0;+ list-style: none;+ display: grid;+ gap: 0.35rem;+}++.model-analysis-dialog .model-analysis-material {+ margin: 0;+ padding: 0.35rem 0.5rem;+ border-left: 2px solid var(--border-color);+ background: color-mix(in srgb, var(--bg-primary) 60%, transparent);+}++.model-analysis-dialog .model-analysis-material-name {+ display: block;+ margin: 0 0 0.15rem;+ opacity: 0.7;+}+.model-analysis-dialog .model-analysis-object-properties-inline {margin: 0;padding: 0;diff --git a/src/lib/processes/load-model/ModelAnalysisReport.test.ts b/src/lib/processes/load-model/ModelAnalysisReport.test.tsindex 38f984e..812c508 100644--- a/src/lib/processes/load-model/ModelAnalysisReport.test.ts+++ b/src/lib/processes/load-model/ModelAnalysisReport.test.ts@@ -1,5 +1,5 @@import { describe, it, expect } from 'vitest'-import { BoxGeometry, Group, Mesh, MeshStandardMaterial, Scene } from 'three'+import { BoxGeometry, Group, Mesh, MeshPhongMaterial, MeshStandardMaterial, Scene } from 'three'import { ModelAnalysisReport, type SceneSnapshot } from './ModelAnalysisReport'/** A 1x1x1 box mesh with a name, so snapshots have something real to measure. */@@ -115,6 +115,84 @@ describe('ModelAnalysisReport.snapshot_scene', () => {expect(analyzed.warnings).toEqual([])})+ it('records the material properties that affect how the model looks', () => {+ const scene = new Scene()+ const mesh = make_mesh('body')+ const material = mesh.material as MeshStandardMaterial+ material.name = 'skin'+ material.color.set(0x804020)+ material.metalness = 0.25+ scene.add(mesh)++ const analyzed = ModelAnalysisReport.snapshot_scene(scene).objects[0]++ expect(analyzed.materials).toHaveLength(1)+ expect(analyzed.materials[0].name).toBe('skin')+ expect(analyzed.materials[0].type).toBe('MeshStandardMaterial')+ expect(analyzed.materials[0].color_hex).toBe('#804020')+ expect(analyzed.materials[0].metalness).toBeCloseTo(0.25)+ expect(analyzed.materials[0].texture_slots).toEqual([])+ })++ it('flags an emissive material, which is what makes a GLB export glow', () => {+ const scene = new Scene()+ const mesh = make_mesh('lamp')+ const material = mesh.material as MeshStandardMaterial+ material.name = 'lamp_mat'+ material.emissive.set(0xffffff)+ material.emissiveIntensity = 1+ scene.add(mesh)++ const analyzed = ModelAnalysisReport.snapshot_scene(scene).objects[0]++ expect(analyzed.materials[0].emissive_hex).toBe('#ffffff')+ expect(analyzed.materials[0].emissive_strength).toBeCloseTo(1)+ expect(analyzed.warnings.some((entry) => entry.includes('emissive (glow) color'))).toBe(true)+ })++ it('does not flag a material whose emissive color is black', () => {+ const scene = new Scene()+ const mesh = make_mesh('body')+ const material = mesh.material as MeshStandardMaterial+ material.emissive.set(0x000000)+ scene.add(mesh)++ const analyzed = ModelAnalysisReport.snapshot_scene(scene).objects[0]++ expect(analyzed.materials[0].emissive_strength).toBe(0)+ expect(analyzed.warnings).toEqual([])+ })++ it('flags a phong material whose emissive intensity is dropped on export', () => {+ const scene = new Scene()+ const mesh = new Mesh(new BoxGeometry(1, 1, 1), new MeshPhongMaterial())+ mesh.name = 'lamp'+ const material = mesh.material as MeshPhongMaterial+ material.emissive.set(0xffffff)+ // a dim glow on screen here, but a GLB export keeps the color and loses this+ material.emissiveIntensity = 0.1+ scene.add(mesh)++ const analyzed = ModelAnalysisReport.snapshot_scene(scene).objects[0]++ expect(analyzed.materials[0].exports_emissive_intensity).toBe(false)+ expect(analyzed.warnings.some((entry) => entry.includes('drops the intensity'))).toBe(true)+ })++ it('does not flag dropped intensity on a standard material, which can export it', () => {+ const scene = new Scene()+ const mesh = make_mesh('lamp')+ const material = mesh.material as MeshStandardMaterial+ material.emissive.set(0xffffff)+ material.emissiveIntensity = 0.1+ scene.add(mesh)++ const analyzed = ModelAnalysisReport.snapshot_scene(scene).objects[0]++ expect(analyzed.materials[0].exports_emissive_intensity).toBe(true)+ expect(analyzed.warnings.some((entry) => entry.includes('drops the intensity'))).toBe(false)+ })+it('does not flag a mesh that is already baked at the origin', () => {const scene = new Scene()scene.add(make_mesh('body'))diff --git a/src/lib/processes/load-model/ModelAnalysisReport.ts b/src/lib/processes/load-model/ModelAnalysisReport.tsindex 7bdc311..d59c458 100644--- a/src/lib/processes/load-model/ModelAnalysisReport.ts+++ b/src/lib/processes/load-model/ModelAnalysisReport.ts@@ -1,6 +1,62 @@-import { Box3, Vector3, type BufferGeometry, type Material, type Mesh, type Object3D } from 'three'+import { Box3, SRGBColorSpace, Vector3, type BufferGeometry, type Material, type Mesh, type MeshPhongMaterial, type MeshStandardMaterial, type Object3D, type Texture } from 'three'import { ModalDialog } from '../../ModalDialog.ts'+/**+ * Materials arrive as whatever type the file's loader picked, so read them+ * through one shape that covers the properties we report on and treat anything+ * the material does not have as absent.+ */+type InspectableMaterial = Material & Partial<MeshStandardMaterial & MeshPhongMaterial>++/** Texture slots worth listing, in the order they show up in the report. */+const TEXTURE_SLOTS: Array<[keyof InspectableMaterial, string]> = [+ ['map', 'color'],+ ['emissiveMap', 'emissive'],+ ['normalMap', 'normal'],+ ['bumpMap', 'bump'],+ ['roughnessMap', 'roughness'],+ ['metalnessMap', 'metalness'],+ ['specularMap', 'specular'],+ ['aoMap', 'ambient occlusion'],+ ['alphaMap', 'alpha'],+ ['displacementMap', 'displacement']+]++/**+ * What one material on one mesh looked like at import time. Emissive gets its+ * own fields because it is the property most likely to come in wrong from an+ * FBX and then follow the model all the way out to a GLB export.+ */+export interface MaterialInfo {+ name: string+ type: string+ color_hex: string | null+ emissive_hex: string | null+ emissive_intensity: number+ /**+ * Brightest channel of the emissive color on its own. A GLB export writes the+ * color straight into emissiveFactor whenever this is above zero, so this is+ * the number that decides whether the exported model glows.+ */+ emissive_strength: number+ /**+ * Whether emissive intensity survives an export. Only MeshStandardMaterial+ * carries it, through the KHR_materials_emissive_strength extension. FBX files+ * usually load as MeshPhongMaterial, where the intensity dims the model in the+ * viewport but is dropped on export, leaving the full emissive color behind.+ */+ exports_emissive_intensity: boolean+ specular_hex: string | null+ metalness: number | null+ roughness: number | null+ shininess: number | null+ opacity: number+ transparent: boolean+ vertex_colors: boolean+ side: string+ texture_slots: string[]+}+/*** Snapshot of a single mesh-bearing object, taken at a point in time so later* processing steps cannot change the numbers we are reporting on.@@ -19,7 +75,7 @@ export interface AnalyzedObject {transformed_ancestors: string[]vertex_count: numbertriangle_count: number- material_summary: string+ materials: MaterialInfo[]has_uvs: booleanhas_normals: booleanhas_skin_weights: boolean@@ -68,6 +124,9 @@ export class ModelAnalysisReport {// rotation this far from zero (in degrees) is treated as intentionalprivate static readonly ROTATION_EPSILON_DEGREES = 0.01+ // emissive brightness above this is enough to visibly wash a model out+ private static readonly EMISSIVE_EPSILON = 0.01+/*** Walk a scene and record everything worth reporting on.* @param scene_object root of the scene graph to inspect@@ -147,7 +206,7 @@ export class ModelAnalysisReport {transformed_ancestors: this.find_transformed_ancestors(mesh, scene_root),vertex_count: position_attribute === undefined ? 0 : position_attribute.count,triangle_count: this.count_triangles(geometry),- material_summary: this.describe_material(mesh.material),+ materials: this.analyze_materials(mesh.material),has_uvs: geometry.getAttribute('uv') !== undefined,has_normals: geometry.getAttribute('normal') !== undefined,has_skin_weights: geometry.getAttribute('skinWeight') !== undefined,@@ -195,38 +254,63 @@ export class ModelAnalysisReport {return Math.floor(position_attribute.count / 3)}- private static describe_material (material: Material | Material[]): string {+ private static analyze_materials (material: Material | Material[]): MaterialInfo[] {if (Array.isArray(material)) {- if (material.length === 0) {- return 'none'- }- return `${material.length} materials: ` + material.map((entry) => this.describe_single_material(entry)).join(', ')+ return material+ .filter((entry) => entry !== undefined && entry !== null)+ .map((entry) => this.analyze_material(entry))}if (material === undefined || material === null) {- return 'none'+ return []}- return this.describe_single_material(material)+ return [this.analyze_material(material)]}- private static describe_single_material (material: Material): string {- const parts: string[] = [material.type]-- // texture file name- if (material.name !== '') {- parts.push(`"${material.name}"`)- }+ /**+ * Pull the material properties that explain how a mesh ends up looking, both+ * in the viewport and after export. Anything the material type does not have+ * comes back as null rather than a made up default.+ */+ private static analyze_material (material: Material): MaterialInfo {+ const inspectable: InspectableMaterial = material as InspectableMaterial- // a missing texture map is a common reason a model imports looking flat/grey- // we already show info about the material type if it exists, so don't show anything if we have a map- const has_map: boolean = (material as any).map !== undefined && (material as any).map !== null- parts.push(has_map ? 'textured' : 'no texture')+ // three defaults emissiveIntensity to 1, but a material type without any+ // emissive support has neither property+ const emissive_intensity: number = inspectable.emissiveIntensity ?? 1+ const emissive_strength: number = inspectable.emissive === undefined+ ? 0+ : Math.max(inspectable.emissive.r, inspectable.emissive.g, inspectable.emissive.b)- // single side, double sided, etc.- parts.push(this.describe_material_side(material.side))+ return {+ name: material.name,+ type: material.type,+ color_hex: inspectable.color === undefined ? null : `#${inspectable.color.getHexString(SRGBColorSpace)}`,+ emissive_hex: inspectable.emissive === undefined ? null : `#${inspectable.emissive.getHexString(SRGBColorSpace)}`,+ emissive_intensity,+ emissive_strength,+ exports_emissive_intensity: inspectable.isMeshStandardMaterial === true,+ specular_hex: inspectable.specular === undefined ? null : `#${inspectable.specular.getHexString(SRGBColorSpace)}`,+ metalness: inspectable.metalness ?? null,+ roughness: inspectable.roughness ?? null,+ shininess: inspectable.shininess ?? null,+ opacity: material.opacity,+ transparent: material.transparent,+ vertex_colors: material.vertexColors,+ side: this.describe_material_side(material.side),+ texture_slots: this.find_texture_slots(inspectable)+ }+ }- return parts.join(' / ')+ /** Names of the texture slots this material actually has a texture in. */+ private static find_texture_slots (material: InspectableMaterial): string[] {+ return TEXTURE_SLOTS+ .filter(([property]) => {+ const texture: Texture | null | undefined = material[property] as Texture | null | undefined+ return texture !== undefined && texture !== null+ })+ .map(([, label]) => label)}private static describe_material_side (side: number): string {@@ -269,9 +353,44 @@ export class ModelAnalysisReport {warnings.push('Mesh is already rigged. This workflow drops the existing skeleton - use "Use Your Rigged Model" to keep it.')}+ analyzed.materials.forEach((material) => {+ warnings.push(...this.collect_material_warnings(material))+ })++ return warnings+ }++ /**+ * Material problems that carry through to the export. Emissive is the one that+ * bites most often: FBX files frequently set an emissive color without meaning+ * to, and the GLB export writes that color into emissiveFactor, so the model+ * comes out glowing even though it looked fine in the tool that authored it.+ */+ private static collect_material_warnings (material: MaterialInfo): string[] {+ const warnings: string[] = []+ const label: string = this.material_label(material)++ if (material.emissive_strength <= this.EMISSIVE_EPSILON) {+ return warnings+ }++ warnings.push(`${label} has an emissive (glow) color of ${material.emissive_hex ?? '-'}. GLB export writes that color out as emissiveFactor, so the model glows regardless of lighting. Emissive should usually be black (#000000) unless the part is meant to light up.`)++ // the case that catches people out: the viewport dims the glow by the+ // intensity, the export does not, so the GLB comes back brighter than what+ // was on screen here. FBX materials load as Phong, which is exactly this case+ if (!material.exports_emissive_intensity && Math.abs(material.emissive_intensity - 1) > this.EMISSIVE_EPSILON) {+ warnings.push(`${label} dims its glow with an emissive intensity of ${this.format_number(material.emissive_intensity)}, but only standard (PBR) materials can store intensity in a GLB. This one is a ${material.type}, so the export keeps the full emissive color and drops the intensity - the exported model will glow more than it does here.`)+ }+return warnings}+ /** How a material is referred to in warning text. */+ private static material_label (material: MaterialInfo): string {+ return material.name === '' ? `Material (${material.type})` : `Material "${material.name}"`+ }+/** Scene wide observations that no single object can tell us. */private static collect_scene_warnings (analysis: ModelImportAnalysis): string[] {const warnings: string[] = []@@ -432,6 +551,7 @@ export class ModelAnalysisReport {<span class="model-analysis-name">${this.escape_html(analyzed.name)}</span><span class="model-analysis-parent">Parent: ${this.escape_html(this.describe_parentage(analyzed))}</span>${this.build_mesh_properties_html(analyzed)}+ ${this.build_materials_html(analyzed)}</li>`).join('')@@ -453,12 +573,91 @@ export class ModelAnalysisReport {['Size', this.format_vector(analyzed.world_size, ' x ')],['Vertices', analyzed.vertex_count.toLocaleString()],['Triangles', analyzed.triangle_count.toLocaleString()],- ['Material', analyzed.material_summary],['UVs', analyzed.has_uvs ? 'yes' : 'no'],['Normals', analyzed.has_normals ? 'yes' : 'no'],['Skin weights', analyzed.has_skin_weights ? 'yes' : 'no']]+ return this.build_property_list_html(properties)+ }++ /** One material block per material on the mesh, since a mesh can carry several. */+ private static build_materials_html (analyzed: AnalyzedObject): string {+ if (analyzed.materials.length === 0) {+ return '<p class="model-analysis-material-name">Material: none</p>'+ }++ const blocks: string = analyzed.materials+ .map((material) => `+ <li class="model-analysis-material">+ <span class="model-analysis-material-name">${this.escape_html(this.material_label(material))} - ${this.escape_html(material.type)}</span>+ ${this.build_property_list_html(this.material_properties(material))}+ </li>+ `)+ .join('')++ return `<ul class="model-analysis-material-list">${blocks}</ul>`+ }++ /**+ * Emissive gets three rows rather than one because the color, what the+ * viewport shows, and what an export writes are all different numbers.+ */+ private static material_properties (material: MaterialInfo): Array<[string, string]> {+ const properties: Array<[string, string]> = []++ if (material.color_hex !== null) {+ properties.push(['Base color', material.color_hex])+ }++ if (material.emissive_hex !== null) {+ properties.push(['Emissive color', material.emissive_hex])+ properties.push(['Emissive intensity', this.format_number(material.emissive_intensity)])+ properties.push(['Emissive in GLB export', this.describe_exported_emissive(material)])+ }++ if (material.metalness !== null) {+ properties.push(['Metalness', this.format_number(material.metalness)])+ }++ if (material.roughness !== null) {+ properties.push(['Roughness', this.format_number(material.roughness)])+ }++ if (material.shininess !== null) {+ properties.push(['Shininess', this.format_number(material.shininess)])+ }++ if (material.specular_hex !== null) {+ properties.push(['Specular color', material.specular_hex])+ }++ properties.push(['Opacity', this.format_number(material.opacity)])+ properties.push(['Transparent', material.transparent ? 'yes' : 'no'])+ properties.push(['Vertex colors', material.vertex_colors ? 'yes' : 'no'])+ properties.push(['Renders', material.side])+ properties.push([+ 'Textures',+ material.texture_slots.length === 0 ? 'none' : material.texture_slots.join(', ')+ ])++ return properties+ }++ /** What a GLB export will actually do with this material's emissive settings. */+ private static describe_exported_emissive (material: MaterialInfo): string {+ if (material.emissive_strength <= this.EMISSIVE_EPSILON) {+ return 'none (black, no glow)'+ }++ if (material.exports_emissive_intensity) {+ return `${material.emissive_hex ?? '-'} at ${this.format_number(material.emissive_intensity)} intensity`+ }++ return `${material.emissive_hex ?? '-'} at full intensity (${material.type} cannot export intensity)`+ }++ private static build_property_list_html (properties: Array<[string, string]>): string {return `<ul class="model-analysis-object-properties-inline">${properties