From a159ef28c37b3fa5fabc621610d9ebce3e0e8c0b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 23 Jan 2024 23:23:03 +0100 Subject: [PATCH] Cleanup --- Penumbra/Import/Models/Import/BoundingBox.cs | 8 ++--- Penumbra/Import/Models/Import/MeshImporter.cs | 5 +-- .../Import/Models/Import/PrimitiveImporter.cs | 17 ++++----- .../Import/Models/Import/SubMeshImporter.cs | 35 ++++++++++--------- Penumbra/Import/Models/Import/Utility.cs | 21 ++++++----- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/Penumbra/Import/Models/Import/BoundingBox.cs b/Penumbra/Import/Models/Import/BoundingBox.cs index be47ebb8..b6d670ae 100644 --- a/Penumbra/Import/Models/Import/BoundingBox.cs +++ b/Penumbra/Import/Models/Import/BoundingBox.cs @@ -2,11 +2,11 @@ using Lumina.Data.Parsing; namespace Penumbra.Import.Models.Import; -/// Mutable representation of the bounding box surrouding a collection of vertices. +/// Mutable representation of the bounding box surrounding a collection of vertices. public class BoundingBox { - private Vector3 _minimum = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - private Vector3 _maximum = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); + private Vector3 _minimum = new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); + private Vector3 _maximum = new(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); /// Use the specified position to update this bounding box, expanding it if necessary. public void Merge(Vector3 position) @@ -25,7 +25,7 @@ public class BoundingBox /// Convert this bounding box to the struct format used in .mdl data structures. public MdlStructs.BoundingBoxStruct ToStruct() - => new MdlStructs.BoundingBoxStruct + => new() { Min = [_minimum.X, _minimum.Y, _minimum.Z, 1], Max = [_maximum.X, _maximum.Y, _maximum.Z, 1], diff --git a/Penumbra/Import/Models/Import/MeshImporter.cs b/Penumbra/Import/Models/Import/MeshImporter.cs index f76fa1ea..8ab55734 100644 --- a/Penumbra/Import/Models/Import/MeshImporter.cs +++ b/Penumbra/Import/Models/Import/MeshImporter.cs @@ -53,7 +53,7 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) private List? _bones; - private readonly BoundingBox _boundingBox = new BoundingBox(); + private readonly BoundingBox _boundingBox = new(); private readonly List _metaAttributes = []; @@ -124,7 +124,8 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) _material ??= subMesh.Material; if (subMesh.Material != null && _material != subMesh.Material) - notifier.Warning($"Meshes may only reference one material. Sub-mesh {subMeshName} material \"{subMesh.Material}\" has been ignored."); + notifier.Warning( + $"Meshes may only reference one material. Sub-mesh {subMeshName} material \"{subMesh.Material}\" has been ignored."); // Check that vertex declarations match - we need to combine the buffers, so a mismatch would take a whole load of resolution. if (_vertexDeclaration == null) diff --git a/Penumbra/Import/Models/Import/PrimitiveImporter.cs b/Penumbra/Import/Models/Import/PrimitiveImporter.cs index 9ac3d73c..0c2968df 100644 --- a/Penumbra/Import/Models/Import/PrimitiveImporter.cs +++ b/Penumbra/Import/Models/Import/PrimitiveImporter.cs @@ -35,21 +35,21 @@ public class PrimitiveImporter private readonly IDictionary? _nodeBoneMap; private ushort[]? _indices; - + private List? _vertexAttributes; - + private ushort _vertexCount; private byte[] _strides = [0, 0, 0]; private readonly List[] _streams = [[], [], []]; - - private BoundingBox _boundingBox = new BoundingBox(); + + private readonly BoundingBox _boundingBox = new(); private List>? _shapeValues; private PrimitiveImporter(MeshPrimitive primitive, IDictionary? nodeBoneMap, IoNotifier notifier) { - _notifier = notifier; - _primitive = primitive; + _notifier = notifier; + _primitive = primitive; _nodeBoneMap = nodeBoneMap; } @@ -154,9 +154,10 @@ public class PrimitiveImporter _streams[attribute.Stream].AddRange(attribute.Build(vertexIndex)); // Record which morph targets have values for this vertex, if any. + var index = vertexIndex; var changedMorphs = morphModifiedVertices .WithIndex() - .Where(pair => _vertexAttributes.Any(attribute => attribute.HasMorph(pair.Index, vertexIndex))) + .Where(pair => _vertexAttributes.Any(attribute => attribute.HasMorph(pair.Index, index))) .Select(pair => pair.Value); foreach (var modifiedVertices in changedMorphs) modifiedVertices.Add(vertexIndex); @@ -200,7 +201,7 @@ public class PrimitiveImporter _shapeValues = morphShapeValues; } - + private void BuildBoundingBox() { var positions = _primitive.VertexAccessors["POSITION"].AsVector3Array(); diff --git a/Penumbra/Import/Models/Import/SubMeshImporter.cs b/Penumbra/Import/Models/Import/SubMeshImporter.cs index 26f98b04..e81bb622 100644 --- a/Penumbra/Import/Models/Import/SubMeshImporter.cs +++ b/Penumbra/Import/Models/Import/SubMeshImporter.cs @@ -46,12 +46,12 @@ public class SubMeshImporter private byte[]? _strides; private readonly List[] _streams = [[], [], []]; - private List _indices = []; + private readonly List _indices = []; - private BoundingBox _boundingBox = new BoundingBox(); + private readonly BoundingBox _boundingBox = new(); private readonly List? _morphNames; - private Dictionary> _shapeValues = []; + private readonly Dictionary> _shapeValues = []; private SubMeshImporter(Node node, IDictionary? nodeBoneMap, IoNotifier notifier) { @@ -86,7 +86,7 @@ public class SubMeshImporter var attributeMask = metaAttributes.Length switch { < 32 => (1u << metaAttributes.Length) - 1, - 32 => uint.MaxValue, + 32 => uint.MaxValue, > 32 => throw _notifier.Exception("Models may utilise a maximum of 32 attributes."), }; @@ -102,22 +102,22 @@ public class SubMeshImporter BoneStartIndex = 0, BoneCount = 0, }, - Material = _material, + Material = _material, VertexDeclaration = _vertexDeclaration.Value, - VertexCount = _vertexCount, - Strides = _strides, - Streams = _streams, - Indices = _indices, - BoundingBox = _boundingBox, - MetaAttributes = metaAttributes, - ShapeValues = _shapeValues, + VertexCount = _vertexCount, + Strides = _strides, + Streams = _streams, + Indices = _indices, + BoundingBox = _boundingBox, + MetaAttributes = metaAttributes, + ShapeValues = _shapeValues, }; } private void BuildPrimitive(MeshPrimitive meshPrimitive, int index) { var vertexOffset = _vertexCount; - var indexOffset = _indices.Count; + var indexOffset = _indices.Count; var primitive = PrimitiveImporter.Import(meshPrimitive, _nodeBoneMap, _notifier.WithContext($"Primitive {index}")); @@ -141,7 +141,7 @@ public class SubMeshImporter stream.AddRange(primitiveStream); // Indices - _indices.AddRange(primitive.Indices.Select(index => (ushort)(index + vertexOffset))); + _indices.AddRange(primitive.Indices.Select(i => (ushort)(i + vertexOffset))); // Shape values foreach (var (primitiveShapeValues, morphIndex) in primitive.ShapeValues.WithIndex()) @@ -181,8 +181,9 @@ public class SubMeshImporter // We consider any "extras" key with a boolean value set to `true` to be an attribute. return nodeExtras? - .Where(pair => pair.Value.ValueKind == JsonValueKind.True) - .Select(pair => pair.Key) - .ToArray() ?? []; + .Where(pair => pair.Value.ValueKind == JsonValueKind.True) + .Select(pair => pair.Key) + .ToArray() + ?? []; } } diff --git a/Penumbra/Import/Models/Import/Utility.cs b/Penumbra/Import/Models/Import/Utility.cs index 28f47024..a1e44136 100644 --- a/Penumbra/Import/Models/Import/Utility.cs +++ b/Penumbra/Import/Models/Import/Utility.cs @@ -4,11 +4,11 @@ namespace Penumbra.Import.Models.Import; public static class Utility { - /// Merge attributes into an existing attribute array, providing an updated submesh mask. - /// Old submesh attribute mask. + /// Merge attributes into an existing attribute array, providing an updated sub mesh mask. + /// Old sub mesh attribute mask. /// Old attribute array that should be merged. /// New attribute array. Will be mutated. - /// New submesh attribute mask, updated to match the merged attribute array. + /// New sub mesh attribute mask, updated to match the merged attribute array. public static uint GetMergedAttributeMask(uint oldMask, IList oldAttributes, List newAttributes) { var metaAttributes = Enumerable.Range(0, 32) @@ -28,6 +28,7 @@ public static class Utility newAttributes.Add(metaAttribute); attributeIndex = newAttributes.Count - 1; } + newMask |= 1u << attributeIndex; } @@ -35,18 +36,22 @@ public static class Utility } /// Ensures that the two vertex declarations provided are equal, throwing if not. - public static void EnsureVertexDeclarationMatch(MdlStructs.VertexDeclarationStruct current, MdlStructs.VertexDeclarationStruct @new, IoNotifier notifier) + public static void EnsureVertexDeclarationMatch(MdlStructs.VertexDeclarationStruct current, MdlStructs.VertexDeclarationStruct @new, + IoNotifier notifier) { if (VertexDeclarationMismatch(current, @new)) throw notifier.Exception( - $@"All sub-meshes of a mesh must have equivalent vertex declarations. - Current: {FormatVertexDeclaration(current)} - New: {FormatVertexDeclaration(@new)}" + $""" + All sub-meshes of a mesh must have equivalent vertex declarations. + Current: {FormatVertexDeclaration(current)} + New: {FormatVertexDeclaration(@new)} + """ ); } private static string FormatVertexDeclaration(MdlStructs.VertexDeclarationStruct vertexDeclaration) - => string.Join(", ", vertexDeclaration.VertexElements.Select(element => $"{element.Usage} ({element.Type}@{element.Stream}:{element.Offset})")); + => string.Join(", ", + vertexDeclaration.VertexElements.Select(element => $"{element.Usage} ({element.Type}@{element.Stream}:{element.Offset})")); private static bool VertexDeclarationMismatch(MdlStructs.VertexDeclarationStruct a, MdlStructs.VertexDeclarationStruct b) {