拖动导入模型帧率异常修复,添加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
import os
import io
import tempfile
import time
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:
@ -36,10 +43,10 @@ class EditorPanels:
self.app.web_panel_url_input = "https://www.example.com"
if not hasattr(self.app, "_imgui_webview"):
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"):
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):
self._ensure_web_panel_state()
@ -76,7 +83,13 @@ class EditorPanels:
except Exception:
pass
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):
self._ensure_web_panel_state()
@ -102,30 +115,41 @@ class EditorPanels:
if not jpeg_bytes:
return
pnm = PNMImage()
if not pnm.read(StringStream(jpeg_bytes), "imgui_webview.png"):
try:
if Image is None:
self.app.add_warning_message("缺少 Pillow无法更新Web纹理")
return
img = Image.open(io.BytesIO(jpeg_bytes)).convert("RGBA")
# p3dimgui 纹理坐标系与网页截图存在Y轴方向差异先在像素层修正
img = img.transpose(Image.FLIP_TOP_BOTTOM)
temp_dir = Path(tempfile.gettempdir()) / "eg_imgui_webpanel"
temp_dir.mkdir(parents=True, exist_ok=True)
temp_path = temp_dir / f"webview_{time.time_ns()}.png"
img.save(temp_path, format="PNG")
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)
if old_tex_id:
try:
self.app.imgui.removeTexture(old_tex_id)
except Exception:
pass
old_texture_path = getattr(self.app, "_imgui_webview_texture_path", None)
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
return
# p3dimgui 纹理坐标系与网页截图存在Y轴方向差异先在像素层修正
pnm.flip(False, True, False)
# 某些后端在“已注册纹理原位更新”时存在稳定性问题。
# 改为每次创建新纹理并替换旧纹理ID避免原位更新导致崩溃。
texture = Texture("imgui_web_panel_texture")
texture.load(pnm)
new_tex_id = self.app.imgui.loadTexture(texture)
old_tex_id = getattr(self.app, "_imgui_webview_tex_id", None)
if old_tex_id:
try:
self.app.imgui.removeTexture(old_tex_id)
except Exception:
pass
self.app._imgui_webview_texture = texture
self.app._imgui_webview_tex_id = new_tex_id
webview.tex_dirty = False
@staticmethod
def _clamp01(value):