Import a 3D Model and automatically assign and export animations
TypeScript
scale down more aggressively if model gets too tall
Scott Petrovic37c53f061981
diff --git a/src/lib/processes/load-model/ModelAnalysisReport.ts b/src/lib/processes/load-model/ModelAnalysisReport.tsindex 1900696..d0b6014 100644--- a/src/lib/processes/load-model/ModelAnalysisReport.ts+++ b/src/lib/processes/load-model/ModelAnalysisReport.ts@@ -1,5 +1,6 @@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'+import { MODEL_HEIGHT_LIMIT } from './ModelCleanupUtility.ts'/*** Materials arrive as whatever type the file's loader picked, so read them@@ -402,10 +403,10 @@ export class ModelAnalysisReport {}const imported_size: Vector3 = imported.world_size- const largest_dimension: number = Math.max(imported_size.x, imported_size.y, imported_size.z)+ const imported_height: number = imported_size.y- if (largest_dimension <= 0.5 || largest_dimension >= 20) {- warnings.push(`Model came in at ${this.format_number(largest_dimension)} units across, so import auto-scaled it to a workable size.`)+ if (imported_height >= MODEL_HEIGHT_LIMIT) {+ warnings.push(`Model came in at ${this.format_number(imported_height)} units tall, so import auto-scaled it to a workable size.`)}if (imported_size.z > imported_size.y * 1.25) {diff --git a/src/lib/processes/load-model/ModelCleanupUtility.ts b/src/lib/processes/load-model/ModelCleanupUtility.tsindex 6ef959c..cc7597c 100644--- a/src/lib/processes/load-model/ModelCleanupUtility.ts+++ b/src/lib/processes/load-model/ModelCleanupUtility.ts@@ -1,6 +1,17 @@import { Box3, type BufferGeometry, Group, Matrix4, MathUtils, type Object3DEventMap, type Object3D, Scene, Mesh, MeshPhongMaterial, type Material, type SkinnedMesh } from 'three'import { FrontSide } from 'three/src/constants.js'+/**+ * Maximum imported model height in scene units. Tune this against the built-in+ * rigs and representative custom models; elongated models may exceed this in+ * width or length without needing to be scaled.+ *+ * We are doing height because the snake is much longer and we don't want to scale it down+ * Humans are by far the most common, so let's just do scaling by the height for now.+ *+ */+export const MODEL_HEIGHT_LIMIT = 2+/*** Utility helpers to clean up and normalize loaded model geometry.*/@@ -111,19 +122,15 @@ export class ModelCleanupUtility {const bounding_box = this.calculate_bounding_box(scene_object)const height = bounding_box.max.y - bounding_box.min.y- const width = bounding_box.max.x - bounding_box.min.x- const depth = bounding_box.max.z - bounding_box.min.z-- const largest_dimension = Math.max(height, width, depth)- if (largest_dimension > 0.5 && largest_dimension < 20) {+ if (height <= MODEL_HEIGHT_LIMIT) {console.log('Model a reasonable size, so no scaling applied: ', bounding_box, ' units is bounding box')return} else {- console.log('Model is very large or small, so scaling applied: ', bounding_box, ' units is bounding box')+ console.log('Model is too tall, so scaling applied: ', bounding_box, ' units is bounding box')}- scale_factor = 1.5 / largest_dimension+ scale_factor = MODEL_HEIGHT_LIMIT / heightscene_object.traverse((child) => {const child_obj = child as Mesh