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

Delphi FMX中如何启用TLight使其正常投射光线?

TLight无光照效果排查与解决方案

基础属性必查项

  • 显式设置TLight.Enabled = True,部分框架默认初始化时该属性为关闭状态
  • 匹配光源类型与场景需求:
    • 模拟远光照射立方体选择ltDirectional定向光,只需调整Direction属性指向立方体所在方位
    • 选择点光源/聚光灯时,需保证Position属性不在立方体几何内部,同时光线覆盖范围包含立方体
  • 避免TLight.Color设置为全黑,测试阶段可直接设为纯白色,亮度调整到0.8以上验证效果

场景依赖配置检查

  • 3D立方体必须使用支持光照计算的材质,不可使用无光照材质:例如FireMonkey框架下必须绑定TLightMaterialSource,直接使用TColorMaterialSource不会响应任何光照效果
  • 确认全局渲染配置未关闭光照通道:部分3D框架的上下文配置项中存在光照开关,需保持开启状态

阴影投射额外配置(如需光源投射阴影)

  • 开启TLight.CastsShadows = True
  • 开启立方体的CastsShadows = True(投射阴影)和ReceivesShadows = True(接收其他物体阴影)属性
  • 调整TLight.ShadowMapResolution到1024或更高值,避免阴影贴图精度为0导致效果不显示

最小可运行代码示例(Delphi FireMonkey环境)

procedure TForm3D.FormCreate(Sender: TObject);
var
  DirLight: TLight;
  TestCube: TCube;
  LightMat: TLightMaterialSource;
begin
  // 创建定向光源
  DirLight := TLight.Create(Self);
  DirLight.Parent := Self;
  DirLight.LightType := ltDirectional;
  DirLight.Enabled := True;
  DirLight.Position := Point3D(8, 8, -8);
  DirLight.Direction := Vector3D(0, 0, 1);
  DirLight.Color := claWhite;
  DirLight.CastsShadows := True;

  // 创建响应光照的材质
  LightMat := TLightMaterialSource.Create(Self);
  LightMat.Material.Diffuse := claDodgerBlue;
  LightMat.Material.Specular := claLightGray;
  LightMat.Material.Shininess := 30;

  // 创建测试立方体
  TestCube := TCube.Create(Self);
  TestCube.Parent := Self;
  TestCube.Position := Point3D(0, 0, 0);
  TestCube.MaterialSource := LightMat;
  TestCube.CastsShadows := True;
  TestCube.ReceivesShadows := True;
end;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 12:54:04