拖动导入模型帧率异常修复,添加web视图

This commit is contained in:
Hector 2026-02-26 18:04:12 +08:00
parent 79b9aa44dc
commit b3d758a3e3

View File

@ -1,7 +1,14 @@
from imgui_bundle import imgui, imgui_ctx from imgui_bundle import imgui, imgui_ctx
import os import os
import io
import tempfile
import time
from pathlib import Path from pathlib import Path
from panda3d.core import PNMImage, StringStream, Texture from panda3d.core import Filename
try:
from PIL import Image
except Exception: # pragma: no cover - pillow may be missing in minimal env
Image = None
class EditorPanels: class EditorPanels:
@ -36,10 +43,10 @@ class EditorPanels:
self.app.web_panel_url_input = "https://www.example.com" self.app.web_panel_url_input = "https://www.example.com"
if not hasattr(self.app, "_imgui_webview"): if not hasattr(self.app, "_imgui_webview"):
self.app._imgui_webview = None self.app._imgui_webview = None
if not hasattr(self.app, "_imgui_webview_texture"):
self.app._imgui_webview_texture = None
if not hasattr(self.app, "_imgui_webview_tex_id"): if not hasattr(self.app, "_imgui_webview_tex_id"):
self.app._imgui_webview_tex_id = None self.app._imgui_webview_tex_id = None
if not hasattr(self.app, "_imgui_webview_texture_path"):
self.app._imgui_webview_texture_path = None
def _start_imgui_webview(self, url): def _start_imgui_webview(self, url):
self._ensure_web_panel_state() self._ensure_web_panel_state()
@ -76,7 +83,13 @@ class EditorPanels:
except Exception: except Exception:
pass pass
self.app._imgui_webview_tex_id = None self.app._imgui_webview_tex_id = None
self.app._imgui_webview_texture = None texture_path = getattr(self.app, "_imgui_webview_texture_path", None)
if texture_path:
try:
Path(texture_path).unlink(missing_ok=True)
except Exception:
pass
self.app._imgui_webview_texture_path = None
def _navigate_web_panel(self): def _navigate_web_panel(self):
self._ensure_web_panel_state() self._ensure_web_panel_state()
@ -102,19 +115,21 @@ class EditorPanels:
if not jpeg_bytes: if not jpeg_bytes:
return return
pnm = PNMImage() try:
if not pnm.read(StringStream(jpeg_bytes), "imgui_webview.png"): if Image is None:
webview.tex_dirty = False self.app.add_warning_message("缺少 Pillow无法更新Web纹理")
return return
img = Image.open(io.BytesIO(jpeg_bytes)).convert("RGBA")
# p3dimgui 纹理坐标系与网页截图存在Y轴方向差异先在像素层修正 # p3dimgui 纹理坐标系与网页截图存在Y轴方向差异先在像素层修正
pnm.flip(False, True, False) img = img.transpose(Image.FLIP_TOP_BOTTOM)
# 某些后端在“已注册纹理原位更新”时存在稳定性问题。 temp_dir = Path(tempfile.gettempdir()) / "eg_imgui_webpanel"
# 改为每次创建新纹理并替换旧纹理ID避免原位更新导致崩溃。 temp_dir.mkdir(parents=True, exist_ok=True)
texture = Texture("imgui_web_panel_texture") temp_path = temp_dir / f"webview_{time.time_ns()}.png"
texture.load(pnm) img.save(temp_path, format="PNG")
new_tex_id = self.app.imgui.loadTexture(texture)
panda_path = Filename.fromOsSpecific(str(temp_path)).getFullpath()
new_tex_id = self.app.imgui.loadTexture(panda_path)
old_tex_id = getattr(self.app, "_imgui_webview_tex_id", None) old_tex_id = getattr(self.app, "_imgui_webview_tex_id", None)
if old_tex_id: if old_tex_id:
try: try:
@ -122,9 +137,18 @@ class EditorPanels:
except Exception: except Exception:
pass pass
self.app._imgui_webview_texture = texture old_texture_path = getattr(self.app, "_imgui_webview_texture_path", None)
self.app._imgui_webview_tex_id = new_tex_id if old_texture_path:
try:
Path(old_texture_path).unlink(missing_ok=True)
except Exception:
pass
self.app._imgui_webview_tex_id = new_tex_id
self.app._imgui_webview_texture_path = str(temp_path)
except Exception as e:
self.app.add_warning_message(f"Web纹理更新失败: {e}")
finally:
webview.tex_dirty = False webview.tex_dirty = False
@staticmethod @staticmethod