Skip to content

Commit 9f09c79

Browse files
committed
Code cleanup
1 parent 878f8de commit 9f09c79

File tree

6 files changed

+67
-31
lines changed

6 files changed

+67
-31
lines changed

EngineKit.sln.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<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">
22
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GL/@EntryIndexedValue">GL</s:String>
33
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
4-
<s:Boolean x:Key="/Default/Environment/UnitTesting/DisabledProviders/=VsTest/@EntryIndexedValue">False</s:Boolean>
4+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EUnitTestFramework_002EMigrations_002EEnableDisabledProvidersMigration/@EntryIndexedValue">True</s:Boolean>
5+
56
<s:Int64 x:Key="/Default/Environment/UnitTesting/ParallelProcessesCount/@EntryValue">8</s:Int64>
67
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
78
<s:Boolean x:Key="/Default/UserDictionary/Words/=Datas/@EntryIndexedValue">True</s:Boolean>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using EngineKit.Mathematics;
2+
3+
namespace EngineKit.Extensions;
4+
5+
public static class NumericsExtensions
6+
{
7+
public static Vector2 ToVector2(this System.Numerics.Vector2 v)
8+
{
9+
return new Vector2(v.X, v.Y);
10+
}
11+
12+
public static Vector3 ToVector3(this System.Numerics.Vector3 v)
13+
{
14+
return new Vector3(v.X, v.Y, v.Z);
15+
}
16+
17+
public static Vector4 ToVector4(this System.Numerics.Vector4 v)
18+
{
19+
return new Vector4(v.X, v.Y, v.Z, v.W);
20+
}
21+
}

src/EngineKit/Graphics/MeshLoaders/SharpGltfMeshLoader.cs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ namespace EngineKit.Graphics.MeshLoaders;
1717

