This commit is contained in:
ousttrue
2021-04-26 18:45:58 +09:00
parent 1354414209
commit 67811f720b
4 changed files with 95 additions and 52 deletions

View File

@@ -197,20 +197,22 @@ namespace UniGLTF
{
materialIndex = unityMaterials.IndexOf(material);
}
var indexMap = usedIndices.Select((used, index) => (used, index)).ToDictionary(x => x.used, x => x.index);
var flipped = new List<int>();
for (int j = 0; j < indices.Length; j += 3)
{
if (buffer.ContainsTriangle(j, j + 1, j + 2))
var t0 = indices[j];
var t1 = indices[j + 1];
var t2 = indices[j + 2];
if (buffer.ContainsTriangle(t0, t1, t2))
{
flipped.Add(indexMap[indices[j + 2]]);
flipped.Add(indexMap[indices[j + 1]]);
flipped.Add(indexMap[indices[j]]);
flipped.Add(t2);
flipped.Add(t1);
flipped.Add(t0);
}
else
{
Debug.LogWarning($"triangle not contains [{j}, {j + 1}, {j + 2}]");
Debug.LogWarning($"triangle not contains [{t0}, {t1}, {t2}]");
}
}
var gltfPrimitive = buffer.ToGltfPrimitive(gltf, bufferIndex, materialIndex, flipped);

View File

@@ -1,45 +0,0 @@
using NUnit.Framework;
using UnityEngine;
namespace UniGLTF
{
public class DividedMeshTests
{
/// <summary>
/// positions: [
/// {1, 1, 0}
/// {1, 1, 1}
/// {1, 1, 2}
/// {1, 1, 3}
/// {1, 1, 4}
/// {1, 1, 5}
/// ]
/// submesh
/// 0 1 2
/// submesh
/// 3 4 5
/// </summary>
[Test]
public void ExportDividedMeshTest()
{
var gltf = new glTF();
gltf.AddBuffer(new UniGLTF.ArrayByteBuffer());
{
var buffer = new UniGLTF.MeshExporterDivided.VertexBuffer(6, null);
buffer.Push(0, new Vector3(1, 1, 0), Vector3.up, Vector2.zero);
buffer.Push(1, new Vector3(1, 1, 1), Vector3.up, Vector2.zero);
buffer.Push(2, new Vector3(1, 1, 2), Vector3.up, Vector2.zero);
var prim = buffer.ToGltfPrimitive(gltf, 0, 0, new[] { 0, 1, 2 });
}
{
var buffer = new UniGLTF.MeshExporterDivided.VertexBuffer(6, null);
buffer.Push(3, new Vector3(1, 1, 3), Vector3.up, Vector2.zero);
buffer.Push(4, new Vector3(1, 1, 4), Vector3.up, Vector2.zero);
buffer.Push(5, new Vector3(1, 1, 5), Vector3.up, Vector2.zero);
var prim = buffer.ToGltfPrimitive(gltf, 0, 0, new[] { 3, 4, 5 });
}
}
}
}

View File

@@ -0,0 +1,86 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using UniGLTF;
using UnityEngine;
using VRMShaders;
namespace VRM
{
public class DividedMeshTests
{
static string AliciaPath
{
get
{
return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm")
.Replace("\\", "/");
}
}
static GameObject Load(byte[] bytes, string path)
{
var parser = new GltfParser();
parser.Parse(path, bytes);
using (var loader = new VRMImporterContext(parser))
{
loader.Load();
loader.ShowMeshes();
return loader.DisposeOnGameObjectDestroyed().gameObject;
}
}
static IEnumerable<Mesh> GetMeshes(GameObject gameObject)
{
foreach (var r in gameObject.GetComponentsInChildren<Renderer>())
{
if (r is SkinnedMeshRenderer smr)
{
yield return smr.sharedMesh;
}
else if (r is MeshRenderer mr)
{
yield return r.GetComponent<MeshFilter>().sharedMesh;
}
}
}
/// <summary>
/// positions: [
/// {1, 1, 0}
/// {1, 1, 1}
/// {1, 1, 2}
/// {1, 1, 3}
/// {1, 1, 4}
/// {1, 1, 5}
/// ]
/// submesh
/// 0 1 2
/// submesh
/// 3 4 5
/// </summary>
[Test]
public void ExportDividedMeshTest()
{
var path = AliciaPath;
var loaded = Load(File.ReadAllBytes(path), path);
var exported = VRMExporter.Export(new UniGLTF.MeshExportSettings
{
DivideVertexBuffer = true, // test this
ExportOnlyBlendShapePosition = true,
ExportTangents = false,
UseSparseAccessorForMorphTarget = true,
}, loaded, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
var bytes = exported.ToGlbBytes();
var divided = Load(bytes, path);
var src = GetMeshes(loaded).ToArray();
var div = GetMeshes(divided).ToArray();
Assert.AreEqual(src[0].triangles.Length, div[0].triangles.Length);
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1e859e08abedf9346bdadedd79eb3e67
guid: 5739a74d19763bb498ed1a035e25daf7
MonoImporter:
externalObjects: {}
serializedVersion: 2