Merge pull request #1503 from ousttrue/fix/gltfdata_dispose

GltfData.Dispose の呼び忘れを修正
This commit is contained in:
ousttrue
2022-02-04 17:45:53 +09:00
committed by GitHub
8 changed files with 186 additions and 173 deletions

View File

@@ -54,10 +54,12 @@ namespace UniGLTF
}
};
var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment<byte>(bytes));
var (getter, len) = WeightsAccessor.GetAccessor(data, 0);
Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0));
Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1));
using (var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment<byte>(bytes)))
{
var (getter, len) = WeightsAccessor.GetAccessor(data, 0);
Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0));
Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1));
}
}
/// <summary>
@@ -188,33 +190,35 @@ namespace UniGLTF
: MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings)
;
var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes);
using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes))
{
var indices = parsed.GetIndices(gltfMesh.primitives[0].indices);
Assert.AreEqual(0, indices[0]);
Assert.AreEqual(1, indices[1]);
Assert.AreEqual(3, indices[2]);
Assert.AreEqual(3, indices[3]);
Assert.AreEqual(1, indices[4]);
Assert.AreEqual(2, indices[5]);
}
{
var positions = parsed.GetArrayFromAccessor<Vector3>(gltfMesh.primitives[0].attributes.POSITION);
Assert.AreEqual(4, positions.Length);
}
{
var indices = parsed.GetIndices(gltfMesh.primitives[0].indices);
Assert.AreEqual(0, indices[0]);
Assert.AreEqual(1, indices[1]);
Assert.AreEqual(3, indices[2]);
Assert.AreEqual(3, indices[3]);
Assert.AreEqual(1, indices[4]);
Assert.AreEqual(2, indices[5]);
}
{
var positions = parsed.GetArrayFromAccessor<Vector3>(gltfMesh.primitives[0].attributes.POSITION);
Assert.AreEqual(4, positions.Length);
}
{
var indices = parsed.GetIndices(gltfMesh.primitives[1].indices);
Assert.AreEqual(0, indices[0]);
Assert.AreEqual(1, indices[1]);
Assert.AreEqual(3, indices[2]);
Assert.AreEqual(3, indices[3]);
Assert.AreEqual(1, indices[4]);
Assert.AreEqual(2, indices[5]);
}
{
var positions = parsed.GetArrayFromAccessor<Vector3>(gltfMesh.primitives[1].attributes.POSITION);
Assert.AreEqual(4, positions.Length);
{
var indices = parsed.GetIndices(gltfMesh.primitives[1].indices);
Assert.AreEqual(0, indices[0]);
Assert.AreEqual(1, indices[1]);
Assert.AreEqual(3, indices[2]);
Assert.AreEqual(3, indices[3]);
Assert.AreEqual(1, indices[4]);
Assert.AreEqual(2, indices[5]);
}
{
var positions = parsed.GetArrayFromAccessor<Vector3>(gltfMesh.primitives[1].attributes.POSITION);
Assert.AreEqual(4, positions.Length);
}
}
}
}

View File

