Skip to content

Commit

Permalink
Added HasNormals property to Weighted Meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin113D committed Aug 2, 2024
1 parent 51f79b7 commit 9c941be
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private FromWeldedBasicConverter(Node rootNode, Node[] weldingGroups, Dictionary
return null;
}

WeightedMesh result = WeightedMesh.Create(_outVertices, _polygonCorners, _materials, _hasColors);
WeightedMesh result = WeightedMesh.Create(_outVertices, _polygonCorners, _materials, _hasColors, true);

result.Label = _weldingGroups[^1].Attach!.Label;
result.RootIndices.Add(_nodeIndices[_rootNode]);
Expand Down
19 changes: 14 additions & 5 deletions src/SA3D.Modeling/Mesh/Converters/ToWeightedConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ public static int ComputeCommonNodeIndex(Node[] nodes, SortedSet<int> dependingN
return 0;
}

private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex)
private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex, out bool hasNormals)
{
BufferMesh[] meshes = GetMeshData(meshNodeIndex);
hasNormals = false;

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

hasNormals |= mesh.HasNormals;

for(int i = 0; i < mesh.Vertices.Length; i++)
{
BufferVertex sourceVertex = mesh.Vertices[i];
Expand All @@ -237,9 +240,11 @@ private WeightedVertex[] EvaluateNoWeightVertices(int meshNodeIndex)

/// <param name="meshNodeIndices">Indices to meshes to use</param>
/// <param name="baseNodeIndex">Index to the node that the vertex data should be made relative to.</param>
/// <param name="hasNormals"/>
/// <returns></returns>
private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseNodeIndex)
private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseNodeIndex, out bool hasNormals)
{
hasNormals = false;
Matrix4x4 baseMatrix = _worldMatrices[_nodes[baseNodeIndex]];
Matrix4x4.Invert(baseMatrix, out Matrix4x4 invbaseMatrix);

Expand Down Expand Up @@ -271,6 +276,8 @@ private WeightedVertex[] EvaluateWeightVertices(int[] meshNodeIndices, int baseN
continue;
}

hasNormals |= bufferMesh.HasNormals;

foreach(BufferVertex vtx in bufferMesh.Vertices)
{
int index = vtx.Index + bufferMesh.VertexWriteOffset;
Expand Down Expand Up @@ -388,6 +395,7 @@ private void EvaluateMesh()
int rootNodeIndex = _meshNodeIndexMapping[dependingMeshNodeIndices[0]];
SortedSet<int> dependingRelativeNodeIndices = new();
string label;
bool hasNormals;

WeightedVertex[] vertices;

Expand All @@ -399,7 +407,7 @@ private void EvaluateMesh()
return;
}

vertices = EvaluateNoWeightVertices(dependingMeshNodeIndices[0]);
vertices = EvaluateNoWeightVertices(dependingMeshNodeIndices[0], out hasNormals);
label = _nodes[rootNodeIndex].Attach!.Label;
}
else
Expand All @@ -408,7 +416,7 @@ private void EvaluateMesh()
rootNodeIndex = ComputeCommonNodeIndex(_nodes, absoluteDepends);
dependingRelativeNodeIndices = new(absoluteDepends.Select(x => x - rootNodeIndex));

vertices = EvaluateWeightVertices(dependingMeshNodeIndices, rootNodeIndex);
vertices = EvaluateWeightVertices(dependingMeshNodeIndices, rootNodeIndex, out hasNormals);
label = _nodes[absoluteDepends.Max].Attach!.Label;
}

Expand All @@ -420,7 +428,8 @@ private void EvaluateMesh()
materials,
new() { rootNodeIndex },
dependingRelativeNodeIndices,
hasColors
hasColors,
hasNormals
)
{
Label = label
Expand Down
24 changes: 19 additions & 5 deletions src/SA3D.Modeling/Mesh/Weighted/WeightedMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class WeightedMesh : ICloneable
/// </summary>
public bool IsWeighted => DependingNodeIndices.Count > 0;

/// <summary>
/// Whether the model has normals (even if all normals point in the same direction).
/// </summary>
public bool HasNormals { get; }

/// <summary>
/// Whether the model has colors (even if all colors are white).
/// </summary>
Expand Down Expand Up @@ -88,14 +93,16 @@ internal WeightedMesh(
BufferMaterial[] materials,
HashSet<int> rootIndices,
SortedSet<int> dependingNodeIndices,
bool hasColors)
bool hasColors,
bool hasNormals)
{
Vertices = vertices;
TriangleSets = triangleSets;
Materials = materials;
RootIndices = rootIndices;
DependingNodeIndices = dependingNodeIndices;
HasColors = hasColors;
HasNormals = hasNormals;
}

