Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Oct 14, 2023
1 parent f114592 commit 866e632
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
23 changes: 12 additions & 11 deletions Assets/Scripts/Pal3.Core/DataReader/Cvd/CvdFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,14 @@ private static CvdMeshSection ReadMeshSection(IBinaryReader reader,

var frameVertices = new CvdVertex[allFrameVertices.Length][];

(List<int> triangles, List<int> indexBuffer) = CalculateTriangles(indices);
(int[] triangles, int[] indexBuffer) = CalculateTriangles(indices);

for (var i = 0; i < allFrameVertices.Length; i++)
{
var verts = new CvdVertex[indexBuffer.Count];
var verts = new CvdVertex[indexBuffer.Length];
var allVertices = allFrameVertices[i];

for (var j = 0; j < indexBuffer.Count; j++)
for (var j = 0; j < indexBuffer.Length; j++)
{
verts[j] = allVertices[indexBuffer[j]];
}
Expand All @@ -417,22 +417,22 @@ private static CvdMeshSection ReadMeshSection(IBinaryReader reader,
BlendFlag = blendFlag,
Material = material,
FrameVertices = frameVertices,
GameBoxTriangles = triangles.ToArray(),
GameBoxTriangles = triangles,
AnimationTimeKeys = animationTimeKeys,
AnimationMaterials = animationMaterials
};
}

private static (List<int> triangles, List<int> indexBuffer) CalculateTriangles(
private static (int[] triangles, int[] indexBuffer) CalculateTriangles(
(ushort x, ushort y, ushort z)[] allIndices)
{
var indexBuffer = new List<int>();
var triangles = new List<int>();
var index = 0;
var indexBuffer = new int[allIndices.Length * 3];
var triangles = new int[allIndices.Length * 3];
int index = 0;

for (var i = 0; i < allIndices.Length; i++)
{
var indices = new[]
ushort[] indices =
{
allIndices[i].x,
allIndices[i].y,
Expand All @@ -441,8 +441,9 @@ private static (List<int> triangles, List<int> indexBuffer) CalculateTriangles(

for (var j = 0; j < 3; j++)
{
indexBuffer.Add(indices[j]);
triangles.Add(index++);
indexBuffer[index] = indices[j];
triangles[index] = index;
index++;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public Mv3ActionConfig Read(IBinaryReader reader, int codepage)

public Mv3ActionConfig Read(byte[] data, int codepage)
{
var parser = new FileIniDataParser();
using var stream = new MemoryStream(data);
using var reader = new StreamReader(stream, Encoding.GetEncoding(codepage));
FileIniDataParser parser = new ();
using MemoryStream stream = new (data);
using StreamReader reader = new (stream, Encoding.GetEncoding(codepage));

IniData iniData = parser.ReadData(reader);

var actions = new List<ActorAction>();
List<ActorAction> actions = new();
foreach (SectionData section in iniData.Sections)
{
if (section.SectionName.StartsWith(ACTION_SECTION_HEADER_PREFIX))
Expand Down
18 changes: 12 additions & 6 deletions Assets/Scripts/Pal3.Core/DataReader/Mv3/Mv3FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,16 @@ private static Mv3Mesh GetMv3Mesh(string name,
Mv3VertFrame[] vertFrames,
GameBoxVector2[] texCoords)
{
var triangles = new int[attributes[0].IndexBuffers.Length * 3];
var keyFrameVertices = new List<GameBoxVector3>[vertFrames.Length]
.Select(item=>new List<GameBoxVector3>()).ToArray();
var uvs = new GameBoxVector2[attributes[0].IndexBuffers.Length * 3];
int numberOfTriangles = attributes[0].IndexBuffers.Length * 3;
var triangles = new int[numberOfTriangles];
var uvs = new GameBoxVector2[numberOfTriangles];

int totalVerticesPerKeyFrame = attributes[0].IndexBuffers.Length * 3;
var keyFrameVertices = new GameBoxVector3[vertFrames.Length][];
for (int i = 0; i < vertFrames.Length; i++)
{
keyFrameVertices[i] = new GameBoxVector3[totalVerticesPerKeyFrame];
}

var triangleIndex = 0;

Expand All @@ -176,7 +182,7 @@ private static Mv3Mesh GetMv3Mesh(string name,
Mv3VertFrame frame = vertFrames[k];
Mv3Vert vertex = frame.Vertices[indexBuffer.TriangleIndex[j]];

keyFrameVertices[k].Add(new GameBoxVector3(vertex.X, vertex.Y, vertex.Z));
keyFrameVertices[k][triangleIndex] = new GameBoxVector3(vertex.X, vertex.Y, vertex.Z);
}

uvs[triangleIndex] = texCoords[indexBuffer.TexCoordIndex[j]];
Expand All @@ -191,7 +197,7 @@ private static Mv3Mesh GetMv3Mesh(string name,
animationKeyFrames[i] = new Mv3AnimationKeyFrame()
{
GameBoxTick = vertFrames[i].GameBoxTick,
GameBoxVertices = keyFrameVertices[i].ToArray(),
GameBoxVertices = keyFrameVertices[i],
};
}

Expand Down

0 comments on commit 866e632

Please sign in to comment.