public unsafe static void importBR2(string sourceFilename, string outputFilename, GrannyContext grannyContext) { IGrannyFile file = CivNexusSixApplicationForm.form.OpenFileAsTempFileCopy(CivNexusSixApplicationForm.form.modelTemplateFilename, "tempimport"); GrannyFileWrapper fileWrapper = new GrannyFileWrapper(file); fileWrapper.setNumMeshes(0); fileWrapper.setNumVertexDatas(0); fileWrapper.setNumSkeletons(0); fileWrapper.setNumTriTopologies(0); GrannyModelInfo modelInfo = loadModelInfo(sourceFilename); doBoneBindings(modelInfo); List <IGrannyFile> meshFileList = new List <IGrannyFile>(); foreach (GrannyMeshInfo meshInfo in modelInfo.meshBindings) { meshFileList.Add(writeMesh(meshInfo)); } IGrannyModel model = file.Models[0]; GrannyModelWrapper modelWrapper = new GrannyModelWrapper(model); modelWrapper.setNumMeshBindings(0); model.MeshBindings.Clear(); foreach (IGrannyFile meshFile in meshFileList) { doAppendMeshBinding(file, meshFile, 0); } fileWrapper.setNumMeshes(0); file.Meshes.Clear(); foreach (IGrannyMesh mesh in file.Models[0].MeshBindings) { file.AddMeshReference(mesh); } GrannySkeletonWrapper skeletonWrapper = new GrannySkeletonWrapper(file.Models[0].Skeleton); skeletonWrapper.writeSkeletonInfo(modelInfo.skeleton); string worldBoneName = modelInfo.skeleton.bones[0].name; modelWrapper.setName(worldBoneName); skeletonWrapper.setName(worldBoneName); fileWrapper.setFromArtToolInfo("Blender", 2, 0); float[] matrix = { 1f, 0f, 0f, 0f, 0f, 1f, 0f, -1f, 0f }; fileWrapper.setMatrix(matrix); CivNexusSixApplicationForm.SetExporterInfo(fileWrapper); fileWrapper.setFromFileName(sourceFilename); fileWrapper.addSkeletonPointer((int)skeletonWrapper.m_pkSkeleton); CivNexusSixApplicationForm.form.SaveAsAction(file, outputFilename, false); }
public unsafe static void importCN6(string sourceFilename, string outputFilename, GrannyContext grannyContext, int vertexFormat) { string templateFilename = getTemplateFilename(vertexFormat); IGrannyFile file = CivNexusSixApplicationForm.form.OpenFileAsTempFileCopy(templateFilename, "tempimport"); GrannyFileWrapper fileWrapper = new GrannyFileWrapper(file); fileWrapper.setNumMeshes(0); fileWrapper.setNumVertexDatas(0); fileWrapper.setNumSkeletons(0); fileWrapper.setNumTriTopologies(0); List <GrannyModelInfo> modelInfos = loadModelInfos(sourceFilename, vertexFormat); fileWrapper.setNumMeshes(0); fileWrapper.setNumModels(0); foreach (GrannyModelInfo modelInfo in modelInfos) { doBoneBindings(modelInfo); List <IGrannyFile> meshFileList = new List <IGrannyFile>(); foreach (GrannyMeshInfo meshInfo in modelInfo.meshBindings) { meshFileList.Add(writeMesh(meshInfo, templateFilename)); } IGrannyFile modelFile = CivNexusSixApplicationForm.form.OpenFileAsTempFileCopy(templateFilename, "tempimport"); IGrannyModel model = modelFile.Models[0]; GrannyModelWrapper modelWrapper = new GrannyModelWrapper(model); modelWrapper.setNumMeshBindings(0); model.MeshBindings.Clear(); GrannySkeletonWrapper skeletonWrapper = new GrannySkeletonWrapper(modelFile.Models[0].Skeleton); skeletonWrapper.writeSkeletonInfo(modelInfo.skeleton); string worldBoneName = modelInfo.skeleton.bones[0].name; string modelName = worldBoneName; foreach (GrannyBoneInfo bone in modelInfo.skeleton.bones) { if (!bone.name.Contains("ADJUSTMENT_BONE")) { modelName = bone.name; break; } } modelWrapper.setName(modelName); skeletonWrapper.setName(modelName); foreach (IGrannyFile meshFile in meshFileList) { doAppendMeshBinding(modelFile, meshFile, 0); } foreach (IGrannyMesh mesh in model.MeshBindings) { file.AddMeshReference(mesh); } file.AddModelReference(model); CivNexusSixApplicationForm.form.RefreshAppData(); } fileWrapper.setNumModels(modelInfos.Count()); fileWrapper.setFromArtToolInfo("Blender", 2, 0); float[] matrix = { 1f, 0f, 0f, 0f, 0f, 1f, 0f, -1f, 0f }; fileWrapper.setMatrix(matrix); CivNexusSixApplicationForm.SetExporterInfo(fileWrapper); fileWrapper.setFromFileName(sourceFilename); createAndBindMaterials(outputFilename, file, modelInfos); }
public static void exportNB2Model(IGrannyFile grannyFile, int modelId) { string fileExtension = ".nb2"; string outputFilename = ""; string numberFormat = "f6"; if (grannyFile.Models.Count > 1) { outputFilename = grannyFile.Filename.Replace(".fgx", "_model" + modelId + fileExtension); outputFilename = outputFilename.Replace(".FGX", "_model" + modelId + fileExtension); } else { outputFilename = grannyFile.Filename.Replace(".fgx", fileExtension); outputFilename = outputFilename.Replace(".FGX", fileExtension); } StreamWriter outputWriter = new StreamWriter(new FileStream(outputFilename, FileMode.Create)); IGrannyModel model = grannyFile.Models[modelId]; IGrannySkeleton skeleton = model.Skeleton; GrannySkeletonWrapper skeletonWrapper = new GrannySkeletonWrapper(skeleton); skeletonWrapper.readSkeletonInfo(); // Lookup so we can identify the meshes belonging the current model in the list of file meshes Dictionary <int, int> meshBindingToMesh = new Dictionary <int, int>(); HashSet <string> distinctMeshNames = new HashSet <string>(); for (int i = 0; i < model.MeshBindings.Count; i++) { for (int j = 0; j < grannyFile.Meshes.Count; j++) { GrannyMeshWrapper modelMesh = new GrannyMeshWrapper(model.MeshBindings[i]); IGrannyMesh fileMesh = grannyFile.Meshes[j]; if (modelMesh.meshEqual(fileMesh)) { meshBindingToMesh.Add(i, j); } } distinctMeshNames.Add(model.MeshBindings[i].Name); } // Used to give meshes distinct names where we have multiple meshes with the same name in our source gr2 Dictionary <string, int> meshNameCount = new Dictionary <string, int>(); foreach (string meshName in distinctMeshNames) { meshNameCount.Add(meshName, 0); } List <GrannyMeshInfo> grannyMeshInfos = new List <GrannyMeshInfo>(); for (int i = 0; i < model.MeshBindings.Count; i++) { GrannyMeshWrapper meshWrapper = new GrannyMeshWrapper(model.MeshBindings[i]); grannyMeshInfos.Add(meshWrapper.getMeshInfo()); } BiLookup <int, string> boneLookup = new BiLookup <int, string>(); for (int i = 0; i < skeleton.Bones.Count; i++) { boneLookup.Add(i, skeleton.Bones[i].Name); } Dictionary <string, double[]> boneNameToPositionMap = new Dictionary <string, double[]>(); foreach (IGrannyBone bone in skeleton.Bones) { double[] bonePosition = getBoneWorldPosition(bone); boneNameToPositionMap.Add(bone.Name, bonePosition); } outputWriter.WriteLine("// Nexus Buddy NB2 - Exported from Nexus Buddy 2"); outputWriter.WriteLine("Frames: 30"); outputWriter.WriteLine("Frame: 1"); // Write Meshes outputWriter.WriteLine("Meshes: " + model.MeshBindings.Count); for (int mi = 0; mi < grannyMeshInfos.Count; mi++) { GrannyMeshInfo grannyMeshInfo = grannyMeshInfos[mi]; string meshName = model.MeshBindings[mi].Name; meshNameCount[meshName]++; if (meshNameCount[meshName] > 1) { meshName += meshNameCount[meshName]; } outputWriter.WriteLine("\"" + meshName + "\" 0 0"); // Write Vertices outputWriter.WriteLine(grannyMeshInfo.vertices.Count); for (int vi = 0; vi < grannyMeshInfo.vertices.Count; vi++) { GrannyVertexInfo vertex = grannyMeshInfo.vertices[vi]; string boneName0 = grannyMeshInfo.boneBindings[vertex.boneIndices[0]]; float boneWeight0 = (float)vertex.boneWeights[0] / 255; int boneId0 = getBoneIdForBoneName(boneLookup, boneNameToPositionMap, boneName0, boneWeight0, vertex.position); string boneName1 = grannyMeshInfo.boneBindings[vertex.boneIndices[1]]; float boneWeight1 = (float)vertex.boneWeights[1] / 255; int boneId1 = getBoneIdForBoneName(boneLookup, boneNameToPositionMap, boneName1, boneWeight1, vertex.position); string boneName2 = grannyMeshInfo.boneBindings[vertex.boneIndices[2]]; float boneWeight2 = (float)vertex.boneWeights[2] / 255; int boneId2 = getBoneIdForBoneName(boneLookup, boneNameToPositionMap, boneName2, boneWeight2, vertex.position); string boneName3 = grannyMeshInfo.boneBindings[vertex.boneIndices[3]]; float boneWeight3 = (float)vertex.boneWeights[3] / 255; int boneId3 = getBoneIdForBoneName(boneLookup, boneNameToPositionMap, boneName3, boneWeight3, vertex.position); outputWriter.WriteLine("0 " + vertex.position[0].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.position[1].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.position[2].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.uv[0].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.uv[1].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + boneId0 + " " + boneWeight0.ToString(numberFormat, CultureInfo.InvariantCulture) + " " + boneId1 + " " + boneWeight1.ToString(numberFormat, CultureInfo.InvariantCulture) + " " + boneId2 + " " + boneWeight2.ToString(numberFormat, CultureInfo.InvariantCulture) + " " + boneId3 + " " + boneWeight3.ToString(numberFormat, CultureInfo.InvariantCulture) ); } // Write Normals outputWriter.WriteLine(grannyMeshInfo.vertices.Count); for (int ni = 0; ni < grannyMeshInfo.vertices.Count; ni++) { GrannyVertexInfo vertex = grannyMeshInfo.vertices[ni]; outputWriter.WriteLine(vertex.normal[0].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.normal[1].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + vertex.normal[2].ToString(numberFormat, CultureInfo.InvariantCulture)); } // Write Triangles outputWriter.WriteLine(grannyMeshInfo.triangles.Count); for (int ti = 0; ti < grannyMeshInfo.triangles.Count; ti++) { int[] triangle = grannyMeshInfo.triangles[ti]; outputWriter.WriteLine("0 " + triangle[0] + " " + triangle[1] + " " + triangle[2] + " " + triangle[0] + " " + triangle[1] + " " + triangle[2] + " 1"); } } // Write Material outputWriter.WriteLine("Materials: 1"); outputWriter.WriteLine("\"Material_0\""); outputWriter.WriteLine("0.200000 0.200000 0.200000 1.000000"); outputWriter.WriteLine("0.800000 0.800000 0.800000 1.000000"); outputWriter.WriteLine("0.000000 0.000000 0.000000 1.000000"); outputWriter.WriteLine("0.000000 0.000000 0.000000 1.000000"); outputWriter.WriteLine("0.000000"); outputWriter.WriteLine("1.000000"); outputWriter.WriteLine("\"Material_0\""); outputWriter.WriteLine("\"\""); // Write Bones outputWriter.WriteLine("Bones: " + skeleton.Bones.Count); for (int bi = 0; bi < skeleton.Bones.Count; bi++) { IGrannyBone bone = skeleton.Bones[bi]; string boneName = bone.Name; outputWriter.WriteLine("\"" + boneName + "\""); if (bone.ParentIndex == -1) { outputWriter.WriteLine("\"\""); } else { string parentBoneName = skeleton.Bones[bone.ParentIndex].Name; outputWriter.WriteLine("\"" + parentBoneName + "\""); } IGrannyTransform transform = bone.LocalTransform; float[] orientation = transform.Orientation; float[] position = transform.Position; outputWriter.WriteLine("0 " + position[0].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + position[1].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + position[2].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + orientation[0].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + orientation[1].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + orientation[2].ToString(numberFormat, CultureInfo.InvariantCulture) + " " + orientation[3].ToString(numberFormat, CultureInfo.InvariantCulture)); // number of position keys outputWriter.WriteLine("0"); // number of rotation keys outputWriter.WriteLine("0"); } outputWriter.Close(); }