public static void ReplaceMaterial(odfParser parser, ImportedMaterial material) { odfMaterial mat = CreateMaterial(material, null); bool found = false; for (int i = 0; i < parser.MaterialSection.Count; i++) { if (parser.MaterialSection[i].Name == material.Name) { odfMaterial original = parser.MaterialSection[i]; mat.Id = original.Id; CopyUnknown(original, mat); parser.MaterialSection.RemoveChild(i); parser.MaterialSection.InsertChild(i, mat); found = true; break; } } if (!found) { mat.Id = parser.GetNewID(typeof(odfMaterial)); CreateUnknown(mat); parser.MaterialSection.AddChild(mat); } }
public static void ReplaceMaterial(remParser parser, ImportedMaterial material) { remMaterial mat = CreateMaterial(material); bool found = false; for (int i = 0; i < parser.MATC.Count; i++) { if (parser.MATC[i].name == material.Name) { remMaterial original = parser.MATC[i]; CopyUnknown(original, mat); parser.MATC.RemoveChild(i); parser.MATC.InsertChild(i, mat); found = true; break; } } if (!found) { CreateUnknown(mat); parser.MATC.AddChild(mat); } }
public static xxMaterial CreateMaterial(ImportedMaterial material) { xxMaterial xxMat = new xxMaterial(); xxMat.Name = material.Name; xxMat.Diffuse = material.Diffuse; xxMat.Ambient = material.Ambient; xxMat.Specular = material.Specular; xxMat.Emissive = material.Emissive; xxMat.Power = material.Power; xxMat.Textures = new xxMaterialTexture[4]; for (int i = 0; i < xxMat.Textures.Length; i++) { xxMat.Textures[i] = new xxMaterialTexture(); if ((material.Textures != null) && (i < material.Textures.Length) && (material.Textures[i] != null)) { xxMat.Textures[i].Name = material.Textures[i]; } else { xxMat.Textures[i].Name = String.Empty; } } return xxMat; }
public static odfMaterial CreateMaterial(ImportedMaterial impMat, ObjectID id) { odfMaterial odfMat = new odfMaterial(new ObjectName(impMat.Name, null), id); odfMat.Diffuse = impMat.Diffuse; odfMat.Ambient = impMat.Ambient; odfMat.Specular = impMat.Specular; odfMat.Emissive = impMat.Emissive; odfMat.SpecularPower = impMat.Power; return(odfMat); }
public static remMaterial CreateMaterial(ImportedMaterial impMat) { remMaterial remMat = new remMaterial(); remMat.name = new remId(impMat.Name); remMat.diffuse = new Color3(impMat.Diffuse.Red, impMat.Diffuse.Green, impMat.Diffuse.Blue); remMat.ambient = new Color3(impMat.Ambient.Red, impMat.Ambient.Green, impMat.Ambient.Blue); remMat.specular = new Color3(impMat.Specular.Red, impMat.Specular.Green, impMat.Specular.Blue); remMat.emissive = new Color3(impMat.Emissive.Red, impMat.Emissive.Green, impMat.Emissive.Blue); remMat.specularPower = (int)impMat.Power; remMat.texture = new remId(impMat.Textures[0]); return remMat; }
public static remMaterial CreateMaterial(ImportedMaterial impMat) { remMaterial remMat = new remMaterial(); remMat.name = new remId(impMat.Name); remMat.diffuse = new Color3(impMat.Diffuse.Red, impMat.Diffuse.Green, impMat.Diffuse.Blue); remMat.ambient = new Color3(impMat.Ambient.Red, impMat.Ambient.Green, impMat.Ambient.Blue); remMat.specular = new Color3(impMat.Specular.Red, impMat.Specular.Green, impMat.Specular.Blue); remMat.emissive = new Color3(impMat.Emissive.Red, impMat.Emissive.Green, impMat.Emissive.Blue); remMat.specularPower = (int)impMat.Power; remMat.texture = new remId(impMat.Textures[0]); return(remMat); }
public static void ReplaceMaterial(UnityParser parser, ImportedMaterial material) { for (int i = 0; i < parser.Cabinet.Components.Count; i++) { Component comp = parser.Cabinet.Components[i]; if (comp.classID1 == UnityClassID.Material) { Material mat = parser.Cabinet.LoadComponent(comp.pathID); if (mat.m_Name == material.Name) { ReplaceMaterial(mat, material); return; } } } throw new Exception("Replacing a material currently requires an existing material with the same name"); }
private ImportedMaterial ImportMaterial(Document.Material material, Dictionary<string, ImportedTexture> textureIdDic) { ImportedMaterial mat = null; try { Document.Effect effect = (Document.Effect)colladaDoc.dic[material.instanceEffect.Fragment]; if (effect == null) { throw new Exception("Couldn't find effect " + material.instanceEffect.Fragment); } Document.ProfileCOMMON profile = null; for (int i = 0; i < effect.profiles.Count; i++) { if (effect.profiles[i] is Document.ProfileCOMMON) { profile = (Document.ProfileCOMMON)effect.profiles[i]; break; } } if (profile == null) { throw new Exception("Couldn't find profile_COMMON for " + effect.id); } if (!(profile.technique.shader is Document.Phong)) { throw new Exception(effect.id + " isn't phong shading"); } ImportedMaterial matInfo = new ImportedMaterial(); matInfo.Name = DecodeName(material.id); matInfo.Textures = new string[4] { String.Empty, String.Empty, String.Empty, String.Empty }; Document.Phong phong = (Document.Phong)profile.technique.shader; if (phong.diffuse is Document.Texture) { matInfo.Diffuse = new Color4(1.0f, 1.0f, 1.0f, 1.0f); Document.Texture docTex = (Document.Texture)phong.diffuse; ImportedTexture diffuseTex; if (textureIdDic.TryGetValue(docTex.texture, out diffuseTex)) { matInfo.Textures[0] = diffuseTex.Name; } } else { matInfo.Diffuse = ((Document.Color)phong.diffuse).floats.ToColor4(); } if (phong.reflective is Document.Texture) { Document.Texture docTex = (Document.Texture)phong.reflective; ImportedTexture reflectiveTex; if (textureIdDic.TryGetValue(docTex.texture, out reflectiveTex)) { matInfo.Textures[1] = reflectiveTex.Name; } } matInfo.Ambient = ((Document.Color)phong.ambient).floats.ToColor4(); matInfo.Emissive = ((Document.Color)phong.emission).floats.ToColor4(); matInfo.Specular = ((Document.Color)phong.specular).floats.ToColor4(); matInfo.Power = ((Document.Float)phong.shininess).theFloat; } catch (Exception e) { Report.ReportLog("Error importing material " + material.id + ": " + e.Message); } return mat; }
public void MergeMaterial(ImportedMaterial material) { Operations.ReplaceMaterial(Parser, material); Changed = true; }
private void ConvertMaterial(Material mat) { ImportedMaterial iMat = ImportedHelpers.FindMaterial(mat.m_Name, MaterialList); if (iMat != null) { return; } iMat = new ImportedMaterial(); iMat.Name = mat.m_Name; foreach (var col in mat.m_SavedProperties.m_Colors) { switch (col.Key.name) { case "_Color": iMat.Diffuse = col.Value; break; case "_SColor": iMat.Ambient = col.Value; break; case "_ReflectColor": iMat.Emissive = col.Value; break; case "_SpecColor": iMat.Specular = col.Value; break; case "_RimColor": case "_OutlineColor": case "_ShadowColor": break; } } foreach (var flt in mat.m_SavedProperties.m_Floats) { switch (flt.Key.name) { case "_Shininess": iMat.Power = flt.Value; break; case "_RimPower": case "_Outline": break; } } iMat.Textures = new string[4]; int numTex = mat.m_SavedProperties.m_TexEnvs.Count > 4 ? 4 : mat.m_SavedProperties.m_TexEnvs.Count; for (int i = 0; i < numTex; i++) { var tex = mat.m_SavedProperties.m_TexEnvs[i]; Texture2D tex2D = tex.Value.m_Texture.instance; if (tex2D != null) { iMat.Textures[i] = tex2D.m_Name + "-" + tex2D.m_TextureFormat + (tex2D.m_TextureFormat == TextureFormat.DXT1 || tex2D.m_TextureFormat == TextureFormat.DXT5 ? ".dds" : ".tga"); ConvertTexture2D(tex2D, iMat.Textures[i]); } } MaterialList.Add(iMat); }
public static void ReplaceMesh(odfFrame frame, odfParser parser, WorkspaceMesh mesh, List <ImportedMaterial> materials, List <ImportedTexture> textures, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod) { Matrix transform = Matrix.Identity; odfFrame transformFrame = frame; while (transformFrame != null) { transform *= transformFrame.Matrix; transformFrame = transformFrame.Parent as odfFrame; } transform.Invert(); string[] materialNames; int[] indices; bool[] worldCoords; bool[] replaceSubmeshesOption; odfMesh newMesh = CreateMesh(mesh, parser.MeshSection._FormatType, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption); odfMesh frameMesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection); if (frameMesh != null) { if (parser.UsedIDs == null) // prevent misleading error message { parser.CollectObjectIDs(); } newMesh.Id = frameMesh.Id; newMesh.Name = frameMesh.Name; parser.MeshSection.InsertChild(parser.MeshSection.IndexOf(frameMesh), newMesh); } else { newMesh.Id = parser.GetNewID(typeof(odfMesh)); frame.MeshId = newMesh.Id; parser.MeshSection.AddChild(newMesh); } Dictionary <ObjectID, ObjectID> submeshIDtranslation = new Dictionary <ObjectID, ObjectID>(newMesh.Count); odfSubmesh[] replaceSubmeshes = frameMesh != null ? new odfSubmesh[frameMesh.Count] : null; List <odfSubmesh> addSubmeshes = new List <odfSubmesh>(newMesh.Count); for (int i = 0; i < newMesh.Count; i++) { ObjectID[] texIDs = new ObjectID[4] { ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID }; odfMaterial mat = odf.FindMaterialInfo(materialNames[i], parser.MaterialSection); if (materials != null && mat == null) { ImportedMaterial impMat = ImportedHelpers.FindMaterial(materialNames[i], materials); if (impMat != null) { mat = CreateMaterial(impMat, parser.GetNewID(typeof(odfMaterial))); parser.MaterialSection.AddChild(mat); for (int j = 0; j < impMat.Textures.Length; j++) { string texName = impMat.Textures[j]; odfTexture tex = odf.FindTextureInfo(texName, parser.TextureSection); if (tex == null) { ImportedTexture impTex = ImportedHelpers.FindTexture(texName, textures); if (impTex != null) { tex = CreateTexture(impTex, parser.GetNewID(typeof(odfTexture)), parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath)); parser.TextureSection.AddChild(tex); texIDs[j] = tex.Id; } } else { texIDs[j] = tex.Id; } } } } odfSubmesh newSubmesh = newMesh[i]; newSubmesh.Id = parser.GetNewID(typeof(odfSubmesh)); newSubmesh.MaterialId = mat != null ? mat.Id : ObjectID.INVALID; newSubmesh.TextureIds = texIDs; List <odfVertex> newVertexList = newSubmesh.VertexList; if (worldCoords[i]) { for (int j = 0; j < newVertexList.Count; j++) { newVertexList[j].Position = Vector3.TransformCoordinate(newVertexList[j].Position, transform); } } odfSubmesh baseSubmesh = null; odfBoneList newBones = null; int newBonesIdx = -1; int idx = indices[i]; if ((frameMesh != null) && (idx >= 0) && (idx < frameMesh.Count)) { baseSubmesh = frameMesh[idx]; submeshIDtranslation.Add(newSubmesh.Id, baseSubmesh.Id); for (int j = 0; j < baseSubmesh.TextureIds.Length; j++) { ObjectID texID = baseSubmesh.TextureIds[j]; newSubmesh.TextureIds[j] = texID; } newSubmesh.Name = new ObjectName(baseSubmesh.Name.Name, baseSubmesh.Name.Info); CopyUnknowns(baseSubmesh, newSubmesh, parser.MeshSection._FormatType); if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear)) { odfBoneList baseBones = odf.FindBoneList(baseSubmesh.Id, parser.EnvelopeSection); if (baseBones != null) { newBones = baseBones.Clone(); newBones.Id = ObjectID.INVALID; // parser.GetNewID(typeof(odfBoneList)); newBones.SubmeshId = newSubmesh.Id; newBonesIdx = parser.EnvelopeSection.IndexOf(baseBones); } } else if (bonesMethod == CopyMeshMethod.Replace) { newBones = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame); newBonesIdx = parser.EnvelopeSection.Count; } } else { CreateUnknowns(newSubmesh, parser.MeshSection._FormatType); newBones = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame); newBonesIdx = parser.EnvelopeSection.Count; } if (newBones != null) { parser.EnvelopeSection.InsertChild(newBonesIdx, newBones); } if (baseSubmesh != null) { if (normalsMethod == CopyMeshMethod.CopyOrder) { odf.CopyNormalsOrder(baseSubmesh.VertexList, newSubmesh.VertexList); } else if (normalsMethod == CopyMeshMethod.CopyNear) { odf.CopyNormalsNear(baseSubmesh.VertexList, newSubmesh.VertexList); } if (bonesMethod == CopyMeshMethod.CopyOrder) { odf.CopyBonesOrder(baseSubmesh.VertexList, newSubmesh.VertexList, newBones); } else if (bonesMethod == CopyMeshMethod.CopyNear) { odf.CopyBonesNear(baseSubmesh.VertexList, newSubmesh.VertexList, newBones); } } if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i]) { replaceSubmeshes[idx] = newSubmesh; } else { addSubmeshes.Add(newSubmesh); } } if ((frameMesh != null) && merge) { newMesh.Clear(); newMesh.Capacity = replaceSubmeshes.Length + addSubmeshes.Count; for (int i = 0, submeshesRemoved = 0; i < replaceSubmeshes.Length; i++) { if (replaceSubmeshes[i] == null) { odfSubmesh newSubmesh = frameMesh[i - submeshesRemoved++]; frameMesh.RemoveChild(newSubmesh); // save the bone list from being deleted in RemoveMesh newMesh.AddChild(newSubmesh); } else { newMesh.AddChild(replaceSubmeshes[i]); } } newMesh.AddRange(addSubmeshes); } if (frameMesh != null) { RemoveMesh(parser, frameMesh, frame, false); parser.UsedIDs.Add((int)newMesh.Id, typeof(odfMesh)); frame.MeshId = newMesh.Id; List <ObjectID> removeKeyList = new List <ObjectID>(); foreach (odfSubmesh submesh in newMesh) { ObjectID newSubmeshID = submesh.Id; ObjectID baseSubmeshID; if (submeshIDtranslation.TryGetValue(newSubmeshID, out baseSubmeshID)) { if (odf.FindBoneList(baseSubmeshID, parser.EnvelopeSection) == null) { odfBoneList boneList = odf.FindBoneList(newSubmeshID, parser.EnvelopeSection); if (boneList != null) { boneList.SubmeshId = baseSubmeshID; } submesh.Id = baseSubmeshID; parser.UsedIDs.Remove((int)newSubmeshID); } foreach (KeyValuePair <ObjectID, ObjectID> pair in submeshIDtranslation) { if (pair.Value == baseSubmeshID) { removeKeyList.Add(pair.Key); } } foreach (ObjectID removeId in removeKeyList) { submeshIDtranslation.Remove(removeId); } removeKeyList.Clear(); } } } }
private void ConvertMeshes(List<odfMesh> meshes, odfParser parser) { MeshList = new List<ImportedMesh>(meshes.Count); MaterialList = new List<ImportedMaterial>(meshes.Count); TextureList = new List<ImportedTexture>(parser.TextureSection != null ? parser.TextureSection.Count : 0); foreach (odfMesh mesh in meshes) { ImportedMesh iMesh = new ImportedMesh(); MeshList.Add(iMesh); iMesh.Name = odf.FindMeshFrame(mesh.Id, parser.FrameSection.RootFrame).Name; iMesh.BoneList = new List<ImportedBone>(); Dictionary<ObjectID, byte> boneDic = new Dictionary<ObjectID, byte>(); iMesh.SubmeshList = new List<ImportedSubmesh>(mesh.Count); foreach (odfSubmesh submesh in mesh) { ImportedSubmesh iSubmesh = new ImportedSubmesh(); iMesh.SubmeshList.Add(iSubmesh); odfMaterial mat = odf.FindMaterialInfo(submesh.MaterialId, parser.MaterialSection); if (mat != null) { iSubmesh.Material = mat.Name; ImportedMaterial iMat = ImportedHelpers.FindMaterial(iSubmesh.Material, MaterialList); if (iMat == null) { iMat = new ImportedMaterial(); MaterialList.Add(iMat); iMat.Name = iSubmesh.Material; iMat.Diffuse = mat.Diffuse; iMat.Ambient = mat.Ambient; iMat.Specular = mat.Specular; iMat.Emissive = mat.Emissive; iMat.Power = mat.SpecularPower; iMat.Textures = new string[4]; for (int i = 0; i < 4; i++) { if (submesh.TextureIds[i] != ObjectID.INVALID) { odfTexture tex = odf.FindTextureInfo(submesh.TextureIds[i], parser.TextureSection); iMat.Textures[i] = tex.Name; if (ImportedHelpers.FindTexture(iMat.Textures[i], TextureList) == null) { try { odfTextureFile texFile = new odfTextureFile(iMat.Textures[i], Path.GetDirectoryName(parser.ODFPath) + @"\" + iMat.Textures[i]); MemoryStream memStream; int filesize = 0; using (BinaryReader reader = texFile.DecryptFile(ref filesize)) { memStream = new MemoryStream(reader.ReadBytes(filesize)); } ImportedTexture iTex = new ImportedTexture(memStream, iMat.Textures[i]); TextureList.Add(iTex); } catch { Report.ReportLog("cant read texture " + iMat.Textures[i]); } } } else { iMat.Textures[i] = String.Empty; } } } } List<Tuple<byte, float>>[] skin = new List<Tuple<byte, float>>[submesh.NumVertices]; for (int i = 0; i < submesh.NumVertices; i++) { skin[i] = new List<Tuple<byte, float>>(4); } odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); if (boneList != null) { if (iMesh.BoneList.Capacity < boneList.Count) { iMesh.BoneList.Capacity += boneList.Count; } foreach (odfBone bone in boneList) { byte idx; if (!boneDic.TryGetValue(bone.FrameId, out idx)) { ImportedBone iBone = new ImportedBone(); iMesh.BoneList.Add(iBone); iBone.Name = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; iBone.Matrix = bone.Matrix; boneDic.Add(bone.FrameId, idx = (byte)boneDic.Count); } for (int i = 0; i < bone.NumberIndices; i++) { skin[bone.VertexIndexArray[i]].Add(new Tuple<byte, float>(idx, bone.WeightArray[i])); } } } iSubmesh.VertexList = new List<ImportedVertex>(submesh.NumVertices); for (int i = 0; i < submesh.NumVertices; i++) { odfVertex vert = submesh.VertexList[i]; ImportedVertex iVert = new ImportedVertex(); iSubmesh.VertexList.Add(iVert); iVert.Position = vert.Position; iVert.Normal = vert.Normal; iVert.UV = new float[] { vert.UV[0], vert.UV[1] }; iVert.BoneIndices = new byte[4]; iVert.Weights = new float[4]; for (int j = 0; j < 4; j++) { if (j < skin[i].Count) { Tuple<byte, float> vertIdxWeight = skin[i][j]; iVert.BoneIndices[j] = vertIdxWeight.Item1; iVert.Weights[j] = vertIdxWeight.Item2; } else { iVert.BoneIndices[j] = 0xFF; } } } iSubmesh.FaceList = new List<ImportedFace>(submesh.NumVertexIndices / 3); foreach (odfFace face in submesh.FaceList) { ImportedFace iFace = new ImportedFace(); iSubmesh.FaceList.Add(iFace); iFace.VertexIndices = new int[3]; for (int i = 0; i < 3; i++) { iFace.VertexIndices[i] = face.VertexIndices[i]; } } } } }
public void MergeMaterial(ImportedMaterial mat) { Material dest = Operations.FindMaterial(Materials, mat.Name); Operations.ReplaceMaterial(dest, mat); if (Parser.file.Bundle != null) { Parser.file.Bundle.RegisterForUpdate(dest); } Changed = true; }
private void ConvertMeshes(List <remMesh> meshes, remParser parser) { MeshList = new List <ImportedMesh>(meshes.Count); MaterialList = new List <ImportedMaterial>(meshes.Count); TextureList = new List <ImportedTexture>(parser.MATC.Count); foreach (remMesh mesh in meshes) { ImportedMesh iMesh = new ImportedMesh(); MeshList.Add(iMesh); iMesh.BoneList = new List <ImportedBone>(); Dictionary <remId, byte> boneDic = new Dictionary <remId, byte>(); remSkin skin = rem.FindSkin(mesh.name, parser.SKIC); rem.Mesh convertedMesh = new rem.Mesh(mesh, skin); iMesh.SubmeshList = new List <ImportedSubmesh>(convertedMesh.Count); remBone meshFrame = rem.FindFrame(mesh.frame, parser.BONC.rootFrame); ImportedFrame iFrame = ImportedHelpers.FindFrame(mesh.frame, FrameList[0]); float s = (float)Math.Round(Math.Abs(meshFrame.matrix.M11), 5); iFrame.Name = iMesh.Name = mesh.name + (s != 1f ? "(Scale=" + s.ToString() + ")" : String.Empty); foreach (rem.Submesh submesh in convertedMesh) { ImportedSubmesh iSubmesh = new ImportedSubmesh(); iMesh.SubmeshList.Add(iSubmesh); remMaterial mat = rem.FindMaterial(submesh.MaterialName, parser.MATC); if (mat != null) { iSubmesh.Material = mat.name; ImportedMaterial iMat = ImportedHelpers.FindMaterial(iSubmesh.Material, MaterialList); if (iMat == null) { iMat = new ImportedMaterial(); MaterialList.Add(iMat); iMat.Name = iSubmesh.Material; iMat.Diffuse = new Color4(mat.diffuse); iMat.Ambient = new Color4(mat.ambient); iMat.Specular = new Color4(mat.specular); iMat.Emissive = new Color4(mat.emissive); iMat.Power = mat.specularPower; iMat.Textures = new string[4] { String.Empty, String.Empty, String.Empty, String.Empty }; if (mat.texture != null) { iMat.Textures[0] = mat.texture; if (ImportedHelpers.FindTexture(iMat.Textures[0], TextureList) == null) { try { ImportedTexture iTex = rem.ImportedTexture(mat.texture, parser.RemPath, true); TextureList.Add(iTex); } catch { Report.ReportLog("cant read texture " + iMat.Textures[0]); } } } } } List <Tuple <byte, float> >[] iSkin = new List <Tuple <byte, float> > [submesh.numVertices]; for (int i = 0; i < submesh.numVertices; i++) { iSkin[i] = new List <Tuple <byte, float> >(4); } List <remBoneWeights> boneList = submesh.BoneList; if (boneList != null) { if (iMesh.BoneList.Capacity < boneList.Count) { iMesh.BoneList.Capacity += boneList.Count; } foreach (remBoneWeights boneWeights in boneList) { byte idx; if (!boneDic.TryGetValue(boneWeights.bone, out idx)) { ImportedBone iBone = new ImportedBone(); iMesh.BoneList.Add(iBone); iBone.Name = boneWeights.bone; Vector3 scale, translate; Quaternion rotate; meshFrame.matrix.Decompose(out scale, out rotate, out translate); scale.X = Math.Abs(scale.X); scale.Y = Math.Abs(scale.Y); scale.Z = Math.Abs(scale.Z); iBone.Matrix = Matrix.Scaling(1f, 1f, -1f) * Matrix.Invert(meshFrame.matrix) * Matrix.Scaling(scale) * boneWeights.matrix; boneDic.Add(boneWeights.bone, idx = (byte)boneDic.Count); } for (int i = 0; i < boneWeights.numVertIdxWts; i++) { iSkin[boneWeights.vertexIndices[i]].Add(new Tuple <byte, float>(idx, boneWeights.vertexWeights[i])); } } } iSubmesh.VertexList = new List <ImportedVertex>(submesh.numVertices); for (int i = 0; i < submesh.numVertices; i++) { remVertex vert = submesh.VertexList[i]; ImportedVertex iVert = new ImportedVertex(); iSubmesh.VertexList.Add(iVert); iVert.Position = new Vector3(vert.Position.X, vert.Position.Z, -vert.Position.Y); iVert.Normal = new Vector3(vert.Normal.X, vert.Normal.Z, -vert.Normal.Y); iVert.UV = new float[] { vert.UV[0], vert.UV[1] }; iVert.BoneIndices = new byte[4]; iVert.Weights = new float[4]; for (int j = 0; j < 4; j++) { if (j < iSkin[i].Count) { Tuple <byte, float> vertIdxWeight = iSkin[i][j]; iVert.BoneIndices[j] = vertIdxWeight.Item1; iVert.Weights[j] = vertIdxWeight.Item2; } else { iVert.BoneIndices[j] = 0xFF; } } } iSubmesh.FaceList = rem.ImportedFaceList(submesh.FaceList); } } }
public void MergeMaterial(ImportedMaterial mat) { rem.ReplaceMaterial(Parser, mat); InitTextures(false, false); }
public static void ReplaceMaterial(Material mat, ImportedMaterial material) { if (mat == null) { throw new Exception("Replacing a material currently requires an existing material with the same name"); } for (int i = 0; i < mat.m_SavedProperties.m_Colors.Count; i++) { var col = mat.m_SavedProperties.m_Colors[i]; Color4 att; switch (col.Key.name) { case "_Color": att = material.Diffuse; break; case "_SColor": att = material.Ambient; break; case "_ReflectColor": att = material.Emissive; break; case "_SpecColor": att = material.Specular; break; case "_RimColor": case "_OutlineColor": case "_ShadowColor": default: continue; } mat.m_SavedProperties.m_Colors.RemoveAt(i); col = new KeyValuePair <FastPropertyName, Color4>(col.Key, att); mat.m_SavedProperties.m_Colors.Insert(i, col); } for (int i = 0; i < mat.m_SavedProperties.m_Floats.Count; i++) { var flt = mat.m_SavedProperties.m_Floats[i]; float att; switch (flt.Key.name) { case "_Shininess": att = material.Power; break; case "_RimPower": case "_Outline": default: continue; } mat.m_SavedProperties.m_Floats.RemoveAt(i); flt = new KeyValuePair <FastPropertyName, float>(flt.Key, att); mat.m_SavedProperties.m_Floats.Insert(i, flt); } for (int i = 0; i < material.Textures.Length && i < mat.m_SavedProperties.m_TexEnvs.Count; i++) { try { Texture2D tex = null; if (material.Textures[i] != string.Empty) { tex = mat.file.Parser.GetTexture(material.Textures[i]); } if (mat.m_SavedProperties.m_TexEnvs[i].Value.m_Texture.asset != tex) { mat.m_SavedProperties.m_TexEnvs[i].Value.m_Texture = new PPtr <Texture2D>(tex); } } catch (Exception e) { Report.ReportLog(e.ToString()); } } }
public static void ReplaceMaterial(Material mat, ImportedMaterial material) { if (mat == null) { throw new Exception("Replacing a material currently requires an existing material with the same name"); } for (int i = 0; i < mat.m_SavedProperties.m_Colors.Count; i++) { var col = mat.m_SavedProperties.m_Colors[i]; Color4 att; switch (col.Key.name) { case "_Color": att = material.Diffuse; break; case "_SColor": att = material.Ambient; break; case "_ReflectColor": att = material.Emissive; break; case "_SpecColor": att = material.Specular; break; case "_RimColor": case "_OutlineColor": case "_ShadowColor": default: continue; } mat.m_SavedProperties.m_Colors.RemoveAt(i); col = new KeyValuePair<FastPropertyName, Color4>(col.Key, att); mat.m_SavedProperties.m_Colors.Insert(i, col); } for (int i = 0; i < mat.m_SavedProperties.m_Floats.Count; i++) { var flt = mat.m_SavedProperties.m_Floats[i]; float att; switch (flt.Key.name) { case "_Shininess": att = material.Power; break; case "_RimPower": case "_Outline": default: continue; } mat.m_SavedProperties.m_Floats.RemoveAt(i); flt = new KeyValuePair<FastPropertyName, float>(flt.Key, att); mat.m_SavedProperties.m_Floats.Insert(i, flt); } for (int i = 0; i < material.Textures.Length && i < mat.m_SavedProperties.m_TexEnvs.Count; i++) { try { Texture2D tex = null; if (material.Textures[i] != string.Empty) { tex = mat.file.Parser.GetTexture(material.Textures[i]); } if (mat.m_SavedProperties.m_TexEnvs[i].Value.m_Texture.asset != tex) { mat.m_SavedProperties.m_TexEnvs[i].Value.m_Texture = new PPtr<Texture2D>(tex); } } catch (Exception e) { Report.ReportLog(e.ToString()); } } }
public static void ReplaceMaterial(xxParser parser, ImportedMaterial material) { xxMaterial mat = xx.CreateMaterial(material); bool found = false; for (int i = 0; i < parser.MaterialList.Count; i++) { if (parser.MaterialList[i].Name == material.Name) { CopyUnknowns(parser.MaterialList[i], mat); parser.MaterialList.RemoveAt(i); parser.MaterialList.Insert(i, mat); found = true; break; } } if (!found) { CreateUnknowns(mat, parser.Format); parser.MaterialList.Add(mat); } }
public static odfMaterial CreateMaterial(ImportedMaterial impMat, ObjectID id) { odfMaterial odfMat = new odfMaterial(new ObjectName(impMat.Name, null), id); odfMat.Diffuse = impMat.Diffuse; odfMat.Ambient = impMat.Ambient; odfMat.Specular = impMat.Specular; odfMat.Emissive = impMat.Emissive; odfMat.SpecularPower = impMat.Power; return odfMat; }
private void ConvertMaterial(Material mat) { ImportedMaterial iMat = ImportedHelpers.FindMaterial(mat.m_Name, MaterialList); if (iMat != null) { return; } iMat = new ImportedMaterial(); iMat.Name = mat.m_Name; foreach (var col in mat.m_SavedProperties.m_Colors) { switch (col.Key.name) { case "_Color": iMat.Diffuse = col.Value; break; case "_SColor": iMat.Ambient = col.Value; break; case "_ReflectColor": iMat.Emissive = col.Value; break; case "_SpecColor": iMat.Specular = col.Value; break; case "_RimColor": case "_OutlineColor": case "_ShadowColor": break; } } foreach (var flt in mat.m_SavedProperties.m_Floats) { switch (flt.Key.name) { case "_Shininess": iMat.Power = flt.Value; break; case "_RimPower": case "_Outline": break; } } iMat.Textures = new string[4]; int numTex = mat.m_SavedProperties.m_TexEnvs.Count > 4 ? 4 : mat.m_SavedProperties.m_TexEnvs.Count; for (int i = 0; i < numTex; i++) { var tex = mat.m_SavedProperties.m_TexEnvs[i]; Texture2D tex2D = tex.Value.m_Texture.instance; if (tex2D != null) { iMat.Textures[i] = tex2D.m_Name + "-" + tex.Key.name + "-" + "offset(X" + tex.Value.m_Offset.X.ToFloatString() + "Y" + tex.Value.m_Offset.Y.ToFloatString() + ")-scale(X" + tex.Value.m_Scale.X.ToFloatString() + "Y" + tex.Value.m_Scale.Y.ToFloatString() + ")" + (tex2D.m_TextureFormat == TextureFormat.DXT1 || tex2D.m_TextureFormat == TextureFormat.DXT5 ? ".dds" : ".tga"); ConvertTexture2D(tex2D, iMat.Textures[i]); } } MaterialList.Add(iMat); }
public void MergeMaterial(ImportedMaterial mat) { xx.ReplaceMaterial(Parser, mat); Changed = true; }
private string[] ImportMaterials(Section section, DXFace[] faces) { LinkedListNode<object> node = section.data.First; int numMaterials = ConvertInt32(node.Value); node = node.Next; if (numMaterials != section.children.Count) { throw new Exception("number of materials doesn't match number of children"); } int numFaces = ConvertInt32(node.Value); node = node.Next; if (numFaces != faces.Length) { throw new Exception("number of faces doesn't match with material"); } for (int i = 0; i < numFaces; i++) { faces[i].materialIndex = ConvertInt32(node.Value); node = node.Next; } string[] materialNames = new string[numMaterials]; for (int i = 0; i < numMaterials; i++) { Section matSection = section.children[i]; if (matSection.type == "Material") { string texName = String.Empty; foreach (Section texSection in matSection.children) { if (texSection.type == "TextureFilename") { texName = ImportTexture(texSection); break; } else { Report.ReportLog("Warning: unexpected section " + matSection.type); } } if (matSection.name == null) { if (texName == String.Empty) { materialNames[i] = "no_name_" + noNameCount; } else { materialNames[i] = Path.GetFileNameWithoutExtension(texName) + "_" + noNameCount; } noNameCount++; } else { materialNames[i] = matSection.name; } if (matList.Add(materialNames[i])) { LinkedListNode<object> dataNode = section.children[i].data.First; ImportedMaterial matInfo = new ImportedMaterial(); matInfo.Name = materialNames[i]; matInfo.Diffuse = new Color4(1, 1, 1, 1); float[] ambient = new float[4]; for (int j = 0; j < ambient.Length; j++) { ambient[j] = ConvertFloat(dataNode.Value); dataNode = dataNode.Next; } matInfo.Ambient = new Color4(ambient[3], ambient[0], ambient[1], ambient[2]); matInfo.Power = ConvertFloat(dataNode.Value); dataNode = dataNode.Next; float[] specular = new float[4]; for (int j = 0; j < 3; j++) { specular[j] = ConvertFloat(dataNode.Value); dataNode = dataNode.Next; } specular[3] = 1; matInfo.Specular = new Color4(specular[3], specular[0], specular[1], specular[2]); float[] emissive = new float[4]; for (int j = 0; j < 3; j++) { emissive[j] = ConvertFloat(dataNode.Value); dataNode = dataNode.Next; } emissive[3] = 1; matInfo.Emissive = new Color4(emissive[3], emissive[0], emissive[1], emissive[2]); if (texName != String.Empty) { matInfo.Textures = new string[] { texName }; } MaterialList.Add(matInfo); } } else if (matSection.type == "ref") { if (matSection.name != null) { materialNames[i] = matSection.name; } } else { Report.ReportLog("Warning: unexpected section " + matSection.type); } } return materialNames; }
public void MergeMaterial(ImportedMaterial mat) { odf.ReplaceMaterial(Parser, mat); }
public void MergeMaterial(ImportedMaterial mat) { xx.ReplaceMaterial(Parser, mat); }
private void ConvertMeshes(List <odfMesh> meshes, odfParser parser) { MeshList = new List <ImportedMesh>(meshes.Count); MaterialList = new List <ImportedMaterial>(meshes.Count); TextureList = new List <ImportedTexture>(parser.TextureSection != null ? parser.TextureSection.Count : 0); foreach (odfMesh mesh in meshes) { ImportedMesh iMesh = new ImportedMesh(); MeshList.Add(iMesh); iMesh.Name = odf.FindMeshFrame(mesh.Id, parser.FrameSection.RootFrame).Name; iMesh.BoneList = new List <ImportedBone>(); Dictionary <ObjectID, byte> boneDic = new Dictionary <ObjectID, byte>(); iMesh.SubmeshList = new List <ImportedSubmesh>(mesh.Count); foreach (odfSubmesh submesh in mesh) { ImportedSubmesh iSubmesh = new ImportedSubmesh(); iMesh.SubmeshList.Add(iSubmesh); odfMaterial mat = odf.FindMaterialInfo(submesh.MaterialId, parser.MaterialSection); if (mat != null) { iSubmesh.Material = mat.Name; ImportedMaterial iMat = ImportedHelpers.FindMaterial(iSubmesh.Material, MaterialList); if (iMat == null) { iMat = new ImportedMaterial(); MaterialList.Add(iMat); iMat.Name = iSubmesh.Material; iMat.Diffuse = mat.Diffuse; iMat.Ambient = mat.Ambient; iMat.Specular = mat.Specular; iMat.Emissive = mat.Emissive; iMat.Power = mat.SpecularPower; iMat.Textures = new string[4]; for (int i = 0; i < 4; i++) { if (submesh.TextureIds[i] != ObjectID.INVALID) { odfTexture tex = odf.FindTextureInfo(submesh.TextureIds[i], parser.TextureSection); iMat.Textures[i] = tex.Name; if (ImportedHelpers.FindTexture(iMat.Textures[i], TextureList) == null) { try { odfTextureFile texFile = new odfTextureFile(iMat.Textures[i], Path.GetDirectoryName(parser.ODFPath) + @"\" + iMat.Textures[i]); MemoryStream memStream; int filesize = 0; using (BinaryReader reader = texFile.DecryptFile(ref filesize)) { memStream = new MemoryStream(reader.ReadBytes(filesize)); } ImportedTexture iTex = new ImportedTexture(memStream, iMat.Textures[i]); TextureList.Add(iTex); } catch { Report.ReportLog("cant read texture " + iMat.Textures[i]); } } } else { iMat.Textures[i] = String.Empty; } } } } List <Tuple <byte, float> >[] skin = new List <Tuple <byte, float> > [submesh.NumVertices]; for (int i = 0; i < submesh.NumVertices; i++) { skin[i] = new List <Tuple <byte, float> >(4); } odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); if (boneList != null) { if (iMesh.BoneList.Capacity < boneList.Count) { iMesh.BoneList.Capacity += boneList.Count; } foreach (odfBone bone in boneList) { byte idx; if (!boneDic.TryGetValue(bone.FrameId, out idx)) { ImportedBone iBone = new ImportedBone(); iMesh.BoneList.Add(iBone); iBone.Name = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; iBone.Matrix = bone.Matrix; boneDic.Add(bone.FrameId, idx = (byte)boneDic.Count); } for (int i = 0; i < bone.NumberIndices; i++) { skin[bone.VertexIndexArray[i]].Add(new Tuple <byte, float>(idx, bone.WeightArray[i])); } } } iSubmesh.VertexList = new List <ImportedVertex>(submesh.NumVertices); for (int i = 0; i < submesh.NumVertices; i++) { odfVertex vert = submesh.VertexList[i]; ImportedVertex iVert = new ImportedVertex(); iSubmesh.VertexList.Add(iVert); iVert.Position = vert.Position; iVert.Normal = vert.Normal; iVert.UV = new float[] { vert.UV[0], vert.UV[1] }; iVert.BoneIndices = new byte[4]; iVert.Weights = new float[4]; for (int j = 0; j < 4; j++) { if (j < skin[i].Count) { Tuple <byte, float> vertIdxWeight = skin[i][j]; iVert.BoneIndices[j] = vertIdxWeight.Item1; iVert.Weights[j] = vertIdxWeight.Item2; } else { iVert.BoneIndices[j] = 0xFF; } } } iSubmesh.FaceList = new List <ImportedFace>(submesh.NumVertexIndices / 3); foreach (odfFace face in submesh.FaceList) { ImportedFace iFace = new ImportedFace(); iSubmesh.FaceList.Add(iFace); iFace.VertexIndices = new int[3]; for (int i = 0; i < 3; i++) { iFace.VertexIndices[i] = face.VertexIndices[i]; } } } } }