Skip to content

Commit

Permalink
Merge pull request #1167 from ousttrue/fix10/mtoon_export
Browse files Browse the repository at this point in the history
Fix10/mtoon export/import
  • Loading branch information
ousttrue authored Aug 24, 2021
2 parents 8d4c9c2 + 584f9c6 commit 39d1dc8
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 146 deletions.
290 changes: 145 additions & 145 deletions Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,163 +14,155 @@ public static class Vrm10MToonMaterialExporter

public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst)
{
try
if (src.shader.name != MToon10Meta.UnityShaderName)
{
if (src.shader.name != MToon10Meta.UnityShaderName)
{
dst = null;
return false;
}

// Get MToon10 Context
var context = new MToon10Context(src);
context.Validate();

// base material
dst = glTF_KHR_materials_unlit.CreateDefault();
dst.name = src.name;

// vrmc_materials_mtoon ext
var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon();
mtoon.SpecVersion = MTOON_SPEC_VERSION;

// Rendering
dst.alphaMode = ExportAlphaMode(context.AlphaMode);
mtoon.TransparentWithZWrite = context.TransparentWithZWriteMode == MToon10TransparentWithZWriteMode.On;
dst.alphaCutoff = Mathf.Max(0, context.AlphaCutoff);
mtoon.RenderQueueOffsetNumber = context.RenderQueueOffsetNumber;
dst.doubleSided = context.DoubleSidedMode == MToon10DoubleSidedMode.On;

// Lighting
dst.pbrMetallicRoughness = new glTFPbrMetallicRoughness();
dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
var baseColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.BaseColorTexture, context.AlphaMode != MToon10AlphaMode.Opaque);
if (baseColorTextureIndex != -1)
{
dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
index = baseColorTextureIndex,
};
}
mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
var shadeColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.ShadeColorTexture, needsAlpha: false);
if (shadeColorTextureIndex != -1)
dst = null;
return false;
}

// Get MToon10 Context
var context = new MToon10Context(src);
context.Validate();

// base material
dst = glTF_KHR_materials_unlit.CreateDefault();
dst.name = src.name;

// vrmc_materials_mtoon ext
var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon();
mtoon.SpecVersion = MTOON_SPEC_VERSION;

// Rendering
dst.alphaMode = ExportAlphaMode(context.AlphaMode);
mtoon.TransparentWithZWrite = context.TransparentWithZWriteMode == MToon10TransparentWithZWriteMode.On;
dst.alphaCutoff = Mathf.Max(0, context.AlphaCutoff);
mtoon.RenderQueueOffsetNumber = context.RenderQueueOffsetNumber;
dst.doubleSided = context.DoubleSidedMode == MToon10DoubleSidedMode.On;

// Lighting
dst.pbrMetallicRoughness = new glTFPbrMetallicRoughness();
dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
var baseColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.BaseColorTexture, context.AlphaMode != MToon10AlphaMode.Opaque);
if (baseColorTextureIndex != -1)
{
dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
mtoon.ShadeMultiplyTexture = new TextureInfo
{
Index = shadeColorTextureIndex,
};
}
var normalTextureIndex = textureExporter.RegisterExportingAsNormal(context.NormalTexture);
if (normalTextureIndex != -1)
index = baseColorTextureIndex,
};
}
mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
var shadeColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.ShadeColorTexture, needsAlpha: false);
if (shadeColorTextureIndex != -1)
{
mtoon.ShadeMultiplyTexture = new TextureInfo
{
dst.normalTexture = new glTFMaterialNormalTextureInfo
{
index = normalTextureIndex,
scale = context.NormalTextureScale,
};
}
mtoon.ShadingShiftFactor = context.ShadingShiftFactor;
var shadingShiftTextureIndex = textureExporter.RegisterExportingAsLinear(context.ShadingShiftTexture, needsAlpha: false);
if (shadingShiftTextureIndex != -1)
Index = shadeColorTextureIndex,
};
}
var normalTextureIndex = textureExporter.RegisterExportingAsNormal(context.NormalTexture);
if (normalTextureIndex != -1)
{
dst.normalTexture = new glTFMaterialNormalTextureInfo
{
mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo
{
Index = shadingShiftTextureIndex,
Scale = context.ShadingShiftTextureScale,
};
}
mtoon.ShadingToonyFactor = context.ShadingToonyFactor;

// GI
mtoon.GiEqualizationFactor = context.GiEqualizationFactor;

// Emission
dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear);
var emissiveTextureIndex = textureExporter.RegisterExportingAsSRgb(context.EmissiveTexture, needsAlpha: false);
if (emissiveTextureIndex != -1)
index = normalTextureIndex,
scale = context.NormalTextureScale,
};
}
mtoon.ShadingShiftFactor = context.ShadingShiftFactor;
var shadingShiftTextureIndex = textureExporter.RegisterExportingAsLinear(context.ShadingShiftTexture, needsAlpha: false);
if (shadingShiftTextureIndex != -1)
{
mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo
{
dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo
{
index = emissiveTextureIndex,
};
}

// Rim Lighting
var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false);
if (matcapTextureIndex != -1)
Index = shadingShiftTextureIndex,
Scale = context.ShadingShiftTextureScale,
};
}
mtoon.ShadingToonyFactor = context.ShadingToonyFactor;

