private void ExportBrgMesh(string mainObject, BrgMesh mesh, float time, Dictionary<int, int> matIdMapping)
{
time += Maxscript.QueryFloat("animationRange.start.ticks / 4800.0");
string mainMesh = "mainMesh";
string mainObjectName = Maxscript.QueryString("{0}.name", mainObject);
// Figure out the proper data to import
if (!mesh.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH))
{
Maxscript.Command("{0} = ExportPreservedTexCoordData (GetMeshSnapshotAtTime {1} {2})", mainMesh, mainObject, time);
}
else
{
Maxscript.Command("{0} = GetMeshSnapshotAtTime {1} {2}", mainMesh, mainObject, time);
}
Maxscript.Command("ExportBrgVertNormals {0}", time);
Maxscript.SetVarAtTime(time, "meshCenter", "{0}.center", mainObject);
mesh.Header.CenterPosition = new Vector3D
(
-Maxscript.QueryFloat("meshCenter.x"),
Maxscript.QueryFloat("meshCenter.z"),
-Maxscript.QueryFloat("meshCenter.y")
);
Maxscript.Command("grnd = getNodeByName \"Dummy_hotspot\"");
if (!Maxscript.QueryBoolean("grnd == undefined"))
{
mesh.Header.HotspotPosition = new Vector3D
(
-Maxscript.QueryFloat("grnd.position.x"),
Maxscript.QueryFloat("grnd.position.z"),
-Maxscript.QueryFloat("grnd.position.y")
);
}
Maxscript.SetVarAtTime(time, "{0}BBMax", "{0}.max", mainObject);
Maxscript.SetVarAtTime(time, "{0}BBMin", "{0}.min", mainObject);
Vector3D bBoxMax = new Vector3D(Maxscript.QueryFloat("{0}BBMax.X", mainObject), Maxscript.QueryFloat("{0}BBMax.Y", mainObject), Maxscript.QueryFloat("{0}BBMax.Z", mainObject));
Vector3D bBoxMin = new Vector3D(Maxscript.QueryFloat("{0}BBMin.X", mainObject), Maxscript.QueryFloat("{0}BBMin.Y", mainObject), Maxscript.QueryFloat("{0}BBMin.Z", mainObject));
Vector3D bBox = (bBoxMax - bBoxMin) / 2;
mesh.Header.MinimumExtent = new Vector3D(-bBox.X, -bBox.Z, -bBox.Y);
mesh.Header.MaximumExtent = new Vector3D(bBox.X, bBox.Z, bBox.Y);
int numVertices = Maxscript.QueryInteger("brgVertIndices.count");
int numFaces = Maxscript.QueryInteger("brgFaceArray.count");
int currNumVertices = mesh.Vertices.Count;
//System.Windows.Forms.MessageBox.Show("1 " + numVertices);
for (int i = 0; i < numVertices; i++)
{
//System.Windows.Forms.MessageBox.Show("1.1");
try
{
Maxscript.Command("vertex = getVert {0} brgVertIndices[{1}]", mainMesh, i + 1);
//System.Windows.Forms.MessageBox.Show("1.4");
mesh.Vertices.Add(new Vector3D(-Maxscript.QueryFloat("vertex.x"), Maxscript.QueryFloat("vertex.z"), -Maxscript.QueryFloat("vertex.y")));
//System.Windows.Forms.MessageBox.Show("1.5");
mesh.Normals.Add(new Vector3D(
-Maxscript.QueryFloat("{0}[{1}].x", "averagedNormals", i + 1),
Maxscript.QueryFloat("{0}[{1}].z", "averagedNormals", i + 1),
-Maxscript.QueryFloat("{0}[{1}].y", "averagedNormals", i + 1)));
//System.Windows.Forms.MessageBox.Show("1.7");
}
catch (Exception ex)
{
throw new Exception("In mesh " + mainObjectName + " error exporting verts/normals index " + (i + 1) + ".", ex);
}
}
//System.Windows.Forms.MessageBox.Show("2");
if (!mesh.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) || mesh.Header.Flags.HasFlag(BrgMeshFlag.ANIMTEXCOORDS))
{
if (mesh.Header.Flags.HasFlag(BrgMeshFlag.TEXCOORDSA))
{
for (int i = 0; i < numVertices; i++)
{
Maxscript.Command("tVert = getTVert {0} brgVertTVIndices[{1}]", mainMesh, i + 1);
mesh.TextureCoordinates.Add(new Vector3D(Maxscript.QueryFloat("tVert.x"), Maxscript.QueryFloat("tVert.y"), 0f));
}
}
}
//System.Windows.Forms.MessageBox.Show("3");
if (!mesh.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH))
{
if (mesh.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL))
{
mesh.VertexMaterials.AddRange(new Int16[numVertices]);
}
for (int i = 0; i < numFaces; ++i)
{
Face f = new Face();
mesh.Faces.Add(f);
if (mesh.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL))
{
int faceMatId = Maxscript.QueryInteger("getFaceMatId {0} {1}", mainMesh, i + 1);
if (matIdMapping.ContainsKey(faceMatId))
{
f.MaterialIndex = (Int16)matIdMapping[faceMatId];
}
else
{
throw new Exception("In mesh " + mainObjectName + " face index " + (i + 1) + " has an invalid material id " + faceMatId + ".");
}
}
//System.Windows.Forms.MessageBox.Show("3.1");
Maxscript.Command("face = brgFaceArray[{0}]", i + 1);
f.Indices.Add((Int16)(Maxscript.QueryInteger("face.x") - 1 + currNumVertices));
f.Indices.Add((Int16)(Maxscript.QueryInteger("face.z") - 1 + currNumVertices));
f.Indices.Add((Int16)(Maxscript.QueryInteger("face.y") - 1 + currNumVertices));
//System.Windows.Forms.MessageBox.Show("3.2");
if (mesh.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL))
{
mesh.VertexMaterials[f.Indices[0]] = f.MaterialIndex;
mesh.VertexMaterials[f.Indices[1]] = f.MaterialIndex;
mesh.VertexMaterials[f.Indices[2]] = f.MaterialIndex;
}
}
}
Maxscript.Command("delete {0}", mainMesh);
}