private void ExportMaterialFlagsFromName(BrgMaterial mat) { string flags = Maxscript.QueryString("mat.name").ToLower(); StringComparison cmp = StringComparison.InvariantCultureIgnoreCase; if (flags.Contains("colorxform1", cmp)) { mat.Flags |= BrgMatFlag.PlayerXFormColor1; } if (flags.Contains("2-sided", cmp) || flags.Contains("2 sided", cmp) || flags.Contains("2sided", cmp)) { mat.Flags |= BrgMatFlag.TwoSided; } if (flags.Contains("pixelxform1", cmp)) { mat.Flags |= BrgMatFlag.PixelXForm1; } }
private void ExportBrgMaterial(string mainObject, BrgMaterial mat) { //mat.id = Maxscript.QueryInteger("{0}.material.materialIDList[{1}]", mainObject, materialIndex + 1); //Maxscript.Command("mat = {0}.material[{1}]", mainObject, mat.id); mat.DiffuseColor = new Color3D(Maxscript.QueryFloat("mat.diffuse.r") / 255f, Maxscript.QueryFloat("mat.diffuse.g") / 255f, Maxscript.QueryFloat("mat.diffuse.b") / 255f); mat.AmbientColor = new Color3D(Maxscript.QueryFloat("mat.ambient.r") / 255f, Maxscript.QueryFloat("mat.ambient.g") / 255f, Maxscript.QueryFloat("mat.ambient.b") / 255f); mat.SpecularColor = new Color3D(Maxscript.QueryFloat("mat.specular.r") / 255f, Maxscript.QueryFloat("mat.specular.g") / 255f, Maxscript.QueryFloat("mat.specular.b") / 255f); mat.EmissiveColor = new Color3D(Maxscript.QueryFloat("mat.selfIllumColor.r") / 255f, Maxscript.QueryFloat("mat.selfIllumColor.g") / 255f, Maxscript.QueryFloat("mat.selfIllumColor.b") / 255f); mat.SpecularExponent = Maxscript.QueryFloat("mat.specularLevel"); if (mat.SpecularExponent > 0) { mat.Flags |= BrgMatFlag.SpecularExponent; } mat.Opacity = Maxscript.QueryFloat("mat.opacity") / 100f; if (mat.Opacity < 1f) { mat.Flags |= BrgMatFlag.Alpha; } int opacityType = Maxscript.QueryInteger("mat.opacityType"); if (opacityType == 1) { mat.Flags |= BrgMatFlag.SubtractiveBlend; } else if (opacityType == 2) { mat.Flags |= BrgMatFlag.AdditiveBlend; } if (Maxscript.QueryBoolean("mat.twoSided")) { mat.Flags |= BrgMatFlag.TwoSided; } if (Maxscript.QueryBoolean("mat.faceMap")) { mat.Flags |= BrgMatFlag.FaceMap; } if (Maxscript.QueryBoolean("(classof mat.reflectionMap) == BitmapTexture")) { mat.Flags |= BrgMatFlag.WrapUTx1 | BrgMatFlag.WrapVTx1 | BrgMatFlag.REFLECTIONTEXTURE; BrgMatSFX sfxMap = new BrgMatSFX(); sfxMap.Id = 30; sfxMap.Name = Maxscript.QueryString("getFilenameFile(mat.reflectionMap.filename)") + ".cub"; mat.sfx.Add(sfxMap); } if (Maxscript.QueryBoolean("(classof mat.bumpMap) == BitmapTexture")) { mat.BumpMap = Maxscript.QueryString("getFilenameFile(mat.bumpMap.filename)"); if (mat.BumpMap.Length > 0) { mat.Flags |= BrgMatFlag.WrapUTx3 | BrgMatFlag.WrapVTx3 | BrgMatFlag.BumpMap; } } if (Maxscript.QueryBoolean("(classof mat.diffusemap) == BitmapTexture")) { mat.DiffuseMap = Maxscript.QueryString("getFilenameFile(mat.diffusemap.filename)"); int parenthIndex = mat.DiffuseMap.IndexOf('('); if (parenthIndex > 0) { mat.DiffuseMap = mat.DiffuseMap.Remove(parenthIndex); } if (mat.DiffuseMap.Length > 0) { mat.Flags |= BrgMatFlag.WrapUTx1 | BrgMatFlag.WrapVTx1; if (Maxscript.QueryBoolean("mat.diffusemap.filename == mat.filtermap.filename")) { mat.Flags |= BrgMatFlag.PixelXForm1; } } } else if (Maxscript.QueryBoolean("(classof mat.diffusemap) == CompositeTextureMap") && Maxscript.QueryBoolean("(classof mat.diffusemap.mapList[1]) == BitmapTexture")) { mat.Flags |= BrgMatFlag.WrapUTx1 | BrgMatFlag.WrapVTx1 | BrgMatFlag.PixelXForm1; mat.DiffuseMap = Maxscript.QueryString("getFilenameFile(mat.diffusemap.mapList[1].filename)"); int parenthIndex = mat.DiffuseMap.IndexOf('('); if (parenthIndex > 0) { mat.DiffuseMap = mat.DiffuseMap.Remove(parenthIndex); } } this.ExportMaterialFlagsFromName(mat); }
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 Vector3 ( -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 Vector3 ( -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); Vector3 bBoxMax = new Vector3(Maxscript.QueryFloat("{0}BBMax.X", mainObject), Maxscript.QueryFloat("{0}BBMax.Y", mainObject), Maxscript.QueryFloat("{0}BBMax.Z", mainObject)); Vector3 bBoxMin = new Vector3(Maxscript.QueryFloat("{0}BBMin.X", mainObject), Maxscript.QueryFloat("{0}BBMin.Y", mainObject), Maxscript.QueryFloat("{0}BBMin.Z", mainObject)); Vector3 bBox = (bBoxMax - bBoxMin) / 2; mesh.Header.MinimumExtent = new Vector3(-bBox.X, -bBox.Z, -bBox.Y); mesh.Header.MaximumExtent = new Vector3(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 Vector3(-Maxscript.QueryFloat("vertex.x"), Maxscript.QueryFloat("vertex.z"), -Maxscript.QueryFloat("vertex.y"))); //System.Windows.Forms.MessageBox.Show("1.5"); mesh.Normals.Add(new Vector3( -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 Vector3(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); }
private void ExportAttachpoints(string attachDummy, BrgMesh mesh, float time) { time += Maxscript.QueryFloat("animationRange.start.ticks / 4800.0"); //System.Windows.Forms.MessageBox.Show("4"); int numAttachpoints = Maxscript.QueryInteger("{0}.count", attachDummy); //System.Windows.Forms.MessageBox.Show("5 " + numAttachpoints); if (mesh.Header.Flags.HasFlag(BrgMeshFlag.ATTACHPOINTS)) { mesh.Attachpoints = new List <BrgAttachpoint>(); for (int i = 0; i < numAttachpoints; i++) { string aName = Maxscript.QueryString("{0}[{1}].name", attachDummy, i + 1); int nameId; if (!BrgAttachpoint.TryGetIdByName(aName.Substring(6), out nameId)) { continue; } BrgAttachpoint att = new BrgAttachpoint(); //System.Windows.Forms.MessageBox.Show(aName); //System.Windows.Forms.MessageBox.Show("5.1"); //System.Windows.Forms.MessageBox.Show(mesh.Attachpoints.Count + " " + i); att.NameId = nameId; Maxscript.Command("{0}[{1}].name = \"{2}\"", attachDummy, i + 1, att.GetMaxName()); //System.Windows.Forms.MessageBox.Show("5.2"); Maxscript.SetVarAtTime(time, "{0}Transform", "{0}[{1}].rotation as matrix3", attachDummy, i + 1); Maxscript.SetVarAtTime(time, "{0}Position", "{0}[{1}].position", attachDummy, i + 1); Maxscript.SetVarAtTime(time, "{0}Scale", "{0}[{1}].scale * {0}[{1}].boxsize", attachDummy, i + 1); //System.Windows.Forms.MessageBox.Show("5.3"); Vector3 scale = new Vector3(Maxscript.QueryFloat("{0}Scale.X", attachDummy), Maxscript.QueryFloat("{0}Scale.Y", attachDummy), Maxscript.QueryFloat("{0}Scale.Z", attachDummy)); Vector3 bBox = scale / 2; //System.Windows.Forms.MessageBox.Show("5.4"); att.XVector.X = -Maxscript.QueryFloat("{0}Transform[1].z", attachDummy); att.XVector.Y = Maxscript.QueryFloat("{0}Transform[3].z", attachDummy); att.XVector.Z = -Maxscript.QueryFloat("{0}Transform[2].z", attachDummy); att.YVector.X = -Maxscript.QueryFloat("{0}Transform[1].y", attachDummy); att.YVector.Y = Maxscript.QueryFloat("{0}Transform[3].y", attachDummy); att.YVector.Z = -Maxscript.QueryFloat("{0}Transform[2].y", attachDummy); att.ZVector.X = -Maxscript.QueryFloat("{0}Transform[1].x", attachDummy); att.ZVector.Y = Maxscript.QueryFloat("{0}Transform[3].x", attachDummy); att.ZVector.Z = -Maxscript.QueryFloat("{0}Transform[2].x", attachDummy); att.Position.X = -Maxscript.QueryFloat("{0}Position.x", attachDummy); att.Position.Z = -Maxscript.QueryFloat("{0}Position.y", attachDummy); att.Position.Y = Maxscript.QueryFloat("{0}Position.z", attachDummy); //System.Windows.Forms.MessageBox.Show("5.5"); att.BoundingBoxMin.X = -bBox.X; att.BoundingBoxMin.Z = -bBox.Y; att.BoundingBoxMin.Y = -bBox.Z; att.BoundingBoxMax.X = bBox.X; att.BoundingBoxMax.Z = bBox.Y; att.BoundingBoxMax.Y = bBox.Z; mesh.Attachpoints.Add(att); } //System.Windows.Forms.MessageBox.Show("# Atpts: " + Attachpoint.Count); } }
public void Export() { BrgFile brg = this.File; Maxscript.Command("exportStartTime = timeStamp()"); //Maxscript.Command("print heapSize"); BrgMeshFlag flags = brg.Meshes[0].Header.Flags; BrgMeshFormat format = brg.Meshes[0].Header.Format; BrgMeshAnimType animationType = brg.Meshes[0].Header.AnimationType; BrgMeshInterpolationType interpolationType = brg.Meshes[0].Header.InterpolationType; Maxscript.Command("ExportBrgData()"); int totalNumVerts = Maxscript.QueryInteger("brgtotalNumVerts"); int totalNumFaces = Maxscript.QueryInteger("brgtotalNumFaces"); int meshCount = Maxscript.QueryInteger("brgMeshes.count"); if (meshCount == 0) { throw new Exception("No Editable_Mesh objects detected!"); } if (Maxscript.QueryBoolean("keys.Count == 0")) { throw new Exception("Could not acquire animation keys!"); } brg.Header.NumMeshes = Maxscript.QueryInteger("keys.Count"); brg.Animation = new Animation(); for (int i = 1; i <= brg.Header.NumMeshes; ++i) { brg.Animation.MeshKeys.Add(Maxscript.QueryFloat("keys[{0}]", i)); } if (brg.Header.NumMeshes == 1) { brg.Animation.Duration = 0; } else { brg.Animation.Duration = Maxscript.QueryFloat("(animationRange.end.ticks - animationRange.start.ticks) / 4800.0"); } brg.Animation.TimeStep = brg.Animation.Duration / (float)brg.Header.NumMeshes; string mainObject = "mainObject"; brg.Materials = new List <BrgMaterial>(); brg.Meshes = new List <BrgMesh>(brg.Header.NumMeshes); for (int m = 0; m < meshCount; ++m) { Maxscript.Command("{0} = brgMeshes[{1}]", mainObject, m + 1); // Materials Dictionary <int, int> matIdMapping = new Dictionary <int, int>(); if (Maxscript.QueryBoolean("classof {0}.material == Multimaterial", mainObject)) { brg.Header.NumMaterials = Maxscript.QueryInteger("{0}.material.materialList.count", mainObject); for (int i = 0; i < brg.Header.NumMaterials; i++) { BrgMaterial mat = new BrgMaterial(brg); mat.Id = brg.Materials.Count + 1; Maxscript.Command("mat = {0}.material.materialList[{1}]", mainObject, i + 1); this.ExportBrgMaterial(mainObject, mat); int matListIndex = brg.Materials.IndexOf(mat); int actualMatId = Maxscript.QueryInteger("{0}.material.materialIdList[{1}]", mainObject, i + 1); if (matListIndex >= 0) { if (!matIdMapping.ContainsKey(actualMatId)) { matIdMapping.Add(actualMatId, brg.Materials[matListIndex].Id); } } else { brg.Materials.Add(mat); if (matIdMapping.ContainsKey(actualMatId)) { matIdMapping[actualMatId] = mat.Id; } else { matIdMapping.Add(actualMatId, mat.Id); } } } } else if (Maxscript.QueryBoolean("classof {0}.material == Standardmaterial", mainObject)) { BrgMaterial mat = new BrgMaterial(brg); mat.Id = brg.Materials.Count + 1; Maxscript.Command("mat = {0}.material", mainObject); this.ExportBrgMaterial(mainObject, mat); int matListIndex = brg.Materials.IndexOf(mat); if (matListIndex >= 0) { matIdMapping.Add(1, brg.Materials[matListIndex].Id); } else { brg.Materials.Add(mat); matIdMapping.Add(1, mat.Id); } } else { if (flags.HasFlag(BrgMeshFlag.MATERIAL)) { throw new Exception("Not all meshes have a material applied! " + Maxscript.QueryString("{0}.name", mainObject)); } } // Mesh Animations for (int i = 0; i < brg.Header.NumMeshes; i++) { if (i > 0) { if (m == 0) { BrgMesh mesh = new BrgMesh(brg); mesh.Vertices = new List <Vector3>(totalNumVerts); mesh.Normals = new List <Vector3>(totalNumVerts); mesh.TextureCoordinates = new List <Vector3>(totalNumVerts); mesh.Faces = new List <Face>(totalNumFaces); brg.Meshes[0].MeshAnimations.Add(mesh); } brg.UpdateMeshSettings(i, flags, format, animationType, interpolationType); this.ExportBrgMesh(mainObject, (BrgMesh)brg.Meshes[0].MeshAnimations[i - 1], brg.Animation.MeshKeys[i], matIdMapping); } else { if (m == 0) { BrgMesh mesh = new BrgMesh(brg); mesh.Vertices = new List <Vector3>(totalNumVerts); mesh.Normals = new List <Vector3>(totalNumVerts); mesh.TextureCoordinates = new List <Vector3>(totalNumVerts); mesh.Faces = new List <Face>(totalNumFaces); brg.Meshes.Add(mesh); } brg.UpdateMeshSettings(i, flags, format, animationType, interpolationType); this.ExportBrgMesh(mainObject, brg.Meshes[i], brg.Animation.MeshKeys[i], matIdMapping); } } } // Export Attachpoints, and Update some Mesh data HashSet <int> usedFaceMaterials = new HashSet <int>(); string attachDummy = Maxscript.NewArray("attachDummy"); Maxscript.Command("{0} = for helpObj in ($helpers/Dummy_*) where classof helpObj == Dummy collect helpObj", attachDummy);//"$helpers/Dummy_* as array"); for (int i = 0; i < brg.Header.NumMeshes; i++) { BrgMesh mesh; if (i > 0) { mesh = (BrgMesh)brg.Meshes[0].MeshAnimations[i - 1]; } else { mesh = brg.Meshes[i]; } this.ExportAttachpoints(attachDummy, mesh, brg.Animation.MeshKeys[i]); HashSet <int> diffFaceMats = new HashSet <int>(); if (!mesh.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) && mesh.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL)) { for (int j = 0; j < mesh.Faces.Count; ++j) { diffFaceMats.Add(mesh.Faces[j].MaterialIndex); } if (diffFaceMats.Count > 0) { mesh.ExtendedHeader.NumMaterials = (byte)(diffFaceMats.Count - 1); mesh.ExtendedHeader.NumUniqueMaterials = diffFaceMats.Count; } } usedFaceMaterials.UnionWith(diffFaceMats); mesh.ExtendedHeader.AnimationLength = this.File.Animation.Duration; } List <BrgMaterial> usedMats = new List <BrgMaterial>(brg.Materials.Count); for (int i = 0; i < brg.Materials.Count; ++i) { if (usedFaceMaterials.Contains(brg.Materials[i].Id)) { usedMats.Add(brg.Materials[i]); } } brg.Materials = usedMats; brg.Header.NumMaterials = brg.Materials.Count; //Maxscript.Command("print heapSize"); Maxscript.Command("exportEndTime = timeStamp()"); Maxscript.Format("Export took % seconds\n", "((exportEndTime - exportStartTime) / 1000.0)"); }