Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

make fbx export compatibility service a bit easier to read

Scott Petrovic2856ee67152e
diff --git a/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts b/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts
index fefb9dd..49f4377 100644
--- a/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts
+++ b/src/lib/processes/export-to-file/FbxTextureCompatibilityService.ts
@@ -6,39 +6,20 @@ interface TextureExportState {
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 []
-}
-
+/**
+ * FBX exporters in three.js commonly assume textures are uploaded with flipY=true.
+ * glTF textures are typically flipY=false, so this service converts UV transform
+ * values for FBX export in the constructor and restores original values with restore().
+ */
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>()
+ private readonly textures_to_restore = new Map<Texture, TextureExportState>()
+ constructor (root: Object3D) {
root.traverse((node) => {
- const material_list = collect_materials(node)
+ const material_list = this.collect_materials(node)
material_list.forEach((material) => {
Object.values(material).forEach((value) => {
- if (!is_texture(value)) {
+ if (!this.is_texture(value)) {
return
}
@@ -57,14 +38,19 @@ export class FbxTextureCompatibilityService {
return
}
- if (!textures_to_restore.has(texture)) {
- textures_to_restore.set(texture, {
+ if (!this.textures_to_restore.has(texture)) {
+ // Save original values only once per texture so shared textures
+ // can be mutated for export and restored exactly once.
+ this.textures_to_restore.set(texture, {
flip_y: texture.flipY,
repeat_y: texture.repeat.y,
offset_y: texture.offset.y
})
}
+ // flipY conversion for FBX path:
+ // if v' = 1 - v, then `repeat.y` is negated and `offset.y` is shifted
+ // so the final sampled UVs match the original visual output.
texture.repeat.y = -texture.repeat.y
texture.offset.y = 1 - texture.offset.y
texture.flipY = true
@@ -72,14 +58,48 @@ export class FbxTextureCompatibilityService {
})
})
})
+ }
+
+ /**
+ * Restores the original texture values for all textures that were modified for FBX export.
+ */
+ public restore (): void {
+ this.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
+ })
+ }
- 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
- })
+ /**
+ * Determines whether the given value is a Texture object.
+ * @param value an unknown value that comes in that we will verify if the object is a texture
+ * @returns true if the value is a Texture object, false otherwise
+ */
+ private is_texture (value: unknown): value is Texture {
+ return typeof value === 'object' && value !== null && (value as Texture).isTexture === true
+ }
+
+ /**
+ * Collects all materials from the given node and its descendants.
+ * @param node the Object3D node to collect materials from
+ * @returns an array of material objects found on the node and its descendants
+ */
+ private 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 []
}
}
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.ts
index 13f1faa..ee482cb 100644
--- a/src/lib/processes/export-to-file/StepExportToFile.ts
+++ b/src/lib/processes/export-to-file/StepExportToFile.ts
@@ -174,7 +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)
+ const texture_compatibility_session = new FbxTextureCompatibilityService(exported_scene)
try {
const result = await this.fbx_exporter.parseAsync(exported_scene, {
@@ -187,7 +187,7 @@ export class StepExportToFile extends EventTarget {
this.save_uint8_array(result, `${file_name}.fbx`)
} finally {
- restore_texture_settings()
+ texture_compatibility_session.restore()
exported_scene.animations = []
}
}
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index c30f4f8..3b7e496 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -156,7 +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)
+ const texture_compatibility_session = new FbxTextureCompatibilityService(exported_scene)
try {
const result = await this.fbx_exporter.parseAsync(exported_scene, {
@@ -169,7 +169,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
this.save_uint8_array(result, `${file_name}.fbx`)
} finally {
- restore_texture_settings()
+ texture_compatibility_session.restore()
exported_scene.animations = []
}
}