コード例 #1
0
        private static int Stitch(string[] allTextures, int textureSize, out Dictionary <string, TextureMapElement> sprites)
        {
            int id;

            sprites = new Dictionary <string, TextureMapElement>();

            using (var map = new Bitmap(256, 256))
            {
                int countX = 0;
                int countY = 0;

                foreach (var texName in allTextures)
                {
                    Vector2 start = new Vector2((float)countX / map.Width, (float)countY / map.Height);
                    Vector2 end   = start + new Vector2((float)textureSize / map.Width, (float)textureSize / map.Height);

                    TextureMapElement mapElement = new TextureMapElement(start, end);

                    WriteBitmap(map, texName, textureSize, ref countX, ref countY);

                    sprites.Add(texName, mapElement);
                }

                map.Save("debug.png");

                var mipmaps = SettingsManager.GetBool("mipmap");

                id = mipmaps ? TextureManager.LoadTextureWithMipMap(map) : TextureManager.LoadTexture(map);
            }

            return(id);
        }
コード例 #2
0
        private static int Stitch(string[] allTextures, int textureSize, Dictionary <string, TextureMapElement> sprites)
        {
            Bitmap map = new Bitmap(256, 256);

            int id;

            using (map)
            {
                int countX = 0;
                int countY = 0;

                foreach (var texName in allTextures)
                {
                    Vector2 start = new Vector2((float)countX / map.Width, (float)countY / map.Height);
                    Vector2 end   = start + new Vector2((float)textureSize / map.Width, (float)textureSize / map.Height);

                    TextureMapElement mapElement = new TextureMapElement(start, end);

                    WriteBitmap(map, texName, textureSize, ref countX, ref countY);

                    sprites.Add(texName, mapElement);
                }

                map.Save("debug.png");

                id = TextureManager.LoadTexture(map);
            }

            return(id);
        }
コード例 #3
0
        public void AddTexture(SharpDX.Direct3D11.Texture2D texture)
        {
            using (texture)
            {
                var srv = new ShaderResourceView(m_d3dDevice, texture);
                TextureMapElement tme = new TextureMapElement();
                tme.srv  = srv;
                tme.size = new Vector2((float)texture.Description.Width, (float)texture.Description.Height);

                m_textureMap.Add(texture, tme);
            }
        }
コード例 #4
0
        public void AddTexture(SharpDX.Direct3D11.Texture2D texture)
        {
            using (texture)
            {
                var srv = new ShaderResourceView(m_d3dDevice, texture);
                TextureMapElement tme = new TextureMapElement();
                tme.srv = srv;
                tme.size = new Vector2((float)texture.Description.Width, (float)texture.Description.Height);

                m_textureMap.Add(texture, tme);

            }
        }
コード例 #5
0
 public ModelBlock(TextureMapElement particleTexture, Shader <ModelBlock> shader, ModelBlockRaw rawModel) : base(null, shader)
 {
     RawModel         = rawModel;
     _particleTexture = particleTexture;
 }
コード例 #6
0
 public ModelItem(TextureMapElement texture, Shader shader, ModelItemRaw rawModel) : base(rawModel, shader)
 {
     SlotTexture = texture;
 }
コード例 #7
0
 public ModelBlock(TextureMapElement slotTexture, TextureMapElement particleTexture, Shader shader, ModelBlockRaw rawModel) : base(rawModel, shader)
 {
     SlotTexture     = slotTexture;
     ParticleTexture = particleTexture;
 }
コード例 #8
0
        public void LoadItemModels()
        {
            string dir = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/models/item";

            var listOfItems = ItemRegistry.AllItems();

            List <string> nonDuplicateTextures = new List <string>();

            var itemModels = new ConcurrentDictionary <string, List <JsonModel> >();

            foreach (var item in listOfItems)
            {
                if (item is ItemBlock)
                {
                    continue;
                }

                string unlocalizedLast = LangUtil.GetUnlocalizedNameLast(item.UnlocalizedName);

                string file = $"{dir}/{unlocalizedLast}.json";

                var models = new List <JsonModel>();

                if (!File.Exists(file))
                {
                    var cube = new JsonCube();
                    var uv   = new JsonCubeFaceUv {
                        texture = "item"
                    };

                    cube.faces = new Dictionary <Facing, JsonCubeFaceUv>
                    {
                        { Facing.west, uv }
                    };

                    cube.from = new[] { 8f, 0, 0 };
                    cube.to   = new[] { 8f, 16, 16 };

                    var bjm = new JsonModel
                    {
                        cubes = new[]
                        {
                            cube
                        },
                        textures = new Dictionary <string, string> {
                            { "item", "items/" + unlocalizedLast }
                        }
                    };

                    models.Add(bjm);
                }
                else
                {
                    models.Add(FixBlockJson(file));

                    while (models.Last() is JsonModel jm && !string.IsNullOrEmpty(jm.inherit))
                    {
                        string inhertiedFile = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/models/{jm.inherit}.json";

                        if (File.Exists(inhertiedFile))
                        {
                            models.Add(FixBlockJson(inhertiedFile));
                        }
                        else
                        {
                            break;
                        }
                    }

                    models.Reverse();
                }

                itemModels.TryAdd(item.UnlocalizedName, models); //save what block is using what model

                foreach (var jsonModel in models)
                {
                    if (jsonModel.textures == null)
                    {
                        continue;
                    }

                    foreach (var pair in jsonModel.textures) //iterating over the textureMap in the Json model
                    {
                        if (!nonDuplicateTextures.Contains(pair.Value))
                        {
                            nonDuplicateTextures
                            .Add(pair
                                 .Value);    //add the current texture name to a list of all textureMap if isn't already there
                        }
                    }
                }
            }

            //each texture name has it's UV values TODO - maybe make a TextureMap class where this could be used
            var id = Stitch(nonDuplicateTextures.ToArray(), 16, out var textureMapElements); // stitch all textureMap, return the texture ID of the registered texture in VRAM

            //TODO - if json doesn't contain cube model, assume it's a full cube
            foreach (var pair in itemModels) //one model per registered item
            {
                string name = pair.Key;

                TextureMapElement tme = null;

                List <float> vertexes = new List <float>();
                List <float> normals  = new List <float>();
                List <float> uvs      = new List <float>();

                List <JsonCube> cubes = new List <JsonCube>();

                Dictionary <string, string> textures = new Dictionary <string, string>();

                foreach (var model in pair.Value)
                {
                    foreach (var cube in model.cubes)
                    {
                        cubes.Add(cube);
                    }

                    foreach (var pairtex in model.textures)
                    {
                        textures.Remove(pairtex.Key);
                        textures.Add(pairtex.Key, pairtex.Value);
                    }

                    if (model.textures.TryGetValue("item", out var texName))
                    {
                        textureMapElements.TryGetValue(texName, out tme);
                    }
                }

                foreach (var cube in cubes)
                {
                    CubeModelBuilder.AppendCubeModel(cube, textures, textureMapElements, ref vertexes,
                                                     ref normals, ref uvs);
                }

                ModelItem mi = new ModelItem(tme, _itemShader, ModelManager.LoadItemModelToVao(vertexes.ToArray(), normals.ToArray(), uvs.ToArray()));

                ItemModels.TryAdd(name, mi);
            }

            TextureItems = id;
        }