Skip to content

Commit 9c941be

Browse files
committed
Added HasNormals property to Weighted Meshes
1 parent 51f79b7 commit 9c941be

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

src/SA3D.Modeling/Mesh/Converters/FromWeldedBasicConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private FromWeldedBasicConverter(Node rootNode, Node[] weldingGroups, Dictionary
7070
return null;
7171
}
7272

73-
WeightedMesh result = WeightedMesh.Create(_outVertices, _polygonCorners, _materials, _hasColors);
73+
WeightedMesh result = WeightedMesh.Create(_outVertices, _polygonCorners, _materials, _hasColors, true);
7474

7575
result.Label = _weldingGroups[^1].Attach!.Label;
7676
result.RootIndices.Add(_nodeIndices[_rootNode]);

src/SA3D.Modeling/Mesh/Converters/ToWeightedConverter.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ public static int ComputeCommonNodeIndex(Node[] nodes, SortedSet<int> dependingN
197197
return 0;
198198
}
199199

200-
private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex)
200+
private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex, out bool hasNormals)
201201
{
202202
BufferMesh[] meshes = GetMeshData(meshNodeIndex);
203+
hasNormals = false;
203204

204205
ushort[] indices = _usedVertices.ToArray();
205206
BufferVertex[] sourceVertices = new BufferVertex[0x10000];
@@ -211,6 +212,8 @@ private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex)
211212
continue;
212213
}
213214

215+
hasNormals |= mesh.HasNormals;
216+
214217
for(int i = 0; i < mesh.Vertices.Length; i++)
215218
{
216219
BufferVertex sourceVertex = mesh.Vertices[i];
@@ -237,9 +240,11 @@ private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex)
237240

238241
/// <param name="meshNodeIndices">Indices to meshes to use</param>
239242
/// <param name="baseNodeIndex">Index to the node that the vertex data should be made relative to.</param>
243+
/// <param name="hasNormals"/>
240244
/// <returns></returns>
241-
private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseNodeIndex)
245+
private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseNodeIndex, out bool hasNormals)
242246
{
247+
hasNormals = false;
243248
Matrix4x4 baseMatrix = _worldMatrices[_nodes[baseNodeIndex]];
244249
Matrix4x4.Invert(baseMatrix, out Matrix4x4 invbaseMatrix);
245250

@@ -271,6 +276,8 @@ private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseN
271276
continue;
272277
}
273278

279+
hasNormals |= bufferMesh.HasNormals;
280+
274281
foreach(BufferVertex vtx in bufferMesh.Vertices)
275282
{
276283
int index = vtx.Index + bufferMesh.VertexWriteOffset;
@@ -388,6 +395,7 @@ private void EvaluateMesh()
388395
int rootNodeIndex = _meshNodeIndexMapping[dependingMeshNodeIndices[0]];
389396
SortedSet<int> dependingRelativeNodeIndices = new();
390397
string label;
398+
bool hasNormals;
391399

392400
WeightedVertex[] vertices;
393401

@@ -399,7 +407,7 @@ private void EvaluateMesh()
399407
return;
400408
}
401409

402-
vertices = EvaluateNoWeightVertices(dependingMeshNodeIndices[0]);
410+
vertices = EvaluateNoWeightVertices(dependingMeshNodeIndices[0], out hasNormals);
403411
label = _nodes[rootNodeIndex].Attach!.Label;
404412
}
405413
else
@@ -408,7 +416,7 @@ private void EvaluateMesh()
408416
rootNodeIndex = ComputeCommonNodeIndex(_nodes, absoluteDepends);
409417
dependingRelativeNodeIndices = new(absoluteDepends.Select(x => x - rootNodeIndex));
410418

411-
vertices = EvaluateWeightVertices(dependingMeshNodeIndices, rootNodeIndex);
419+
vertices = EvaluateWeightVertices(dependingMeshNodeIndices, rootNodeIndex, out hasNormals);
412420
label = _nodes[absoluteDepends.Max].Attach!.Label;
413421
}
414422

@@ -420,7 +428,8 @@ private void EvaluateMesh()
420428
materials,
421429
new() { rootNodeIndex },
422430
dependingRelativeNodeIndices,
423-
hasColors
431+
hasColors,
432+
hasNormals
424433
)
425434
{
426435
Label = label

src/SA3D.Modeling/Mesh/Weighted/WeightedMesh.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public class WeightedMesh : ICloneable
5353
/// </summary>
5454
public bool IsWeighted => DependingNodeIndices.Count > 0;
5555

56+
/// <summary>
57+
/// Whether the model has normals (even if all normals point in the same direction).
58+
/// </summary>
59+
public bool HasNormals { get; }
60+
5661
/// <summary>
5762
/// Whether the model has colors (even if all colors are white).
5863
/// </summary>
@@ -88,14 +93,16 @@ internal WeightedMesh(
8893
BufferMaterial[] materials,
8994
HashSet<int> rootIndices,
9095
SortedSet<int> dependingNodeIndices,
91-
bool hasColors)
96+
bool hasColors,
97+
bool hasNormals)
9298
{
9399
Vertices = vertices;
94100
TriangleSets = triangleSets;
95101
Materials = materials;
96102
RootIndices = rootIndices;
97103
DependingNodeIndices = dependingNodeIndices;
98104
HasColors = hasColors;
105+
HasNormals = hasNormals;
99106
}
100107

