You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Delphi FMX中动态生成的TPanel内容渲染到3D平面TPlane上?

将动态TPanel内容渲染到FMX 3D平面的实现方案

核心思路

通过定时捕获TPanel的动态内容到TBitmap,再将该Bitmap赋值给TTextureMaterialSource.Texture,让3D平面实时同步Panel的内容变化。

具体步骤

1. 准备组件

确保项目中已添加以下控件:

  • TViewport3D(承载3D场景)
  • TPlane(需要渲染纹理的3D平面,需将其MaterialSource绑定到TTextureMaterialSource)
  • TTextureMaterialSource(管理3D纹理资源)
  • TPanel(动态内容载体,示例中命名为PanelDynamic)
  • TTimer(定时触发纹理更新,示例中命名为TimerUpdateTexture)

2. 配置Timer

设置TimerUpdateTexture.Interval为30-60(对应30-60帧/秒),根据动画流畅度和性能需求调整数值。

3. 编写捕获与更新代码

在Timer的OnTimer事件中实现内容捕获和纹理更新,建议复用Bitmap以优化内存性能:

// 在Form类中声明全局Bitmap变量
private
  FCaptureBmp: TBitmap;

// Timer触发事件
procedure TMainForm.TimerUpdateTextureTimer(Sender: TObject);
begin
  // 初始化或调整Bitmap尺寸
  if not Assigned(FCaptureBmp) then
    FCaptureBmp := TBitmap.Create(PanelDynamic.Width, PanelDynamic.Height)
  else if (FCaptureBmp.Width <> PanelDynamic.Width) or (FCaptureBmp.Height <> PanelDynamic.Height) then
    FCaptureBmp.SetSize(PanelDynamic.Width, PanelDynamic.Height);

  // 将Panel内容绘制到Bitmap
  PanelDynamic.DrawToBitmap(FCaptureBmp, RectF(0, 0, PanelDynamic.Width, PanelDynamic.Height));

  // 更新3D纹理
  TextureMaterialSource1.Texture := FCaptureBmp;
end;

// Form销毁时释放全局Bitmap
procedure TMainForm.FormDestroy(Sender: TObject);
begin
  FCaptureBmp.Free;
end;

4. 关键注意事项

  • 内存管理:复用全局Bitmap避免频繁创建销毁;若复用Bitmap,需将TextureMaterialSource1.OwnsBitmap设为False,防止Bitmap被提前释放。
  • 隐藏Panel兼容:即使PanelDynamic.Visible=False,DrawToBitmap依然可以正常捕获内容,无需让Panel显示在界面上。
  • 性能平衡:若Panel内容变化频率较低,可适当调大Timer的Interval值,降低CPU占用。

简化方案:使用MakeScreenshot

如果不需要自定义捕获区域,可直接用MakeScreenshot快速实现:

procedure TMainForm.TimerUpdateTextureTimer(Sender: TObject);
begin
  TextureMaterialSource1.Texture := PanelDynamic.MakeScreenshot;
end;

注:MakeScreenshot每次都会创建新的TBitmap,TextureMaterialSource会自动释放旧的Bitmap,适合快速验证但内存开销略高。


内容的提问来源于stack exchange,提问作者user3384674

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:59:57