Submesh.Material to int

This commit is contained in:
ousttrue
2021-03-30 17:41:28 +09:00
parent 0a11bd1568
commit ce4d393013
18 changed files with 219 additions and 500 deletions

View File

@@ -164,8 +164,7 @@ namespace UniVRM10
return true;
}
public static MeshGroup FromGltf(this glTFMesh x,
Vrm10Storage storage, List<Material> materials)
public static MeshGroup FromGltf(this glTFMesh x, Vrm10Storage storage)
{
var group = new MeshGroup(x.name);
@@ -176,7 +175,7 @@ namespace UniVRM10
var materialIndex = primitive.material;
mesh.Submeshes.Add(
new Submesh(0, mesh.IndexBuffer.Count, materials[materialIndex]));
new Submesh(0, mesh.IndexBuffer.Count, materialIndex));
group.Meshes.Add(mesh);
}
@@ -189,7 +188,7 @@ namespace UniVRM10
var materialIndex = primitive.material;
mesh.Submeshes.Add(
new Submesh(offset, mesh.IndexBuffer.Count, materials[materialIndex]));
new Submesh(offset, mesh.IndexBuffer.Count, materialIndex));
offset += mesh.IndexBuffer.Count;
group.Meshes.Add(mesh);
@@ -206,7 +205,7 @@ namespace UniVRM10
var materialIndex = primitive.material;
var count = storage.Gltf.accessors[primitive.indices].count;
mesh.Submeshes.Add(
new Submesh(offset, count, materials[materialIndex]));
new Submesh(offset, count, materialIndex));
offset += count;
}
@@ -258,7 +257,7 @@ namespace UniVRM10
}
}
static void ExportMesh(this Mesh mesh, List<Material> materials, Vrm10Storage storage, glTFMesh gltfMesh, ExportArgs option)
static void ExportMesh(this Mesh mesh, List<object> materials, Vrm10Storage storage, glTFMesh gltfMesh, ExportArgs option)
{
//
// primitive share vertex buffer
@@ -383,7 +382,7 @@ namespace UniVRM10
}
}
public static glTFMesh ExportMeshGroup(this MeshGroup src, List<Material> materials, Vrm10Storage storage, ExportArgs option)
public static glTFMesh ExportMeshGroup(this MeshGroup src, List<object> materials, Vrm10Storage storage, ExportArgs option)
{
var mesh = new glTFMesh
{

View File

@@ -49,7 +49,7 @@ namespace UniVRM10
model.Skins.AddRange(Enumerable.Range(0, storage.SkinCount).Select(x => storage.CreateSkin(x, model.Nodes)));
// mesh
model.MeshGroups.AddRange(Enumerable.Range(0, storage.MeshCount).Select(x => storage.CreateMesh(x, model.Materials)));
model.MeshGroups.AddRange(Enumerable.Range(0, storage.MeshCount).Select(x => storage.CreateMesh(x)));
// skin
for (int i = 0; i < storage.NodeCount; ++i)

View File

@@ -58,7 +58,7 @@ namespace UniVRM10
Storage.Reserve(bytesLength);
}
public void ExportMeshes(List<MeshGroup> groups, List<Material> materials, ExportArgs option)
public void ExportMeshes(List<MeshGroup> groups, List<object> materials, ExportArgs option)
{
foreach (var group in groups)
{

View File

@@ -15,11 +15,6 @@ namespace UniVRM10
///
{
var reserveBytes = 0;
// image
foreach (var image in m.Images)
{
reserveBytes += image.Bytes.Count;
}
// mesh
foreach (var g in m.MeshGroups)
{

View File

@@ -439,10 +439,10 @@ namespace UniVRM10
return skin;
}
public MeshGroup CreateMesh(int index, List<Material> materials)
public MeshGroup CreateMesh(int index)
{
var x = Gltf.meshes[index];
var group = x.FromGltf(this, materials);
var group = x.FromGltf(this);
return group;
}

View File

@@ -49,57 +49,6 @@ namespace UniVRM10
// VRM-1.0 では値域は [0-1.0f]
return new UniVRM10.MorphTargetBinding(relativePath, names.IndexOf(bind.Name), bind.Value * 100.0f);
}
static UniVRM10.MaterialColorBinding? Build10(this VrmLib.MaterialColorBind bind, ModelMap loader)
{
var kv = bind.Property;
var value = kv.Value.ToUnityVector4();
var material = loader.Materials[bind.Material];
var binding = default(UniVRM10.MaterialColorBinding?);
if (material != null)
{
try
{
binding = new UniVRM10.MaterialColorBinding
{
MaterialName = bind.Material.Name, // UniVRM-0Xの実装は名前で持っている
BindType = bind.BindType,
TargetValue = value,
// BaseValue = material.GetColor(kv.Key),
};
}
catch (Exception)
{
// do nothing
}
}
return binding;
}
static UniVRM10.MaterialUVBinding? Build10(this VrmLib.TextureTransformBind bind, ModelMap loader)
{
var material = loader.Materials[bind.Material];
var binding = default(UniVRM10.MaterialUVBinding?);
if (material != null)
{
try
{
binding = new UniVRM10.MaterialUVBinding
{
MaterialName = bind.Material.Name, // UniVRM-0Xの実装は名前で持っている
Scaling = new Vector2(bind.Scale.X, bind.Scale.Y),
Offset = new Vector2(bind.Offset.X, bind.Offset.Y),
};
}
catch (Exception)
{
// do nothing
}
}
return binding;
}
#endregion
}
}

View File

@@ -6,8 +6,6 @@ namespace UniVRM10
public class ModelMap
{
public readonly Dictionary<VrmLib.Node, GameObject> Nodes = new Dictionary<VrmLib.Node, GameObject>();
public readonly Dictionary<VrmLib.Texture, Texture2D> Textures = new Dictionary<VrmLib.Texture, Texture2D>();
public readonly Dictionary<VrmLib.Material, Material> Materials = new Dictionary<VrmLib.Material, Material>();
public readonly Dictionary<VrmLib.MeshGroup, Mesh> Meshes = new Dictionary<VrmLib.MeshGroup, Mesh>();
public readonly Dictionary<VrmLib.Node, Renderer> Renderers = new Dictionary<VrmLib.Node, Renderer>();
}

View File

@@ -44,7 +44,6 @@ namespace UniVRM10
{
var src = m_model.Materials[i];
var dst = MaterialFactory.Materials[i].Asset;
m_asset.Map.Materials.Add(src, dst);
}
// mesh
@@ -62,7 +61,7 @@ namespace UniVRM10
Meshes.Add(new MeshWithMaterials
{
Mesh = mesh,
Materials = src.Meshes[0].Submeshes.Select(x => m_asset.Map.Materials[x.Material]).ToArray(),
Materials = src.Meshes[0].Submeshes.Select(x => MaterialFactory.Materials[x.Material].Asset).ToArray(),
});
}
else
@@ -105,7 +104,7 @@ namespace UniVRM10
throw new NotImplementedException("invalid isolated vertexbuffer");
}
var renderer = CreateRenderer(node, go, map);
var renderer = CreateRenderer(node, go, map, MaterialFactory.Materials);
map.Renderers.Add(node, renderer);
m_asset.Renderers.Add(renderer);
}
@@ -163,7 +162,8 @@ namespace UniVRM10
/// <summary>
/// MeshFilter + MeshRenderer もしくは SkinnedMeshRenderer を構築する
/// </summary>
public static Renderer CreateRenderer(VrmLib.Node node, GameObject go, ModelMap map)
public static Renderer CreateRenderer(VrmLib.Node node, GameObject go, ModelMap map,
IReadOnlyList<VRMShaders.MaterialFactory.MaterialLoadInfo> materialLoadInfos)
{
var mesh = node.MeshGroup.Meshes[0];
@@ -189,7 +189,7 @@ namespace UniVRM10
renderer = go.AddComponent<MeshRenderer>();
meshFilter.sharedMesh = map.Meshes[node.MeshGroup];
}
var materials = mesh.Submeshes.Select(x => map.Materials[x.Material]).ToArray();
var materials = mesh.Submeshes.Select(x => materialLoadInfos[x.Material].Asset).ToArray();
renderer.sharedMaterials = materials;
return renderer;