// GI
mtoon.GiEqualizationFactor = context.GiEqualizationFactor;

// Emission
dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear);
var emissiveTextureIndex = textureExporter.RegisterExportingAsSRgb(context.EmissiveTexture, needsAlpha: false);
if (emissiveTextureIndex != -1)
{
dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo
{
mtoon.MatcapTexture = new TextureInfo
{
Index = matcapTextureIndex,
};
}
mtoon.ParametricRimColorFactor = context.ParametricRimColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
mtoon.ParametricRimFresnelPowerFactor = context.ParametricRimFresnelPowerFactor;
mtoon.ParametricRimLiftFactor = context.ParametricRimLiftFactor;
var rimMultiplyTextureIndex = textureExporter.RegisterExportingAsSRgb(context.RimMultiplyTexture, needsAlpha: false);
if (rimMultiplyTextureIndex != -1)
index = emissiveTextureIndex,
};
}

// Rim Lighting
var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false);
if (matcapTextureIndex != -1)
{
mtoon.MatcapTexture = new TextureInfo
{
mtoon.RimMultiplyTexture = new TextureInfo
{
Index = rimMultiplyTextureIndex,
};
}
mtoon.RimLightingMixFactor = context.RimLightingMixFactor;

// Outline
mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode);
mtoon.OutlineWidthFactor = context.OutlineWidthFactor;
var outlineWidthMultiplyTextureIndex = textureExporter.RegisterExportingAsLinear(context.OutlineWidthMultiplyTexture, needsAlpha: false);
if (outlineWidthMultiplyTextureIndex != -1)
Index = matcapTextureIndex,
};
}
mtoon.ParametricRimColorFactor = context.ParametricRimColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
mtoon.ParametricRimFresnelPowerFactor = context.ParametricRimFresnelPowerFactor;
mtoon.ParametricRimLiftFactor = context.ParametricRimLiftFactor;
var rimMultiplyTextureIndex = textureExporter.RegisterExportingAsSRgb(context.RimMultiplyTexture, needsAlpha: false);
if (rimMultiplyTextureIndex != -1)
{
mtoon.RimMultiplyTexture = new TextureInfo
{
mtoon.OutlineWidthMultiplyTexture = new TextureInfo
{
Index = outlineWidthMultiplyTextureIndex,
};
}
mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor;

// UV Animation
var uvAnimationMaskTextureIndex = textureExporter.RegisterExportingAsLinear(context.UvAnimationMaskTexture, needsAlpha: false);
if (uvAnimationMaskTextureIndex != -1)
Index = rimMultiplyTextureIndex,
};
}
mtoon.RimLightingMixFactor = context.RimLightingMixFactor;

