From 9f09c791f9eb1b13f17b6430d1d470bbe8cce76d Mon Sep 17 00:00:00 2001 From: deccer Date: Sun, 16 Jul 2023 14:41:29 +0200 Subject: [PATCH] Code cleanup --- EngineKit.sln.DotSettings | 3 +- .../Extensions/NumericsExtensions.cs | 21 +++++++ .../MeshLoaders/SharpGltfMeshLoader.cs | 63 ++++++++++--------- src/EngineKit/Graphics/MeshPrimitive.cs | 2 +- src/EngineKit/Graphics/SamplerInformation.cs | 7 +++ .../Mathematics/Vector3Extensions.cs | 2 +- 6 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 src/EngineKit/Extensions/NumericsExtensions.cs diff --git a/EngineKit.sln.DotSettings b/EngineKit.sln.DotSettings index e06e0cb..61a6427 100644 --- a/EngineKit.sln.DotSettings +++ b/EngineKit.sln.DotSettings @@ -1,7 +1,8 @@  GL UI - False + True + 8 True True diff --git a/src/EngineKit/Extensions/NumericsExtensions.cs b/src/EngineKit/Extensions/NumericsExtensions.cs new file mode 100644 index 0000000..52b1ac8 --- /dev/null +++ b/src/EngineKit/Extensions/NumericsExtensions.cs @@ -0,0 +1,21 @@ +using EngineKit.Mathematics; + +namespace EngineKit.Extensions; + +public static class NumericsExtensions +{ + public static Vector2 ToVector2(this System.Numerics.Vector2 v) + { + return new Vector2(v.X, v.Y); + } + + public static Vector3 ToVector3(this System.Numerics.Vector3 v) + { + return new Vector3(v.X, v.Y, v.Z); + } + + public static Vector4 ToVector4(this System.Numerics.Vector4 v) + { + return new Vector4(v.X, v.Y, v.Z, v.W); + } +} \ No newline at end of file diff --git a/src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs b/src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs index f380276..662d5b6 100644 --- a/src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs +++ b/src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs @@ -17,6 +17,18 @@ namespace EngineKit.Graphics.MeshLoaders; internal sealed class SharpGltfMeshLoader : IMeshLoader { + private const string SkipBecausePrimitiveHasNoVertices = "{Category}: Primitive has no vertices. Skipping"; + private const string SkipBecausePrimitiveIsNotTriangulated = "{Category}: Primitive must be triangulated. Skipping"; + + private static class VertexAccessorName + { + public const string Position = "POSITION"; + public const string Normal = "NORMAL"; + public const string Uv0 = "TEXCOORD_0"; + public const string Uv1 = "TEXCOORD_1"; + public const string Tangent = "TANGENT"; + } + private readonly ILogger _logger; private readonly IMaterialLibrary _materialLibrary; @@ -99,11 +111,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m { if (materialChannel.Key is "BaseColor" or "Diffuse" or "RGB") { - material.BaseColor = new Color4( - materialChannel.Color.X, - materialChannel.Color.Y, - materialChannel.Color.Z, - materialChannel.Color.W); + material.BaseColor = new Color4(materialChannel.Color.ToVector4()); if (materialChannel.Texture?.PrimaryImage != null) { @@ -146,7 +154,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m } else if (materialChannel.Key == "SpecularColor") { - material.SpecularColor = new Color4(materialChannel.Color.X, materialChannel.Color.Y, materialChannel.Color.Z, materialChannel.Color.W); + material.SpecularColor = new Color4(materialChannel.Color.ToVector4()); } else if (materialChannel.Key == "SpecularFactor") { @@ -180,7 +188,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m else if (materialChannel.Key == "Emissive") { // Color - material.EmissiveColor = new Color4(materialChannel.Color.X, materialChannel.Color.Y, materialChannel.Color.Z, 0.0f); + material.EmissiveColor = new Color4(materialChannel.Color.ToVector4()); if (materialChannel.Texture?.PrimaryImage != null) { @@ -204,13 +212,13 @@ private void ProcessNode(ICollection meshPrimitives, Node node, I { if (primitive?.VertexAccessors?.Keys == null) { - _logger.Error("{Category}: Primitives has no vertices. Skipping", nameof(SharpGltfMeshLoader)); + _logger.Error(SkipBecausePrimitiveHasNoVertices, nameof(SharpGltfMeshLoader)); continue; } if (primitive.DrawPrimitiveType != PrimitiveType.TRIANGLES) { - _logger.Error("{Category}: Only triangle primitives are allowed", nameof(SharpGltfMeshLoader)); + _logger.Error(SkipBecausePrimitiveIsNotTriangulated, nameof(SharpGltfMeshLoader)); continue; } @@ -220,24 +228,25 @@ private void ProcessNode(ICollection meshPrimitives, Node node, I ? node.Name + "_" + Guid.NewGuid() : node.Name; - var positions = primitive.VertexAccessors.GetValueOrDefault("POSITION").AsSpan(); + var positions = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Position).AsSpan(); if (positions.Length == 0) { - _logger.Error("{Category}: Mesh primitive {MeshName} has no valid vertex data", nameof(SharpGltfMeshLoader), meshName); + _logger.Error("{Category}: Primitive {MeshName} has no valid vertex data", nameof(SharpGltfMeshLoader), meshName); continue; } var meshPrimitive = new MeshPrimitive(meshName); meshPrimitive.Transform = node.WorldMatrix.ToMatrix(); meshPrimitive.MaterialName = primitive.Material?.Name ?? (primitive.Material == null ? "M_NotFound" : materials.ElementAt(primitive.Material.LogicalIndex)?.Name) ?? "M_NotFound"; - - var vertexType = GetVertexTypeFromVertexAccessorNames(primitive!.VertexAccessors!.Keys.ToList()); - meshPrimitive.BoundingBox = BoundingBox.FromPoints(positions.ToArray()); - var normals = primitive.VertexAccessors.GetValueOrDefault("NORMAL").AsSpan(); - var uvs = primitive.VertexAccessors.GetValueOrDefault("TEXCOORD_0").AsSpan(); - var realTangents = primitive.VertexAccessors.GetValueOrDefault("TANGENT").AsSpan(); + var vertexType = GetVertexTypeFromVertexAccessorNames(primitive!.VertexAccessors!.Keys.ToList()); + var normalsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Normal); + var normals = normalsAccessor.AsSpan(); + var uvsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Uv0); + var uvs = uvsAccessor.AsSpan(); + var tangentsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Tangent); + var realTangents = tangentsAccessor.AsSpan(); if (uvs.Length == 0) { uvs = new Vector2[positions.Length].AsSpan(); @@ -261,9 +270,11 @@ private void ProcessNode(ICollection meshPrimitives, Node node, I for (var i = 0; i < positions.Length; i++) { var position = Vector3.TransformPosition(positions[i], meshPrimitive.Transform); + //var position = positions[i]; var normal = Vector3.TransformDirection(normals[i], meshPrimitive.Transform); + //var normal = normals[i];//Vector3.TransformDirection(normals[i], meshPrimitive.Transform); var realTangentXyz = new Vector3(realTangents[i].X, realTangents[i].Y, realTangents[i].Z); - var realTangent = new Vector4(Vector3.TransformDirection(realTangentXyz, meshPrimitive.Transform), realTangents[i].W); + var realTangent = new Vector4(realTangentXyz, realTangents[i].W); switch (vertexType) { @@ -285,11 +296,7 @@ private void ProcessNode(ICollection meshPrimitives, Node node, I break; default: { - if (vertexType == VertexType.PositionUv) - { - meshPrimitive.AddPositionUv(position, uvs[i]); - } - + meshPrimitive.AddPositionUv(position, uvs[i]); break; } } @@ -300,13 +307,13 @@ private void ProcessNode(ICollection meshPrimitives, Node node, I private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection vertexAccessorNames) { - if (vertexAccessorNames.Contains("POSITION")) + if (vertexAccessorNames.Contains(VertexAccessorName.Position)) { - if (vertexAccessorNames.Contains("NORMAL")) + if (vertexAccessorNames.Contains(VertexAccessorName.Normal)) { - if (vertexAccessorNames.Contains("TEXCOORD_0")) + if (vertexAccessorNames.Contains(VertexAccessorName.Uv0)) { - if (vertexAccessorNames.Contains("TANGENT")) + if (vertexAccessorNames.Contains(VertexAccessorName.Tangent)) { return VertexType.PositionNormalUvTangent; } @@ -317,7 +324,7 @@ private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection { public SamplerInformation(SharpGLTF.Schema2.TextureSampler textureSampler) @@ -17,6 +19,11 @@ public SamplerInformation(SharpGLTF.Schema2.TextureSampler textureSampler) public readonly TextureAddressMode TextureAddressingModeT; public readonly TextureInterpolationFilter TextureInterpolationFilter; public readonly TextureMipmapFilter TextureMipmapFilter; + + public override string ToString() + { + return $"S = {TextureAddressingModeS}, T = {TextureAddressingModeT}, IF = {TextureInterpolationFilter}, MF = {TextureMipmapFilter}"; + } public bool Equals(SamplerInformation other) { diff --git a/src/EngineKit/Mathematics/Vector3Extensions.cs b/src/EngineKit/Mathematics/Vector3Extensions.cs index 32bf5b0..e93fa20 100644 --- a/src/EngineKit/Mathematics/Vector3Extensions.cs +++ b/src/EngineKit/Mathematics/Vector3Extensions.cs @@ -28,5 +28,5 @@ public static class Vector3Extensions public static class Vector4Extensions { - + public static Vector3 XYZ(this Vector4 v) => new Vector3(v.X, v.Y, v.Z); } \ No newline at end of file