示例#1
0
        private List <JsonModel> LoadModel(string file, string defaultTextureVar)
        {
            List <JsonModel> models = new List <JsonModel>();

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

                cube.faces = new Dictionary <Facing, JsonCubeFaceUv>
                {
                    { Facing.north, uv },
                    { Facing.south, uv },
                    { Facing.west, uv },
                    { Facing.east, uv },
                    { Facing.up, uv },
                    { Facing.down, uv }
                };

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

                var bjm = new JsonModel
                {
                    cubes = new[]
                    {
                        cube
                    },
                    textures = new Dictionary <string, string> {
                        { defaultTextureVar, "" }
                    }
                };

                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));
                    }
                }

                models.Reverse();
            }

            return(models);
        }
示例#2
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;
        }