From b1883d5d48588321999d0aa896bcd9cf03792a83 Mon Sep 17 00:00:00 2001 From: triangle drawer <48007646+t18b219k@users.noreply.github.com> Date: Sat, 13 Mar 2021 20:41:51 +0900 Subject: [PATCH] Add functions to register textures in egui_web and egui_glium (#226) * add texture registering function * fmt * Revert "add texture registering function" This reverts commit f9b4db12 * make get_texture public to get render target owned by Painter . * revert egui_web painter.rs change --- egui_glium/src/painter.rs | 20 ++++++++++++++++++-- egui_web/src/webgl1.rs | 15 ++++++++++++--- egui_web/src/webgl2.rs | 14 ++++++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index cb254183..b6876cb1 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -250,7 +250,23 @@ impl Painter { self.user_textures.push(Some(Default::default())); id } - + /// register glium texture as egui texture + /// Usable for render to image rectangle + pub fn register_glium_texture( + &mut self, + texture: glium::texture::SrgbTexture2d, + ) -> egui::TextureId { + let id = self.alloc_user_texture(); + if let egui::TextureId::User(id) = id { + if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) { + *user_texture = UserTexture { + pixels: vec![], + gl_texture: Some(texture), + } + } + } + id + } pub fn set_user_texture( &mut self, id: egui::TextureId, @@ -283,7 +299,7 @@ impl Painter { } } - fn get_texture(&self, texture_id: egui::TextureId) -> Option<&SrgbTexture2d> { + pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<&SrgbTexture2d> { match texture_id { egui::TextureId::Egui => self.egui_texture.as_ref(), egui::TextureId::User(id) => self diff --git a/egui_web/src/webgl1.rs b/egui_web/src/webgl1.rs index 855d5bec..994bacf2 100644 --- a/egui_web/src/webgl1.rs +++ b/egui_web/src/webgl1.rs @@ -136,8 +136,7 @@ impl WebGlPainter { } } } - - fn get_texture(&self, texture_id: egui::TextureId) -> Option<&WebGlTexture> { + pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<&WebGlTexture> { match texture_id { egui::TextureId::Egui => Some(&self.egui_texture), egui::TextureId::User(id) => self @@ -190,7 +189,17 @@ impl WebGlPainter { } } } - + pub fn register_webgl_texture(&mut self, texture: WebGlTexture) -> egui::TextureId { + let id = self.alloc_user_texture_index(); + if let Some(Some(user_texture)) = self.user_textures.get_mut(id) { + *user_texture = UserTexture { + size: (0, 0), + pixels: vec![], + gl_texture: Some(texture), + } + } + egui::TextureId::User(id as u64) + } fn paint_mesh(&self, mesh: &egui::epaint::Mesh16) -> Result<(), JsValue> { debug_assert!(mesh.is_valid()); diff --git a/egui_web/src/webgl2.rs b/egui_web/src/webgl2.rs index 7e741be4..f0457c5c 100644 --- a/egui_web/src/webgl2.rs +++ b/egui_web/src/webgl2.rs @@ -139,7 +139,7 @@ impl WebGl2Painter { } } - fn get_texture(&self, texture_id: egui::TextureId) -> Option<&WebGlTexture> { + pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<&WebGlTexture> { match texture_id { egui::TextureId::Egui => Some(&self.egui_texture), egui::TextureId::User(id) => self @@ -192,7 +192,17 @@ impl WebGl2Painter { } } } - + pub fn register_webgl_texture(&mut self, texture: WebGlTexture) -> egui::TextureId { + let id = self.alloc_user_texture_index(); + if let Some(Some(user_texture)) = self.user_textures.get_mut(id) { + *user_texture = UserTexture { + size: (0, 0), + pixels: vec![], + gl_texture: Some(texture), + } + } + egui::TextureId::User(id as u64) + } fn paint_mesh(&self, mesh: &egui::epaint::Mesh16) -> Result<(), JsValue> { debug_assert!(mesh.is_valid());