/// <summary>
Expand All @@ -105,12 +112,14 @@ internal WeightedMesh(
/// <param name="triangleSets">Triangle sets.</param>
/// <param name="materials">Materials for each triangle set.</param>
/// <param name="hasColors">Whether the model contains colors.</param>
/// <param name="hasNormals">Whether the model contains normals.</param>
/// <returns>The created weighted mesh.</returns>
public static WeightedMesh Create(
WeightedVertex[] vertices,
BufferCorner[][] triangleSets,
BufferMaterial[] materials,
bool hasColors)
bool hasColors,
bool hasNormals)
{
SortedSet<int> dependingNodes = new();

Expand Down Expand Up @@ -156,7 +165,8 @@ public static WeightedMesh Create(
materials,
new(),
dependingNodes,
hasColors);
hasColors,
hasNormals);
}


Expand Down Expand Up @@ -475,6 +485,7 @@ public static WeightedMesh MergeWeightedMeshes(IEnumerable<WeightedMesh> meshes)
List<BufferMaterial> materials = new();
SortedSet<int> dependingNodes = new();
bool hasColors = false;
bool hasNormals = false;

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

if(vertices.Count > 0)
{
Expand Down Expand Up @@ -546,7 +558,8 @@ public static WeightedMesh MergeWeightedMeshes(IEnumerable<WeightedMesh> meshes)
materials.ToArray(),
new(),
dependingNodes,
hasColors);
hasColors,
hasNormals);
}

/// <summary>
Expand Down Expand Up @@ -639,7 +652,8 @@ public WeightedMesh Clone()
Materials.ToArray(),
new(RootIndices),
new(DependingNodeIndices),
HasColors);
HasColors,
HasNormals);
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion src/SA3D.Modeling/PublicAPI/net7.0/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,6 @@ static SA3D.Modeling.Mesh.Gamecube.GCVertexSet.WriteArray(SA3D.Common.IO.EndianS
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCLightingParameter.DefaultColorParam -> ushort
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCLightingParameter.DefaultNormalParam -> ushort
static SA3D.Modeling.Mesh.Gamecube.Parameters.GCParameterExtensions.Write(this SA3D.Modeling.Mesh.Gamecube.Parameters.IGCParameter! parameter, SA3D.Common.IO.EndianStackWriter! writer) -> void
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!
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.EnsurePolygonsValid(ref SA3D.Modeling.Mesh.Weighted.WeightedMesh![]! meshes) -> void
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.FromAttach(SA3D.Modeling.Mesh.Attach! attach, SA3D.Modeling.Mesh.Weighted.BufferMode bufferMode) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh!
static SA3D.Modeling.Mesh.Weighted.WeightedMesh.FromModel(SA3D.Modeling.ObjectData.Node! model, SA3D.Modeling.Mesh.Weighted.BufferMode bufferMode) -> SA3D.Modeling.Mesh.Weighted.WeightedMesh![]!
Expand Down
4 changes: 3 additions & 1 deletion src/SA3D.Modeling/PublicAPI/net7.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
SA3D.Modeling.ObjectData.Node.AutoNodeAttributes(bool overrideExisting = false) -> void
SA3D.Modeling.Mesh.Weighted.WeightedMesh.HasNormals.get -> bool
SA3D.Modeling.ObjectData.Node.AutoNodeAttributes(bool overrideExisting = false) -> void
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!
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector2 value, System.Numerics.Vector2 other) -> bool
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector2 value, System.Numerics.Vector2 other, float epsilon) -> bool
static SA3D.Modeling.Structs.VectorUtilities.IsDistanceApproximate(this System.Numerics.Vector3 value, System.Numerics.Vector3 other) -> bool
Expand Down

0 comments on commit 9c941be

Please sign in to comment.