@@ -17,6 +17,18 @@ namespace EngineKit.Graphics.MeshLoaders;
1717
1818internal 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 }
0 commit comments