private List <SegaSaturnTexture> GetTextures(List <Texture> textureLibrary, List <SegaSaturnAttribute> attributes, int maxTextureSize) { if (textureLibrary == null || textureLibrary.Count <= 0) { return(new List <SegaSaturnTexture>()); } if (textureLibrary.Count > 1) { MessageBox.Show("Sorry, only one texture per file are supported yet :'(", Editor.Instance.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); throw new InvalidOperationException("Too many textures"); } MeshSource textureSource = this.MeshSources.FirstOrDefault(item => item.MeshSourceKind == MeshSource.MeshSourceKindEnum.Texcoord); if (textureSource == null) { return(new List <SegaSaturnTexture>()); } List <SegaSaturnTexture> toReturn = new List <SegaSaturnTexture>(); Dictionary <string, SegaSaturnTexture> dict = new Dictionary <string, SegaSaturnTexture>(); List <SegaSaturnTextureVerticesIndexes> list = this.GetTextureVerticesIndexes(); Texture tmp = textureLibrary.First(); Bitmap img = JoMapEditorTools.GetBitmap(tmp.Path); if (img.Width > maxTextureSize && img.Height > maxTextureSize) { img = JoMapEditorTools.ResizeImage(img, maxTextureSize, maxTextureSize); } else if (img.Width > maxTextureSize) { img = JoMapEditorTools.ResizeImage(img, maxTextureSize, img.Height); } else if (img.Height > maxTextureSize) { img = JoMapEditorTools.ResizeImage(img, img.Width, maxTextureSize); } for (int i = 0; i < list.Count; ++i) { SegaSaturnTextureCoordinates p1 = Geometry.ConvertToSegaSaturnTextureCoordinates(img, textureSource.Floats, list[i].Vertice1); SegaSaturnTextureCoordinates p2 = Geometry.ConvertToSegaSaturnTextureCoordinates(img, textureSource.Floats, list[i].Vertice2); SegaSaturnTextureCoordinates p3 = Geometry.ConvertToSegaSaturnTextureCoordinates(img, textureSource.Floats, list[i].Vertice3); SegaSaturnTextureCoordinates p4 = Geometry.ConvertToSegaSaturnTextureCoordinates(img, textureSource.Floats, list[i].Vertice4); if (p1.Hash == p2.Hash && p2.Hash == p3.Hash) { continue; } SegaSaturnTexture texture = SegaSaturnTexture.ConvertFrom(img, tmp.Name ?? Path.GetFileNameWithoutExtension(tmp.Path), p1, p2, p3, p4, list[i].IsTriangleMapping); if (!dict.ContainsKey(texture.Hash)) { texture.Name += toReturn.Count.ToString(); dict.Add(texture.Hash, texture); toReturn.Add(texture); } attributes[i].SpriteIndex = toReturn.FindIndex(item => item.Hash == texture.Hash); attributes[i].Color = null; } return(toReturn); }
private Dictionary <string, Material> ParseMaterial(string filename, string dir, int maxTextureSize) { Dictionary <string, Material> toReturn = new Dictionary <string, Material>(); string path = Path.Combine(dir, filename); if (!File.Exists(path)) { if (MessageBox.Show(String.Format("File {0} doesn't exists. Would you like to select it manually ?", path), "Wavefront material file", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Multiselect = false; dlg.Filter = "Wavefront material file (MTL)|*.mtl"; dlg.CheckFileExists = true; if (dlg.ShowDialog() != DialogResult.OK) { return(toReturn); } path = dlg.FileName; } } else { return(toReturn); } } Material currentMaterial = null; using (StreamReader reader = new StreamReader(path)) { string line; while ((line = reader.ReadLine()) != null) { string[] data = line.Split(new[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); if (data.Length <= 0) { continue; } switch (data[0].ToLower()) { case "newmtl": if (currentMaterial != null) { toReturn.Add(currentMaterial.Name, currentMaterial); } currentMaterial = new Material { Name = data[1] }; break; case "map_kd": string texturePath = Path.Combine(dir, data[1]); if (File.Exists(texturePath)) { currentMaterial.TexturePath = texturePath; try { currentMaterial.Texture = JoMapEditorTools.GetBitmap(currentMaterial.TexturePath); if (currentMaterial.Texture.Width > maxTextureSize && currentMaterial.Texture.Height > maxTextureSize) { currentMaterial.Texture = JoMapEditorTools.ResizeImage(currentMaterial.Texture, maxTextureSize, maxTextureSize); } else if (currentMaterial.Texture.Width > maxTextureSize) { currentMaterial.Texture = JoMapEditorTools.ResizeImage(currentMaterial.Texture, maxTextureSize, currentMaterial.Texture.Height); } else if (currentMaterial.Texture.Height > maxTextureSize) { currentMaterial.Texture = JoMapEditorTools.ResizeImage(currentMaterial.Texture, currentMaterial.Texture.Width, maxTextureSize); } } catch (Exception ex) { Program.Logger.Error(String.Format("Texture {0} format is not supported", texturePath), ex); throw new NotSupportedException(String.Format("Texture {0} format is not supported", texturePath), ex); } } break; } } } if (currentMaterial != null) { toReturn.Add(currentMaterial.Name, currentMaterial); } return(toReturn); }