From 0242055e4de71b2eab7c9f5629a8451549a0aa0e Mon Sep 17 00:00:00 2001 From: PoChangSu Date: Tue, 9 Jun 2020 14:45:50 +0900 Subject: [PATCH] Add an option to remove vertex color in export menu --- Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs | 13 +++++++++---- Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs | 14 +++++++++++--- .../VRM/UniVRM/Editor/Format/VRMEditorExporter.cs | 3 ++- .../VRM/UniVRM/Editor/Format/VRMExportSettings.cs | 12 ++++++++++++ Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs | 3 ++- .../Scripts/Format/VRMExporterConfiguation.cs | 2 ++ 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs index c8368436f..f22021c01 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs @@ -18,7 +18,8 @@ namespace UniGLTF static glTFMesh ExportPrimitives(glTF gltf, int bufferIndex, string rendererName, Mesh mesh, Material[] materials, - List unityMaterials) + List unityMaterials, + bool removeVertexColor) { var positions = mesh.vertices.Select(y => y.ReverseZ()).ToArray(); var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, positions, glBufferTarget.ARRAY_BUFFER); @@ -30,7 +31,10 @@ namespace UniGLTF var tangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.tangents.Select(y => y.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER); #endif var uvAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.uv.Select(y => y.ReverseUV()).ToArray(), glBufferTarget.ARRAY_BUFFER); - var colorAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.colors, glBufferTarget.ARRAY_BUFFER); + + var colorAccessorIndex = -1; + if (!removeVertexColor) + colorAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.colors, glBufferTarget.ARRAY_BUFFER); var boneweights = mesh.boneWeights; var weightAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, boneweights.Select(y => new Vector4(y.weight0, y.weight1, y.weight2, y.weight3)).ToArray(), glBufferTarget.ARRAY_BUFFER); @@ -231,7 +235,8 @@ namespace UniGLTF public static void ExportMeshes(glTF gltf, int bufferIndex, List unityMeshes, List unityMaterials, bool useSparseAccessorForMorphTarget, - bool exportOnlyBlendShapePosition) + bool exportOnlyBlendShapePosition, + bool removeVertexColor) { for (int i = 0; i < unityMeshes.Count; ++i) { @@ -241,7 +246,7 @@ namespace UniGLTF var gltfMesh = ExportPrimitives(gltf, bufferIndex, x.Renderer.name, - mesh, materials, unityMaterials); + mesh, materials, unityMaterials, removeVertexColor); for (int j = 0; j < mesh.blendShapeCount; ++j) { diff --git a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs index 72b687661..f8db7a1b0 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs @@ -66,6 +66,12 @@ namespace UniGLTF set; } + public bool RemoveVertexColor + { + get; + set; + } + public GameObject Copy { get; @@ -142,7 +148,7 @@ namespace UniGLTF public void Export() { - FromGameObject(glTF, Copy, UseSparseAccessorForBlendShape); + FromGameObject(glTF, Copy, UseSparseAccessorForBlendShape, RemoveVertexColor); } public void Dispose() @@ -188,7 +194,8 @@ namespace UniGLTF return node; } - void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false) + void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false, + bool removeVertexColor = false) { var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); var bufferIndex = gltf.AddBuffer(bytesBuffer); @@ -247,7 +254,8 @@ namespace UniGLTF return true; }) .ToList(); - MeshExporter.ExportMeshes(gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget, ExportOnlyBlendShapePosition); + MeshExporter.ExportMeshes(gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget, + ExportOnlyBlendShapePosition, removeVertexColor); Meshes = unityMeshes.Select(x => x.Mesh).ToList(); #endregion diff --git a/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs index 98f7ddac9..0cd072270 100644 --- a/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs @@ -152,7 +152,8 @@ namespace VRM var vrm = VRMExporter.Export(target, new VRMExporterConfiguration { UseSparseAccessorForBlendShape = settings.UseSparseAccessor, - ExportOnlyBlendShapePosition = settings.OnlyBlendshapePosition + ExportOnlyBlendShapePosition = settings.OnlyBlendshapePosition, + RemoveVertexColor = settings.RemoveVertexColor }); vrm.extensions.VRM.meta.title = settings.Title; vrm.extensions.VRM.meta.version = settings.Version; diff --git a/Assets/VRM/UniVRM/Editor/Format/VRMExportSettings.cs b/Assets/VRM/UniVRM/Editor/Format/VRMExportSettings.cs index a0f420869..3b9b1bff9 100644 --- a/Assets/VRM/UniVRM/Editor/Format/VRMExportSettings.cs +++ b/Assets/VRM/UniVRM/Editor/Format/VRMExportSettings.cs @@ -84,6 +84,12 @@ namespace VRM /// [Tooltip("Remove blendShapeClip that preset is Unknown")] public bool ReduceBlendshapeClip = false; + + /// + /// 頂点カラーを削除する + /// + [Tooltip("Remove vertex color")] + public bool RemoveVertexColor = false; #endregion public struct Validation @@ -186,6 +192,12 @@ namespace VRM { yield return Validation.Error("ReduceBlendshapeSize is need VRMBlendShapeProxy, you need to convert to VRM once."); } + + var vertexColor = Source.GetComponentsInChildren().ToList().Any(x => x.sharedMesh.colors.Length > 0); + if (vertexColor) + { + yield return Validation.Warning("This model contains vertex color"); + } var renderers = Source.GetComponentsInChildren(); if (renderers.All(x => !x.gameObject.activeInHierarchy)) diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs index ee588e253..4f683aa1e 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs @@ -33,7 +33,8 @@ namespace VRM using (var exporter = new VRMExporter(gltf) { UseSparseAccessorForBlendShape = configuration.UseSparseAccessorForBlendShape, - ExportOnlyBlendShapePosition = configuration.ExportOnlyBlendShapePosition + ExportOnlyBlendShapePosition = configuration.ExportOnlyBlendShapePosition, + RemoveVertexColor = configuration.RemoveVertexColor, }) { _Export(gltf, exporter, go); diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMExporterConfiguation.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMExporterConfiguation.cs index a416c40d4..a998a4bb1 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMExporterConfiguation.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMExporterConfiguation.cs @@ -4,11 +4,13 @@ { public bool UseSparseAccessorForBlendShape; public bool ExportOnlyBlendShapePosition; + public bool RemoveVertexColor; public static VRMExporterConfiguration Default => new VRMExporterConfiguration { UseSparseAccessorForBlendShape = true, ExportOnlyBlendShapePosition = false, + RemoveVertexColor = false, }; } }