// Outline
mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode);
mtoon.OutlineWidthFactor = context.OutlineWidthFactor;
var outlineWidthMultiplyTextureIndex = textureExporter.RegisterExportingAsLinear(context.OutlineWidthMultiplyTexture, needsAlpha: false);
if (outlineWidthMultiplyTextureIndex != -1)
{
mtoon.OutlineWidthMultiplyTexture = new TextureInfo
{
mtoon.UvAnimationMaskTexture = new TextureInfo
{
Index = uvAnimationMaskTextureIndex,
};
}
mtoon.UvAnimationScrollXSpeedFactor = context.UvAnimationScrollXSpeedFactor;
Index = outlineWidthMultiplyTextureIndex,
};
}
mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear);
mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor;

// UV Animation
var uvAnimationMaskTextureIndex = textureExporter.RegisterExportingAsLinear(context.UvAnimationMaskTexture, needsAlpha: false);
if (uvAnimationMaskTextureIndex != -1)
{
mtoon.UvAnimationMaskTexture = new TextureInfo
{
// Coordinate Conversion
const float invertY = -1f;
mtoon.UvAnimationScrollYSpeedFactor = context.UvAnimationScrollYSpeedFactor * invertY;
}
mtoon.UvAnimationRotationSpeedFactor = context.UvAnimationRotationSpeedFactor;

// Texture Transforms
var scale = context.TextureScale;
var offset = context.TextureOffset;
ExportTextureTransform(dst.pbrMetallicRoughness.baseColorTexture, scale, offset);
ExportTextureTransform(dst.emissiveTexture, scale, offset);
ExportTextureTransform(dst.normalTexture, scale, offset);
ExportTextureTransform(mtoon.ShadeMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.ShadingShiftTexture, scale, offset);
ExportTextureTransform(mtoon.MatcapTexture, scale, offset);
ExportTextureTransform(mtoon.RimMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.OutlineWidthMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset);

UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon);

return true;
}
catch (Exception)
Index = uvAnimationMaskTextureIndex,
};
}
mtoon.UvAnimationScrollXSpeedFactor = context.UvAnimationScrollXSpeedFactor;
{
dst = null;
return false;
// Coordinate Conversion
const float invertY = -1f;
mtoon.UvAnimationScrollYSpeedFactor = context.UvAnimationScrollYSpeedFactor * invertY;
}
mtoon.UvAnimationRotationSpeedFactor = context.UvAnimationRotationSpeedFactor;

// Texture Transforms
var scale = context.TextureScale;
var offset = context.TextureOffset;
ExportTextureTransform(dst.pbrMetallicRoughness.baseColorTexture, scale, offset);
ExportTextureTransform(dst.emissiveTexture, scale, offset);
ExportTextureTransform(dst.normalTexture, scale, offset);
ExportTextureTransform(mtoon.ShadeMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.ShadingShiftTexture, scale, offset);
ExportTextureTransform(mtoon.MatcapTexture, scale, offset);
ExportTextureTransform(mtoon.RimMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.OutlineWidthMultiplyTexture, scale, offset);
ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset);

UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon);

return true;
}

private static string ExportAlphaMode(MToon10AlphaMode alphaMode)
Expand Down Expand Up @@ -217,6 +209,10 @@ public static void ExportTextureTransform(glTFTextureInfo textureInfo, Vector2 u

public static void ExportTextureTransform(TextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset)
{
if (textureInfo == null)
{
return;
}
// Generate extension to empty holder.
var gltfTextureInfo = new EmptyGltfTextureInfo();
ExportTextureTransform(gltfTextureInfo, unityScale, unityOffset);
Expand All @@ -227,6 +223,10 @@ public static void ExportTextureTransform(TextureInfo textureInfo, Vector2 unity

public static void ExportTextureTransform(ShadingShiftTextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset)
{
if (textureInfo == null)
{
return;
}
// Generate extension to empty holder.
var gltfTextureInfo = new EmptyGltfTextureInfo();
ExportTextureTransform(gltfTextureInfo, unityScale, unityOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static bool TryGetShadingShiftTexture(GltfData data, VRMC_materials_mtoo

private static bool TryGetMatcapTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
{
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.ShadingShiftTexture), out pair);
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.MatcapTexture), out pair);
}

private static bool TryGetRimMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
Expand Down

0 comments on commit 39d1dc8

Please sign in to comment.