try to fix combined mesh export error

This commit is contained in:
Perfare 2018-07-17 01:47:55 +08:00
parent 66a2dbe730
commit 9b1e3435d6
3 changed files with 95 additions and 9 deletions

View File

@ -5,10 +5,18 @@ using System.Text;
namespace AssetStudio namespace AssetStudio
{ {
public class StaticBatchInfo
{
public ushort firstSubMesh;
public ushort subMeshCount;
}
public class MeshRenderer public class MeshRenderer
{ {
public PPtr m_GameObject; public PPtr m_GameObject;
public PPtr[] m_Materials; public PPtr[] m_Materials;
public StaticBatchInfo m_StaticBatchInfo;
public uint[] m_SubsetIndices;
protected MeshRenderer() { } protected MeshRenderer() { }
@ -56,6 +64,27 @@ namespace AssetStudio
{ {
m_Materials[m] = sourceFile.ReadPPtr(); m_Materials[m] = sourceFile.ReadPPtr();
} }
if (version[0] < 3)
{
reader.Position += 16;//m_LightmapTilingOffset vector4d
}
else
{
if ((sourceFile.version[0] == 5 && sourceFile.version[1] >= 5) || sourceFile.version[0] > 5)//5.5.0 and up
{
m_StaticBatchInfo = new StaticBatchInfo
{
firstSubMesh = reader.ReadUInt16(),
subMeshCount = reader.ReadUInt16()
};
}
else
{
int numSubsetIndices = reader.ReadInt32();
m_SubsetIndices = reader.ReadUInt32Array(numSubsetIndices);
}
}
} }
} }
} }

View File

@ -64,12 +64,16 @@ namespace AssetStudio
{ {
if ((sourceFile.version[0] == 5 && sourceFile.version[1] >= 5) || sourceFile.version[0] > 5)//5.5.0 and up if ((sourceFile.version[0] == 5 && sourceFile.version[1] >= 5) || sourceFile.version[0] > 5)//5.5.0 and up
{ {
reader.Position += 4;//m_StaticBatchInfo m_StaticBatchInfo = new StaticBatchInfo
{
firstSubMesh = reader.ReadUInt16(),
subMeshCount = reader.ReadUInt16()
};
} }
else else
{ {
int m_SubsetIndices_size = reader.ReadInt32(); int numSubsetIndices = reader.ReadInt32();
reader.Position += m_SubsetIndices_size * 4; m_SubsetIndices = reader.ReadUInt32Array(numSubsetIndices);
} }
var m_StaticBatchRoot = sourceFile.ReadPPtr(); var m_StaticBatchRoot = sourceFile.ReadPPtr();

View File

@ -228,10 +228,35 @@ namespace AssetStudio
assetsfileList.TryGetGameObject(meshR.m_GameObject, out var m_GameObject2); assetsfileList.TryGetGameObject(meshR.m_GameObject, out var m_GameObject2);
assetsfileList.TryGetTransform(m_GameObject2.m_Transform, out var meshTransform); assetsfileList.TryGetTransform(m_GameObject2.m_Transform, out var meshTransform);
iMesh.Name = GetTransformPath(meshTransform); iMesh.Name = GetTransformPath(meshTransform);
iMesh.SubmeshList = new List<ImportedSubmesh>(mesh.m_SubMeshes.Count); iMesh.SubmeshList = new List<ImportedSubmesh>();
int sum = 0; var subHashSet = new HashSet<int>();
var combine = false;
if (meshR.m_StaticBatchInfo != null && meshR.m_StaticBatchInfo.subMeshCount > 0)
{
var finalSubMesh = meshR.m_StaticBatchInfo.firstSubMesh + meshR.m_StaticBatchInfo.subMeshCount;
for (int i = meshR.m_StaticBatchInfo.firstSubMesh; i < finalSubMesh; i++)
{
subHashSet.Add(i);
}
combine = true;
}
else if (meshR.m_SubsetIndices != null)
{
foreach (var index in meshR.m_SubsetIndices)
{
subHashSet.Add((int)index);
}
combine = true;
}
int firstFace = 0;
for (int i = 0; i < mesh.m_SubMeshes.Count; i++) for (int i = 0; i < mesh.m_SubMeshes.Count; i++)
{ {
int numFaces = (int)mesh.m_SubMeshes[i].indexCount / 3;
if (subHashSet.Count > 0 && !subHashSet.Contains(i))
{
firstFace += numFaces;
continue;
}
var submesh = mesh.m_SubMeshes[i]; var submesh = mesh.m_SubMeshes[i];
var iSubmesh = new ImportedSubmesh(); var iSubmesh = new ImportedSubmesh();
Material mat = null; Material mat = null;
@ -310,10 +335,9 @@ namespace AssetStudio
iSubmesh.VertexList.Add(iVertex); iSubmesh.VertexList.Add(iVertex);
} }
//Face //Face
int numFaces = (int)mesh.m_SubMeshes[i].indexCount / 3;
iSubmesh.FaceList = new List<ImportedFace>(numFaces); iSubmesh.FaceList = new List<ImportedFace>(numFaces);
var end = sum + numFaces; var end = firstFace + numFaces;
for (int f = sum; f < end; f++) for (int f = firstFace; f < end; f++)
{ {
var face = new ImportedFace(); var face = new ImportedFace();
face.VertexIndices = new int[3]; face.VertexIndices = new int[3];
@ -322,7 +346,7 @@ namespace AssetStudio
face.VertexIndices[2] = (int)(mesh.m_Indices[f * 3] - submesh.firstVertex); face.VertexIndices[2] = (int)(mesh.m_Indices[f * 3] - submesh.firstVertex);
iSubmesh.FaceList.Add(face); iSubmesh.FaceList.Add(face);
} }
sum = end; firstFace = end;
iMesh.SubmeshList.Add(iSubmesh); iMesh.SubmeshList.Add(iSubmesh);
} }
@ -435,6 +459,35 @@ namespace AssetStudio
} }
} }
} }
//TODO
if (combine)
{
assetsfileList.TryGetGameObject(meshR.m_GameObject, out var m_GameObject);
foreach (var root in FrameList)
{
var frame = ImportedHelpers.FindFrame(m_GameObject.m_Name, root);
if (frame != null)
{
if (frame.Parent != null)
{
var parent = frame;
while (true)
{
if (parent.Parent != null)
{
parent = parent.Parent;
}
else
{
frame.Matrix = parent.Matrix;
break;
}
}
}
break;
}
}
}
MeshList.Add(iMesh); MeshList.Add(iMesh);
} }