@@ -551,8 +551,8 @@ namespace UniGLTF
Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh);
// import
using (var parsed = GltfData.CreateFromExportForTest(data))
{
var parsed = GltfData.CreateFromExportForTest(data);
using (var context = new ImporterContext(parsed))
using (var loaded = context.Load())
{
@@ -569,8 +569,8 @@ namespace UniGLTF
}
// import new version
using (var parsed = GltfData.CreateFromExportForTest(data))
{
var parsed = GltfData.CreateFromExportForTest(data);
using (var context = new ImporterContext(parsed))
using (var loaded = context.Load())
{
@@ -621,8 +621,8 @@ namespace UniGLTF
Assert.AreEqual(-1, gltf.nodes[0].mesh);
// import
using (var parsed = GltfData.CreateFromExportForTest(data))
{
var parsed = GltfData.CreateFromExportForTest(data);
using (var context = new ImporterContext(parsed))
using (var loaded = context.Load())
{
@@ -685,8 +685,8 @@ namespace UniGLTF
Assert.AreEqual(-1, gltf.nodes[1].mesh);
// import
using (var parsed = GltfData.CreateFromExportForTest(data))
{
var parsed = GltfData.CreateFromExportForTest(data);
using (var context = new ImporterContext(parsed))
using (var loaded = context.Load())
{

View File

@@ -46,13 +46,14 @@ namespace VRM
return;
}
var data = new GlbFileParser(path).Parse();
var vrm = new VRMData(data);
using (var data = new GlbFileParser(path).Parse())
{
var vrm = new VRMData(data);
var importer = new VRMImporterContext(vrm, null);
var importer = new VRMImporterContext(vrm, null);
Assert.AreEqual(73, vrm.Data.GLTF.materials.Count);
Assert.True(VRMMToonMaterialImporter.TryCreateParam(vrm.Data, importer.VRM, 0, out MaterialDescriptor matDesc));
Assert.AreEqual(73, vrm.Data.GLTF.materials.Count);
Assert.True(VRMMToonMaterialImporter.TryCreateParam(vrm.Data, importer.VRM, 0, out MaterialDescriptor matDesc));
}
}
static string AliciaPath
@@ -68,14 +69,16 @@ namespace VRM
public void MaterialImporterTest()
{
var path = AliciaPath;
var data = new GlbFileParser(path).Parse();
var vrmImporter = new VRMImporterContext(new VRMData(data), null);
var materialParam = new VRMMaterialDescriptorGenerator(vrmImporter.VRM).Get(data, 0);
Assert.AreEqual("VRM/MToon", materialParam.ShaderName);
Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName);
using (var data = new GlbFileParser(path).Parse())
{
var vrmImporter = new VRMImporterContext(new VRMData(data), null);
var materialParam = new VRMMaterialDescriptorGenerator(vrmImporter.VRM).Get(data, 0);
Assert.AreEqual("VRM/MToon", materialParam.ShaderName);
Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName);
var (key, value) = materialParam.EnumerateSubAssetKeyValue().First();
Assert.AreEqual(new SubAssetKey(typeof(Texture), "Alicia_body"), key);
var (key, value) = materialParam.EnumerateSubAssetKeyValue().First();
Assert.AreEqual(new SubAssetKey(typeof(Texture), "Alicia_body"), key);
}
}
}
}

View File

@@ -43,8 +43,7 @@ namespace VRM.Samples
public void ImportExportTest()
{
var path = AliciaPath;
var data = new GlbFileParser(path).Parse();
using (var data = new GlbFileParser(path).Parse())
using (var context = new VRMImporterContext(new VRMData(data)))
using (var loaded = context.Load())
{
@@ -191,8 +190,7 @@ namespace VRM.Samples
public void MeshCopyTest()
{
var path = AliciaPath;
var data = new GlbFileParser(path).Parse();
using (var data = new GlbFileParser(path).Parse())
using (var context = new VRMImporterContext(new VRMData(data)))
using (var loaded = context.Load())
{
@@ -212,8 +210,8 @@ namespace VRM.Samples
{
// Aliciaを古いデシリアライザでロードする
var path = AliciaPath;
var data = new GlbFileParser(path).Parse();
using (var data = new GlbFileParser(path).Parse())
using (var context = new VRMImporterContext(new VRMData(data)))
{
var oldJson = context.GLTF.ToJson().ParseAsJson().ToString(" ");

View File

@@ -16,7 +16,7 @@ namespace VRM
[Test]
public void TextureEnumerationTest()
{
{
using (
var data = GltfData.CreateFromGltfDataForTest(
new glTF
{
@@ -60,7 +60,8 @@ namespace VRM
}
},
default
);
))
{
var vrm = new glTF_VRM_extensions
{
materialProperties = new List<glTF_VRM_Material>
@@ -89,7 +90,7 @@ namespace VRM
[Test]
public void TextureEnumerationInUnknownShader()
{
var data = GltfData.CreateFromGltfDataForTest(
using (var data = GltfData.CreateFromGltfDataForTest(
new glTF
{
images = new List<glTFImage>
@@ -122,10 +123,11 @@ namespace VRM
}
},
default
);
var vrm = new glTF_VRM_extensions
))
{
materialProperties = new List<glTF_VRM_Material>
var vrm = new glTF_VRM_extensions
{
materialProperties = new List<glTF_VRM_Material>
{
new glTF_VRM_Material
{
@@ -136,14 +138,15 @@ namespace VRM
}
},
}
};
};
// 2系統ある?
Assert.IsTrue(VRMMToonMaterialImporter.TryCreateParam(data, vrm, 0, out VRMShaders.MaterialDescriptor matDesc));
Assert.AreEqual(1, matDesc.TextureSlots.Count);
// 2系統ある?
Assert.IsTrue(VRMMToonMaterialImporter.TryCreateParam(data, vrm, 0, out VRMShaders.MaterialDescriptor matDesc));
Assert.AreEqual(1, matDesc.TextureSlots.Count);
var items = new VrmTextureDescriptorGenerator(data, vrm).Get().GetEnumerable().ToArray();
Assert.AreEqual(1, items.Length);
var items = new VrmTextureDescriptorGenerator(data, vrm).Get().GetEnumerable().ToArray();
Assert.AreEqual(1, items.Length);
}
}
}
}

View File

@@ -30,45 +30,46 @@ namespace UniVRM10
var weights = src.VertexBuffer.Weights?.AsNativeArray<Vector4>(Allocator.TempJob) ?? default;
var joints = src.VertexBuffer.Joints?.AsNativeArray<SkinJoints>(Allocator.TempJob) ?? default;
var vertices = new NativeArray<MeshVertex>(positions.Length, Allocator.TempJob);
// JobとBindPoseの更新を並行して行う
var jobHandle =
new InterleaveMeshVerticesJob(vertices, positions, normals, texCoords, colors, weights, joints)
.Schedule(vertices.Length, 1);
JobHandle.ScheduleBatchedJobs();
// BindPoseを更新
if (weights.IsCreated && joints.IsCreated)
using (var vertices = new NativeArray<MeshVertex>(positions.Length, Allocator.TempJob))
{
if (weights.Length != positions.Length || joints.Length != positions.Length)
// JobとBindPoseの更新を並行して行う
var jobHandle =
new InterleaveMeshVerticesJob(vertices, positions, normals, texCoords, colors, weights, joints)
.Schedule(vertices.Length, 1);
JobHandle.ScheduleBatchedJobs();
// BindPoseを更新
if (weights.IsCreated && joints.IsCreated)
{
throw new ArgumentException();
}
if (skin != null)
{
mesh.bindposes = skin.InverseMatrices.GetSpan<Matrix4x4>().ToArray();
if (weights.Length != positions.Length || joints.Length != positions.Length)
{
throw new ArgumentException();
}
if (skin != null)
{
mesh.bindposes = skin.InverseMatrices.GetSpan<Matrix4x4>().ToArray();
}
}
// Jobを完了
jobHandle.Complete();
// 入力のNativeArrayを開放
positions.Dispose();
if (normals.IsCreated) normals.Dispose();
if (texCoords.IsCreated) texCoords.Dispose();
if (colors.IsCreated) colors.Dispose();
if (weights.IsCreated) weights.Dispose();
if (joints.IsCreated) joints.Dispose();
// 頂点を更新
MeshVertex.SetVertexBufferParamsToMesh(mesh, vertices.Length);
mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length);
// 出力のNativeArrayを開放
}
// Jobを完了
jobHandle.Complete();
// 入力のNativeArrayを開放
positions.Dispose();
if (normals.IsCreated) normals.Dispose();
if (texCoords.IsCreated) texCoords.Dispose();
if (colors.IsCreated) colors.Dispose();
if (weights.IsCreated) weights.Dispose();
if (joints.IsCreated) joints.Dispose();
// 頂点を更新
MeshVertex.SetVertexBufferParamsToMesh(mesh, vertices.Length);
mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length);
// 出力のNativeArrayを開放
vertices.Dispose();
// Indexを更新
switch (src.IndexBuffer.ComponentType)
{

View File

@@ -57,40 +57,42 @@ namespace UniVRM10
normalsCount += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count;
}
var blendShapePositions = new NativeArray<Vector3>(positionsCount, Allocator.Temp);
var blendShapeNormals = new NativeArray<Vector3>(normalsCount, Allocator.Temp);
var blendShapePositionOffset = 0;
var blendShapeNormalOffset = 0;
foreach (var mesh in meshGroup.Meshes)
using (var blendShapePositions = new NativeArray<Vector3>(positionsCount, Allocator.Temp))
using (var blendShapeNormals = new NativeArray<Vector3>(normalsCount, Allocator.Temp))
{
var morphTarget = mesh.MorphTargets[i];
morphTarget.VertexBuffer.Positions.CopyToNativeSlice(
new NativeSlice<Vector3>(
blendShapePositions,
blendShapePositionOffset,
morphTarget.VertexBuffer.Positions.Count
)
);
// nullならdefault(0)のまま
morphTarget.VertexBuffer.Normals?.CopyToNativeSlice(
new NativeSlice<Vector3>(
blendShapeNormals,
blendShapeNormalOffset,
morphTarget.VertexBuffer.Normals.Count
)
);
var blendShapePositionOffset = 0;
var blendShapeNormalOffset = 0;
foreach (var mesh in meshGroup.Meshes)
{
var morphTarget = mesh.MorphTargets[i];
morphTarget.VertexBuffer.Positions.CopyToNativeSlice(
new NativeSlice<Vector3>(
blendShapePositions,
blendShapePositionOffset,
morphTarget.VertexBuffer.Positions.Count
)
);
blendShapePositionOffset += morphTarget.VertexBuffer.Positions.Count;
blendShapeNormalOffset += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count;
// nullならdefault(0)のまま
morphTarget.VertexBuffer.Normals?.CopyToNativeSlice(
new NativeSlice<Vector3>(
blendShapeNormals,
blendShapeNormalOffset,
morphTarget.VertexBuffer.Normals.Count
)
);
blendShapePositionOffset += morphTarget.VertexBuffer.Positions.Count;
blendShapeNormalOffset += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count;
}
resultMesh.AddBlendShapeFrame(meshGroup.Meshes[0].MorphTargets[i].Name,
100.0f,
blendShapePositions.ToArray(),
blendShapeNormals.ToArray(),
null);
}
resultMesh.AddBlendShapeFrame(meshGroup.Meshes[0].MorphTargets[i].Name,
100.0f,
blendShapePositions.ToArray(),
blendShapeNormals.ToArray(),
null);
}
Profiler.EndSample();
@@ -122,29 +124,29 @@ namespace UniVRM10
switch (mesh.IndexBuffer.ComponentType)
{
case AccessorValueType.SHORT:
{
// unsigned short -> unsigned short
var source = mesh.IndexBuffer.AsNativeArray<ushort>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Ushort2Ushort(
(ushort)vertexOffset,
new NativeSlice<ushort>(source),
new NativeSlice<ushort>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
{
// unsigned short -> unsigned short
var source = mesh.IndexBuffer.AsNativeArray<ushort>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Ushort2Ushort(
(ushort)vertexOffset,
new NativeSlice<ushort>(source),
new NativeSlice<ushort>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
case AccessorValueType.UNSIGNED_INT:
{
// unsigned int -> unsigned short
var source = mesh.IndexBuffer.AsNativeArray<uint>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Uint2Ushort(
(ushort)vertexOffset,
source,
new NativeSlice<ushort>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
{
// unsigned int -> unsigned short
var source = mesh.IndexBuffer.AsNativeArray<uint>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Uint2Ushort(
(ushort)vertexOffset,
source,
new NativeSlice<ushort>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
default:
throw new ArgumentOutOfRangeException();
}
@@ -169,29 +171,29 @@ namespace UniVRM10
switch (mesh.IndexBuffer.ComponentType)
{
case AccessorValueType.SHORT:
{
// unsigned short -> unsigned int
var source = mesh.IndexBuffer.AsNativeArray<ushort>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Ushort2Uint(
(uint)vertexOffset,
source,
new NativeSlice<uint>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
{
// unsigned short -> unsigned int
var source = mesh.IndexBuffer.AsNativeArray<ushort>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.Ushort2Uint(
(uint)vertexOffset,
source,
new NativeSlice<uint>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
case AccessorValueType.UNSIGNED_INT:
{
// unsigned int -> unsigned int
var source = mesh.IndexBuffer.AsNativeArray<uint>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.UInt2UInt(
(uint)vertexOffset,
source,
new NativeSlice<uint>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
{
// unsigned int -> unsigned int
var source = mesh.IndexBuffer.AsNativeArray<uint>(Allocator.TempJob);
disposables.Add(source);
jobHandle = new CopyIndicesJobs.UInt2UInt(
(uint)vertexOffset,
source,
new NativeSlice<uint>(indices, indexOffset, mesh.IndexBuffer.Count))
.Schedule(mesh.IndexBuffer.Count, 1, jobHandle);
break;
}
default:
throw new ArgumentOutOfRangeException();
}

View File

@@ -12,8 +12,10 @@ namespace UniVRM10
{
public static byte[] Migrate(byte[] src)
{
var data = new GlbBinaryParser(src, "migration").Parse();
return Migrate(data);
using (var data = new GlbBinaryParser(src, "migration").Parse())
{
return Migrate(data);
}
}
static (int, int) GetVertexRange(SpanLike<int> indices)