1818
internal sealed class SharpGltfMeshLoader : IMeshLoader
1919
{
20+
private const string SkipBecausePrimitiveHasNoVertices = "{Category}: Primitive has no vertices. Skipping";
21+
private const string SkipBecausePrimitiveIsNotTriangulated = "{Category}: Primitive must be triangulated. Skipping";
22+
23+
private static class VertexAccessorName
24+
{
25+
public const string Position = "POSITION";
26+
public const string Normal = "NORMAL";
27+
public const string Uv0 = "TEXCOORD_0";
28+
public const string Uv1 = "TEXCOORD_1";
29+
public const string Tangent = "TANGENT";
30+
}
31+
2032
private readonly ILogger _logger;
2133
private readonly IMaterialLibrary _materialLibrary;
2234

@@ -99,11 +111,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m
99111
{
100112
if (materialChannel.Key is "BaseColor" or "Diffuse" or "RGB")
101113
{
102-
material.BaseColor = new Color4(
103-
materialChannel.Color.X,
104-
materialChannel.Color.Y,
105-
materialChannel.Color.Z,
106-
materialChannel.Color.W);
114+
material.BaseColor = new Color4(materialChannel.Color.ToVector4());
107115

108116
if (materialChannel.Texture?.PrimaryImage != null)
109117
{
@@ -146,7 +154,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m
146154
}
147155
else if (materialChannel.Key == "SpecularColor")
148156
{
149-
material.SpecularColor = new Color4(materialChannel.Color.X, materialChannel.Color.Y, materialChannel.Color.Z, materialChannel.Color.W);
157+
material.SpecularColor = new Color4(materialChannel.Color.ToVector4());
150158
}
151159
else if (materialChannel.Key == "SpecularFactor")
152160
{
@@ -180,7 +188,7 @@ private static ImageInformation GetImageInformationFromChannel(MaterialChannel m
180188
else if (materialChannel.Key == "Emissive")
181189
{
182190
// Color
183-
material.EmissiveColor = new Color4(materialChannel.Color.X, materialChannel.Color.Y, materialChannel.Color.Z, 0.0f);
191+
material.EmissiveColor = new Color4(materialChannel.Color.ToVector4());
184192

185193
if (materialChannel.Texture?.PrimaryImage != null)
186194
{
@@ -204,13 +212,13 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
204212
{
205213
if (primitive?.VertexAccessors?.Keys == null)
206214
{
207-
_logger.Error("{Category}: Primitives has no vertices. Skipping", nameof(SharpGltfMeshLoader));
215+
_logger.Error(SkipBecausePrimitiveHasNoVertices, nameof(SharpGltfMeshLoader));
208216
continue;
209217
}
210218

211219
if (primitive.DrawPrimitiveType != PrimitiveType.TRIANGLES)
212220
{
213-
_logger.Error("{Category}: Only triangle primitives are allowed", nameof(SharpGltfMeshLoader));
221+
_logger.Error(SkipBecausePrimitiveIsNotTriangulated, nameof(SharpGltfMeshLoader));
214222
continue;
215223
}
216224

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

223-
var positions = primitive.VertexAccessors.GetValueOrDefault("POSITION").AsSpan<Vector3>();
231+
var positions = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Position).AsSpan<Vector3>();
224232
if (positions.Length == 0)
225233
{
226-
_logger.Error("{Category}: Mesh primitive {MeshName} has no valid vertex data", nameof(SharpGltfMeshLoader), meshName);
234+
_logger.Error("{Category}: Primitive {MeshName} has no valid vertex data", nameof(SharpGltfMeshLoader), meshName);
227235
continue;
228236
}
229237

230238
var meshPrimitive = new MeshPrimitive(meshName);
231239
meshPrimitive.Transform = node.WorldMatrix.ToMatrix();
232240
meshPrimitive.MaterialName = primitive.Material?.Name ?? (primitive.Material == null ? "M_NotFound" : materials.ElementAt(primitive.Material.LogicalIndex)?.Name) ?? "M_NotFound";
233-
234-
var vertexType = GetVertexTypeFromVertexAccessorNames(primitive!.VertexAccessors!.Keys.ToList());
235-
236241
meshPrimitive.BoundingBox = BoundingBox.FromPoints(positions.ToArray());
237242

238-
var normals = primitive.VertexAccessors.GetValueOrDefault("NORMAL").AsSpan<Vector3>();
239-
var uvs = primitive.VertexAccessors.GetValueOrDefault("TEXCOORD_0").AsSpan<Vector2>();
240-
var realTangents = primitive.VertexAccessors.GetValueOrDefault("TANGENT").AsSpan<Vector4>();
243+
var vertexType = GetVertexTypeFromVertexAccessorNames(primitive!.VertexAccessors!.Keys.ToList());
244+
var normalsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Normal);
245+
var normals = normalsAccessor.AsSpan<Vector3>();
246+
var uvsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Uv0);
247+
var uvs = uvsAccessor.AsSpan<Vector2>();
248+
var tangentsAccessor = primitive.VertexAccessors.GetValueOrDefault(VertexAccessorName.Tangent);
249+
var realTangents = tangentsAccessor.AsSpan<Vector4>();
241250
if (uvs.Length == 0)
242251
{
243252
uvs = new Vector2[positions.Length].AsSpan();
@@ -261,9 +270,11 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
261270
for (var i = 0; i < positions.Length; i++)
262271
{
263272
var position = Vector3.TransformPosition(positions[i], meshPrimitive.Transform);
273+
//var position = positions[i];
264274
var normal = Vector3.TransformDirection(normals[i], meshPrimitive.Transform);
275+
//var normal = normals[i];//Vector3.TransformDirection(normals[i], meshPrimitive.Transform);
265276
var realTangentXyz = new Vector3(realTangents[i].X, realTangents[i].Y, realTangents[i].Z);
266-
var realTangent = new Vector4(Vector3.TransformDirection(realTangentXyz, meshPrimitive.Transform), realTangents[i].W);
277+
var realTangent = new Vector4(realTangentXyz, realTangents[i].W);
267278

268279
switch (vertexType)
269280
{
@@ -285,11 +296,7 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
285296
break;
286297
default:
287298
{
288-
if (vertexType == VertexType.PositionUv)
289-
{
290-
meshPrimitive.AddPositionUv(position, uvs[i]);
291-
}
292-
299+
meshPrimitive.AddPositionUv(position, uvs[i]);
293300
break;
294301
}
295302
}
@@ -300,13 +307,13 @@ private void ProcessNode(ICollection<MeshPrimitive> meshPrimitives, Node node, I
300307

301308
private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection<string> vertexAccessorNames)
302309
{
303-
if (vertexAccessorNames.Contains("POSITION"))
310+
if (vertexAccessorNames.Contains(VertexAccessorName.Position))
304311
{
305-
if (vertexAccessorNames.Contains("NORMAL"))
312+
if (vertexAccessorNames.Contains(VertexAccessorName.Normal))
306313
{
307-
if (vertexAccessorNames.Contains("TEXCOORD_0"))
314+
if (vertexAccessorNames.Contains(VertexAccessorName.Uv0))
308315
{
309-
if (vertexAccessorNames.Contains("TANGENT"))
316+
if (vertexAccessorNames.Contains(VertexAccessorName.Tangent))
310317
{
311318
return VertexType.PositionNormalUvTangent;
312319
}
@@ -317,7 +324,7 @@ private static VertexType GetVertexTypeFromVertexAccessorNames(ICollection<strin
317324
return VertexType.PositionNormal;
318325
}
319326

320-
if (vertexAccessorNames.Contains("TEXCOORD_0"))
327+
if (vertexAccessorNames.Contains(VertexAccessorName.Uv0))
321328
{
322329
return VertexType.PositionUv;
323330
}

src/EngineKit/Graphics/MeshPrimitive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void CalculateTangents()
216216
var biTangent = _biTangents[i];
217217

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

src/EngineKit/Graphics/SamplerInformation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Diagnostics;
23
using EngineKit.Extensions;
34

45
namespace EngineKit.Graphics;
56

7+
[DebuggerDisplay("S = {TextureAddressingModeS}, T = {TextureAddressingModeT}, IF = {TextureInterpolationFilter}, MF = {TextureMipmapFilter}")]
68
public readonly struct SamplerInformation : IEquatable<SamplerInformation>
79
{
810
public SamplerInformation(SharpGLTF.Schema2.TextureSampler textureSampler)
@@ -17,6 +19,11 @@ public SamplerInformation(SharpGLTF.Schema2.TextureSampler textureSampler)
1719
public readonly TextureAddressMode TextureAddressingModeT;
1820
public readonly TextureInterpolationFilter TextureInterpolationFilter;
1921
public readonly TextureMipmapFilter TextureMipmapFilter;
22+
23+
public override string ToString()
24+
{
25+
return $"S = {TextureAddressingModeS}, T = {TextureAddressingModeT}, IF = {TextureInterpolationFilter}, MF = {TextureMipmapFilter}";
26+
}
2027

2128
public bool Equals(SamplerInformation other)
2229
{

src/EngineKit/Mathematics/Vector3Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public static class Vector3Extensions
2828

2929
public static class Vector4Extensions
3030
{
31-
31+
public static Vector3 XYZ(this Vector4 v) => new Vector3(v.X, v.Y, v.Z);
3232
}

0 commit comments

Comments
 (0)