public static void ReplaceTexture(UnityParser parser, ImportedTexture texture) { if (!Operations.ReplaceTexture(parser, texture, false)) { throw new Exception("Texture " + texture.Name + " not present"); } }
private IntPtr ExportTexture(ImportedTexture texture) { if (texture == null) { return(IntPtr.Zero); } if (_createdTextures.ContainsKey(texture.Name)) { return(_createdTextures[texture.Name]); } var pTex = AsFbxCreateTexture(_pContext, texture.Name); _createdTextures.Add(texture.Name, pTex); var file = new FileInfo(texture.Name); using (var writer = new BinaryWriter(file.Create())) { writer.Write(texture.Data); } return(pTex); }
public odfTexture AddTexture(ImportedTexture image) { odfTexture tex = odf.CreateTexture(image, Parser.GetNewID(typeof(odfTexture)), Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); Parser.TextureSection.AddChild(tex); return(tex); }
public static void ImportTexture(ImportedTexture texture, string path) { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(path))) { writer.Write(texture.Data); } }
public void AddTexture(ImportedTexture image) { xxTexture tex = xx.CreateTexture(image); xx.CreateUnknowns(tex); Parser.TextureList.Add(tex); }
public void ReplaceTexture(int idx, ImportedTexture image) { String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } var oldImg = image.Name; image.Name = Path.GetFileNameWithoutExtension(Textures[idx]) + Path.GetExtension(image.Name); rem.CreateTexture(image, texh_folder); remId newTex = new remId(image.Name); foreach (remMaterial mat in Parser.MATC) { if (mat.texture == Textures[idx]) { mat.texture = newTex; } } Textures.RemoveAt(idx); Textures.Insert(idx, image.Name); image.Name = oldImg; }
public void MergeTexture(string tex, remParser srcParser) { String src_TEXH_folder = rem.TexturePathFromREM(srcParser.RemPath); if (src_TEXH_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } if (src_TEXH_folder != texh_folder) { ImportedTexture impTex = new ImportedTexture(src_TEXH_folder + @"\" + tex); string newTex = rem.CreateTexture(impTex, texh_folder); if (!Textures.Contains(newTex)) { Textures.Add(newTex); } } }
public void MergeTexture(string path) { ImportedTexture texture = new ImportedTexture(path); Operations.ReplaceTexture(Parser, texture); Changed = true; }
public new void LoadFrom(ImportedTexture tex) { base.LoadFrom(tex); while (m_SourceTextures.Count < 6) { m_SourceTextures.Add(new PPtr <Texture2D>((Component)null)); } }
public void ReplaceTexture(int idx, ImportedTexture image) { odfTexture oldTex = Parser.TextureSection[idx]; Parser.TextureSection.RemoveChild(oldTex); odfTexture newTex = odf.CreateTexture(image, oldTex.Id, Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); Parser.TextureSection.InsertChild(idx, newTex); }
public Texture2D AddTexture(ImportedTexture texture) { Texture2D tex = new Texture2D(Cabinet, 0, UnityClassID.Texture2D, UnityClassID.Texture2D); tex.LoadFrom(texture); Cabinet.ReplaceSubfile(-1, tex, null); Cabinet.Bundle.AddComponent(tex); Textures.Add(tex); return(tex); }
public static void ReplaceTexture(odfParser parser, ImportedTexture impTex) { odfTexture newTex = CreateTexture(impTex, null, parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath)); if (odf.FindTextureInfo(impTex.Name, parser.TextureSection) == null) { newTex.Id = parser.GetNewID(typeof(odfTexture)); parser.TextureSection.AddChild(newTex); } }
public static void ReplaceTexture(UnityParser parser, ImportedTexture texture) { Texture2D tex = parser.GetTexture(Path.GetFileNameWithoutExtension(texture.Name)); if (tex == null) { parser.AddTexture(texture); return; } ReplaceTexture(tex, texture); }
public void AddTexture(ImportedTexture image) { String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } string tex = rem.CreateTexture(image, texh_folder); if (!Textures.Contains(tex)) { Textures.Add(tex); } }
private void ConvertTexture2D(Texture2D tex2D, string name) { ImportedTexture iTex = ImportedHelpers.FindTexture(name, TextureList); if (iTex != null) { return; } using (MemoryStream memStream = new MemoryStream()) { tex2D.Export(memStream); memStream.Position = 0; iTex = new ImportedTexture(memStream, name); } TextureList.Add(iTex); }
public static void ReplaceTexture(Texture2D tex, ImportedTexture texture) { if (tex == null) { throw new Exception("This type of replacing a texture requires an existing texture with the same name"); } Texture2D t2d = new Texture2D(null, tex.pathID, tex.classID1, tex.classID2); t2d.LoadFrom(texture); tex.m_MipMap = t2d.m_MipMap; tex.m_Width = t2d.m_Width; tex.m_Height = t2d.m_Height; tex.m_CompleteImageSize = t2d.m_CompleteImageSize; tex.m_TextureFormat = t2d.m_TextureFormat; tex.m_ImageCount = t2d.m_ImageCount; tex.m_TextureDimension = t2d.m_TextureDimension; tex.image_data = t2d.image_data; }
public static string CreateTexture(ImportedTexture impTex, string destFolder) { string destPath = destFolder + @"\" + impTex.Name; DirectoryInfo dir = new DirectoryInfo(destFolder); if (!dir.Exists) { dir.Create(); } if (File.Exists(destPath)) { string backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(impTex.Name)); File.Move(destPath, backup); } rem.ImportTexture(impTex, destPath); return(impTex.Name); }
public void MergeTexture(odfTexture tex, odfParser srcParser) { ImportedTexture impTex = new ImportedTexture(Path.GetDirectoryName(srcParser.ODFPath) + @"\" + tex.TextureFile); odfTexture newTex = odf.CreateTexture(impTex, null, Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); newTex = tex.Clone(Parser.TextureSection._FormatType); bool found = false; for (int i = 0; i < Parser.TextureSection.Count; i++) { var oldTex = Parser.TextureSection[i]; if (oldTex.Name == newTex.Name) { newTex.Id = oldTex.Id; Parser.TextureSection.RemoveChild(i); Parser.TextureSection.InsertChild(i, newTex); found = true; break; } } if (!found) { Parser.TextureSection.AddChild(newTex); if (Parser.IsUsedID(newTex.Id)) { newTex.Id = Parser.GetNewID(typeof(odfTexture)); Report.ReportLog("Warning! Texture " + newTex.Name + " got a new ID : " + newTex.Id); } else { Parser.UsedIDs.Add((int)newTex.Id, typeof(odfTexture)); } } }
public static odfTexture CreateTexture(ImportedTexture impTex, ObjectID id, int format, string odfPath) { odfTexture odfTex = new odfTexture(new ObjectName(impTex.Name, null), id, format); odfTex.TextureFile = new ObjectName(impTex.Name, null); string destPath = odfPath + @"\" + odfTex.TextureFile; DirectoryInfo dir = new DirectoryInfo(odfPath); if (!dir.Exists) { dir.Create(); } if (File.Exists(destPath)) { string backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(odfTex.TextureFile)); File.Move(destPath, backup); } odf.ImportTexture(impTex, destPath); return(odfTex); }
void LoadImage(ImportedTexture tex) { try { if (tex == null) { pictureBox1.Image = null; textBoxName.Text = String.Empty; textBoxSize.Text = String.Empty; } else { Texture renderTexture = Texture.FromMemory(Gui.Renderer.Device, tex.Data); Bitmap bitmap = new Bitmap(Texture.ToStream(renderTexture, ImageFileFormat.Bmp)); renderTexture.Dispose(); pictureBox1.Image = bitmap; ResizeImage(); if (!this.IsHidden) { Enabled = false; Activate(); Enabled = true; } textBoxName.Text = tex.Name; textBoxSize.Text = bitmap.Width + "x" + bitmap.Height; } image = tex; } catch (Exception ex) { Utility.ReportException(ex); } }
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); } } }
private AnimationFrame CreateFrame(odfFrame frame, odfParser parser, HashSet <int> extractFrames, HashSet <int> meshIDs, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.Name; animationFrame.TransformationMatrix = frame.Matrix; animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = combinedParent * animationFrame.TransformationMatrix; if ((int)frame.MeshId != 0 && meshIDs.Contains((int)frame.MeshId)) { odfMesh mesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection); ExtendedMaterial[] materials = new ExtendedMaterial[mesh.Count]; AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); for (int i = 0; i < mesh.Count; i++) { odfSubmesh submesh = mesh[i]; List <odfFace> faceList = submesh.FaceList; List <odfVertex> vertexList = submesh.VertexList; odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); bool skinned = boneList != null; int numBones = skinned ? boneList.Count : 0; string[] boneNames = new string[numBones]; Matrix[] boneOffsets = new Matrix[numBones]; for (int boneIdx = 0; boneIdx < numBones; boneIdx++) { odfBone bone = boneList[boneIdx]; boneNames[boneIdx] = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; Matrix mirrored; if (!BoneMatrixDic.TryGetValue(boneNames[boneIdx], out mirrored)) { #if !DONT_MIRROR Vector3 translate, scale; Quaternion rotate; bone.Matrix.Decompose(out scale, out rotate, out translate); mirrored = Matrix.Scaling(scale.X, scale.Y, -scale.Z) * Matrix.RotationQuaternion(rotate) * Matrix.Translation(translate); #else mirrored = bone.Matrix; #endif BoneMatrixDic.Add(boneNames[boneIdx], mirrored); } boneOffsets[boneIdx] = mirrored; } Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { ushort[] indices = faceList[j].VertexIndices; indexStream.Write(indices[0]); indexStream.Write(indices[1]); indexStream.Write(indices[2]); } animationMesh.UnlockIndexBuffer(); } float[][] vertexWeights = ConvertVertexWeights(vertexList, boneList); FillVertexBuffer(animationMesh, vertexList, vertexWeights, -1); var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { odfVertex vertex = vertexList[j]; #if !DONT_MIRROR Vector3 position = new Vector3(vertex.Position.X, vertex.Position.Y, -vertex.Position.Z); Vector3 normal = new Vector3(vertex.Normal.X, vertex.Normal.Y, -vertex.Normal.Z); #else Vector3 position = vertex.Position; Vector3 normal = vertex.Normal; #endif float[] boneWeights = vertexWeights[j]; normalLines[j * 2] = new PositionBlendWeightsIndexedColored(position, boneWeights, vertex.BoneIndices, Color.Yellow.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(position + (normal / 11), boneWeights, vertex.BoneIndices, Color.Blue.ToArgb()); min = Vector3.Minimize(min, position); max = Vector3.Maximize(max, position); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; meshContainer.BoneNames = boneNames; meshContainer.BoneOffsets = boneOffsets; meshContainers[i] = meshContainer; odfMaterial mat = odf.FindMaterialInfo(submesh.MaterialId, parser.MaterialSection); if (mat != null) { Material material3D = new Material(); material3D.Ambient = mat.Ambient; material3D.Diffuse = mat.Diffuse; material3D.Emissive = mat.Emissive; material3D.Specular = mat.Specular; material3D.Power = mat.SpecularPower; int matIdx = parser.MaterialSection.IndexOf(mat); Materials[matIdx] = material3D; meshContainer.MaterialIndex = matIdx; int texIdx = -1; if ((int)submesh.TextureIds[0] != 0 && !TextureDic.TryGetValue((int)submesh.TextureIds[0], out texIdx)) { odfTexture tex = odf.FindTextureInfo(submesh.TextureIds[0], parser.TextureSection); if (tex != null) { try { odfTextureFile texFile = new odfTextureFile(null, Path.GetDirectoryName(parser.ODFPath) + Path.DirectorySeparatorChar + tex.TextureFile); int fileSize = 0; ImportedTexture impTex = new ImportedTexture(texFile.DecryptFile(ref fileSize).BaseStream, tex.TextureFile); Texture memTex = impTex.ToTexture(device); texIdx = TextureDic.Count; TextureDic.Add((int)submesh.TextureIds[0], texIdx); Textures[texIdx] = memTex; } catch (SlimDXException ex) { Utility.ReportException(ex); Report.ReportLog("Please check " + tex.TextureFile + ". It may have an unsupported format."); } catch (Exception ex) { Utility.ReportException(ex); } } } meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } for (int i = 0; i < frame.Count; i++) { odfFrame child = frame[i]; if (extractFrames.Contains((int)child.Id)) { AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshIDs, device, animationFrame.CombinedTransform, meshFrames); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return(animationFrame); }
public void MergeTexture(ImportedTexture tex) { xx.ReplaceTexture(Parser, tex); Changed = true; }
public static void MergeTexture(UnityParser parser, string path) { ImportedTexture texture = new ImportedTexture(path); MergeTexture(parser, texture); }
public void MergeTexture(ImportedTexture tex) { xx.ReplaceTexture(Parser, tex); }
public void ReplaceTexture(int id, ImportedTexture image) { var oldTex = Textures[id]; oldTex.LoadFrom(image); if (Parser.file.Bundle != null) { Parser.file.Bundle.RegisterForUpdate(oldTex); } Changed = true; }
public static void ReplaceTexture(UnityParser parser, ImportedTexture texture) { Texture2D tex = parser.GetTexture(Path.GetFileNameWithoutExtension(texture.Name)); if (tex == null) { parser.AddTexture(texture); return; } ReplaceTexture(tex, texture); }
public static void ExportTexture(ImportedTexture impTex, Stream stream) { BinaryWriter writer = new BinaryWriter(stream); writer.Write(impTex.Data); }
public static void ReplaceTexture(Texture2D tex, ImportedTexture texture) { if (tex == null) { throw new Exception("This type of replacing a texture requires an existing texture with the same name"); } Texture2D t2d = new Texture2D(null, tex.pathID, tex.classID1, tex.classID2); t2d.LoadFrom(texture); tex.m_MipMap = t2d.m_MipMap; tex.m_Width = t2d.m_Width; tex.m_Height = t2d.m_Height; tex.m_CompleteImageSize = t2d.m_CompleteImageSize; tex.m_TextureFormat = t2d.m_TextureFormat; tex.m_ImageCount = t2d.m_ImageCount; tex.m_TextureDimension = t2d.m_TextureDimension; tex.image_data = t2d.image_data; }
public void AddTexture(ImportedTexture image) { String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } string tex = rem.CreateTexture(image, texh_folder); if (!Textures.Contains(tex)) { Textures.Add(tex); } }
public void ReplaceTexture(int idx, ImportedTexture image) { String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } var oldImg = image.Name; image.Name = Path.GetFileNameWithoutExtension(Textures[idx]) + Path.GetExtension(image.Name); rem.CreateTexture(image, texh_folder); remId newTex = new remId(image.Name); foreach (remMaterial mat in Parser.MATC) { if (mat.texture == Textures[idx]) { mat.texture = newTex; } } Textures.RemoveAt(idx); Textures.Insert(idx, image.Name); image.Name = oldImg; }
private ImportedTexture ImportImage(Document.Image img) { ImportedTexture tex = null; try { string texPath = Path.GetDirectoryName(img.init_from.Uri.LocalPath) + Path.DirectorySeparatorChar + Path.GetFileName(img.init_from.Uri.LocalPath); if (texPath.StartsWith("./") || texPath.StartsWith("/") || texPath.StartsWith(".\\") || texPath.StartsWith("\\")) { texPath = Path.GetDirectoryName(colladaDoc.baseURI.LocalPath) + Path.DirectorySeparatorChar + texPath.TrimStart(new char[] { '.', '/', '\\' }); } tex = new ImportedTexture(texPath); } catch (Exception e) { Report.ReportLog("Error importing texture " + img.id + ": " + e.Message); } return tex; }
public void MergeTexture(string tex, remParser srcParser) { String src_TEXH_folder = rem.TexturePathFromREM(srcParser.RemPath); if (src_TEXH_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } String texh_folder = rem.TexturePathFromREM(Parser.RemPath); if (texh_folder == null) { Report.ReportLog("TEXH folder could not be located."); return; } if (src_TEXH_folder != texh_folder) { ImportedTexture impTex = new ImportedTexture(src_TEXH_folder + @"\" + tex); string newTex = rem.CreateTexture(impTex, texh_folder); if (!Textures.Contains(newTex)) { Textures.Add(newTex); } } }
public void MergeTexture(ImportedTexture tex) { odf.ReplaceTexture(Parser, tex); }
public void LoadFrom(ImportedTexture tex) { m_Name = Path.GetFileNameWithoutExtension(tex.Name); Device dev = new Device(new Direct3D(), 0, DeviceType.Hardware, new IntPtr(), CreateFlags.SoftwareVertexProcessing, new PresentParameters()); ImageInformation imageInfo; using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo)) { m_Width = imageInfo.Width; m_Height = imageInfo.Height; TextureFormat tf; if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf)) { m_TextureFormat = tf; } else if (imageInfo.Format == Format.R8G8B8) { m_TextureFormat = TextureFormat.RGB24; } else if (imageInfo.Format == Format.A8R8G8B8) { m_TextureFormat = TextureFormat.ARGB32; } else { throw new Exception("Unknown format " + imageInfo.Format); } m_MipMap = imageInfo.MipLevels > 1; m_ReadAllowed = true; m_ImageCount = 1; m_TextureDimension = 2; m_TextureSettings = new GLTextureSettings(); m_ColorSpace = 1; int bytesPerPixel = 0, originY = 0; using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data))) { switch (m_TextureFormat) { case TextureFormat.DXT1: case TextureFormat.DXT5: reader.BaseStream.Position = 0x80; m_CompleteImageSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position); break; case TextureFormat.RGB24: case TextureFormat.ARGB32: reader.BaseStream.Position = 0x0A; originY = reader.ReadInt16(); reader.BaseStream.Position = 0x12; bytesPerPixel = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4; m_CompleteImageSize = m_Width * m_Height * bytesPerPixel; break; } image_data = reader.ReadBytes(m_CompleteImageSize); } switch (m_TextureFormat) { case TextureFormat.RGB24: for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; } break; case TextureFormat.ARGB32: for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; b = image_data[l]; image_data[l] = image_data[k]; image_data[k] = b; } break; } if (bytesPerPixel > 0 && originY > 0) { for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel) { for (int i = 0; i < m_Width * bytesPerPixel; i++) { byte b = image_data[srcIdx + i]; image_data[srcIdx + i] = image_data[dstIdx + i]; image_data[dstIdx + i] = b; } } } } dev.Direct3D.Dispose(); dev.Dispose(); }
public void MergeTexture(ImportedTexture tex) { string texName; Match m = Regex.Match(tex.Name, @"(.+)-([^-]+)(\..+)", RegexOptions.CultureInvariant); if (m.Success) { texName = m.Groups[1].Value; } else { texName = Path.GetFileNameWithoutExtension(tex.Name); } Texture2D dstTex = Parser.file.Parser.GetTexture(texName); bool isNew = false; if (dstTex == null) { dstTex = new Texture2D(Parser.file); isNew = true; } dstTex.LoadFrom(tex); if (isNew) { if (Parser.file.Bundle != null) { Parser.file.Bundle.AddComponent(dstTex); } Textures.Add(dstTex); } Changed = true; }
public void ReplaceTexture(int id, ImportedTexture image) { var oldTex = Parser.TextureList[id]; var newTex = xx.CreateTexture(image); xx.CopyUnknowns(oldTex, newTex); Parser.TextureList.RemoveAt(id); Parser.TextureList.Insert(id, newTex); for (int i = 0; i < Parser.MaterialList.Count; i++) { var mat = Parser.MaterialList[i]; for (int j = 0; j < mat.Textures.Length; j++) { var matTex = mat.Textures[j]; if (matTex.Name == oldTex.Name) { matTex.Name = newTex.Name; } } } }
public static void ReplaceTexture(odfParser parser, ImportedTexture impTex) { odfTexture newTex = CreateTexture(impTex, null, parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath)); if (odf.FindTextureInfo(impTex.Name, parser.TextureSection) == null) { newTex.Id = parser.GetNewID(typeof(odfTexture)); parser.TextureSection.AddChild(newTex); } }
public static void MergeTexture(UnityParser parser, ImportedTexture texture) { Operations.ReplaceTexture(parser, texture); }
public void MergeTexture(string path) { ImportedTexture texture = new ImportedTexture(path); Operations.ReplaceTexture(Parser, texture); Changed = true; }
public void MergeTexture(ImportedTexture tex) { AddTexture(tex); }
public static void MergeTexture(UnityParser parser, string path) { ImportedTexture texture = new ImportedTexture(path); MergeTexture(parser, texture); }
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 AddTexture(ImportedTexture image) { Texture2D tex = new Texture2D(Parser.file); Textures.Add(tex); tex.LoadFrom(image); if (Parser.file.Bundle != null) { Parser.file.Bundle.AddComponent(tex); } Changed = true; }
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 AnimationFrame CreateFrame(remBone frame, remParser parser, HashSet <string> extractFrames, remMesh mesh, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.name.ToString(); animationFrame.TransformationMatrix = frame.matrix; animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = combinedParent * animationFrame.TransformationMatrix; if (frame.name == mesh.frame) { ExtendedMaterial[] materials = new ExtendedMaterial[mesh.numMats]; List <List <remVertex> > submeshVertLists = new List <List <remVertex> >(mesh.numMats); List <List <ushort> > submeshFaceLists = new List <List <ushort> >(mesh.numMats); List <int[]> submeshVertIndices = new List <int[]>(mesh.numMats); SplitMesh(mesh, submeshVertLists, submeshFaceLists, submeshVertIndices); remSkin boneList = rem.FindSkin(mesh.name, parser.SKIC); bool skinned = boneList != null; int numBones = skinned ? boneList.Count : 0; List <string> boneNamesList = new List <string>(numBones); List <Matrix> boneOffsetsList = new List <Matrix>(numBones); for (int boneIdx = 0; boneIdx < numBones; boneIdx++) { boneNamesList.Add(boneList[boneIdx].bone.ToString()); boneOffsetsList.Add(boneList[boneIdx].matrix); } List <string> boneFrameParentNames = new List <string>(numBones); List <Matrix> boneFrameParentMatrices = new List <Matrix>(numBones); for (int boneIdx = 0; boneIdx < numBones; boneIdx++) { remBone boneFrame = rem.FindFrame(boneList[boneIdx].bone, parser.BONC.rootFrame); if (boneFrame == null) { continue; } remBone boneFrameParent = boneFrame.Parent; if (!boneNamesList.Contains(boneFrameParent.name) && !boneFrameParentNames.Contains(boneFrameParent.name)) { boneFrameParentNames.Add(boneFrameParent.name); Matrix incompleteMeshFrameCorrection = Matrix.Invert(frame.matrix); boneFrameParentMatrices.Add(incompleteMeshFrameCorrection * Matrix.Invert(boneFrame.matrix) * boneList[boneIdx].matrix); } } boneNamesList.AddRange(boneFrameParentNames); string[] boneNames = boneNamesList.ToArray(); boneOffsetsList.AddRange(boneFrameParentMatrices); Matrix[] boneOffsets = boneOffsetsList.ToArray(); AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[submeshFaceLists.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); for (int i = 0; i < submeshFaceLists.Count; i++) { List <ushort> faceList = submeshFaceLists[i]; List <remVertex> vertexList = submeshVertLists[i]; Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { indexStream.Write(faceList[j]); } animationMesh.UnlockIndexBuffer(); } byte[][] vertexBoneIndices = null; float[][] vertexWeights = ConvertVertexWeights(vertexList, submeshVertIndices[i], boneList, out vertexBoneIndices); FillVertexBuffer(animationMesh, vertexList, vertexWeights, vertexBoneIndices, -1); var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { remVertex vertex = vertexList[j]; Vector3 position = vertex.Position; Vector3 normal = vertex.Normal; float[] boneWeights = vertexWeights[j]; normalLines[j * 2] = new PositionBlendWeightsIndexedColored(position, boneWeights, vertexBoneIndices[j], Color.Coral.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(position + normal, boneWeights, vertexBoneIndices[j], Color.Blue.ToArgb()); #if !DONT_MIRROR position.Z *= -1f; #endif min = Vector3.Minimize(min, position); max = Vector3.Maximize(max, position); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; meshContainer.BoneNames = boneNames; meshContainer.BoneOffsets = boneOffsets; meshContainers[i] = meshContainer; remMaterial mat = rem.FindMaterial(mesh.materials[i], parser.MATC); if (mat != null) { Material material3D = new Material(); material3D.Ambient = new Color4(mat.ambient); material3D.Diffuse = new Color4(mat.diffuse); material3D.Emissive = new Color4(mat.emissive); material3D.Specular = new Color4(mat.specular); material3D.Power = mat.specularPower; int matIdx = parser.MATC.IndexOf(mat); Materials[matIdx] = material3D; meshContainer.MaterialIndex = matIdx; int texIdx = 0; if (mat.texture != null && !TextureDic.TryGetValue(mat.texture.ToString(), out texIdx)) { ImportedTexture importedTex = null; if (!ImportedTextures.TryGetValue(mat.texture.ToString(), out importedTex)) { importedTex = rem.ImportedTexture(mat.texture, parser.RemPath, true); if (importedTex == null) { Report.ReportLog("Export textures of TEXH.FPK!"); continue; } ImportedTextures.Add(mat.texture.ToString(), importedTex); } Texture memTex = Texture.FromMemory(device, importedTex.Data); texIdx = TextureDic.Count; TextureDic.Add(mat.texture.ToString(), texIdx); Textures.Add(memTex); } meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } min = Vector3.TransformCoordinate(min, animationFrame.CombinedTransform); max = Vector3.TransformCoordinate(max, animationFrame.CombinedTransform); animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } for (int i = 0; i < frame.Count; i++) { remBone child = frame[i]; if (extractFrames.Contains(child.name.ToString())) { AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, mesh, device, animationFrame.CombinedTransform, meshFrames); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return(animationFrame); }
private void ConvertTexture2D(Texture2D tex2D, string name) { ImportedTexture iTex = ImportedHelpers.FindTexture(name, TextureList); if (iTex != null) { return; } using (MemoryStream memStream = new MemoryStream()) { tex2D.Export(memStream); memStream.Position = 0; iTex = new ImportedTexture(memStream, name); } TextureList.Add(iTex); }
private AnimationFrame CreateFrame(Transform frame, AnimatorEditor editor, HashSet<string> extractFrames, HashSet<string> meshNames, Device device, List<AnimationFrame> meshFrames, Dictionary<string, Tuple<Matrix, Matrix>> extractMatrices) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.GetTransformPath(); animationFrame.TransformationMatrix = Matrix.Scaling(frame.m_LocalScale) * Matrix.RotationQuaternion(frame.m_LocalRotation) * Matrix.Translation(frame.m_LocalPosition); animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = extractMatrices[animationFrame.Name].Item1; if (meshNames.Contains(animationFrame.Name)) { MeshRenderer meshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.SkinnedMeshRenderer); if (meshR == null) { meshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.MeshRenderer); } if (meshR != null) { Mesh mesh = Operations.GetMesh(meshR); if (mesh != null) { SkinnedMeshRenderer smr = meshR as SkinnedMeshRenderer; List<PPtr<Transform>> boneList = null; string[] boneNames = null; Matrix[] boneOffsets = null; if (smr != null && smr.m_Bones.Count > 0) { boneList = smr.m_Bones; int numBones = boneList.Count > 0 ? extractFrames.Count : 0; boneNames = new string[numBones]; boneOffsets = new Matrix[numBones]; if (numBones > 0) { string[] extractArray = new string[numBones]; extractFrames.CopyTo(extractArray); HashSet<string> extractCopy = new HashSet<string>(extractArray); int invalidBones = 0; for (int i = 0; i < boneList.Count; i++) { Transform bone = boneList[i].instance; if (bone == null || bone.m_GameObject.instance == null || !extractCopy.Remove(bone.GetTransformPath())) { invalidBones++; } else if (i < numBones) { boneNames[i] = bone.GetTransformPath(); boneOffsets[i] = Operations.Mirror(Matrix.Transpose(mesh.m_BindPose[i])); } } extractCopy.CopyTo(boneNames, boneList.Count - invalidBones); for (int i = boneList.Count; i < extractFrames.Count; i++) { boneOffsets[i] = extractMatrices[boneNames[i]].Item2; } } } AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.m_SubMeshes.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); Operations.vMesh vMesh = new Operations.vMesh(meshR, true, true); for (int i = 0; i < mesh.m_SubMeshes.Count; i++) { Operations.vSubmesh submesh = vMesh.submeshes[i]; List<Operations.vFace> faceList = submesh.faceList; List<Operations.vVertex> vertexList = submesh.vertexList; SlimDX.Direct3D9.Mesh animationMesh = null; PositionBlendWeightsIndexedColored[] normalLines = null; try { animationMesh = new SlimDX.Direct3D9.Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { ushort[] indices = faceList[j].index; indexStream.Write(indices[0]); indexStream.Write(indices[2]); indexStream.Write(indices[1]); } animationMesh.UnlockIndexBuffer(); } FillVertexBuffer(animationMesh, vertexList, -1); normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { Operations.vVertex vertex = vertexList[j]; byte[] bIdx; float[] bWeights; if (vertex.boneIndices != null) { bIdx = new byte[4] { (byte)vertex.boneIndices[0], (byte)vertex.boneIndices[1], (byte)vertex.boneIndices[2], (byte)vertex.boneIndices[3] }; bWeights = vertex.weights; } else { bIdx = new byte[4]; bWeights = new float[4]; } normalLines[j * 2] = new PositionBlendWeightsIndexedColored(vertex.position, bWeights, bIdx, Color.Yellow.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(vertex.position + (vertex.normal / 64), bWeights, bIdx, Color.Yellow.ToArgb()); min = Vector3.Minimize(min, vertex.position); max = Vector3.Maximize(max, vertex.position); } } catch { Report.ReportLog("No display of submeshes with more than 64k vertices!"); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); if (animationMesh != null) { meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; } meshContainers[i] = meshContainer; if (submesh.matList.Count > 0 && submesh.matList[0].instance != null) { Material mat = submesh.matList[0].instance; int matIdx = editor.Materials.IndexOf(mat); int texIdx; if (!MatTexIndices.TryGetValue(matIdx, out texIdx)) { texIdx = -1; SlimDX.Direct3D9.Material materialD3D = new SlimDX.Direct3D9.Material(); materialD3D.Ambient = GetColour(mat, "_SColor"); materialD3D.Diffuse = GetColour(mat, "_Color"); materialD3D.Emissive = GetColour(mat, "_ReflectColor"); materialD3D.Specular = GetColour(mat, "_SpecColor"); materialD3D.Power = GetFloat(mat, "_Shininess"); Materials[matIdx] = materialD3D; Texture2D matTex = GetTexture(mat, "_MainTex"); if (matTex != null) { texIdx = editor.Textures.IndexOf(matTex); if (Textures[texIdx] == null) { using (MemoryStream mem = new MemoryStream()) { matTex.Export(mem); mem.Position = 0; ImportedTexture image = new ImportedTexture(mem, matTex.m_Name); Textures[texIdx] = Texture.FromMemory(device, image.Data); } } } MatTexIndices.Add(matIdx, texIdx); } meshContainer.MaterialIndex = matIdx; meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } if (boneList != null) { for (int i = 0; i < meshContainers.Length; i++) { meshContainers[i].BoneNames = boneNames; meshContainers[i].BoneOffsets = boneOffsets; meshContainers[i].RealBones = boneList.Count; } } Matrix mirrorCombined = Operations.Mirror(animationFrame.CombinedTransform); min = Vector3.TransformCoordinate(min, mirrorCombined); max = Vector3.TransformCoordinate(max, mirrorCombined); animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } } } for (int i = 0; i < frame.Count; i++) { Transform child = frame[i]; if (extractFrames.Contains(child.GetTransformPath())) { AnimationFrame childAnimationFrame = CreateFrame(child, editor, extractFrames, meshNames, device, meshFrames, extractMatrices); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return animationFrame; }
void LoadImage(ImportedTexture tex) { try { if (tex == null) { pictureBox1.Image = null; textBoxName.Text = String.Empty; textBoxSize.Text = String.Empty; } else { textBoxName.Text = tex.Name; if (tex.Data.Length > 0x12) { Texture renderTexture = null; try { ImageInformation imageInfo; renderTexture = Texture.FromMemory(Gui.Renderer.Device, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo); using (Image img = System.Drawing.Image.FromStream(Texture.ToStream(renderTexture, imageInfo.Height <= 512 && imageInfo.Width <= 512 ? ImageFileFormat.Png : ImageFileFormat.Bmp))) { int shift = 0; for (int max = imageInfo.Width > imageInfo.Height ? imageInfo.Width : imageInfo.Height; max > 256; max >>= 1) { shift++; } pictureBox1.Image = new Bitmap(img, new Size(imageInfo.Width >> shift, imageInfo.Height >> shift)); } string format = renderTexture.GetLevelDescription(0).Format.GetDescription(); int bpp = (format.Contains("A8") ? 8 : 0) + (format.Contains("R8") ? 8 : 0) + (format.Contains("G8") ? 8 : 0) + (format.Contains("B8") ? 8 : 0); textBoxSize.Text = imageInfo.Width + "x" + imageInfo.Height + (bpp > 0 ? "x" + bpp : String.Empty); } catch (Exception e) { pictureBox1.Image = pictureBox1.ErrorImage; Utility.ReportException(e); } finally { if (renderTexture != null) { renderTexture.Dispose(); } } ResizeImage(); if (!this.IsHidden) { Enabled = false; Activate(); Enabled = true; } } else { pictureBox1.Image = null; textBoxSize.Text = "0x0"; } } image = tex; } catch (Exception ex) { Utility.ReportException(ex); } }
public void MergeTexture(ImportedTexture tex) { AddTexture(tex); }
public static odfTexture CreateTexture(ImportedTexture impTex, ObjectID id, int format, string odfPath) { odfTexture odfTex = new odfTexture(new ObjectName(impTex.Name, null), id, format); odfTex.TextureFile = new ObjectName(impTex.Name, null); string destPath = odfPath + @"\" + odfTex.TextureFile; DirectoryInfo dir = new DirectoryInfo(odfPath); if (!dir.Exists) { dir.Create(); } if (File.Exists(destPath)) { string backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(odfTex.TextureFile)); File.Move(destPath, backup); } odf.ImportTexture(impTex, destPath); return odfTex; }
private AnimationFrame CreateFrame(odfFrame frame, odfParser parser, HashSet<int> extractFrames, HashSet<int> meshIDs, Device device, Matrix combinedParent, List<AnimationFrame> meshFrames) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.Name; animationFrame.TransformationMatrix = frame.Matrix; animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = combinedParent * animationFrame.TransformationMatrix; if ((int)frame.MeshId != 0 && meshIDs.Contains((int)frame.MeshId)) { odfMesh mesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection); ExtendedMaterial[] materials = new ExtendedMaterial[mesh.Count]; AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); for (int i = 0; i < mesh.Count; i++) { odfSubmesh submesh = mesh[i]; List<odfFace> faceList = submesh.FaceList; List<odfVertex> vertexList = submesh.VertexList; odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); bool skinned = boneList != null; int numBones = skinned ? boneList.Count : 0; string[] boneNames = new string[numBones]; Matrix[] boneOffsets = new Matrix[numBones]; for (int boneIdx = 0; boneIdx < numBones; boneIdx++) { odfBone bone = boneList[boneIdx]; boneNames[boneIdx] = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; Matrix mirrored; if (!BoneMatrixDic.TryGetValue(boneNames[boneIdx], out mirrored)) { #if !DONT_MIRROR Vector3 translate, scale; Quaternion rotate; bone.Matrix.Decompose(out scale, out rotate, out translate); mirrored = Matrix.Scaling(scale.X, scale.Y, -scale.Z) * Matrix.RotationQuaternion(rotate) * Matrix.Translation(translate); #else mirrored = bone.Matrix; #endif BoneMatrixDic.Add(boneNames[boneIdx], mirrored); } boneOffsets[boneIdx] = mirrored; } Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { ushort[] indices = faceList[j].VertexIndices; indexStream.Write(indices[0]); indexStream.Write(indices[1]); indexStream.Write(indices[2]); } animationMesh.UnlockIndexBuffer(); } float[][] vertexWeights = ConvertVertexWeights(vertexList, boneList); FillVertexBuffer(animationMesh, vertexList, vertexWeights, -1); var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { odfVertex vertex = vertexList[j]; #if !DONT_MIRROR Vector3 position = new Vector3(vertex.Position.X, vertex.Position.Y, -vertex.Position.Z); Vector3 normal = new Vector3(vertex.Normal.X, vertex.Normal.Y, -vertex.Normal.Z); #else Vector3 position = vertex.Position; Vector3 normal = vertex.Normal; #endif float[] boneWeights = vertexWeights[j]; normalLines[j * 2] = new PositionBlendWeightsIndexedColored(position, boneWeights, vertex.BoneIndices, Color.Yellow.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(position + (normal / 11), boneWeights, vertex.BoneIndices, Color.Blue.ToArgb()); min = Vector3.Minimize(min, position); max = Vector3.Maximize(max, position); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; meshContainer.BoneNames = boneNames; meshContainer.BoneOffsets = boneOffsets; meshContainers[i] = meshContainer; odfMaterial mat = odf.FindMaterialInfo(submesh.MaterialId, parser.MaterialSection); if (mat != null) { Material material3D = new Material(); material3D.Ambient = mat.Ambient; material3D.Diffuse = mat.Diffuse; material3D.Emissive = mat.Emissive; material3D.Specular = mat.Specular; material3D.Power = mat.SpecularPower; int matIdx = parser.MaterialSection.IndexOf(mat); Materials[matIdx] = material3D; meshContainer.MaterialIndex = matIdx; int texIdx = -1; if ((int)submesh.TextureIds[0] != 0 && !TextureDic.TryGetValue((int)submesh.TextureIds[0], out texIdx)) { odfTexture tex = odf.FindTextureInfo(submesh.TextureIds[0], parser.TextureSection); if (tex != null) { try { odfTextureFile texFile = new odfTextureFile(null, Path.GetDirectoryName(parser.ODFPath) + Path.DirectorySeparatorChar + tex.TextureFile); int fileSize = 0; ImportedTexture impTex = new ImportedTexture(texFile.DecryptFile(ref fileSize).BaseStream, tex.TextureFile); Texture memTex = impTex.ToTexture(device); texIdx = TextureDic.Count; TextureDic.Add((int)submesh.TextureIds[0], texIdx); Textures[texIdx] = memTex; } catch (SlimDXException ex) { Utility.ReportException(ex); Report.ReportLog("Please check " + tex.TextureFile + ". It may have an unsupported format."); } catch (Exception ex) { Utility.ReportException(ex); } } } meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } for (int i = 0; i < frame.Count; i++) { odfFrame child = frame[i]; if (extractFrames.Contains((int)child.Id)) { AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshIDs, device, animationFrame.CombinedTransform, meshFrames); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return animationFrame; }
private string ImportTexture(Section section) { string texName = String.Empty; try { if (section.data.First != null) { texName = Path.GetFileName(ConvertString(section.data.First.Value)); if (texList.Add(texName)) { string texPath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + texName; ImportedTexture tex = new ImportedTexture(texPath); TextureList.Add(tex); } } } catch (Exception ex) { Utility.ReportException(ex); } return texName; }
public void LoadFrom(ImportedTexture tex) { TextureFormat destFormat = (TextureFormat)0; Match nameWithFormat = Regex.Match(tex.Name, @"(.+)-([^-]+)(\..+)", RegexOptions.CultureInvariant); if (nameWithFormat.Success) { m_Name = nameWithFormat.Groups[1].Value; TextureFormat.TryParse(nameWithFormat.Groups[2].Value, true, out destFormat); } else { m_Name = Path.GetFileNameWithoutExtension(tex.Name); } Device dev = null; try { if (Gui.Renderer != null) { dev = Gui.Renderer.Device; } else { IntPtr handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; if (handle.ToInt64() == 0) { handle = FindWindowByCaption(IntPtr.Zero, Console.Title); } dev = new Device(new Direct3D(), 0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, new PresentParameters()); } ImageInformation imageInfo; using (Texture renderTexture = Texture.FromMemory(dev, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo)) { m_Width = imageInfo.Width; m_Height = imageInfo.Height; TextureFormat tf; if (TextureFormat.TryParse(imageInfo.Format.ToString(), true, out tf)) { m_TextureFormat = tf; } else if (imageInfo.Format == Format.R8G8B8) { m_TextureFormat = TextureFormat.RGB24; } else if (imageInfo.Format == Format.A8R8G8B8) { m_TextureFormat = destFormat != (TextureFormat)0 ? destFormat : TextureFormat.ARGB32; } else { throw new Exception("Unknown format " + imageInfo.Format); } m_MipMap = imageInfo.MipLevels > 1; m_ReadAllowed = true; m_ImageCount = 1; m_TextureDimension = 2; m_TextureSettings = new GLTextureSettings(); m_ColorSpace = 1; int bytesPerPixel = 0, originY = 0; using (BinaryReader reader = new BinaryReader(new MemoryStream(tex.Data))) { switch (m_TextureFormat) { case TextureFormat.DXT1: case TextureFormat.DXT5: reader.BaseStream.Position = 0x80; m_CompleteImageSize = (int)(reader.BaseStream.Length - reader.BaseStream.Position); break; case TextureFormat.RGB24: case TextureFormat.ARGB32: case TextureFormat.RGBA32: reader.BaseStream.Position = 0x0A; originY = reader.ReadInt16(); reader.BaseStream.Position = 0x12; bytesPerPixel = m_TextureFormat == TextureFormat.RGB24 ? 3 : 4; m_CompleteImageSize = m_Width * m_Height * bytesPerPixel; break; } image_data = reader.ReadBytes(m_CompleteImageSize); } switch (m_TextureFormat) { case TextureFormat.RGB24: for (int i = 0, j = 2; j < m_CompleteImageSize; i += 3, j += 3) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; } break; case TextureFormat.ARGB32: for (int i = 0, j = 3, k = 1, l = 2; j < m_CompleteImageSize; i += 4, j += 4, k += 4, l += 4) { byte b = image_data[j]; image_data[j] = image_data[i]; image_data[i] = b; b = image_data[l]; image_data[l] = image_data[k]; image_data[k] = b; } break; } if (bytesPerPixel > 0 && originY > 0) { for (int srcIdx = 0, dstIdx = (originY - 1) * m_Width * bytesPerPixel; srcIdx < dstIdx; srcIdx += m_Width * bytesPerPixel, dstIdx -= m_Width * bytesPerPixel) { for (int i = 0; i < m_Width * bytesPerPixel; i++) { byte b = image_data[srcIdx + i]; image_data[srcIdx + i] = image_data[dstIdx + i]; image_data[dstIdx + i] = b; } } } } } catch (Exception e) { Utility.ReportException(e); } finally { if (Gui.Renderer == null && dev != null) { dev.Direct3D.Dispose(); dev.Dispose(); } } }
public odfTexture AddTexture(ImportedTexture image) { odfTexture tex = odf.CreateTexture(image, Parser.GetNewID(typeof(odfTexture)), Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); Parser.TextureSection.AddChild(tex); return tex; }
public void MergeTexture(odfTexture tex, odfParser srcParser) { ImportedTexture impTex = new ImportedTexture(Path.GetDirectoryName(srcParser.ODFPath) + @"\" + tex.TextureFile); odfTexture newTex = odf.CreateTexture(impTex, null, Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); newTex = tex.Clone(Parser.TextureSection._FormatType); bool found = false; for (int i = 0; i < Parser.TextureSection.Count; i++) { var oldTex = Parser.TextureSection[i]; if (oldTex.Name == newTex.Name) { newTex.Id = oldTex.Id; Parser.TextureSection.RemoveChild(i); Parser.TextureSection.InsertChild(i, newTex); found = true; break; } } if (!found) { Parser.TextureSection.AddChild(newTex); if (Parser.IsUsedID(newTex.Id)) { newTex.Id = Parser.GetNewID(typeof(odfTexture)); Report.ReportLog("Warning! Texture " + newTex.Name + " got a new ID : " + newTex.Id); } else Parser.UsedIDs.Add((int)newTex.Id, typeof(odfTexture)); } }
public static void MergeTexture(UnityParser parser, ImportedTexture texture) { Operations.ReplaceTexture(parser, texture); }
public void ReplaceTexture(int idx, ImportedTexture image) { odfTexture oldTex = Parser.TextureSection[idx]; Parser.TextureSection.RemoveChild(oldTex); odfTexture newTex = odf.CreateTexture(image, oldTex.Id, Parser.TextureSection._FormatType, Path.GetDirectoryName(Parser.ODFPath)); Parser.TextureSection.InsertChild(idx, newTex); }
public static string CreateTexture(ImportedTexture impTex, string destFolder) { string destPath = destFolder + @"\" + impTex.Name; DirectoryInfo dir = new DirectoryInfo(destFolder); if (!dir.Exists) { dir.Create(); } if (File.Exists(destPath)) { string backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(impTex.Name)); File.Move(destPath, backup); } rem.ImportTexture(impTex, destPath); return impTex.Name; }
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]; } } } } }