Example #1
0
        public static bool LoadCustomModel(string path, Shader shader)
        {
            string file = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/models/{path}.json";

            if (!File.Exists(file))
            {
                return(false);
            }

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

            List <JsonModel> models = new List <JsonModel> {
                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();

            foreach (var model in models)
            {
                foreach (var pair in model.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); //TODO - make the texture size variable

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

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

            var customModel = new ModelCustom(id, ModelManager.LoadModel3ToVao(vertexes.ToArray(), normals.ToArray(), uvs.ToArray()), shader);

            CustomModels.Add(path, customModel);

            return(true);
        }
Example #2
0
        public void LoadBlockModels()
        {
            string modelsDir      = $"{SharpCraft.Instance.GameFolderDir}/assets/sharpcraft/models";
            string blockModelsDir = $"{modelsDir}/block";
            string blockStatesDir = $"{modelsDir}/block/states";

            var listOfBlocks = BlockRegistry.AllBlocks();

            var nonDuplicateTextures = new List <string>();

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

            foreach (var block in listOfBlocks)              // for each Block that's been registered
            {
                var states = new List <List <JsonModel> >(); //save state models for each block
                blockModels.TryAdd(block.UnlocalizedName, states);

                string        unlocalizedLast = LangUtil.GetUnlocalizedNameLast(block.UnlocalizedName);
                List <string> stateFiles      = new List <string> {
                    $"{blockModelsDir}/{unlocalizedLast}.json"
                };

                string statesFile = $"{blockStatesDir}/{unlocalizedLast}.json";

                if (File.Exists(statesFile))
                {
                    try
                    {
                        string          json = File.ReadAllText(statesFile);
                        JsonBlockStates jbs  = JsonConvert.DeserializeObject <JsonBlockStates>(json);

                        if (jbs.states != null)
                        {
                            foreach (var modelFileName in jbs.states)
                            {
                                var stateFile = $"{modelsDir}/{modelFileName.model}.json";

                                if (File.Exists(stateFile))
                                {
                                    stateFiles.Add(stateFile);
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }

                foreach (var stateFile in stateFiles)
                {
                    //load state
                    var models = LoadModel(stateFile, "particle");

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

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

                    states.Add(models);
                }
            }

            //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 blockModels) //one model per registered item
            {
                string blockName = pair.Key;

                var states = new List <ModelBlock>();

                foreach (var state in pair.Value)
                {
                    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 jsonModel in state)
                    {
                        foreach (var cube in jsonModel.cubes)
                        {
                            cubes.Add(cube);
                        }

                        if (jsonModel.textures == null)
                        {
                            continue;
                        }

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

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

                    if (!textures.TryGetValue("particle", out var particleTexture))
                    {
                        particleTexture = textures.Values.ToArray()[SharpCraft.Instance.Random.Next(0, textures.Count)];
                    }
                    if (!textures.TryGetValue("item", out var slotTexture))
                    {
                        if (cubes.Count > 0 && cubes[0].faces.TryGetValue(Facing.south, out var uv))
                        {
                            slotTexture = textures[uv.texture];
                        }
                    }

                    var particleTme = textureMapElements[particleTexture];
                    var slotTme     = textureMapElements.TryGetValue(slotTexture ?? "", out var result)
                        ? result
                        : particleTme;

                    ModelBlock mb = new ModelBlock(slotTme, particleTme, _blockShader, ModelManager.LoadBlockModelToVao(vertexes.ToArray(), normals.ToArray(), uvs.ToArray()));

                    states.Add(mb);
                }

                BlockStateModels.TryAdd(blockName, states);
            }

            TextureBlocks = id;
        }
Example #3
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;
        }