101108
/// <summary>
@@ -105,12 +112,14 @@ internal WeightedMesh(
105112
/// <param name="triangleSets">Triangle sets.</param>
106113
/// <param name="materials">Materials for each triangle set.</param>
107114
/// <param name="hasColors">Whether the model contains colors.</param>
115+
/// <param name="hasNormals">Whether the model contains normals.</param>
108116
/// <returns>The created weighted mesh.</returns>
109117
public static WeightedMesh Create(
110118
WeightedVertex[] vertices,
111119
BufferCorner[][] triangleSets,
112120
BufferMaterial[] materials,
113-
bool hasColors)
121+
bool hasColors,
122+
bool hasNormals)
114123
{
115124
SortedSet<int> dependingNodes = new();
116125

@@ -156,7 +165,8 @@ public static WeightedMesh Create(
156165
materials,
157166
new(),
158167
dependingNodes,
159-
hasColors);
168+
hasColors,
169+
hasNormals);
160170
}
161171

162172

@@ -475,6 +485,7 @@ public static WeightedMesh MergeWeightedMeshes(IEnumerable<WeightedMesh> meshes)
475485
List<BufferMaterial> materials = new();
476486
SortedSet<int> dependingNodes = new();
477487
bool hasColors = false;
488+
bool hasNormals = false;
478489

479490
int maxNode = meshes.Max(x => x.DependingNodeIndices.Max);
480491
int weightNum = maxNode + 1;
@@ -483,6 +494,7 @@ public static WeightedMesh MergeWeightedMeshes(IEnumerable<WeightedMesh> meshes)
483494
{
484495
materials.AddRange(mesh.Materials);
485496
hasColors |= mesh.HasColors;
497+
hasNormals |= mesh.HasNormals;
486498

487499
if(vertices.Count > 0)
488500
{
@@ -546,7 +558,8 @@ public static WeightedMesh MergeWeightedMeshes(IEnumerable<WeightedMesh> meshes)
546558
materials.ToArray(),
547559
new(),
548560
dependingNodes,
549-
hasColors);
561+
hasColors,
562+
hasNormals);
550563
}
551564

552565
/// <summary>
@@ -639,7 +652,8 @@ public WeightedMesh Clone()
639652
Materials.ToArray(),
640653
new(RootIndices),
641654
new(DependingNodeIndices),
642-
HasColors);
655+
HasColors,
656+
HasNormals);
643657
}
644658

645659
#endregion

src/SA3D.Modeling/PublicAPI/net7.0/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,6 @@ static SA3D.Modeling.Mesh.Gamecube.GCVertexSet.WriteArray(SA3D.Common.IO.EndianS
18791879
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCLightingParameter.DefaultColorParam -> ushort
18801880
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCLightingParameter.DefaultNormalParam -> ushort
18811881
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCParameterExtensions.Write(this SA3D.Modeling.Mesh.Gamecube.Parameters.IGCParameter! parameter, SA3D.Common.IO.EndianStackWriter! writer) -> void
1882-
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.Create(SA3D.Modeling.Mesh.Weighted.WeightedVertex[]! vertices, SA3D.Modeling.Mesh.Buffer.BufferCorner[]![]! triangleSets, SA3D.Modeling.Mesh.Buffer.BufferMaterial[]! materials, bool hasColors) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh!
18831882
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.EnsurePolygonsValid(ref SA3D.Modeling.Mesh.Weighted.WeightedMesh![]! meshes) -> void
18841883
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.FromAttach(SA3D.Modeling.Mesh.Attach! attach, SA3D.Modeling.Mesh.Weighted.BufferMode bufferMode) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh!
18851884
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.FromModel(SA3D.Modeling.ObjectData.Node! model, SA3D.Modeling.Mesh.Weighted.BufferMode bufferMode) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh![]!

src/SA3D.Modeling/PublicAPI/net7.0/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
SA3D.Modeling.ObjectData.Node.AutoNodeAttributes(bool overrideExisting = false) -> void
1+
SA3D.Modeling.Mesh.Weighted.WeightedMesh.HasNormals.get -> bool
2+
SA3D.Modeling.ObjectData.Node.AutoNodeAttributes(bool overrideExisting = false) -> void
3+
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.Create(SA3D.Modeling.Mesh.Weighted.WeightedVertex[]! vertices, SA3D.Modeling.Mesh.Buffer.BufferCorner[]![]! triangleSets, SA3D.Modeling.Mesh.Buffer.BufferMaterial[]! materials, bool hasColors, bool hasNormals) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh!
24
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector2 value, System.Numerics.Vector2 other) -> bool
35
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector2 value, System.Numerics.Vector2 other, float epsilon) -> bool
46
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector3 value, System.Numerics.Vector3 other) -> bool

0 commit comments

Comments
 (0)