/// <summary> /// Gets the texture object for a specific texture name. /// </summary> /// <param name="texturename">The name of the texture.</param> /// <returns>A valid texture object.</returns> public Texture GetTexture(string texturename) { texturename = FileEngine.CleanFileName(texturename); if (LoadedTextures.TryGetValue(texturename, out Texture foundTexture)) { return(foundTexture); } Texture Loaded = LoadTexture(texturename, 0); if (Loaded == null) { Loaded = new Texture() { Engine = this, Name = texturename, Internal_Texture = White.Original_InternalID, Original_InternalID = White.Original_InternalID, LoadedProperly = false, Width = White.Width, Height = White.Height, }; } LoadedTextures.Add(texturename, Loaded); OnTextureLoaded?.Invoke(this, new TextureLoadedEventArgs(Loaded)); return(Loaded); }
/// <summary> /// Gets the a bitmap object for a texture by name. /// </summary> /// <param name="texturename">The name of the texture.</param> /// <param name="twidth">The texture width, if any.</param> /// <returns>A valid bitmap object, or null.</returns> public Bitmap GetTextureBitmapWithWidth(string texturename, int twidth) { texturename = FileEngine.CleanFileName(texturename); if (LoadedTextures.TryGetValue(texturename, out Texture foundTexture) && foundTexture.LoadedProperly) { if (foundTexture.Width == twidth && foundTexture.Height == twidth) { return(foundTexture.SaveToBMP()); } } return(LoadBitmapForTexture(texturename, twidth)); }
/// <summary> /// Loads a shader from file. /// </summary> /// <param name="filename">The name of the file to use.</param> /// <returns>The loaded shader, or null if it does not exist.</returns> public Shader LoadShader(string filename) { try { string oname = filename; string[] datg = filename.SplitFast('?', 1); string geom = datg.Length > 1 ? datg[1] : null; string[] dat1 = datg[0].SplitFast('#', 1); string[] vars = new string[0]; if (dat1.Length == 2) { vars = dat1[1].SplitFast(','); } filename = FileEngine.CleanFileName(dat1[0]); if (!File.Exists("shaders/" + filename + ".vs")) { SysConsole.Output(OutputType.WARNING, "Cannot load vertex shader, file '" + TextStyle.Standout + "shaders/" + filename + ".vs" + TextStyle.Base + "' does not exist."); return(null); } if (!File.Exists("shaders/" + filename + ".fs")) { SysConsole.Output(OutputType.WARNING, "Cannot load fragment shader, file '" + TextStyle.Standout + "shaders/" + filename + ".fs" + TextStyle.Base + "' does not exist."); return(null); } string VS = GetShaderFileText(filename + ".vs"); string FS = GetShaderFileText(filename + ".fs"); string GS = null; if (geom != null) { geom = FileEngine.CleanFileName(geom); if (!File.Exists("shaders/" + geom + ".geom")) { SysConsole.Output(OutputType.WARNING, "Cannot load geomry shader, file '" + TextStyle.Standout + "shaders/" + geom + ".geom" + TextStyle.Base + "' does not exist."); return(null); } GS = GetShaderFileText(geom + ".geom"); } return(CreateShader(VS, FS, oname, vars, GS)); } catch (Exception ex) { SysConsole.Output(OutputType.ERROR, "Failed to load shader from filename '" + TextStyle.Standout + "shaders/" + filename + ".fs or .vs" + TextStyle.Base + "': " + ex.ToString()); return(null); } }
/// <summary> /// Gets the texture object for a specific texture name. /// </summary> /// <param name="textureName">The name of the texture.</param> /// <returns>A valid texture object.</returns> public Texture GetTexture(string textureName) { textureName = FileEngine.CleanFileName(textureName); if (LoadedTextures.TryGetValue(textureName, out Texture foundTexture)) { return(foundTexture); } Texture loaded = DynamicLoadTexture(textureName); LoadedTextures.Add(textureName, loaded); OnTextureLoaded?.Invoke(this, new TextureLoadedEventArgs(loaded)); return(loaded); }
/// <summary> /// Dynamically loads a texture (returns a temporary copy of 'White', then fills it in when possible). /// </summary> /// <param name="textureName">The texture name to load.</param> /// <returns>The texture object.</returns> public Texture DynamicLoadTexture(string textureName) { textureName = FileEngine.CleanFileName(textureName); Texture texture = new Texture() { Engine = this, Name = textureName, Original_InternalID = White.Original_InternalID, Internal_Texture = White.Internal_Texture, LoadedProperly = false, Width = White.Width, Height = White.Height }; void processLoad(byte[] data) { Bitmap bmp = BitmapForBytes(data); Schedule.ScheduleSyncTask(() => { TextureFromBitMap(texture, bmp); texture.LoadedProperly = true; texture.Goal.SyncFollowUp?.Invoke(); texture.Goal = null; bmp.Dispose(); }); } void fileMissing() { SysConsole.Output(OutputType.WARNING, $"Cannot load texture, file '{TextStyle.Standout}textures/{textureName}.png{TextStyle.Base}' does not exist."); texture.LoadedProperly = false; } void handleError(string message) { SysConsole.Output(OutputType.ERROR, $"Failed to load texture from filename '{TextStyle.Standout}textures/{textureName}.png{TextStyle.Error}': {message}"); texture.LoadedProperly = false; } texture.Goal = AssetStreaming.AddGoal($"textures/{textureName}.png", false, processLoad, fileMissing, handleError); return(texture); }
/// <summary> /// Gets the text of a shader file, automatically handling the file cache. /// </summary> /// <param name="filename">The name of the shader file.</param> /// <returns></returns> public string GetShaderFileText(string filename) { filename = FileEngine.CleanFileName(filename.Trim()); if (!File.Exists("shaders/" + filename)) { throw new Exception("File " + filename + " does not exist, but was included by a shader!"); } if (ShaderFilesCache.TryGetValue(filename, out string filedata)) { return(filedata); } if (ShaderFilesCache.Count > 128) // TODO: Configurable? { ShaderFilesCache.Clear(); } string newData = Includes(File.ReadAllText("shaders/" + filename).Replace("\r\n", "\n").Replace("\r", "")); ShaderFilesCache[filename] = newData; return(newData); }
/// <summary> /// Compiles a compute shader by name to a shader. /// </summary> /// <param name="fname">The file name.</param> /// <param name="specialadder">Special additions (EG defines)</param> /// <returns>The shader program.</returns> public int CompileCompute(string fname, string specialadder = "") { fname = FileEngine.CleanFileName(fname.Trim()); string ftxt = GetShaderFileText(fname + ".comp"); int index = ftxt.IndexOf(FILE_START); if (index < 0) { index = 0; } else { index += FILE_START.Length; } ftxt = ftxt.Insert(index, specialadder); int shd = GL.CreateShader(ShaderType.ComputeShader); GL.ShaderSource(shd, ftxt); GL.CompileShader(shd); string SHD_Info = GL.GetShaderInfoLog(shd); GL.GetShader(shd, ShaderParameter.CompileStatus, out int SHD_Status); if (SHD_Status != 1) { throw new Exception("Error creating ComputeShader. Error status: " + SHD_Status + ", info: " + SHD_Info); } int program = GL.CreateProgram(); GL.AttachShader(program, shd); GL.LinkProgram(program); string str = GL.GetProgramInfoLog(program); if (str.Length != 0) { SysConsole.Output(OutputType.INFO, "Linked shader with message: '" + str + "'" + " -- FOR -- " + ftxt); } GL.DeleteShader(shd); GraphicsUtil.CheckError("Shader - Compute - Compile"); return(program); }
/// <summary> /// Loads a texture's bitmap from file. /// </summary> /// <param name="filename">The name of the file to use.</param> /// <param name="twidth">The texture width, if any.</param> /// <returns>The loaded texture bitmap, or null if it does not exist.</returns> public Bitmap LoadBitmapForTexture(string filename, int twidth) { try { filename = FileEngine.CleanFileName(filename); if (!Files.TryReadFileData($"textures/{filename}.png", out byte[] textureFile))
/// <summary> /// Loads a model by name. /// </summary> /// <param name="filename">The name.</param> /// <returns>The model.</returns> public Model LoadModel(string filename) { try { filename = FileEngine.CleanFileName(filename); if (!TheClient.Files.TryReadFileData("models/" + filename + ".vmd", out byte[] bits))