Import a 3D Model and automatically assign and export animations
TypeScript
Add in real props from GLB
Scott Petrovic094d32dac7d4
diff --git a/src/lib/processes/animations-listing/props/ProceduralPropBuilder.ts b/src/lib/processes/animations-listing/props/ProceduralPropBuilder.tsdeleted file mode 100644index 3c3d985..0000000--- a/src/lib/processes/animations-listing/props/ProceduralPropBuilder.ts+++ /dev/null@@ -1,28 +0,0 @@-import { CylinderGeometry, Group, Mesh, MeshStandardMaterial } from 'three'--export class ProceduralPropBuilder {- // sized in meters for a rig at skeleton scale 1.0- public static create_pole (): Group {- return this.create_rod('pole', true)- }-- public static create_staff (): Group {- return this.create_rod('staff', false)- }-- private static create_rod (name: string, origin_at_base: boolean): Group {- const pole_group = new Group()-- const geometry = new CylinderGeometry(0.018, 0.018, 1.7, 12)- const material = new MeshStandardMaterial({ color: 0x8b5a2b, roughness: 0.8, metalness: 0.0 })- const pole_mesh = new Mesh(geometry, material)- pole_mesh.name = `prop_${name}_mesh`- pole_mesh.castShadow = true- if (origin_at_base) {- pole_mesh.position.y = 0.75- }-- pole_group.add(pole_mesh)- return pole_group- }-}diff --git a/src/lib/processes/animations-listing/props/PropCatalog.ts b/src/lib/processes/animations-listing/props/PropCatalog.tsindex 3858e36..99a6c94 100644--- a/src/lib/processes/animations-listing/props/PropCatalog.ts+++ b/src/lib/processes/animations-listing/props/PropCatalog.ts@@ -1,8 +1,10 @@-import { Euler, type Object3D } from 'three'+import { Euler, Group, Mesh, type Object3D } from 'three'+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { SkeletonType } from '../../../enums/SkeletonType.ts'import { PropType } from './PropType.ts'import { PropSide } from './PropSide.ts'-import { ProceduralPropBuilder } from './ProceduralPropBuilder.ts'++export type PropCategory = 'Polearms' | 'Staves' | 'Ranged' | 'Axes' | 'Daggers' | 'Fist Weapons' | 'Hammers' | 'Shields' | 'Swords' | 'Wands'export interface PropMountOffset {// meters for a rig at skeleton scale 1.0, along the hand bone toward the fingers@@ -16,35 +18,72 @@ export interface PropMountOffset {export interface PropDefinition {type: PropTypedisplay_name: string- create_object: () => Object3D+ category: PropCategory+ asset_path: string+ model_offset_y: numberdefault_mount_offsets: Record<PropSide, PropMountOffset>rig_mount_offsets: Partial<Record<SkeletonType, Record<PropSide, PropMountOffset>>>}+function create_definition (+ type: PropType,+ display_name: string,+ category: PropCategory,+ filename: string,+ model_offset_y: number = 0+): PropDefinition {+ return {+ type,+ display_name,+ category,+ asset_path: `props/${filename}`,+ model_offset_y,+ default_mount_offsets: {+ [PropSide.Left]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() },+ [PropSide.Right]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() }+ },+ rig_mount_offsets: {}+ }+}+export class PropCatalog {public static readonly supported_skeleton_types: SkeletonType[] = [SkeletonType.Human, SkeletonType.Kaiju]+ private static readonly loader: GLTFLoader = new GLTFLoader()+ private static readonly model_cache = new Map<string, Promise<Group>>()+private static readonly definitions: PropDefinition[] = [- {- type: PropType.Pole,- display_name: 'Pole',- create_object: () => ProceduralPropBuilder.create_pole(),- default_mount_offsets: {- [PropSide.Left]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() },- [PropSide.Right]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() }- },- rig_mount_offsets: {}- },- {- type: PropType.Staff,- display_name: 'Staff',- create_object: () => ProceduralPropBuilder.create_staff(),- default_mount_offsets: {- [PropSide.Left]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() },- [PropSide.Right]: { along_hand: 0.09, below_palm: 0.035, rotation: new Euler() }- },- rig_mount_offsets: {}- }+ create_definition(PropType.Pole, 'Pole', 'Polearms', 'spear_A.glb', 0.93),+ create_definition(PropType.Halberd, 'Halberd', 'Polearms', 'halberd.glb'),+ create_definition(PropType.Staff, 'Staff', 'Staves', 'staff_A.glb'),+ create_definition(PropType.StaffB, 'Staff B', 'Staves', 'staff_B.glb'),+ create_definition(PropType.ArrowA, 'Arrow A', 'Ranged', 'arrow_A.glb'),+ create_definition(PropType.ArrowB, 'Arrow B', 'Ranged', 'arrow_B.glb'),+ create_definition(PropType.BowA, 'Bow A', 'Ranged', 'bow_A.glb'),+ create_definition(PropType.BowAWithString, 'Bow A (String)', 'Ranged', 'bow_A_withString.glb'),+ create_definition(PropType.BowB, 'Bow B', 'Ranged', 'bow_B.glb'),+ create_definition(PropType.BowBWithString, 'Bow B (String)', 'Ranged', 'bow_B_withString.glb'),+ create_definition(PropType.AxeA, 'Axe A', 'Axes', 'axe_A.glb'),+ create_definition(PropType.AxeB, 'Axe B', 'Axes', 'axe_B.glb'),+ create_definition(PropType.AxeC, 'Axe C', 'Axes', 'axe_C.glb'),+ create_definition(PropType.DaggerA, 'Dagger A', 'Daggers', 'dagger_A.glb'),+ create_definition(PropType.DaggerB, 'Dagger B', 'Daggers', 'dagger_B.glb'),+ create_definition(PropType.FistWeaponA, 'Fist Weapon A', 'Fist Weapons', 'fistweapon_A.glb'),+ create_definition(PropType.FistWeaponAStacked, 'Fist Weapon A (Stacked)', 'Fist Weapons', 'fistweapon_A_stacked.glb'),+ create_definition(PropType.FistWeaponB, 'Fist Weapon B', 'Fist Weapons', 'fistweapon_B.glb'),+ create_definition(PropType.FistWeaponBStacked, 'Fist Weapon B (Stacked)', 'Fist Weapons', 'fistweapon_B_stacked.glb'),+ create_definition(PropType.HammerA, 'Hammer A', 'Hammers', 'hammer_A.glb'),+ create_definition(PropType.HammerB, 'Hammer B', 'Hammers', 'hammer_B.glb'),+ create_definition(PropType.HammerC, 'Hammer C', 'Hammers', 'hammer_C.glb'),+ create_definition(PropType.ShieldA, 'Shield A', 'Shields', 'shield_A.glb'),+ create_definition(PropType.ShieldB, 'Shield B', 'Shields', 'shield_B.glb'),+ create_definition(PropType.ShieldC, 'Shield C', 'Shields', 'shield_C.glb'),+ create_definition(PropType.SwordA, 'Sword A', 'Swords', 'sword_A.glb'),+ create_definition(PropType.SwordB, 'Sword B', 'Swords', 'sword_B.glb'),+ create_definition(PropType.SwordC, 'Sword C', 'Swords', 'sword_C.glb'),+ create_definition(PropType.SwordD, 'Sword D', 'Swords', 'sword_D.glb'),+ create_definition(PropType.SwordE, 'Sword E', 'Swords', 'sword_E.glb'),+ create_definition(PropType.WandA, 'Wand A', 'Wands', 'wand_A.glb')]public static all (): PropDefinition[] {@@ -55,6 +94,23 @@ export class PropCatalog {return this.definitions.find((definition) => definition.type === type)}+ public static async create_object (definition: PropDefinition): Promise<Object3D> {+ const model = (await this.load_model(definition.asset_path)).clone(true)+ model.traverse((child) => {+ if (child instanceof Mesh) {+ child.geometry = child.geometry.clone()+ child.material = Array.isArray(child.material)+ ? child.material.map((material) => material.clone())+ : child.material.clone()+ }+ })++ const prop_object = new Group()+ model.position.y += definition.model_offset_y+ prop_object.add(model)+ return prop_object+ }+public static is_supported_skeleton (skeleton_type: SkeletonType): boolean {return this.supported_skeleton_types.includes(skeleton_type)}@@ -62,4 +118,19 @@ export class PropCatalog {public static mount_offset (definition: PropDefinition, skeleton_type: SkeletonType, side: PropSide): PropMountOffset {return definition.rig_mount_offsets[skeleton_type]?.[side] ?? definition.default_mount_offsets[side]}++ private static async load_model (asset_path: string): Promise<Group> {+ let model_promise = this.model_cache.get(asset_path)+ if (model_promise === undefined) {+ model_promise = this.loader.loadAsync(asset_path)+ .then((gltf) => gltf.scene)+ .catch((error: unknown) => {+ this.model_cache.delete(asset_path)+ throw error+ })+ this.model_cache.set(asset_path, model_promise)+ }++ return await model_promise+ }}diff --git a/src/lib/processes/animations-listing/props/PropType.ts b/src/lib/processes/animations-listing/props/PropType.tsindex 0002590..89d59a4 100644--- a/src/lib/processes/animations-listing/props/PropType.ts+++ b/src/lib/processes/animations-listing/props/PropType.ts@@ -1,5 +1,34 @@export enum PropType {None = 'none',Pole = 'pole',- Staff = 'staff'+ Staff = 'staff',+ ArrowA = 'arrow-a',+ ArrowB = 'arrow-b',+ AxeA = 'axe-a',+ AxeB = 'axe-b',+ AxeC = 'axe-c',+ BowA = 'bow-a',+ BowAWithString = 'bow-a-with-string',+ BowB = 'bow-b',+ BowBWithString = 'bow-b-with-string',+ DaggerA = 'dagger-a',+ DaggerB = 'dagger-b',+ FistWeaponA = 'fistweapon-a',+ FistWeaponAStacked = 'fistweapon-a-stacked',+ FistWeaponB = 'fistweapon-b',+ FistWeaponBStacked = 'fistweapon-b-stacked',+ Halberd = 'halberd',+ HammerA = 'hammer-a',+ HammerB = 'hammer-b',+ HammerC = 'hammer-c',+ ShieldA = 'shield-a',+ ShieldB = 'shield-b',+ ShieldC = 'shield-c',+ StaffB = 'staff-b',+ SwordA = 'sword-a',+ SwordB = 'sword-b',+ SwordC = 'sword-c',+ SwordD = 'sword-d',+ SwordE = 'sword-e',+ WandA = 'wand-a'}diff --git a/src/lib/processes/animations-listing/props/Props.test.ts b/src/lib/processes/animations-listing/props/Props.test.tsindex 9efe921..b075724 100644--- a/src/lib/processes/animations-listing/props/Props.test.ts+++ b/src/lib/processes/animations-listing/props/Props.test.ts@@ -2,7 +2,6 @@ import { describe, it, expect } from 'vitest'import { Bone, Group, Mesh, Skeleton } from 'three'import { HandBoneResolver } from './HandBoneResolver'import { PropCatalog } from './PropCatalog'-import { ProceduralPropBuilder } from './ProceduralPropBuilder'import { PropType } from './PropType'import { PropsExportFilter, PROP_USER_DATA_KEY } from './PropsExportFilter'import { PropSide } from './PropSide'@@ -15,14 +14,15 @@ function make_skeleton (names: string[]): Skeleton {}))}-describe('Procedural props', () => {- it('keeps the staff centered and places the pole origin at its base', () => {- const staff_mesh = ProceduralPropBuilder.create_staff().children[0] as Mesh- const pole_mesh = ProceduralPropBuilder.create_pole().children[0] as Mesh+describe('PropCatalog', () => {+ it('registers every bundled prop model and keeps legacy Pole and Staff choices', () => {+ const definitions = PropCatalog.all()- expect(staff_mesh.position.y).toBe(0)- expect(pole_mesh.position.y).toBe(0.75)- expect(PropCatalog.all().map((definition) => definition.type)).toEqual([PropType.Pole, PropType.Staff])+ expect(definitions).toHaveLength(31)+ expect(new Set(definitions.map((definition) => definition.asset_path)).size).toBe(31)+ expect(PropCatalog.find(PropType.Pole)?.asset_path).toBe('props/spear_A.glb')+ expect(PropCatalog.find(PropType.Pole)?.model_offset_y).toBe(0.93)+ expect(PropCatalog.find(PropType.Staff)?.asset_path).toBe('props/staff_A.glb')})})diff --git a/src/lib/processes/animations-listing/props/PropsManager.ts b/src/lib/processes/animations-listing/props/PropsManager.tsindex c436ecf..0f5a061 100644--- a/src/lib/processes/animations-listing/props/PropsManager.ts+++ b/src/lib/processes/animations-listing/props/PropsManager.ts@@ -26,6 +26,7 @@ export class PropsManager extends EventTarget {private attached_props: Object3D[] = []private readonly grip_frames = new Map<Bone, HandGripFrame>()private added_event_listeners: boolean = false+ private attachment_generation: number = 0public begin (skeleton_type: SkeletonType, skeleton_scale: number): void {this.panel.initialize()@@ -62,6 +63,7 @@ export class PropsManager extends EventTarget {* would also dispose any prop still parented to their bones.*/public detach_all (): void {+ this.attachment_generation++this.dispose_attached_props()this.grip_frames.clear()this.target_skinned_meshes = []@@ -91,6 +93,7 @@ export class PropsManager extends EventTarget {}private refresh_attached_props (): void {+ const generation = ++this.attachment_generationthis.dispose_attached_props()if (!PropCatalog.is_supported_skeleton(this.skeleton_type)) {@@ -110,20 +113,34 @@ export class PropsManager extends EventTarget {}found_hand_bone = true- this.attach_prop(prop_type, side, hand_bone)+ void this.attach_prop(prop_type, side, hand_bone, generation)})this.panel.set_side_enabled(side, skeletons.length === 0 || found_hand_bone)})}- private attach_prop (prop_type: PropType, side: PropSide, hand_bone: Bone): void {+ private async attach_prop (prop_type: PropType, side: PropSide, hand_bone: Bone, generation: number): Promise<void> {const definition = PropCatalog.find(prop_type)if (definition === undefined) {return}- const prop_object = definition.create_object()+ let prop_object: Object3D+ try {+ prop_object = await PropCatalog.create_object(definition)+ } catch (error) {+ if (generation === this.attachment_generation) {+ console.error(`Failed to load prop model ${definition.asset_path}:`, error)+ }+ return+ }++ if (generation !== this.attachment_generation) {+ this.dispose_prop_object(prop_object)+ return+ }+prop_object.name = `prop_${prop_type}_${side}`prop_object.userData[PROP_USER_DATA_KEY] = true@@ -156,16 +173,20 @@ export class PropsManager extends EventTarget {private dispose_attached_props (): void {this.attached_props.forEach((prop_object) => {- prop_object.removeFromParent()- prop_object.traverse((child) => {- if (child instanceof Mesh) {- child.geometry.dispose()- const materials = Array.isArray(child.material) ? child.material : [child.material]- materials.forEach((material) => { material.dispose() })- }- })+ this.dispose_prop_object(prop_object)})this.attached_props = []}++ private dispose_prop_object (prop_object: Object3D): void {+ prop_object.removeFromParent()+ prop_object.traverse((child) => {+ if (child instanceof Mesh) {+ child.geometry.dispose()+ const materials = Array.isArray(child.material) ? child.material : [child.material]+ materials.forEach((material) => { material.dispose() })+ }+ })+ }}diff --git a/src/lib/processes/animations-listing/props/PropsPanel.ts b/src/lib/processes/animations-listing/props/PropsPanel.tsindex dc13619..216c674 100644--- a/src/lib/processes/animations-listing/props/PropsPanel.ts+++ b/src/lib/processes/animations-listing/props/PropsPanel.ts@@ -97,9 +97,17 @@ export class PropsPanel extends EventTarget {}private side_row_html (side: PropSide, label: string): string {+ const definitions = PropCatalog.all()+ const categories = Array.from(new Set(definitions.map((definition) => definition.category)))const options_html = [`<option value="${PropType.None}">None</option>`,- ...PropCatalog.all().map((definition) => `<option value="${definition.type}">${definition.display_name}</option>`)+ ...categories.map((category) => {+ const category_options = definitions+ .filter((definition) => definition.category === category)+ .map((definition) => `<option value="${definition.type}">${definition.display_name}</option>`)+ .join('')+ return `<optgroup label="${category}">${category_options}</optgroup>`+ })].join('')return `diff --git a/static/props/arrow_A.glb b/static/props/arrow_A.glbnew file mode 100644index 0000000..bb3f394Binary files /dev/null and b/static/props/arrow_A.glb differdiff --git a/static/props/arrow_B.glb b/static/props/arrow_B.glbnew file mode 100644index 0000000..4584d69Binary files /dev/null and b/static/props/arrow_B.glb differdiff --git a/static/props/axe_A.glb b/static/props/axe_A.glbnew file mode 100644index 0000000..d19d37dBinary files /dev/null and b/static/props/axe_A.glb differdiff --git a/static/props/axe_B.glb b/static/props/axe_B.glbnew file mode 100644index 0000000..d2c708cBinary files /dev/null and b/static/props/axe_B.glb differdiff --git a/static/props/axe_C.glb b/static/props/axe_C.glbnew file mode 100644index 0000000..302abedBinary files /dev/null and b/static/props/axe_C.glb differdiff --git a/static/props/bow_A.glb b/static/props/bow_A.glbnew file mode 100644index 0000000..b4043f2Binary files /dev/null and b/static/props/bow_A.glb differdiff --git a/static/props/bow_A_withString.glb b/static/props/bow_A_withString.glbnew file mode 100644index 0000000..adf1279Binary files /dev/null and b/static/props/bow_A_withString.glb differdiff --git a/static/props/bow_B.glb b/static/props/bow_B.glbnew file mode 100644index 0000000..3b42e29Binary files /dev/null and b/static/props/bow_B.glb differdiff --git a/static/props/bow_B_withString.glb b/static/props/bow_B_withString.glbnew file mode 100644index 0000000..ac51ddaBinary files /dev/null and b/static/props/bow_B_withString.glb differdiff --git a/static/props/dagger_A.glb b/static/props/dagger_A.glbnew file mode 100644index 0000000..1d2e420Binary files /dev/null and b/static/props/dagger_A.glb differdiff --git a/static/props/dagger_B.glb b/static/props/dagger_B.glbnew file mode 100644index 0000000..2bced7eBinary files /dev/null and b/static/props/dagger_B.glb differdiff --git a/static/props/fistweapon_A.glb b/static/props/fistweapon_A.glbnew file mode 100644index 0000000..a8f596cBinary files /dev/null and b/static/props/fistweapon_A.glb differdiff --git a/static/props/fistweapon_A_stacked.glb b/static/props/fistweapon_A_stacked.glbnew file mode 100644index 0000000..27a0de5Binary files /dev/null and b/static/props/fistweapon_A_stacked.glb differdiff --git a/static/props/fistweapon_B.glb b/static/props/fistweapon_B.glbnew file mode 100644index 0000000..38c98aeBinary files /dev/null and b/static/props/fistweapon_B.glb differdiff --git a/static/props/fistweapon_B_stacked.glb b/static/props/fistweapon_B_stacked.glbnew file mode 100644index 0000000..47f86c1Binary files /dev/null and b/static/props/fistweapon_B_stacked.glb differdiff --git a/static/props/halberd.glb b/static/props/halberd.glbnew file mode 100644index 0000000..f890cadBinary files /dev/null and b/static/props/halberd.glb differdiff --git a/static/props/hammer_A.glb b/static/props/hammer_A.glbnew file mode 100644index 0000000..0f6f52cBinary files /dev/null and b/static/props/hammer_A.glb differdiff --git a/static/props/hammer_B.glb b/static/props/hammer_B.glbnew file mode 100644index 0000000..39c3e29Binary files /dev/null and b/static/props/hammer_B.glb differdiff --git a/static/props/hammer_C.glb b/static/props/hammer_C.glbnew file mode 100644index 0000000..17d3411Binary files /dev/null and b/static/props/hammer_C.glb differdiff --git a/static/props/shield_A.glb b/static/props/shield_A.glbnew file mode 100644index 0000000..a8a6450Binary files /dev/null and b/static/props/shield_A.glb differdiff --git a/static/props/shield_B.glb b/static/props/shield_B.glbnew file mode 100644index 0000000..a03310fBinary files /dev/null and b/static/props/shield_B.glb differdiff --git a/static/props/shield_C.glb b/static/props/shield_C.glbnew file mode 100644index 0000000..9d6450fBinary files /dev/null and b/static/props/shield_C.glb differdiff --git a/static/props/spear_A.glb b/static/props/spear_A.glbnew file mode 100644index 0000000..0febc14Binary files /dev/null and b/static/props/spear_A.glb differdiff --git a/static/props/staff_A.glb b/static/props/staff_A.glbnew file mode 100644index 0000000..0c66ce8Binary files /dev/null and b/static/props/staff_A.glb differdiff --git a/static/props/staff_B.glb b/static/props/staff_B.glbnew file mode 100644index 0000000..24d2076Binary files /dev/null and b/static/props/staff_B.glb differdiff --git a/static/props/sword_A.glb b/static/props/sword_A.glbnew file mode 100644index 0000000..7e1c2b0Binary files /dev/null and b/static/props/sword_A.glb differdiff --git a/static/props/sword_B.glb b/static/props/sword_B.glbnew file mode 100644index 0000000..2b0397fBinary files /dev/null and b/static/props/sword_B.glb differdiff --git a/static/props/sword_C.glb b/static/props/sword_C.glbnew file mode 100644index 0000000..bc113f1Binary files /dev/null and b/static/props/sword_C.glb differdiff --git a/static/props/sword_D.glb b/static/props/sword_D.glbnew file mode 100644index 0000000..f93fef4Binary files /dev/null and b/static/props/sword_D.glb differdiff --git a/static/props/sword_E.glb b/static/props/sword_E.glbnew file mode 100644index 0000000..e81a8ffBinary files /dev/null and b/static/props/sword_E.glb differdiff --git a/static/props/wand_A.glb b/static/props/wand_A.glbnew file mode 100644index 0000000..3802185Binary files /dev/null and b/static/props/wand_A.glb differdiff --git a/static/props/weapons_bits_texture.png b/static/props/weapons_bits_texture.pngnew file mode 100644index 0000000..f94f11eBinary files /dev/null and b/static/props/weapons_bits_texture.png differ