View File

@@ -14,8 +14,7 @@ namespace UniVRM10
public VrmLib.Model Model;
public Dictionary<GameObject, VrmLib.Node> Nodes = new Dictionary<GameObject, VrmLib.Node>();
public Dictionary<Texture2D, VrmLib.TextureInfo> Textures = new Dictionary<Texture2D, VrmLib.TextureInfo>();
public Dictionary<UnityEngine.Material, VrmLib.Material> Materials = new Dictionary<UnityEngine.Material, VrmLib.Material>();
public List<UnityEngine.Material> Materials = new List<UnityEngine.Material>();
public Dictionary<UnityEngine.Mesh, VrmLib.MeshGroup> Meshes = new Dictionary<UnityEngine.Mesh, VrmLib.MeshGroup>();
static string GetSupportedMime(string path)
@@ -53,136 +52,7 @@ namespace UniVRM10
return (copy.EncodeToPNG(), "image/png");
}
public VrmLib.TextureInfo GetOrCreateTexture(UnityEngine.Material material, UnityEngine.Texture srcTexture, VrmLib.Texture.ColorSpaceTypes colorSpace, VrmLib.Texture.TextureTypes textureType)
{
var texture = srcTexture as Texture2D;
if (texture is null)
{
return null;
}
if (!Textures.TryGetValue(texture, out VrmLib.TextureInfo info))
{
UnityEngine.Material converter = null;
if (textureType == VrmLib.Texture.TextureTypes.NormalMap)
{
converter = TextureConvertMaterial.GetNormalMapConvertUnityToGltf();
}
else if (textureType == VrmLib.Texture.TextureTypes.MetallicRoughness)
{
float smoothness = 0.0f;
if (material.HasProperty("_GlossMapScale"))
{
smoothness = material.GetFloat("_GlossMapScale");
}
converter = TextureConvertMaterial.GetMetallicRoughnessUnityToGltf(smoothness);
}
else if (textureType == VrmLib.Texture.TextureTypes.Occlusion)
{
converter = TextureConvertMaterial.GetOcclusionUnityToGltf();
}
var (bytes, mime) = GetImageEncodedBytes(
texture,
(colorSpace == VrmLib.Texture.ColorSpaceTypes.Linear) ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB,
converter
);
if (converter != null)
{
UnityEngine.Object.DestroyImmediate(converter);
}
var sampler = new VrmLib.TextureSampler
{
MagFilter = texture.filterMode.ToVrmLibMagFilter(),
MinFilter = texture.filterMode.ToVrmLibMinFilter(),
WrapS = texture.wrapMode.ToVrmLib(),
WrapT = texture.wrapMode.ToVrmLib(),
};
var image = new VrmLib.Image(texture.name, mime, VrmLib.ImageUsage.None, new ArraySegment<byte>(bytes));
info = new VrmLib.TextureInfo(new VrmLib.ImageTexture(texture.name, sampler, image, colorSpace, textureType));
Textures.Add(texture, info);
}
return info;
}
#region Export 1.0
/// <summary>
/// VRM-0.X の MaterialBindValue を VRM-1.0 仕様に変換する
///
/// * Property名 => enum MaterialBindType
/// * 特に _MainTex_ST の場合、MaterialBindType.UvScale + MaterialBindType.UvScale 2つになりうる
///
/// </summary>
VrmLib.Expression ToVrmLib(VRM10Expression clip, GameObject root)
{
var expression = new VrmLib.Expression(clip.Preset, clip.ExpressionName, clip.IsBinary);
expression.OverrideBlink = EnumUtil.Cast<VrmLib.ExpressionOverrideType>(clip.OverrideBlink);
expression.OverrideLookAt = EnumUtil.Cast<VrmLib.ExpressionOverrideType>(clip.OverrideLookAt);
expression.OverrideMouth = EnumUtil.Cast<VrmLib.ExpressionOverrideType>(clip.OverrideMouth);
foreach (var binding in clip.MorphTargetBindings)
{
var transform = GetTransformFromRelativePath(root.transform, binding.RelativePath);
if (transform == null)
continue;
var renderer = transform.gameObject.GetComponent<SkinnedMeshRenderer>();
if (renderer == null)
continue;
var mesh = renderer.sharedMesh;
if (mesh == null)
continue;
var names = new List<string>();
for (int i = 0; i < mesh.blendShapeCount; ++i)
{
names.Add(mesh.GetBlendShapeName(i));
}
var node = Nodes[transform.gameObject];
var blendShapeValue = new VrmLib.MorphTargetBind(
node,
names[binding.Index],
// Unity Range [0-100] to VRM-1.0 Range [0-1.0]
binding.Weight * 0.01f
);
expression.MorphTargetBinds.Add(blendShapeValue);
}
foreach (var binding in clip.MaterialColorBindings)
{
var materialPair = Materials.FirstOrDefault(x => x.Key.name == binding.MaterialName);
if (materialPair.Value != null)
{
var bind = new VrmLib.MaterialColorBind(
materialPair.Value,
binding.BindType,
binding.TargetValue.ToNumericsVector4()
);
expression.MaterialColorBinds.Add(bind);
}
}
foreach (var binding in clip.MaterialUVBindings)
{
var materialPair = Materials.FirstOrDefault(x => x.Key.name == binding.MaterialName);
if (materialPair.Value != null)
{
var bind = new VrmLib.TextureTransformBind(
materialPair.Value,
binding.Scaling.ToNumericsVector2(),
binding.Offset.ToNumericsVector2()
);
expression.TextureTransformBinds.Add(bind);
}
}
return expression;
}
/// <summary>
/// metaObject が null のときは、root から取得する
/// </summary>
@@ -276,14 +146,13 @@ namespace UniVRM10
var materials = renderer.sharedMaterials; // avoid copy
foreach (var material in materials)
{
if (Materials.ContainsKey(material))
if (Materials.Contains(material))
{
continue;
}
var vrmMaterial = Export10(material, GetOrCreateTexture);
Model.Materials.Add(vrmMaterial);
Materials.Add(material, vrmMaterial);
Model.Materials.Add(material);
Materials.Add(material);
}
}
}
@@ -554,7 +423,7 @@ namespace UniVRM10
return null;
}
private static VrmLib.MeshGroup CreateMesh(UnityEngine.Mesh mesh, Renderer renderer, Dictionary<UnityEngine.Material, VrmLib.Material> materials)
private static VrmLib.MeshGroup CreateMesh(UnityEngine.Mesh mesh, Renderer renderer, List<UnityEngine.Material> materials)
{
var meshGroup = new VrmLib.MeshGroup(mesh.name);
var vrmMesh = new VrmLib.Mesh();
@@ -586,7 +455,7 @@ namespace UniVRM10
var subMesh = mesh.GetSubMesh(i);
try
{
vrmMesh.Submeshes.Add(new VrmLib.Submesh(offset, subMesh.indexCount, materials[renderer.sharedMaterials[i]]));
vrmMesh.Submeshes.Add(new VrmLib.Submesh(offset, subMesh.indexCount, materials.IndexOf(renderer.sharedMaterials[i])));
}
catch (Exception ex)
{
@@ -597,7 +466,7 @@ namespace UniVRM10
var triangles = mesh.GetTriangles(i);
try
{
vrmMesh.Submeshes.Add(new VrmLib.Submesh(offset, triangles.Length, materials[renderer.sharedMaterials[i]]));
vrmMesh.Submeshes.Add(new VrmLib.Submesh(offset, triangles.Length, materials.IndexOf(renderer.sharedMaterials[i])));
}
catch (Exception ex)
{

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
@@ -29,7 +30,7 @@ namespace UniVRM10.Test
VrmLib.MToon.Utils.PropUvAnimMaskTexture
};
private ModelAsset ToUnity(string path)
private (ModelAsset, IReadOnlyList<VRMShaders.MaterialFactory.MaterialLoadInfo>) ToUnity(string path)
{
var fi = new FileInfo(_vrmPath);
var bytes = File.ReadAllBytes(fi.FullName);
@@ -40,7 +41,7 @@ namespace UniVRM10.Test
return ToUnity(bytes);
}
private ModelAsset ToUnity(byte[] bytes)
private (ModelAsset, IReadOnlyList<VRMShaders.MaterialFactory.MaterialLoadInfo>) ToUnity(byte[] bytes)
{
// Vrm => Model
var parser = new UniGLTF.GltfParser();
@@ -49,14 +50,14 @@ namespace UniVRM10.Test
return ToUnity(parser);
}
private ModelAsset ToUnity(GltfParser parser)
private (ModelAsset, IReadOnlyList<VRMShaders.MaterialFactory.MaterialLoadInfo>) ToUnity(GltfParser parser)
{
// Model => Unity
using (var loader = new RuntimeUnityBuilder(parser))
{
loader.Load();
loader.DisposeOnGameObjectDestroyed();
return loader.Asset;
return (loader.Asset, loader.MaterialFactory.Materials);
}
}
@@ -90,232 +91,231 @@ namespace UniVRM10.Test
[UnityTest]
public IEnumerator ColorSpace_UnityBaseColorToLiner()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var (assets, materials) = ToUnity(_vrmPath);
var srcMaterial = materials.First();
var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
var srcGammaColor = srcColor;
var srclinerColor = srcColor.linear;
srcMaterial.Value.color = srcColor;
srcMaterial.Asset.color = srcColor;
var model = ToVrmModel(assets.Root);
var dstMaterial = model.Materials.First(x => x.Name == key.Name);
var dstMaterial = model.Materials.First(x => x is UnityEngine.Material m && m.name == srcMaterial.Asset.name) as UnityEngine.Material;
EqualColor(srclinerColor, dstMaterial.BaseColorFactor.RGBA.ToUnityColor());
EqualColor(srclinerColor, dstMaterial.color);
yield return null;
}
[UnityTest]
public IEnumerator ColorSpace_GltfBaseColorToGamma()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
var srclinerColor = srcColor.ToVector4();
var srcGammaColor = srcColor.gamma.ToVector4();
// [UnityTest]
// public IEnumerator ColorSpace_GltfBaseColorToGamma()
// {
// var assets = ToUnity(_vrmPath);
// var srcMaterial = assets.Map.Materials.First();
// var key = srcMaterial.Key;
// var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
// var srclinerColor = srcColor.ToVector4();
// var srcGammaColor = srcColor.gamma.ToVector4();
var model = ToVrmModel(assets.Root);
var gltfMaterial = model.Materials.First(x => x.Name == key.Name);
gltfMaterial.BaseColorFactor = new LinearColor
{
RGBA = srclinerColor
};
// var model = ToVrmModel(assets.Root);
// var gltfMaterial = model.Materials.First(x => x.Name == key.Name);
// gltfMaterial.BaseColorFactor = new LinearColor
// {
// RGBA = srclinerColor
// };
var bytes = model.ToGlb();
// var bytes = model.ToGlb();
var dstAssets = ToUnity(bytes);
var dstMaterial = dstAssets.Map.Materials.First(x => x.Value.name == key.Name);
// var dstAssets = ToUnity(bytes);
// var dstMaterial = dstAssets.Map.Materials.First(x => x.Value.name == key.Name);
EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.Value.color);
// EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.Value.color);
yield return null;
}
// yield return null;
// }
[UnityTest]
public IEnumerator MToonUnityColorToGltf()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
var srcGammaColor = srcColor;
var srclinerColor = srcColor.linear;
// [UnityTest]
// public IEnumerator MToonUnityColorToGltf()
// {
// var assets = ToUnity(_vrmPath);
// var srcMaterial = assets.Map.Materials.First();
// var key = srcMaterial.Key;
// var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
// var srcGammaColor = srcColor;
// var srclinerColor = srcColor.linear;
srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropColor, srcColor);
srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropShadeColor, srcColor);
srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropEmissionColor, srcColor);
srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropRimColor, srcColor);
srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropOutlineColor, srcColor);
// srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropColor, srcColor);
// srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropShadeColor, srcColor);
// srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropEmissionColor, srcColor);
// srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropRimColor, srcColor);
// srcMaterial.Value.SetColor(VrmLib.MToon.Utils.PropOutlineColor, srcColor);
var model = ToVrmModel(assets.Root);
var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
// var model = ToVrmModel(assets.Root);
// var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
// sRGB
EqualColor(srclinerColor, dstMaterial.Definition.Color.LitColor.RGBA.ToUnityColor());
EqualColor(srclinerColor, dstMaterial.Definition.Color.ShadeColor.RGBA.ToUnityColor());
EqualColor(srclinerColor, dstMaterial.Definition.Outline.OutlineColor.RGBA.ToUnityColor());
// HDR Color
EqualColor(srcColor, dstMaterial.Definition.Emission.EmissionColor.RGBA.ToUnityColor());
EqualColor(srcColor, dstMaterial.Definition.Rim.RimColor.RGBA.ToUnityColor());
// // sRGB
// EqualColor(srclinerColor, dstMaterial.Definition.Color.LitColor.RGBA.ToUnityColor());
// EqualColor(srclinerColor, dstMaterial.Definition.Color.ShadeColor.RGBA.ToUnityColor());
// EqualColor(srclinerColor, dstMaterial.Definition.Outline.OutlineColor.RGBA.ToUnityColor());
// // HDR Color
// EqualColor(srcColor, dstMaterial.Definition.Emission.EmissionColor.RGBA.ToUnityColor());
// EqualColor(srcColor, dstMaterial.Definition.Rim.RimColor.RGBA.ToUnityColor());
yield return null;
}
// yield return null;
// }
[UnityTest]
public IEnumerator MtoonGltfColorToUnity()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
var srclinerColor = srcColor.ToVector4();
var srcGammaColor = srcColor.gamma.ToVector4();
// [UnityTest]
// public IEnumerator MtoonGltfColorToUnity()
// {
// var assets = ToUnity(_vrmPath);
// var srcMaterial = assets.Map.Materials.First();
// var key = srcMaterial.Key;
// var srcColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
// var srclinerColor = srcColor.ToVector4();
// var srcGammaColor = srcColor.gamma.ToVector4();
var model = ToVrmModel(assets.Root);
var gltfMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
if (gltfMaterial == null)
{
throw new NotImplementedException();
}
// var model = ToVrmModel(assets.Root);
// var gltfMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
// if (gltfMaterial == null)
// {
// throw new NotImplementedException();
// }
gltfMaterial.Definition = new VrmLib.MToon.MToonDefinition
{
Color = new VrmLib.MToon.ColorDefinition
{
LitColor = new LinearColor { RGBA = srclinerColor },
ShadeColor = new LinearColor { RGBA = srclinerColor },
},
Outline = new VrmLib.MToon.OutlineDefinition
{
OutlineColor = new LinearColor { RGBA = srclinerColor },
},
Emission = new VrmLib.MToon.EmissionDefinition
{
EmissionColor = new LinearColor { RGBA = srclinerColor },
},
Rim = new VrmLib.MToon.RimDefinition
{
RimColor = new LinearColor { RGBA = srclinerColor },
}
};
// gltfMaterial.Definition = new VrmLib.MToon.MToonDefinition
// {
// Color = new VrmLib.MToon.ColorDefinition
// {
// LitColor = new LinearColor { RGBA = srclinerColor },
// ShadeColor = new LinearColor { RGBA = srclinerColor },
// },
// Outline = new VrmLib.MToon.OutlineDefinition
// {
// OutlineColor = new LinearColor { RGBA = srclinerColor },
// },
// Emission = new VrmLib.MToon.EmissionDefinition
// {
// EmissionColor = new LinearColor { RGBA = srclinerColor },
// },
// Rim = new VrmLib.MToon.RimDefinition
// {
// RimColor = new LinearColor { RGBA = srclinerColor },
// }
// };
var bytes = model.ToGlb();
// var bytes = model.ToGlb();
var dstAssets = ToUnity(bytes);
var dstMaterial = dstAssets.Map.Materials.First(x => x.Value.name == key.Name).Value;
// sRGB
EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropColor));
EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropShadeColor));
EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropOutlineColor));
// HDR Color
EqualColor(srcColor, dstMaterial.GetColor(VrmLib.MToon.Utils.PropEmissionColor));
EqualColor(srcColor, dstMaterial.GetColor(VrmLib.MToon.Utils.PropRimColor));
// var dstAssets = ToUnity(bytes);
// var dstMaterial = dstAssets.Map.Materials.First(x => x.Value.name == key.Name).Value;
// // sRGB
// EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropColor));
// EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropShadeColor));
// EqualColor(srcGammaColor.ToUnityColor(), dstMaterial.GetColor(VrmLib.MToon.Utils.PropOutlineColor));
// // HDR Color
// EqualColor(srcColor, dstMaterial.GetColor(VrmLib.MToon.Utils.PropEmissionColor));
// EqualColor(srcColor, dstMaterial.GetColor(VrmLib.MToon.Utils.PropRimColor));
yield return null;
}
#endregion
// yield return null;
// }
// #endregion
#region Texture
// #region Texture
Texture2D CreateMonoTexture(float mono, float alpha, bool isLinear)
{
Texture2D texture = new Texture2D(128, 128, TextureFormat.ARGB32, mipChain: false, linear: isLinear);
Color col = new Color(mono, mono, mono, alpha);
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
texture.SetPixel(x, y, col);
}
}
texture.Apply();
return texture;
}
// Texture2D CreateMonoTexture(float mono, float alpha, bool isLinear)
// {
// Texture2D texture = new Texture2D(128, 128, TextureFormat.ARGB32, mipChain: false, linear: isLinear);
// Color col = new Color(mono, mono, mono, alpha);
// for (int y = 0; y < texture.height; y++)
// {
// for (int x = 0; x < texture.width; x++)
// {
// texture.SetPixel(x, y, col);
// }
// }
// texture.Apply();
// return texture;
// }
void EqualTextureColor(Texture2D texture, VrmLib.ImageTexture imageTexture, bool isLinear)
{
var srcColor = texture.GetPixel(0, 0);
// void EqualTextureColor(Texture2D texture, VrmLib.ImageTexture imageTexture, bool isLinear)
// {
// var srcColor = texture.GetPixel(0, 0);
var dstTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: isLinear);
dstTexture.LoadImage(imageTexture.Image.Bytes.ToArray());
var dstColor = dstTexture.GetPixel(0, 0);
// var dstTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: isLinear);
// dstTexture.LoadImage(imageTexture.Image.Bytes.ToArray());
// var dstColor = dstTexture.GetPixel(0, 0);
Debug.LogFormat("src:{0}, dst{1}", srcColor, dstColor);
EqualColor(srcColor, dstColor);
}
// Debug.LogFormat("src:{0}, dst{1}", srcColor, dstColor);
// EqualColor(srcColor, dstColor);
// }
[UnityTest]
public IEnumerator MToonTextureToGltf_BaseColorTexture()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var srcSrgbTexture = CreateMonoTexture(0.5f, 0.5f, false);
// [UnityTest]
// public IEnumerator MToonTextureToGltf_BaseColorTexture()
// {
// var assets = ToUnity(_vrmPath);
// var srcMaterial = assets.Map.Materials.First();
// var key = srcMaterial.Key;
// var srcSrgbTexture = CreateMonoTexture(0.5f, 0.5f, false);
srcMaterial.Value.SetTexture(VrmLib.MToon.Utils.PropMainTex, srcSrgbTexture);
// srcMaterial.Value.SetTexture(VrmLib.MToon.Utils.PropMainTex, srcSrgbTexture);
var model = ToVrmModel(assets.Root);
var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
// var model = ToVrmModel(assets.Root);
// var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
var imageTexture = dstMaterial.Definition.Color.LitMultiplyTexture.Texture as VrmLib.ImageTexture;
EqualTextureColor(srcSrgbTexture, imageTexture, false);
// var imageTexture = dstMaterial.Definition.Color.LitMultiplyTexture.Texture as VrmLib.ImageTexture;
// EqualTextureColor(srcSrgbTexture, imageTexture, false);
yield return null;
}
// yield return null;
// }
[UnityTest]
public IEnumerator MToonTextureToGltf_OutlineWidthTexture()
{
var assets = ToUnity(_vrmPath);
var srcMaterial = assets.Map.Materials.First();
var key = srcMaterial.Key;
var srcLinearTexture = CreateMonoTexture(0.5f, 0.5f, true);
// [UnityTest]
// public IEnumerator MToonTextureToGltf_OutlineWidthTexture()
// {
// var assets = ToUnity(_vrmPath);
// var srcMaterial = assets.Map.Materials.First();
// var key = srcMaterial.Key;
// var srcLinearTexture = CreateMonoTexture(0.5f, 0.5f, true);
srcMaterial.Value.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcLinearTexture);
// srcMaterial.Value.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcLinearTexture);
var model = ToVrmModel(assets.Root);
var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
// var model = ToVrmModel(assets.Root);
// var dstMaterial = model.Materials.First(x => x.Name == key.Name) as VrmLib.MToonMaterial;
var imageTexture = dstMaterial.Definition.Outline.OutlineWidthMultiplyTexture.Texture as VrmLib.ImageTexture;
EqualTextureColor(srcLinearTexture, imageTexture, true);
// var imageTexture = dstMaterial.Definition.Outline.OutlineWidthMultiplyTexture.Texture as VrmLib.ImageTexture;
// EqualTextureColor(srcLinearTexture, imageTexture, true);
yield return null;
}
// yield return null;
// }
[UnityTest]
public IEnumerator GetOrCreateTextureTest()
{
var converter = new RuntimeVrmConverter();
var material = new UnityEngine.Material(Shader.Find("VRM/MToon"));
var srcLinearTexture = CreateMonoTexture(0.5f, 1.0f, true);
var srcSRGBTexture = CreateMonoTexture(0.5f, 1.0f, false);
// [UnityTest]
// public IEnumerator GetOrCreateTextureTest()
// {
// var converter = new RuntimeVrmConverter();
// var material = new UnityEngine.Material(Shader.Find("VRM/MToon"));
// var srcLinearTexture = CreateMonoTexture(0.5f, 1.0f, true);
// var srcSRGBTexture = CreateMonoTexture(0.5f, 1.0f, false);
{
material.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcSRGBTexture);
var textureInfo = converter.GetOrCreateTexture(material, srcSRGBTexture, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default);
var imageTexture = textureInfo.Texture as VrmLib.ImageTexture;
EqualTextureColor(srcSRGBTexture, imageTexture, false);
}
// {
// material.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcSRGBTexture);
// var textureInfo = converter.GetOrCreateTexture(material, srcSRGBTexture, VrmLib.Texture.ColorSpaceTypes.Srgb, VrmLib.Texture.TextureTypes.Default);
// var imageTexture = textureInfo.Texture as VrmLib.ImageTexture;
// EqualTextureColor(srcSRGBTexture, imageTexture, false);
// }
{
material.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcLinearTexture);
var textureInfo = converter.GetOrCreateTexture(material, srcLinearTexture, VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.Default);
var imageTexture = textureInfo.Texture as VrmLib.ImageTexture;
EqualTextureColor(srcLinearTexture, imageTexture, true);
}
// {
// material.SetTexture(VrmLib.MToon.Utils.PropOutlineWidthTexture, srcLinearTexture);
// var textureInfo = converter.GetOrCreateTexture(material, srcLinearTexture, VrmLib.Texture.ColorSpaceTypes.Linear, VrmLib.Texture.TextureTypes.Default);
// var imageTexture = textureInfo.Texture as VrmLib.ImageTexture;
// EqualTextureColor(srcLinearTexture, imageTexture, true);
// }
yield return null;
}
// yield return null;
// }
[UnityTest]
public IEnumerator MToonTextureToUnity()
{
yield return null;
}
// [UnityTest]
// public IEnumerator MToonTextureToUnity()
// {
// yield return null;
// }
#endregion
}
}

