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

[1.0]MToon export の実装 #896

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/IO/MeshAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static void ExportMesh(this Mesh mesh, List<object> materials, Vrm10Storage stor
var prim = new glTFPrimitives
{
mode = (int)mesh.Topology,
material = materials.IndexOf(y.Material),
material = y.Material,
indices = indicesAccessorIndex,
attributes = new glTFAttributes(),
};
Expand Down
12 changes: 6 additions & 6 deletions Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ public void Export(GameObject root, Model model, RuntimeVrmConverter converter,
Reserve(reserveBytes);
}

// mesh
ExportMeshes(model.MeshGroups, model.Materials, option);

// node
ExportNodes(model.Root, model.Nodes, model.MeshGroups, option);

// material
var materialExporter = new Vrm10MaterialExporter();
foreach (Material material in model.Materials)
Expand All @@ -176,6 +170,12 @@ public void Export(GameObject root, Model model, RuntimeVrmConverter converter,
Storage.Gltf.materials.Add(glTFMaterial);
}

// mesh
ExportMeshes(model.MeshGroups, model.Materials, option);

// node
ExportNodes(model.Root, model.Nodes, model.MeshGroups, option);

var (vrm, vrmSpringBone, thumbnailTextureIndex) = ExportVrm(root, model, converter, metaObject);

// Extension で Texture が増える場合があるので最後に呼ぶ
Expand Down
205 changes: 20 additions & 185 deletions Assets/VRM10/Runtime/IO/Vrm10MaterialExporter.cs
Original file line number Diff line number Diff line change
@@ -1,124 +1,39 @@
using System;
using UniGLTF;
using UniGLTF;
using UnityEngine;
using VRMShaders;

namespace UniVRM10
{
public class Vrm10MaterialExporter : MaterialExporter
{
protected override glTFMaterial CreateMaterial(Material m)
{
switch (m.shader.name)
{
case "VRM/UnlitTexture":
return Export_VRMUnlitTexture(m);

case "VRM/UnlitTransparent":
return Export_VRMUnlitTransparent(m);

case "VRM/UnlitCutout":
return Export_VRMUnlitCutout(m);

case "VRM/UnlitTransparentZWrite":
return Export_VRMUnlitTransparentZWrite(m);

case "VRM/MToon":
return Export_VRMMToon(m);

default:
return base.CreateMaterial(m);
}
}

static glTFMaterial Export_VRMUnlitTexture(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "OPAQUE";
return material;
}
static glTFMaterial Export_VRMUnlitTransparent(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "BLEND";
return material;
}
static glTFMaterial Export_VRMUnlitCutout(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "MASK";
return material;
}
static glTFMaterial Export_VRMUnlitTransparentZWrite(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "BLEND";
return material;
}

static glTFMaterial Export_VRMMToon(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();

switch (m.GetTag("RenderType", true))
{
case "Transparent":
material.alphaMode = "BLEND";
break;

case "TransparentCutout":
material.alphaMode = "MASK";
material.alphaCutoff = m.GetFloat("_Cutoff");
break;

default:
material.alphaMode = "OPAQUE";
break;
}

switch ((int)m.GetFloat("_CullMode"))
{
case 0:
material.doubleSided = true;
break;

case 1:
Debug.LogWarning("ignore cull front");
break;

case 2:
// cull back
break;

default:
throw new NotImplementedException();
}

return material;
}

#region CreateFromMaterial

static readonly string[] TAGS = new string[]{
"RenderType",
// "Queue",
};

public override glTFMaterial ExportMaterial(Material m, TextureExporter textureExporter)
{
if (m.shader.name != MToon.Utils.ShaderName)
{
return base.ExportMaterial(m, textureExporter);
}

// convert MToon intermediate value from UnityEngine.Material
var def = MToon.Utils.GetMToonParametersFromMaterial(m);

// gltfMaterial
var material = new glTFMaterial
{
name = m.name,

emissiveFactor = new float[] { 0, 0, 0 },
pbrMetallicRoughness = new glTFPbrMetallicRoughness
{
baseColorFactor = def.Color.LitColor.ToArray(),
baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
index = textureExporter.ExportSRGB(def.Color.LitMultiplyTexture),
},
},

emissiveFactor = def.Emission.EmissionColor.ToArray(),
};

// default values
// VRMC_materials_mtoon
var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon
{
Version = "",
Expand All @@ -129,7 +44,10 @@ public override glTFMaterial ExportMaterial(Material m, TextureExporter textureE

ShadeColorFactor = new float[] { 0, 0, 0 },

// ShadeMultiplyTexture;
ShadeMultiplyTexture = new UniGLTF.Extensions.VRMC_materials_mtoon.TextureInfo
{
Index = textureExporter.ExportSRGB(def.Color.ShadeMultiplyTexture),
},

// Lighting
ShadingShiftFactor = 0,
Expand Down Expand Up @@ -172,92 +90,9 @@ public override glTFMaterial ExportMaterial(Material m, TextureExporter textureE
UvAnimationRotationSpeedFactor = 0,
};

// var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
// if (prop == null)
// {
// Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
// }
// else
// {
// foreach (var keyword in m.shaderKeywords)
// {
// material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
// }

// // get properties
// //material.SetProp(prop);
// foreach (var kv in prop.Properties)
// {
// switch (kv.ShaderPropertyType)
// {
// case ShaderPropertyType.Color:
// {
// var value = m.GetColor(kv.Key).ToArray();
// material.vectorProperties.Add(kv.Key, value);
// }
// break;

// case ShaderPropertyType.Range:
// case ShaderPropertyType.Float:
// {
// var value = m.GetFloat(kv.Key);
// material.floatProperties.Add(kv.Key, value);
// }
// break;

// case ShaderPropertyType.TexEnv:
// {
// var texture = m.GetTexture(kv.Key);
// if (texture != null)
// {
// var value = kv.Key == "_BumpMap"
// ? textureExporter.ExportNormal(texture)
// : textureExporter.ExportSRGB(texture)
// ;
// if (value == -1)
// {
// Debug.LogFormat("not found {0}", texture.name);
// }
// else
// {
// material.textureProperties.Add(kv.Key, value);
// }
// }

// // offset & scaling
// var offset = m.GetTextureOffset(kv.Key);
// var scaling = m.GetTextureScale(kv.Key);
// material.vectorProperties.Add(kv.Key,
// new float[] { offset.x, offset.y, scaling.x, scaling.y });
// }
// break;

// case ShaderPropertyType.Vector:
// {
// var value = m.GetVector(kv.Key).ToArray();
// material.vectorProperties.Add(kv.Key, value);
// }
// break;

// default:
// throw new NotImplementedException();
// }
// }
// }

// foreach (var tag in TAGS)
// {
// var value = m.GetTag(tag, false);
// if (!String.IsNullOrEmpty(value))
// {
// material.tagMap.Add(tag, value);
// }
// }

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

return material;
}
#endregion
}
}
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static Color ToColor4(this float[] src, Color defaultValue = default)
}
public static Color ToColor3(this float[] src, Color defaultValue = default)
{
if (src == null || src.Length != 3)
if (src == null || src.Length < 3)
{
throw new NotImplementedException();
}
Expand Down