-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add steps to correct materials from some FBX tools (#23)
- Loading branch information
1 parent
4255993
commit ea1fbcd
Showing
5 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tools/gltf-avatar-exporter/src/scene/correction-steps/removeTransparencyFromMaterials.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})), | ||
}; | ||
}, | ||
}; |
44 changes: 44 additions & 0 deletions
44
tools/gltf-avatar-exporter/src/scene/correction-steps/removeVertexColors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters