Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Optimizer 4 #64

Merged
merged 28 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cc2a3cc
set ui for different material size
memelotsqui Nov 10, 2023
9b40ace
add select export material option
memelotsqui Nov 10, 2023
b8543f4
update css for optimizer
memelotsqui Nov 10, 2023
b07e8e5
set text to right side in model information
memelotsqui Nov 10, 2023
00d7e0f
move clone mesh outside create texture-atlas
memelotsqui Nov 10, 2023
ce1b195
utils function to get sorted materials
memelotsqui Nov 11, 2023
953863f
update modelinformation component to use new function to sort materials
memelotsqui Nov 11, 2023
390612a
update name to be mToonAtlasSize
memelotsqui Nov 11, 2023
1eaf356
correctly detect std material or vrm material
memelotsqui Nov 11, 2023
829dccb
texture atlas per material type
memelotsqui Nov 13, 2023
38e3fde
modularize code and update texture atlas calls
memelotsqui Nov 13, 2023
01f4863
functions to separate materials
memelotsqui Nov 13, 2023
409e59c
remove logs
memelotsqui Nov 13, 2023
4b45dba
update downloadvrm call to code refactor
memelotsqui Nov 13, 2023
3501783
code refactor: save variables into object options
memelotsqui Nov 13, 2023
71c5643
update functions to work with code refactor
memelotsqui Nov 13, 2023
4676aee
update optimizer to reflect code refactor
memelotsqui Nov 13, 2023
f3a15a4
send atlas size in options parameter
memelotsqui Nov 13, 2023
458730b
add comment description to downloadVRM call
memelotsqui Nov 13, 2023
76523c6
src/library/VRMExporterv0.js
memelotsqui Nov 13, 2023
90dc903
save vrm material to material userdata
memelotsqui Nov 13, 2023
cd5ce05
add support for metallic roughness texture
memelotsqui Nov 13, 2023
1e4df6c
update texture atlas generation to include metallic roughness
memelotsqui Nov 13, 2023
550a3ff
remove logs
memelotsqui Nov 13, 2023
ae1c5ee
filter null images
memelotsqui Nov 13, 2023
4fc93b7
remove normal map from atlas for now
memelotsqui Nov 13, 2023
3cc624c
remove download button when no vrm is uploadded
memelotsqui Nov 13, 2023
80316e8
save selection to local storage
memelotsqui Nov 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/components/ExportMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const ExportMenu = ({getFaceScreenshot}) => {
className={styles.button}
onClick={() => {
const screenshot = getFaceScreenshot();

downloadVRMWithAvatar(model, avatar, name, screenshot, 4096,templateInfo.exportScale||1, true, templateInfo.vrmMeta,false)
const options = {screenshot:screenshot, atlasSize: 4096, scale:templateInfo.exportScale||1, isVrm0:true, vrmMeta:templateInfo.vrmMeta,createTextureAtlas:false}
downloadVRMWithAvatar(model, avatar, name, options)
}}
/>
<CustomButton
Expand All @@ -58,9 +58,8 @@ export const ExportMenu = ({getFaceScreenshot}) => {
className={styles.button}
onClick={() => {
const screenshot = getFaceScreenshot();
console.log(model);
console.log(avatar)
downloadVRMWithAvatar(model, avatar, name, screenshot, 4096,templateInfo.exportScale||1, true, templateInfo.vrmMeta,true)
const options = {screenshot:screenshot, atlasSize: 4096, scale:templateInfo.exportScale||1, isVrm0:true, vrmMeta:templateInfo.vrmMeta,createTextureAtlas:true}
downloadVRMWithAvatar(model, avatar, name, options)
}}
/>
</React.Fragment>
Expand Down
47 changes: 9 additions & 38 deletions src/components/ModelInformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react"
import styles from "./ModelInformation.module.css"
import MenuTitle from "./MenuTitle"
import { findChildrenByType } from "../library/utils";
import { getAsArray } from "../library/utils";
import { getAsArray, getMaterialsSortedByArray } from "../library/utils";

export default function ModelInformation({currentVRM}){
const [meshQty, setMeshQty] = useState(0);
Expand All @@ -24,46 +24,17 @@ export default function ModelInformation({currentVRM}){
setMeshQty(meshes.length)
setSkinnedMeshQty(skinnedMesh.length)


const allMeshes = meshes.concat(skinnedMesh);
let stdMaterialCount = 0;
let stdTranspMaterialCount = 0;
let stdCutoutMaterialCount = 0;

let shaderMaterialCount = 0;
let shaderTranspMaterialCount = 0;
let shaderTCutoutMaterialCount = 0;
allMeshes.forEach(mesh => {
const mats = getAsArray(mesh.material);
mats.forEach(mat => {
if (mat.type == "ShaderMaterial"){
if (mat.transparent == true)
shaderTranspMaterialCount++
else if (mat.uniforms.alphaTest.value != 0)
shaderTCutoutMaterialCount++
else
shaderMaterialCount++;
}
else{
if (mat.transparent == true)
stdTranspMaterialCount++
else if (mat.alphaTest != 0)
stdCutoutMaterialCount++
else
stdMaterialCount++;

}
// if (mat.type == "MeshStandardMaterial")
// stdMaterialCount++;
});
});
setStandardMaterialQty(stdMaterialCount);
setStandardTranspMaterialQty(stdTranspMaterialCount);
setStandardCutoutMaterialQty(stdCutoutMaterialCount);
const {stdMats,stdCutoutpMats,stdTranspMats,mToonMats,mToonCutoutMats,mToonTranspMats} = getMaterialsSortedByArray(allMeshes);

setStandardMaterialQty(stdMats.length);
setStandardTranspMaterialQty(stdTranspMats.length);
setStandardCutoutMaterialQty(stdCutoutpMats.length);

setVrmMaterialQty(shaderMaterialCount);
setVrmTranspMaterialQty(shaderTranspMaterialCount);
setVrmCutoutMaterialQty(shaderTCutoutMaterialCount);
setVrmMaterialQty(mToonMats.length);
setVrmTranspMaterialQty(mToonTranspMats.length);
setVrmCutoutMaterialQty(mToonCutoutMats.length);
}
}, [currentVRM])

Expand Down
5 changes: 5 additions & 0 deletions src/components/ModelInformation.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
background: rgba(5, 11, 14, 0.8);
z-index: 1000;
user-select: none;
text-align: right;
}

.scrollContainer {
Expand All @@ -21,6 +22,7 @@
margin: 30px;
height: -webkit-calc(100% - 40px);
height: calc(100% - 40px);

}

.traitInfoTitle {
Expand All @@ -30,6 +32,8 @@
font-size: 16px;
word-spacing: 2px;
margin-bottom: 10px;
text-align: right;

}
.traitInfoText {
color: rgb(179, 179, 179);
Expand All @@ -38,6 +42,7 @@
font-size: 16px;
word-spacing: 2px;
margin-bottom: 30px;
text-align: right;
}
.input-box {
width: 60px; /* Adjust as needed */
Expand Down
158 changes: 111 additions & 47 deletions src/library/VRMExporterv0.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,30 @@ export default class VRMExporterv0 {
if (!material.map)
throw new Error(material.name + " map is null");
return { name: material.name, imageBitmap: material.map.image };
}); // TODO: 画像がないMaterialもある
});
const shadeImages = uniqueMaterials
.filter((material) => material.userData.shadeTexture)
.map((material) => {
if (!material.userData.shadeTexture)
throw new Error(material.userData.shadeTexture + " map is null");
return { name: material.name + "_shade", imageBitmap: material.userData.shadeTexture.image };
}); // TODO: 画像がないMaterialもある\

const images = mainImages.concat(shadeImages);
});
const ormImages = uniqueMaterials
.filter((material) => material.roughnessMap)
.map((material) => {
if (!material.roughnessMap)
return null;
return { name: material.name + "_orm", imageBitmap: material.roughnessMap.image };
});

