Import a 3D Model and automatically assign and export animations
TypeScript
Apply FBX texture fix for UV offset
Scott Petrovice9b0d7f3c33b
diff --git a/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts b/src/lib/processes/export-to-file/FbxTextureCompatibilityService.tsnew file mode 100644index 0000000..fefb9dd--- /dev/null+++ b/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts@@ -0,0 +1,85 @@+import { type Object3D, type Texture } from 'three'++interface TextureExportState {+ flip_y: boolean+ repeat_y: number+ offset_y: number+}++function is_texture (value: unknown): value is Texture {+ return typeof value === 'object' && value !== null && (value as Texture).isTexture === true+}++function collect_materials (node: Object3D): Array<Record<string, unknown>> {+ const mesh_like = node as Object3D & { material?: unknown }+ if (mesh_like.material == null) {+ return []+ }++ if (Array.isArray(mesh_like.material)) {+ return mesh_like.material.filter((material): material is Record<string, unknown> => typeof material === 'object' && material !== null)+ }++ if (typeof mesh_like.material === 'object') {+ return [mesh_like.material as Record<string, unknown>]+ }++ return []+}++export class FbxTextureCompatibilityService {+ // FBX importers in three.js commonly assume textures are uploaded with flipY=true.+ // glTF textures are typically flipY=false, so we convert UV transform values for+ // FBX export and restore the original texture settings afterward.+ public static apply_flip_y_compatibility_for_fbx (root: Object3D): () => void {+ const textures_to_restore = new Map<Texture, TextureExportState>()++ root.traverse((node) => {+ const material_list = collect_materials(node)+ material_list.forEach((material) => {+ Object.values(material).forEach((value) => {+ if (!is_texture(value)) {+ return+ }++ const texture = value+ if (texture.flipY !== false) {+ return+ }++ const has_complex_uv_transform =+ texture.rotation !== 0 ||+ texture.center.x !== 0 ||+ texture.center.y !== 0 ||+ texture.matrixAutoUpdate === false++ if (has_complex_uv_transform) {+ return+ }++ if (!textures_to_restore.has(texture)) {+ textures_to_restore.set(texture, {+ flip_y: texture.flipY,+ repeat_y: texture.repeat.y,+ offset_y: texture.offset.y+ })+ }++ texture.repeat.y = -texture.repeat.y+ texture.offset.y = 1 - texture.offset.y+ texture.flipY = true+ texture.needsUpdate = true+ })+ })+ })++ return () => {+ textures_to_restore.forEach((state, texture) => {+ texture.flipY = state.flip_y+ texture.repeat.y = state.repeat_y+ texture.offset.y = state.offset_y+ texture.needsUpdate = true+ })+ }+ }+}diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.tsindex 9d18797..13f1faa 100644--- a/src/lib/processes/export-to-file/StepExportToFile.ts+++ b/src/lib/processes/export-to-file/StepExportToFile.ts@@ -6,6 +6,7 @@ import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPresimport { ExportBoneNamingService } from './ExportBoneNamingService.ts'import { AnimationUtility } from '../animations-listing/AnimationUtility.ts'import { type AnimationExportSelection } from '../animations-listing/interfaces/AnimationExportSelection.ts'+import { FbxTextureCompatibilityService } from './FbxTextureCompatibilityService.ts'// Note: EventTarget is a built-in interface and do not need to import itexport class StepExportToFile extends EventTarget {@@ -173,6 +174,7 @@ export class StepExportToFile extends EventTarget {fbx_export_preset: FbxExportPreset): Promise<void> {exported_scene.animations = animations_to_export+ const restore_texture_settings = FbxTextureCompatibilityService.apply_flip_y_compatibility_for_fbx(exported_scene)try {const result = await this.fbx_exporter.parseAsync(exported_scene, {@@ -185,6 +187,7 @@ export class StepExportToFile extends EventTarget {this.save_uint8_array(result, `${file_name}.fbx`)} finally {+ restore_texture_settings()exported_scene.animations = []}}diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex 020fa86..c30f4f8 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -6,6 +6,7 @@ import { AnimationUtility } from '../../lib/processes/animations-listing/Animatiimport { type AnimationExportSelection } from '../../lib/processes/animations-listing/interfaces/AnimationExportSelection'import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset } from '../../lib/processes/export-to-file/DownloadSettings'import { ExportBoneNamingService } from '../../lib/processes/export-to-file/ExportBoneNamingService'+import { FbxTextureCompatibilityService } from '../../lib/processes/export-to-file/FbxTextureCompatibilityService'export class StepExportRetargetedAnimations extends EventTarget {public animation_clips_to_export: AnimationClip[] = []@@ -155,6 +156,7 @@ export class StepExportRetargetedAnimations extends EventTarget {fbx_export_preset: FbxExportPreset): Promise<void> {exported_scene.animations = animations_to_export+ const restore_texture_settings = FbxTextureCompatibilityService.apply_flip_y_compatibility_for_fbx(exported_scene)try {const result = await this.fbx_exporter.parseAsync(exported_scene, {@@ -167,6 +169,7 @@ export class StepExportRetargetedAnimations extends EventTarget {this.save_uint8_array(result, `${file_name}.fbx`)} finally {+ restore_texture_settings()exported_scene.animations = []}}