Exemple #1
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;
        }
        //TODO - finish + create model from texture if model not found
        private int LoadItems(Shader <ModelBlock> blockShader)
        {
            string dirBlock = $"{SharpCraft.Instance.GameFolderDir}\\SharpCraft_Data\\assets\\models\\block";
            string dirItem  = $"{SharpCraft.Instance.GameFolderDir}\\SharpCraft_Data\\assets\\models\\item";

            if (!Directory.Exists(dirBlock) || !Directory.Exists(dirItem))
            {
                return(0);
            }

            // string[] files = Directory.GetFiles(dir); //TODO - ONLY LOAD JSONS FOR REGISTERED BLOCKS!

            var listOfItems = ItemRegistry.AllItems();

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

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

            foreach (var iitem in listOfItems)
            {
                string file = "";

                bool isItemBlock = false;

                if (iitem is ItemBlock itemBlock)
                {
                    isItemBlock = true;
                    file        = $"{dirBlock}\\{itemBlock.GetUnlocalizedName()}.json";
                }
                else if (iitem is Item item)
                {
                    file = $"{dirItem}\\{item.GetUnlocalizedName()}.json";
                }

                if (!File.Exists(file))
                {
                    continue;
                }

                JsonBlockModel bjm = JsonConvert.DeserializeObject <JsonBlockModel>(File.ReadAllText(file));

                string itemName = Path.GetFileNameWithoutExtension(file);

                itemModels.TryAdd(itemName, bjm); //save what block is using what model

                if (isItemBlock)
                {
                    continue;
                }

                foreach (var pair in bjm.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
                    }
                }
            }

            var textureMapElements = new Dictionary <string, TextureMapElement>();   //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, textureMapElements); // stitch all textureMap, return the texture ID of the registered texture in VRAM

            foreach (var pair in itemModels)                                         //one model per registered block
            {
                string         name  = pair.Key;
                JsonBlockModel model = pair.Value;

                float[] vertexes = new float[72 * model.cubes.Length];
                float[] normals  = new float[72 * model.cubes.Length];
                float[] uvs      = new float[48 * model.cubes.Length];

                for (var index = 0; index < model.cubes.Length; index++)
                {
                    var cube = model.cubes[index];

                    CubeModelBuilder.AppendCubeModel(cube, model.textures, textureMapElements, ref vertexes, ref normals, ref uvs, index);
                }

                //ModelBlock mb = new ModelBlock(blockShader, ModelManager.LoadBlockModelToVao(vertexes, normals, uvs));

                //_itemModels.Add(name, mb);
            }

            return(id);
        }