View File

@@ -3,7 +3,8 @@
"references": [
"VrmLib",
"VRM10",
"UniGLTF"
"UniGLTF",
"VRMShaders"
],
"optionalUnityReferences": [
"TestAssemblies"

View File

@@ -5,7 +5,7 @@ using System.Numerics;
namespace VrmLib
{
#pragma warning disable 0649
#pragma warning disable 0649
struct BoneWeight
{
public int boneIndex0;
@@ -18,7 +18,7 @@ namespace VrmLib
public float weight2;
public float weight3;
}
#pragma warning restore
#pragma warning restore
class MeshBuilder
{
@@ -68,7 +68,7 @@ namespace VrmLib
{
AddBone(head.SkeletonLocalPosition, tail.SkeletonLocalPosition, bones.IndexOf(head), headTail.XWidth, headTail.ZWidth);
}
else if(headTail.TailOffset!=Vector3.Zero)
else if (headTail.TailOffset != Vector3.Zero)
{
AddBone(head.SkeletonLocalPosition, head.SkeletonLocalPosition + headTail.TailOffset, bones.IndexOf(head), headTail.XWidth, headTail.ZWidth);
}
@@ -150,25 +150,25 @@ namespace VrmLib
void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, int boneIndex, bool reverse = false)
{
var i = m_positioins.Count;
if(float.IsNaN(v0.X) || float.IsNaN(v0.Y) || float.IsNaN(v0.Z))
if (float.IsNaN(v0.X) || float.IsNaN(v0.Y) || float.IsNaN(v0.Z))
{
throw new Exception();
}
m_positioins.Add(v0);
if(float.IsNaN(v1.X) || float.IsNaN(v1.Y) || float.IsNaN(v1.Z))
if (float.IsNaN(v1.X) || float.IsNaN(v1.Y) || float.IsNaN(v1.Z))
{
throw new Exception();
}
m_positioins.Add(v1);
if(float.IsNaN(v2.X) || float.IsNaN(v2.Y) || float.IsNaN(v2.Z))
if (float.IsNaN(v2.X) || float.IsNaN(v2.Y) || float.IsNaN(v2.Z))
{
throw new Exception();
}
m_positioins.Add(v2);
if(float.IsNaN(v3.X) || float.IsNaN(v3.Y) || float.IsNaN(v3.Z))
if (float.IsNaN(v3.X) || float.IsNaN(v3.Y) || float.IsNaN(v3.Z))
{
throw new Exception();
}
@@ -205,40 +205,6 @@ namespace VrmLib
m_indices.Add(i);
}
}
public Mesh CreateMesh()
{
if(m_positioins.Any(x => float.IsNaN(x.X) || float.IsNaN(x.Y) || float.IsNaN(x.Z)))
{
throw new Exception();
}
var mesh = new Mesh();
mesh.VertexBuffer = new VertexBuffer();
mesh.VertexBuffer.Add(VertexBuffer.PositionKey,
m_positioins.ToArray());
mesh.VertexBuffer.Add(VertexBuffer.JointKey,
m_boneWeights.Select(x => new SkinJoints(
(ushort)x.boneIndex0,
(ushort)x.boneIndex1,
(ushort)x.boneIndex2,
(ushort)x.boneIndex3)).ToArray());
mesh.VertexBuffer.Add(VertexBuffer.WeightKey,
m_boneWeights.Select(x => new Vector4(
x.weight0,
x.weight1,
x.weight2,
x.weight3
)).ToArray());
mesh.IndexBuffer = BufferAccessor.Create(m_indices.ToArray());
mesh.Submeshes.Add(new Submesh(0, mesh.IndexBuffer.Count, null));
return mesh;
}
}
struct BoneHeadTail

