Skip to content

Commit

Permalink
Add steps to correct materials from some FBX tools (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusLongmuir authored Dec 7, 2023
1 parent 4255993 commit ea1fbcd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const fixBonesScaleCorrectionStep: Step = {
level: "info",
message: `Bones size: x: ${xBonesSize}, y: ${yBonesSize}, z: ${zBonesSize}`,
};
const bonesAre100TimesTooLarge = zBonesSize > 100 || yBonesSize > 100;
const bonesAre1000TimesTooLarge = zBonesSize > 1000 || yBonesSize > 1000;
const bonesAre100TimesTooLarge = zBonesSize > 50 || yBonesSize > 50;
const bonesAre1000TimesTooLarge = zBonesSize > 500 || yBonesSize > 500;

if (!bonesAre100TimesTooLarge) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const fixMeshScaleCorrectionStep: Step = {
level: "info",
message: `Bones size: x: ${xBonesSize}, y: ${yBonesSize}, z: ${zBonesSize}`,
};
const bonesAre100TimesTooLarge = zBonesSize > 100 || yBonesSize > 100;
const bonesAre1000TimesTooLarge = zBonesSize > 1000 || yBonesSize > 1000;
const bonesAre100TimesTooLarge = zBonesSize > 50 || yBonesSize > 50;
const bonesAre1000TimesTooLarge = zBonesSize > 500 || yBonesSize > 500;

if (!bonesAre100TimesTooLarge) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as THREE from "three";
import { Group } from "three";

import { Step } from "./types";

function fixMaterial(material: THREE.Material): [string, THREE.Material] | null {
if (
material instanceof THREE.MeshLambertMaterial ||
material instanceof THREE.MeshStandardMaterial ||
material instanceof THREE.MeshPhysicalMaterial
) {
if (material.transparent) {
const cloned = material.clone();
cloned.transparent = false;
return ["Disabled transparency", cloned];
}
}
return null;
}

export const removeTransparencyFromMaterialsCorrectionStep: Step = {
name: "removeTransparencyFromMaterials",
action: (group: Group) => {
const logs: Array<string> = [];

group.traverse((child) => {
const asMesh = child as THREE.Mesh;
if (asMesh.isMesh) {
const originalMaterial = asMesh.material;
if (!originalMaterial) {
return;
}
if (Array.isArray(originalMaterial)) {
asMesh.material = originalMaterial.map((innerMaterial) => {
const fixResult = fixMaterial(innerMaterial);
if (fixResult) {
const [message, fixedMaterial] = fixResult;
logs.push(`${asMesh.name} - ${innerMaterial.name} - ${message}`);
return fixedMaterial;
}
return innerMaterial;
});
} else {
const fixResult = fixMaterial(originalMaterial);
if (fixResult) {
const [message, fixedMaterial] = fixResult;
logs.push(`${asMesh.name} - ${originalMaterial.name} - ${message}`);
asMesh.material = fixedMaterial;
}
}
}
});

if (logs.length === 0) {
return {
didApply: false,
topLevelMessage: {
level: "info",
message: "No materials with transparency detected.",
},
};
}

return {
didApply: true,
topLevelMessage: {
level: "info",
message: "Detected materials with transparency.",
},
logs: logs.map((message) => ({
level: "warn",
message,
})),
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as THREE from "three";
import { Group } from "three";

import { LogMessage, Step } from "./types";

export const removeVertexColorsCorrectionStep: Step = {
name: "removeVertexColors",
action: (group: Group) => {
const logs: Array<LogMessage> = [];
group.traverse((child) => {
const asSkinnedMesh = child as THREE.SkinnedMesh;
if (asSkinnedMesh.isSkinnedMesh) {
const geometry = asSkinnedMesh.geometry;
if (geometry.attributes.color) {
geometry.deleteAttribute("color");
logs.push({
level: "info",
message: `Removed vertex colors from ${asSkinnedMesh.name}`,
});
}
}
});

if (logs.length === 0) {
return {
didApply: false,
topLevelMessage: {
level: "info",
message: "No meshes with vertex colors found.",
},
logs,
};
}

return {
didApply: true,
topLevelMessage: {
level: "info",
message: `Found mesh(es) with vertex colors.`,
},
logs: logs,
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { fixBonesScaleCorrectionStep } from "./fixBonesScale";
import { fixMeshScaleCorrectionStep } from "./fixMeshScale";
import { levelOfDetailDedupingCorrectionStep } from "./lodDeduping";
import { placeholderMissingTexturesCorrectionStep } from "./placeholderMissingTextures";
import { removeTransparencyFromMaterialsCorrectionStep } from "./removeTransparencyFromMaterials";
import { removeVertexColorsCorrectionStep } from "./removeVertexColors";
import { replaceIncompatibleMaterialsCorrectionStep } from "./replaceIncompatibleMaterials";
import { rotatePelvisCorrectionStep } from "./rotatePelvis";
import { rotateRootCorrectionStep } from "./rotateRoot";
Expand All @@ -20,6 +22,8 @@ export const correctionSteps = [
boneDedupingCorrectionStep,
rotateRootCorrectionStep,
rotatePelvisCorrectionStep,
removeVertexColorsCorrectionStep,
placeholderMissingTexturesCorrectionStep,
replaceIncompatibleMaterialsCorrectionStep,
removeTransparencyFromMaterialsCorrectionStep,
];

0 comments on commit ea1fbcd

Please sign in to comment.