public static void LoadModel(string path, Shader <ModelCustom> shader) //TODO { string file = $"{SharpCraft.Instance.GameFolderDir}\\SharpCraft_Data\\assets\\models\\{path}.json"; if (!File.Exists(file)) { return; } List <string> nonDuplicateTextures = new List <string>(); JsonBlockModel model = FixBlockJson(file); 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 } } 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); //TODO - make the texture size variable 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); } var customModel = new ModelCustom(id, ModelManager.LoadModel3ToVao(vertexes, normals, uvs), shader); _customModels.Add(path, customModel); //return customModel; }
private static int LoadBlocks() { string dir = $"{SharpCraft.Instance.GameFolderDir}\\SharpCraft_Data\\assets\\models\\block"; if (!Directory.Exists(dir)) { return(0); } var listOfBlocks = BlockRegistry.AllBlocks(); List <string> nonDuplicateTextures = new List <string>(); var blockModels = new ConcurrentDictionary <string, JsonBlockModel>(); foreach (var block in listOfBlocks) { string file = $"{dir}\\{block.UnlocalizedName}.json"; if (!File.Exists(file)) { continue; } JsonBlockModel bjm = FixBlockJson(file); string blockName = Path.GetFileNameWithoutExtension(file); blockModels.TryAdd(blockName, bjm); //save what block is using what model 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 //TODO - if json doesn't contain cube model, assume it's a full cube foreach (var pair in blockModels) //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); } string particleTextureName; if (!model.textures.TryGetValue("particle", out particleTextureName)) { particleTextureName = model.textures.Values.ToArray()[SharpCraft.Instance.Random.Next(0, model.textures.Count)]; } var tme = textureMapElements[particleTextureName]; ModelBlock mb = new ModelBlock(tme, _blockShader, ModelManager.LoadBlockModelToVao(vertexes, normals, uvs)); _blockModels.TryAdd(name, mb); } return(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); }