From e6a167b906df24edecbe42dc0b47dc9f42177972 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 26 Feb 2021 13:52:34 +0900 Subject: [PATCH] IAxisInverter --- .../Editor/UniGLTF/GltfExportWindow.cs | 1 + .../UniGLTF/IO/AnimationImporterUtil.cs | 2 +- Assets/UniGLTF/Runtime/UniGLTF/IO/Axises.cs | 77 +++++++++++++++---- .../Runtime/UniGLTF/IO/ImporterContext.cs | 15 +--- .../Runtime/UniGLTF/IO/MeshExporter.cs | 4 + .../Runtime/UniGLTF/IO/MeshImporter.cs | 6 +- .../Runtime/UniGLTF/IO/NodeImporter.cs | 4 +- .../UniGLTF/IO/RootAnimationImporter.cs | 20 +---- 8 files changed, 74 insertions(+), 55 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs index 9c89a3735..5c3fb0736 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs @@ -240,6 +240,7 @@ namespace UniGLTF // export Export(root, path, new MeshExportSettings { + InvertAxis = settings.InverseAxis, ExportOnlyBlendShapePosition = settings.DropNormal, UseSparseAccessorForMorphTarget = settings.Sparse, }); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs index 0e4c16fe5..a5d9d65a0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/AnimationImporterUtil.cs @@ -185,7 +185,7 @@ namespace UniGLTF return string.Join("/", path); } - public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animation, AxisInverter inverter, glTFNode root = null) + public static AnimationClip ConvertAnimationClip(glTF gltf, glTFAnimation animation, IAxisInverter inverter, glTFNode root = null) { var clip = new AnimationClip(); clip.ClearCurves(); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Axises.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Axises.cs index 9096e708f..abaa02ec5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Axises.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/Axises.cs @@ -9,27 +9,70 @@ namespace UniGLTF X, } - public struct AxisInverter + public interface IAxisInverter { - public Func InvertVector3; - public Func InvertVector4; - public Func InvertQuaternion; - public Func InvertMat4; + Vector3 InvertVector3(Vector3 src); + Vector4 InvertVector4(Vector4 src); + Quaternion InvertQuaternion(Quaternion src); + Matrix4x4 InvertMat4(Matrix4x4 src); + } - public static AxisInverter ReverseZ => new AxisInverter + public struct ReverseZ : IAxisInverter + { + public Matrix4x4 InvertMat4(Matrix4x4 src) { - InvertVector3 = x => x.ReverseZ(), - InvertVector4 = x => x.ReverseZ(), - InvertQuaternion = x => x.ReverseZ(), - InvertMat4 = x => x.ReverseZ(), - }; + return src.ReverseZ(); + } - public static AxisInverter ReverseX => new AxisInverter + public Quaternion InvertQuaternion(Quaternion src) { - InvertVector3 = x => x.ReverseX(), - InvertVector4 = x => x.ReverseX(), - InvertQuaternion = x => x.ReverseX(), - InvertMat4 = x => x.ReverseX(), - }; + return src.ReverseZ(); + } + + public Vector3 InvertVector3(Vector3 src) + { + return src.ReverseZ(); + } + + public Vector4 InvertVector4(Vector4 src) + { + return src.ReverseZ(); + } + } + + public class ReverseX : IAxisInverter + { + public Matrix4x4 InvertMat4(Matrix4x4 src) + { + return src.ReverseX(); + } + + public Quaternion InvertQuaternion(Quaternion src) + { + return src.ReverseX(); + } + + public Vector3 InvertVector3(Vector3 src) + { + return src.ReverseX(); + } + + public Vector4 InvertVector4(Vector4 src) + { + return src.ReverseX(); + } + } + + public static class AxisesExtensions + { + public static IAxisInverter Create(this Axises axis) + { + switch (axis) + { + case Axises.Z: return new ReverseZ(); + case Axises.X: return new ReverseX(); + default: throw new NotImplementedException(); + } + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 5c82ba2fe..a2ea04a55 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -71,20 +71,7 @@ namespace UniGLTF MeasureTime = new ImporterContextSpeedLog().MeasureTime; } - AxisInverter inverter = default; - switch (InvertAxis) - { - case Axises.Z: - inverter = AxisInverter.ReverseZ; - break; - - case Axises.X: - inverter = AxisInverter.ReverseX; - break; - - default: - throw new NotImplementedException(); - } + var inverter = InvertAxis.Create(); if (Root == null) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporter.cs index d2cdd5270..d10638ed4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshExporter.cs @@ -8,6 +8,9 @@ namespace UniGLTF [Serializable] public struct MeshExportSettings { + // 反転軸 + public Axises InvertAxis; + // MorphTarget に Sparse Accessor を使う public bool UseSparseAccessorForMorphTarget; @@ -16,6 +19,7 @@ namespace UniGLTF public static MeshExportSettings Default => new MeshExportSettings { + InvertAxis = Axises.Z, UseSparseAccessorForMorphTarget = false, ExportOnlyBlendShapePosition = false, }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs index aa35e5e27..b651cb98c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs @@ -107,7 +107,7 @@ namespace UniGLTF /// /// /// - public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltfMesh, AxisInverter inverter) + public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltfMesh, IAxisInverter inverter) { foreach (var prim in gltfMesh.primitives) { @@ -291,7 +291,7 @@ namespace UniGLTF /// /// /// - public void ImportMeshSharingVertexBuffer(ImporterContext ctx, glTFMesh gltfMesh, AxisInverter inverter) + public void ImportMeshSharingVertexBuffer(ImporterContext ctx, glTFMesh gltfMesh, IAxisInverter inverter) { { // 同じVertexBufferを共有しているので先頭のモノを使う @@ -507,7 +507,7 @@ namespace UniGLTF return sharedAttributes; } - public MeshContext ReadMesh(ImporterContext ctx, int meshIndex, AxisInverter inverter) + public MeshContext ReadMesh(ImporterContext ctx, int meshIndex, IAxisInverter inverter) { var gltfMesh = ctx.GLTF.meshes[meshIndex]; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs index da02b9272..541b4e034 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs @@ -131,7 +131,7 @@ namespace UniGLTF // // fix node's coordinate. z-back to z-forward // - public static void FixCoordinate(ImporterContext context, List nodes, AxisInverter inverter) + public static void FixCoordinate(ImporterContext context, List nodes, IAxisInverter inverter) { var globalTransformMap = nodes.ToDictionary(x => x.Transform, x => new PosRot { @@ -154,7 +154,7 @@ namespace UniGLTF } } - public static void SetupSkinning(ImporterContext context, List nodes, int i, AxisInverter inverter) + public static void SetupSkinning(ImporterContext context, List nodes, int i, IAxisInverter inverter) { var x = nodes[i]; var skinnedMeshRenderer = x.Transform.GetComponent(); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs index 029efc3c4..d48f7ee81 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs @@ -46,26 +46,10 @@ namespace UniGLTF animation.name = $"animation:{i}"; } - AxisInverter inverter = default; - switch (invertAxis) - { - case Axises.X: - inverter = AxisInverter.ReverseX; - break; - - case Axises.Z: - inverter = AxisInverter.ReverseZ; - break; - - default: - throw new System.Exception(); - - } - - animationClips.Add(AnimationImporterUtil.ConvertAnimationClip(gltf, animation, inverter)); + animationClips.Add(AnimationImporterUtil.ConvertAnimationClip(gltf, animation, invertAxis.Create())); } return animationClips; } } -} \ No newline at end of file +}