Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Jul 16, 2023
1 parent 878f8de commit 9f09c79
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
3 changes: 2 additions & 1 deletion EngineKit.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GL/@EntryIndexedValue">GL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:Boolean x:Key="/Default/Environment/UnitTesting/DisabledProviders/=VsTest/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EUnitTestFramework_002EMigrations_002EEnableDisabledProvidersMigration/@EntryIndexedValue">True</s:Boolean>

<s:Int64 x:Key="/Default/Environment/UnitTesting/ParallelProcessesCount/@EntryValue">8</s:Int64>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Datas/@EntryIndexedValue">True</s:Boolean>
Expand Down
21 changes: 21 additions & 0 deletions src/EngineKit/Extensions/NumericsExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
63 changes: 35 additions & 28 deletions src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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")
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -204,13 +212,13 @@ private void ProcessNode(ICollection<MeshPrimitive> 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;
}

Expand All @@ -220,24 +228,25 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
? node.Name + "_" + Guid.NewGuid()
: node.Name;

var positions = primitive.VertexAccessors.GetValueOrDefault("POSITION").AsSpan<Vector3>();
var positions = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Position).AsSpan<Vector3>();
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<Vector3>();
var uvs = primitive.VertexAccessors.GetValueOrDefault("TEXCOORD_0").AsSpan<Vector2>();
var realTangents = primitive.VertexAccessors.GetValueOrDefault("TANGENT").AsSpan<Vector4>();
var vertexType = GetVertexTypeFromVertexAccessorNames(primitive!.VertexAccessors!.Keys.ToList());
var normalsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Normal);
var normals = normalsAccessor.AsSpan<Vector3>();
var uvsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Uv0);
var uvs = uvsAccessor.AsSpan<Vector2>();
var tangentsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Tangent);
var realTangents = tangentsAccessor.AsSpan<Vector4>();
if (uvs.Length == 0)
{
uvs = new Vector2[positions.Length].AsSpan();
Expand All @@ -261,9 +270,11 @@ private void ProcessNode(ICollection<MeshPrimitive> 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)
{
Expand All @@ -285,11 +296,7 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
break;
default:
{
if (vertexType == VertexType.PositionUv)
{
meshPrimitive.AddPositionUv(position, uvs[i]);
}

meshPrimitive.AddPositionUv(position, uvs[i]);
break;
}
}
Expand All @@ -300,13 +307,13 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I

private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection<string> 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;
}
Expand All @@ -317,7 +324,7 @@ private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection<strin
return VertexType.PositionNormal;
}

if (vertexAccessorNames.Contains("TEXCOORD_0"))
if (vertexAccessorNames.Contains(VertexAccessorName.Uv0))
{
return VertexType.PositionUv;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EngineKit/Graphics/MeshPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void CalculateTangents()
var biTangent = _biTangents[i];

var realTangent = Vector3.Normalize(Vector3.Subtract(tangent, normal * Vector3.Dot(normal, tangent)));
var realBiTangent = Vector3.Dot(Vector3.Cross(normal, tangent), biTangent) < 0.0f
var realBiTangent = Vector3.Dot(Vector3.Cross( Vector3.Normalize(normal), Vector3.Normalize(tangent)), Vector3.Normalize(biTangent)) < 0.0f
? -1.0f
: 1.0f;

Expand Down
7 changes: 7 additions & 0 deletions src/EngineKit/Graphics/SamplerInformation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using EngineKit.Extensions;

namespace EngineKit.Graphics;

[DebuggerDisplay("S = {TextureAddressingModeS}, T = {TextureAddressingModeT}, IF = {TextureInterpolationFilter}, MF = {TextureMipmapFilter}")]
public readonly struct SamplerInformation : IEquatable<SamplerInformation>
{
public SamplerInformation(SharpGLTF.Schema2.TextureSampler textureSampler)
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EngineKit/Mathematics/Vector3Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 9f09c79

Please sign in to comment.