public void LoadTexture(NamespacedId id) { if (LoadedTextures.ContainsKey(id)) { Log.Info($"Texture {id} is already loaded. Reloading..."); LoadedTextures[id].Dispose(); } Log.Info($"Loading texture {id}..."); try { var data = ImageLoader.Load(ResolveTexturePath(id)); if (data == null) { throw new Exception("Not found"); } LoadedTextures[id] = Engine.Renderer.CreateTexture( data.Value.Data, data.Value.Width, data.Value.Height); } catch (Exception e) { Log.Error($"Couldn't load texture {id}"); Log.Error($"{e.GetType().Name}: {e.Message}"); Log.Error($"{e.StackTrace}"); LoadedTextures[id] = ErrorTexture; #if DEBUG throw; #endif } }
/// <summary> /// </summary> /// <param name="drawData"></param> /// <exception cref="InvalidOperationException"></exception> private void RenderCommandLists(ImDrawDataPtr drawData) { var lastIndecies = GraphicsDevice.Indices; var lastScissorRectangle = GraphicsDevice.ScissorRectangle; GraphicsDevice.SetVertexBuffer(VertexBuffer); GraphicsDevice.Indices = IndexBuffer; var vtxOffset = 0; var idxOffset = 0; for (var n = 0; n < drawData.CmdListsCount; n++) { var cmdList = drawData.CmdListsRange[n]; for (var cmdi = 0; cmdi < cmdList.CmdBuffer.Size; cmdi++) { var drawCmd = cmdList.CmdBuffer[cmdi]; if (!LoadedTextures.ContainsKey(drawCmd.TextureId)) { throw new InvalidOperationException($"Could not find a texture with id '{drawCmd.TextureId}', please check your bindings"); } GraphicsDevice.ScissorRectangle = new Rectangle( (int)drawCmd.ClipRect.X, (int)drawCmd.ClipRect.Y, (int)(drawCmd.ClipRect.Z - drawCmd.ClipRect.X), (int)(drawCmd.ClipRect.W - drawCmd.ClipRect.Y) ); var effect = UpdateEffect(LoadedTextures[drawCmd.TextureId]); foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); #pragma warning disable CS0618 // // FNA does not expose an alternative method. GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, vtxOffset, 0, cmdList.VtxBuffer.Size, idxOffset, (int)drawCmd.ElemCount / 3 ); #pragma warning restore CS0618 } idxOffset += (int)drawCmd.ElemCount; } vtxOffset += cmdList.VtxBuffer.Size; } GraphicsDevice.SetVertexBuffer(null); GraphicsDevice.Indices = lastIndecies; GraphicsDevice.ScissorRectangle = lastScissorRectangle; }
public Texture Get(NamespacedId id, bool loadIfUnloaded = true) { if (!LoadedTextures.ContainsKey(id)) { if (!loadIfUnloaded) { return(ErrorTexture); } LoadTexture(id); } return(LoadedTextures[id]); }
public static Texture2D GetTexture(string texture) { if (LoadedTextures.ContainsKey(texture)) { return(LoadedTextures[texture]); } Debug.Log($"{texture} is not already loaded."); WWW www = new WWW(ImageRepository.GetPathInModFolder(texture)); //Debug.Log($"{texture} - {www.texture.width.ToString()}x{www.texture.height.ToString()}"); Texture2D tex = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false); tex.filterMode = ImageRepository.Files[texture].FilterMode; www.LoadImageIntoTexture(tex); LoadedTextures[texture] = tex; return(tex); }