View File

@@ -34,18 +34,18 @@ namespace VrmLib
{
public int Offset;
public int DrawCount;
public Material Material;
public int Material;
public override string ToString()
{
return $"{Material.Name}({DrawCount})";
return $"{Material}({DrawCount})";
}
public Submesh(Material material) : this(0, 0, material)
public Submesh(int material) : this(0, 0, material)
{
}
public Submesh(int offset, int drawCount, Material material)
public Submesh(int offset, int drawCount, int material)
{
Offset = offset;
DrawCount = drawCount;

View File

@@ -653,7 +653,7 @@ namespace VrmLib
{
// 連続して同じMaterialのSubMeshを連結する
mesh.Submeshes.Clear();
Material current = null;
int current = -1;
foreach (var submesh in order)
{
if (current != submesh.Material)

View File

@@ -1,28 +0,0 @@
using System.Numerics;
namespace VrmLib
{
public static class MeshFactory
{
public static Mesh CreateQuadrangle()
{
var mesh = new Mesh();
mesh.IndexBuffer = BufferAccessor.Create(new int[] { 0, 1, 2, 2, 3, 0 });
mesh.VertexBuffer = new VertexBuffer();
mesh.VertexBuffer.Add(VertexBuffer.PositionKey, new Vector3[]{
new Vector3(-1, 1, 0),
new Vector3(1, 1, 0),
new Vector3(1, -1, 0),
new Vector3(-1, -1, 0),
});
mesh.VertexBuffer.Add(VertexBuffer.TexCoordKey, new Vector2[]{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(1, 1),
new Vector2(0, 1),
});
mesh.Submeshes.Add(new Submesh(0, 6, new Material("SCREEN")));
return mesh;
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d7c93f9da54dc08429261518417be9aa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -25,14 +25,8 @@ namespace VrmLib
public string AssetCopyright;
public string AssetMinVersion;
// gltf/images
public readonly List<Image> Images = new List<Image>();
// gltf/textures
public readonly List<Texture> Textures = new List<Texture>();
// gltf/materials
public readonly List<Material> Materials = new List<Material>();
public readonly List<object> Materials = new List<object>();
// gltf/skins
public readonly List<Skin> Skins = new List<Skin>();
@@ -90,16 +84,6 @@ namespace VrmLib
var sb = new StringBuilder();
sb.Append($"[GLTF] generator: {AssetGenerator}\n");
for (int i = 0; i < Images.Count; ++i)
{
var x = Images[i];
sb.Append($"[Image#{i:00}] {x}\n");
}
// for (int i = 0; i < Textures.Count; ++i)
// {
// var t = Textures[i];
// sb.Append($"[Texture#{i:00}] {t}\n");
// }
for (int i = 0; i < Materials.Count; ++i)
{
var m = Materials[i];

View File

@@ -68,9 +68,6 @@ namespace VrmLib
public static void CheckIndex(this Model model)
{
CheckIndex(model.Images, nameof(model.Images));
CheckIndex(model.Textures, nameof(model.Textures));
CheckIndex(model.Materials, nameof(model.Materials));
CheckIndex(model.Nodes, nameof(model.Nodes));
CheckIndex(model.Skins, nameof(model.Skins));
CheckIndex(model.MeshGroups, nameof(model.MeshGroups));