const normalImages = uniqueMaterials
.filter((material) => material.roughnessMap)
.map((material) => {
if (!material.normalMap)
return null
return { name: material.name + "_normal", imageBitmap: material.normalMap.image };
});
const images = [...mainImages, ...shadeImages, ...ormImages,...normalImages].filter(element => element !== null);
const outputImages = toOutputImages(images, icon);
const outputSamplers = toOutputSamplers(outputImages);
const outputTextures = toOutputTextures(outputImages);
Expand Down Expand Up @@ -395,7 +408,7 @@ export default class VRMExporterv0 {
// upperLegTwist: humanoid.humanDescription.upperLegTwist,
// };

const materialProperties = [{
const vrmMaterialProperties = {
floatProperties : {
// _BlendMode : 0,
// _BumpScale : 1,
Expand Down Expand Up @@ -430,7 +443,7 @@ export default class VRMExporterv0 {
MTOON_OUTLINE_COLOR_FIXED : true,
MTOON_OUTLINE_WIDTH_WORLD : true
},
name : "CombinedMat",
name : "VRMCombinedMat",
renderQueue : 2000,
shader : "VRM/MToon",
tagMap : {
Expand All @@ -456,8 +469,26 @@ export default class VRMExporterv0 {
// _SphereAdd : [0, 0, 1, 1],
// _UvAnimMaskTexture : [0, 0, 1, 1]
}
}]
}

const stdMaterialProperties ={
name : "STDCombinedMat",
shader : "VRM_USE_GLTFSHADER",
}

const materialProperties = []
uniqueMaterials.forEach(mat => {
if (mat.type == "ShaderMaterial"){
materialProperties.push(
materialProperties.push(Object.assign({}, vrmMaterialProperties))
)
}
else{
materialProperties.push(
materialProperties.push(Object.assign({}, stdMaterialProperties))
)
}
});
//const outputVrmMeta = ToOutputVRMMeta(vrmMeta, icon, outputImages);
const outputVrmMeta = vrmMeta;

Expand Down Expand Up @@ -624,6 +655,8 @@ export default class VRMExporterv0 {

const outputScenes = toOutputScenes(avatar, outputNodes);



const outputData = {
accessors: outputAccessors,
asset: exporterInfo,
Expand Down Expand Up @@ -668,7 +701,6 @@ export default class VRMExporterv0 {
skins: outputSkins,
textures: outputTextures,
};
console.log(outputData)
const jsonChunk = new GlbChunk(parseString2Binary(JSON.stringify(outputData, undefined, 2)), "JSON");
const binaryChunk = new GlbChunk(concatBinary(bufferViews.map((buf) => buf.buffer)), "BIN\x00");
const fileData = concatBinary([jsonChunk.buffer, binaryChunk.buffer]);
Expand Down Expand Up @@ -977,9 +1009,9 @@ const toOutputMaterials = (uniqueMaterials, images) => {

material = material.userData.vrmMaterial?material.userData.vrmMaterial:material;
if (material.type === "ShaderMaterial") {
VRMC_materials_mtoon = material.userData.gltfExtensions.VRMC_materials_mtoon;
//VRMC_materials_mtoon = material.userData.gltfExtensions.VRMC_materials_mtoon;
VRMC_materials_mtoon = {};
VRMC_materials_mtoon.shadeMultiplyTexture = {index:images.map((image) => image.name).indexOf(material.uniforms.shadeMultiplyTexture.name)};

const mtoonMaterial = material;
baseColor = mtoonMaterial.color ? [
1,
Expand Down Expand Up @@ -1007,6 +1039,14 @@ const toOutputMaterials = (uniqueMaterials, images) => {
}
}

let metalicRoughnessIndex = -1;
if (material.roughnessMap)
metalicRoughnessIndex = images.map((image) => image.name).indexOf(material.name + "_orm");

let normalTextureIndex = -1;
if (material.normalMap)
normalTextureIndex = images.map((image) => image.name).indexOf(material.name + "_normal");

const baseTexture = baseTxrIndex >= 0 ? {
extensions: {
KHR_texture_transform: {
Expand All @@ -1018,44 +1058,68 @@ const toOutputMaterials = (uniqueMaterials, images) => {
texCoord: 0, // TODO:
} :
undefined;
const metallicFactor = (() => {
switch (material.type) {
case MaterialType.MeshStandardMaterial:
return material.metalness;
case MaterialType.MeshBasicMaterial:
return 0;
default:
return 0;
}
})();
const roughnessFactor = (() => {
switch (material.type) {
case MaterialType.MeshStandardMaterial:
return material.roughness;
case MaterialType.MeshBasicMaterial:
return 0.9;
default:
return 0.9;

const pbrMetallicRoughness = {
baseColorFactor: baseColor,
baseColorTexture: baseTexture,
}
})();
return {
alphaCutoff: material.alphaTest > 0 ? material.alphaTest : undefined,
alphaMode: material.transparent ?
"BLEND" : material.alphaTest > 0 ?
"MASK" : "OPAQUE",
doubleSided: material.side === 2,
extensions: material.type === "ShaderMaterial" ? {
KHR_materials_unlit: {}, // TODO:
VRMC_materials_mtoon
} : undefined,
name: material.name,
pbrMetallicRoughness: {
baseColorFactor: baseColor,
baseColorTexture: baseTexture,
metallicFactor: metallicFactor,
roughnessFactor: roughnessFactor,
},
};

const metalRoughTexture = metalicRoughnessIndex >= 0 ?{
index: metalicRoughnessIndex,
texCoord: 0, // TODO:
}:undefined

const normalMapTexture = normalTextureIndex >= 0 ? {
index: normalTextureIndex,
texCoord: 0,
}:undefined;

if (metalRoughTexture){
pbrMetallicRoughness.metallicRoughnessTexture = metalRoughTexture;
}
else{
const metallicFactor = (() => {
switch (material.type) {
case MaterialType.MeshStandardMaterial:
return material.metalness;
case MaterialType.MeshBasicMaterial:
return 0;
default:
return 0;
}
})();
const roughnessFactor = (() => {
switch (material.type) {
case MaterialType.MeshStandardMaterial:
return material.roughness;
case MaterialType.MeshBasicMaterial:
return 0.9;
default:
return 0.9;
}
})();

pbrMetallicRoughness.metallicFactor = metallicFactor;
pbrMetallicRoughness.roughnessFactor = roughnessFactor;
}

const parseMaterial = {
alphaCutoff: material.alphaTest > 0 ? material.alphaTest : undefined,
alphaMode: material.transparent ?
"BLEND" : material.alphaTest > 0 ?
"MASK" : "OPAQUE",
doubleSided: material.side === 2,
extensions: material.type === "ShaderMaterial" ? {
KHR_materials_unlit: {}, // TODO:
VRMC_materials_mtoon
} : undefined,
name: material.name,
pbrMetallicRoughness
}
if (normalMapTexture){
parseMaterial.normalTexture = normalMapTexture;
}
return parseMaterial;
});
};
const toOutputImages = (images, icon) => {
Expand Down
Loading
Loading