Beispiel #1
0
            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);
            }
Beispiel #2
0
        private void ExplodeTile(Tile src, int tileWidth, int tileHeight, bool addRightMarginForSegaSaturn)
        {
            Cursor.Current = Cursors.WaitCursor;
            this.panelGrid.SuspendLayout();
            int newTileWidth = tileWidth;

            if (addRightMarginForSegaSaturn)
            {
                newTileWidth = ((tileWidth / 8) + 1) * 8;
            }
            try
            {
                using (Bitmap bmp = new Bitmap((int)(src.Width / this._zoom), (int)(src.Height / this._zoom)))
                {
                    this.DrawControl(src, bmp, (int)(src.Left / this._zoom), (int)(src.Top / this._zoom));
                    Color defaultColor = bmp.ContainsColor(Color.Transparent) ? Color.Transparent : Editor.TransparentColor;
                    this.panelGrid.Controls.Remove(src);
                    int newX = 0;
                    for (int x = 0; x + tileWidth <= bmp.Width; x += tileWidth, newX += newTileWidth)
                    {
                        for (int y = 0; y + tileHeight <= bmp.Height; y += tileHeight)
                        {
                            Bitmap img = null;
                            if (addRightMarginForSegaSaturn)
                            {
                                img = new Bitmap(newTileWidth, tileHeight, bmp.PixelFormat);
                                using (var graphics = Graphics.FromImage(img))
                                {
                                    graphics.Clear(defaultColor);
                                    graphics.DrawImage(bmp, new Rectangle(0, 0, tileWidth, tileHeight), new Rectangle(x, y, tileWidth, tileHeight), GraphicsUnit.Pixel);
                                }
                            }
                            else
                            {
                                img = JoMapEditorTools.CropBitmap(bmp, new Rectangle(x, y, tileWidth, tileHeight));
                            }
                            if (!img.IsTransparent())
                            {
                                Sprite sprite = new Sprite
                                {
                                    Img  = img,
                                    Path = null
                                };
                                this.AddTile(sprite, new Point(src.Left + (int)(newX * this._zoom), src.Top + (int)(y * this._zoom)), src.Attribute);
                            }
                        }
                    }
                }
            }
            finally
            {
                this.ResumeLayout();
                Cursor.Current = Cursors.Default;
            }
        }
Beispiel #3
0
        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);
        }