From d162fc6f45906a0fa84d8e4c0d5d31d3d5661dd3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 7 Jun 2018 20:45:01 +0900 Subject: [PATCH] Fixed sparse export --- Core/Scripts/Extensions/glTFExtensions.cs | 16 +-- Core/Scripts/Format/glTF.cs | 8 ++ Core/Scripts/IO/gltfExporter.cs | 118 +++++++++++++++------- 3 files changed, 98 insertions(+), 44 deletions(-) diff --git a/Core/Scripts/Extensions/glTFExtensions.cs b/Core/Scripts/Extensions/glTFExtensions.cs index 3aa8e3e..a162a91 100644 --- a/Core/Scripts/Extensions/glTFExtensions.cs +++ b/Core/Scripts/Extensions/glTFExtensions.cs @@ -151,31 +151,33 @@ public static int ExtendBufferAndGetViewIndex(this glTF gltf, int bufferIndex } public static int ExtendSparseBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, - T[] array, int[] sparseIndices, int sparseViewIndex, + int accessorCount, + T[] sparseValues, int[] sparseIndices, int sparseViewIndex, glBufferTarget target = glBufferTarget.NONE) where T : struct { return ExtendSparseBufferAndGetAccessorIndex(gltf, bufferIndex, - new ArraySegment(array), sparseIndices, sparseViewIndex, + accessorCount, + new ArraySegment(sparseValues), sparseIndices, sparseViewIndex, target); } public static int ExtendSparseBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, - ArraySegment array, int[] sparseIndices, int sparseIndicesViewIndex, + int accessorCount, + ArraySegment sparseValues, int[] sparseIndices, int sparseIndicesViewIndex, glBufferTarget target = glBufferTarget.NONE) where T : struct { - if (array.Count == 0) + if (sparseValues.Count == 0) { return -1; } - var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, - sparseIndices.Select(x => array.Array[array.Offset+x]).ToArray(), target); + var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, sparseValues, target); var accessorIndex = gltf.accessors.Count; gltf.accessors.Add(new glTFAccessor { byteOffset = 0, componentType = GetComponentType(), type = GetAccessorType(), - count = array.Count, + count = accessorCount, sparse = new glTFSparse { diff --git a/Core/Scripts/Format/glTF.cs b/Core/Scripts/Format/glTF.cs index d4ad0a0..ac8bd6c 100644 --- a/Core/Scripts/Format/glTF.cs +++ b/Core/Scripts/Format/glTF.cs @@ -165,6 +165,8 @@ public T[] GetArrayFromAccessor(int accessorIndex) where T : struct { var vertexAccessor = accessors[accessorIndex]; + if (vertexAccessor.count <= 0) return new T[] { }; + var result = (vertexAccessor.bufferView != -1) ? GetAttrib(vertexAccessor, bufferViews[vertexAccessor.bufferView]) : new T[vertexAccessor.count] @@ -176,6 +178,12 @@ public T[] GetArrayFromAccessor(int accessorIndex) where T : struct // override sparse values var indices = _GetIndices(bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); var values = GetAttrib(sparse.count, sparse.values.byteOffset, bufferViews[sparse.values.bufferView]); + + if (sparse.count != values.Length) + { + int a = 0; + } + var it = indices.GetEnumerator(); for(int i=0; i 0; + var blendShpaeNormals = mesh.normals; - var useNormal = blendShpaeNormals != null && blendShpaeNormals.Length == blendShapeVertices.Length; + var useNormal = usePosition && blendShpaeNormals != null && blendShpaeNormals.Length == blendShapeVertices.Length; + var blendShapeTangents = mesh.tangents.Select(y => (Vector3)y).ToArray(); - var useTangent = blendShapeTangents != null && blendShapeTangents.Length == blendShapeVertices.Length; + var useTangent = usePosition && blendShapeTangents != null && blendShapeTangents.Length == blendShapeVertices.Length; var frameCount = mesh.GetBlendShapeFrameCount(j); mesh.GetBlendShapeFrameVertices(j, frameCount - 1, blendShapeVertices, blendShpaeNormals, null); - blendShapeVertices = blendShapeVertices.Select(y => y.ReverseZ()).ToArray(); + var blendShapePositionAccessorIndex = -1; + var blendShapeNormalAccessorIndex = -1; + var blendShapeTangentAccessorIndex = -1; if (useSparseAccessorForMorphTarget) { + var accessorCount = blendShapeVertices.Length; var sparseIndices = Enumerable.Range(0, blendShapeVertices.Length) .Where(x => { return - blendShapeVertices[x] != Vector3.zero + (usePosition && blendShapeVertices[x] != Vector3.zero) || (useNormal && blendShpaeNormals[x] != Vector3.zero) || (useTangent && blendShapeTangents[x] != Vector3.zero) ; @@ -542,53 +548,91 @@ static gltfMorphTarget ExportMorphTarget(glTF gltf, int bufferIndex, .ToArray() ; - var sparseBytes = (4 + 12 + 12 + 12) * sparseIndices.Length; - var fullBytes = (12 + 12 + 12) * blendShapeVertices.Length; + if (sparseIndices.Length == 0) + { + usePosition = false; + useNormal = false; + useTangent = false; + } + /* + var vertexSize = 12; + if (useNormal) vertexSize += 12; + if (useTangent) vertexSize += 24; + var sparseBytes = (4 + vertexSize) * sparseIndices.Length; + var fullBytes = (vertexSize) * blendShapeVertices.Length; Debug.LogFormat("Export sparse: {0}/{1}bytes({2}%)", - sparseBytes, fullBytes, (int)((float)sparseBytes/fullBytes) + sparseBytes, fullBytes, (int)((float)sparseBytes / fullBytes) ); + */ - var sparseIndicesViewIndex = gltf.ExtendBufferAndGetViewIndex(bufferIndex, sparseIndices); + var sparseIndicesViewIndex = -1; + if (usePosition) + { + sparseIndicesViewIndex = gltf.ExtendBufferAndGetViewIndex(bufferIndex, sparseIndices); - var blendShapePositionAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, - blendShapeVertices, sparseIndices, sparseIndicesViewIndex, - glBufferTarget.ARRAY_BUFFER); - gltf.accessors[blendShapePositionAccessorIndex].min = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray(); - gltf.accessors[blendShapePositionAccessorIndex].max = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray(); + blendShapeVertices = sparseIndices.Select(x => blendShapeVertices[x].ReverseZ()).ToArray(); + blendShapePositionAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount, + blendShapeVertices, + sparseIndices, sparseIndicesViewIndex, + glBufferTarget.ARRAY_BUFFER); + } - var blendShapeNormalAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, - blendShpaeNormals.Select(y => y.ReverseZ()).ToArray(), sparseIndices, sparseIndicesViewIndex, - glBufferTarget.ARRAY_BUFFER); - var blendShapeTangentAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, - blendShapeTangents.Select(y => y.ReverseZ()).ToArray(), sparseIndices, sparseIndicesViewIndex, - glBufferTarget.ARRAY_BUFFER); + if (useNormal) + { + blendShpaeNormals = sparseIndices.Select(x => blendShpaeNormals[x].ReverseZ()).ToArray(); + blendShapeNormalAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount, + blendShpaeNormals, + sparseIndices, sparseIndicesViewIndex, + glBufferTarget.ARRAY_BUFFER); + } - return new gltfMorphTarget + if (useTangent) { - POSITION = blendShapePositionAccessorIndex, - NORMAL = blendShapeNormalAccessorIndex, - TANGENT = blendShapeTangentAccessorIndex, - }; + blendShapeTangents = sparseIndices.Select(x => blendShapeTangents[x].ReverseZ()).ToArray(); + blendShapeTangentAccessorIndex = gltf.ExtendSparseBufferAndGetAccessorIndex(bufferIndex, accessorCount, + blendShapeTangents, sparseIndices, sparseIndicesViewIndex, + glBufferTarget.ARRAY_BUFFER); + } } else { - var blendShapePositionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, blendShapeVertices - , glBufferTarget.ARRAY_BUFFER); - gltf.accessors[blendShapePositionAccessorIndex].min = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray(); - gltf.accessors[blendShapePositionAccessorIndex].max = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray(); + for (int i = 0; i < blendShapeVertices.Length; ++i) blendShapeVertices[i] = blendShapeVertices[i].ReverseZ(); + if (usePosition) + { + blendShapePositionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, + blendShapeVertices, + glBufferTarget.ARRAY_BUFFER); + } - var blendShapeNormalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, - blendShpaeNormals.Select(y => y.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER); - var blendShapeTangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, - blendShapeTangents.Select(y => y.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER); + if (useNormal) + { + for (int i = 0; i < blendShpaeNormals.Length; ++i) blendShpaeNormals[i] = blendShpaeNormals[i].ReverseZ(); + blendShapeNormalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, + blendShpaeNormals, + glBufferTarget.ARRAY_BUFFER); + } - return new gltfMorphTarget + if (useTangent) { - POSITION = blendShapePositionAccessorIndex, - NORMAL = blendShapeNormalAccessorIndex, - TANGENT = blendShapeTangentAccessorIndex, - }; + for (int i = 0; i < blendShapeTangents.Length; ++i) blendShapeTangents[i] = blendShapeTangents[i].ReverseZ(); + blendShapeTangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, + blendShapeTangents, + glBufferTarget.ARRAY_BUFFER); + } + } + + if(blendShapePositionAccessorIndex!=-1) + { + gltf.accessors[blendShapePositionAccessorIndex].min = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray(); + gltf.accessors[blendShapePositionAccessorIndex].max = blendShapeVertices.Aggregate(blendShapeVertices[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray(); } + + return new gltfMorphTarget + { + POSITION = blendShapePositionAccessorIndex, + NORMAL = blendShapeNormalAccessorIndex, + TANGENT = blendShapeTangentAccessorIndex, + }; } static void ExportMeshes(glTF gltf, int bufferIndex,