From 2e2d27780b906566d20683ea7d4d903fb57b263a Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Sun, 21 Jan 2024 11:33:24 +0000 Subject: [PATCH 1/4] Partial fix for textures and bones --- .../scene/correction-steps/boneDeduping.ts | 3 - .../correction-steps/mergeGeometryGroups.ts | 60 +++++++++++++++++++ .../src/scene/correction-steps/reposeBones.ts | 39 ++++++++++++ .../src/scene/correction-steps/runFixes.ts | 6 +- 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tools/gltf-avatar-exporter/src/scene/correction-steps/mergeGeometryGroups.ts create mode 100644 tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/boneDeduping.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/boneDeduping.ts index f573650..501ff31 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/boneDeduping.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/boneDeduping.ts @@ -1,7 +1,6 @@ import * as THREE from "three"; import { Group } from "three"; -import { reposeSkinnedMeshes } from "./reposeSkinnedMeshes"; import { Step } from "./types"; export const boneDedupingCorrectionStep: Step = { @@ -49,8 +48,6 @@ export const boneDedupingCorrectionStep: Step = { } }); - reposeSkinnedMeshes(group); - return { didApply: true, topLevelMessage: { diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/mergeGeometryGroups.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/mergeGeometryGroups.ts new file mode 100644 index 0000000..0c505be --- /dev/null +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/mergeGeometryGroups.ts @@ -0,0 +1,60 @@ +import * as THREE from "three"; +import { Group } from "three"; +// @ts-ignore +// eslint-disable-next-line import/no-unresolved +import * as BufferGeometryUtils from "three/addons/utils/BufferGeometryUtils.js"; + +import { LogMessage, Step } from "./types"; + +export const mergeGeometryGroupsCorrectionStep: Step = { + name: "mergeGeometryGroups", + action: (group: Group) => { + const logs: Array = []; + + group.traverse((child) => { + const asSkinnedMesh = child as THREE.SkinnedMesh; + if (asSkinnedMesh.isSkinnedMesh) { + const geometry = asSkinnedMesh.geometry; + const existingIds = new Set(); + let didHaveDuplicateIds = false; + if (geometry.groups.length > 0) { + for (const geoGroup of geometry.groups) { + if (geoGroup.materialIndex) { + if (existingIds.has(geoGroup.materialIndex)) { + didHaveDuplicateIds = true; + break; + } + existingIds.add(geoGroup.materialIndex); + } + } + if (didHaveDuplicateIds) { + logs.push({ + level: "info", + message: `Merging geometry groups for mesh: ${child.name}`, + }); + BufferGeometryUtils.mergeGroups(geometry); + } + } + } + }); + + if (logs.length === 0) { + return { + didApply: false, + topLevelMessage: { + level: "info", + message: "No geometries with duplicate material ids found.", + }, + }; + } + + return { + didApply: true, + topLevelMessage: { + level: "info", + message: "Merged geometry groups", + }, + logs, + }; + }, +}; diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts new file mode 100644 index 0000000..cb083a9 --- /dev/null +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts @@ -0,0 +1,39 @@ +import * as THREE from "three"; +import { Group } from "three"; + +import { LogMessage, Step } from "./types"; + +export const reposeBonesCorrectionStep: Step = { + name: "reposeBones", + action: (group: Group) => { + const logs: Array = []; + + group.traverse((child) => { + const asSkinnedMesh = child as THREE.SkinnedMesh; + if (asSkinnedMesh.isSkinnedMesh) { + console.log("reposing", asSkinnedMesh.name); + asSkinnedMesh.skeleton.pose(); + asSkinnedMesh.skeleton.calculateInverses(); + } + }); + + if (logs.length === 0) { + return { + didApply: false, + topLevelMessage: { + level: "info", + message: "No geometries with duplicate material ids found.", + }, + }; + } + + return { + didApply: true, + topLevelMessage: { + level: "info", + message: "Merged geometry groups", + }, + logs, + }; + }, +}; diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts index 67ddf36..9b3482b 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts @@ -4,10 +4,12 @@ import { fixFlippedBitmapTexturesCorrectionStep } from "./fixBitmapTextures"; import { fixBonesScaleCorrectionStep } from "./fixBonesScale"; import { fixMeshScaleCorrectionStep } from "./fixMeshScale"; import { levelOfDetailDedupingCorrectionStep } from "./lodDeduping"; +import { mergeGeometryGroupsCorrectionStep } from "./mergeGeometryGroups"; import { placeholderMissingTexturesCorrectionStep } from "./placeholderMissingTextures"; import { removeTransparencyFromMaterialsCorrectionStep } from "./removeTransparencyFromMaterials"; import { removeVertexColorsCorrectionStep } from "./removeVertexColors"; import { replaceIncompatibleMaterialsCorrectionStep } from "./replaceIncompatibleMaterials"; +import { reposeBonesCorrectionStep } from "./reposeBones"; import { rotatePelvisCorrectionStep } from "./rotatePelvis"; import { rotateRootCorrectionStep } from "./rotateRoot"; import { rotateWholeGroupCorrectionStep } from "./rotateWholeGroup"; @@ -16,12 +18,14 @@ import { zUpMeshCorrectionStep } from "./zUpMeshCorrectionStep"; export const correctionSteps = [ levelOfDetailDedupingCorrectionStep, + mergeGeometryGroupsCorrectionStep, + boneDedupingCorrectionStep, + reposeBonesCorrectionStep, rotateWholeGroupCorrectionStep, zUpMeshCorrectionStep, zUpBonesCorrectionStep, fixMeshScaleCorrectionStep, fixBonesScaleCorrectionStep, - boneDedupingCorrectionStep, connectRootCorrectionStep, rotateRootCorrectionStep, rotatePelvisCorrectionStep, From 11f0b63197860075af5ff8ee935945aa11932b2a Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Sun, 21 Jan 2024 15:10:56 +0000 Subject: [PATCH 2/4] Fixed root child position correction --- .../src/scene/correction-steps/rotateRoot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/rotateRoot.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/rotateRoot.ts index 012bfe1..48c42d0 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/rotateRoot.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/rotateRoot.ts @@ -53,7 +53,7 @@ export const rotateRootCorrectionStep: Step = { if (asBone.isBone) { const tempPosY = asBone.position.y; asBone.position.y = asBone.position.z; - asBone.position.z = tempPosY; + asBone.position.z = -tempPosY; const [x, y, z, w] = asBone.quaternion.toArray(); asBone.quaternion.set(x, -y, -z, w); From 2da4cd3aa482ec0593e031bdfd2a76c9a24d70b4 Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Sun, 21 Jan 2024 16:26:13 +0000 Subject: [PATCH 3/4] Fixed repose ordering --- .../src/scene/correction-steps/reposeBones.ts | 20 ++++++++++++++++--- .../scene/correction-steps/rotatePelvis.ts | 7 +++++++ .../src/scene/correction-steps/runFixes.ts | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts index cb083a9..fde149f 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts @@ -11,9 +11,23 @@ export const reposeBonesCorrectionStep: Step = { group.traverse((child) => { const asSkinnedMesh = child as THREE.SkinnedMesh; if (asSkinnedMesh.isSkinnedMesh) { - console.log("reposing", asSkinnedMesh.name); + const inverses = [...asSkinnedMesh.skeleton.boneInverses]; asSkinnedMesh.skeleton.pose(); asSkinnedMesh.skeleton.calculateInverses(); + const newInverses = asSkinnedMesh.skeleton.boneInverses; + let allInversesEqual = true; + for (let i = 0; i < inverses.length; i++) { + if (!inverses[i].equals(newInverses[i])) { + allInversesEqual = false; + break; + } + } + if (!allInversesEqual) { + logs.push({ + level: "info", + message: `Reposed skeleton for ${asSkinnedMesh.name}`, + }); + } } }); @@ -22,7 +36,7 @@ export const reposeBonesCorrectionStep: Step = { didApply: false, topLevelMessage: { level: "info", - message: "No geometries with duplicate material ids found.", + message: "No skeletons were reposed.", }, }; } @@ -31,7 +45,7 @@ export const reposeBonesCorrectionStep: Step = { didApply: true, topLevelMessage: { level: "info", - message: "Merged geometry groups", + message: "Skeleton(s) were reposed", }, logs, }; diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/rotatePelvis.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/rotatePelvis.ts index bfde991..2ceb9b1 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/rotatePelvis.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/rotatePelvis.ts @@ -47,6 +47,13 @@ export const rotatePelvisCorrectionStep: Step = { pelvisBone.rotation.x += 0; pelvisBone.rotation.y -= HalfPi; pelvisBone.rotation.z += HalfPi; + } else if (isNear(pelvisX, 0) && isNear(pelvisY, -HalfPi) && isNear(pelvisZ, 0)) { + pelvisBone.rotation.x += 0; + pelvisBone.rotation.y += HalfPi; + pelvisBone.rotation.z += HalfPi; + const tempPosY = pelvisBone.position.y; + pelvisBone.position.y = pelvisBone.position.z; + pelvisBone.position.z = -tempPosY; } else { reposeSkinnedMeshes(group); return { diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts index 9b3482b..360e6e4 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/runFixes.ts @@ -20,11 +20,11 @@ export const correctionSteps = [ levelOfDetailDedupingCorrectionStep, mergeGeometryGroupsCorrectionStep, boneDedupingCorrectionStep, - reposeBonesCorrectionStep, rotateWholeGroupCorrectionStep, zUpMeshCorrectionStep, zUpBonesCorrectionStep, fixMeshScaleCorrectionStep, + reposeBonesCorrectionStep, fixBonesScaleCorrectionStep, connectRootCorrectionStep, rotateRootCorrectionStep, From 646c3ca9935813dd34eed0b1f9370fbf7cf7b255 Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Sun, 21 Jan 2024 16:28:53 +0000 Subject: [PATCH 4/4] Fixed message --- .../src/scene/correction-steps/reposeBones.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts index fde149f..54f7150 100644 --- a/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts +++ b/tools/gltf-avatar-exporter/src/scene/correction-steps/reposeBones.ts @@ -45,7 +45,7 @@ export const reposeBonesCorrectionStep: Step = { didApply: true, topLevelMessage: { level: "info", - message: "Skeleton(s) were reposed", + message: "Skeletons were reposed", }, logs, };