Zh/Source 2/Docs/Level Design/Lighting
在3D中渲染对象时,《半衰期:爱莉克斯》背后的引擎会模拟现实生活中灯光的显示方式—允许表面对光源、环境光等做出逼真的响应,静态和动态对象都会从这些光源投射阴影。为了确保高视觉逼真度和高性能,创意工坊工具需要预先计算大量信息—以各种形式存储照明数据,如光照贴图纹理、光探头体积和立方体贴图。
但这些到底是什么?
照明系统的输入可分为以下几部分:
- 静态几何体上的光照贴图纹理(Hammer网格、静态道具)
- 光探头体积和立方体贴图(动态对象上的环境光、反射)
- 光源(环境、点光源、正交光源和聚光灯)
- 体积雾
要预览Hammer中的效果,可以使用顶部的“预览烘焙照明(Preview Baked Lighting)”菜单。这将做一个相对低质量的预览来呈现在虚拟现实中的最终照明效果——极大地帮助艺术指导、游戏设计和其他类似的。它使用顶点照明和许多单像素动态光源,而不是光照贴图——需要更少的预计算,但导致较低的质量和更昂贵的运行时渲染。
你需要为完整的发布版照明创建光照贴图,但你可以在VR中使用这个预览版本的照明进行低性能测试。除了非常简单的地图外,任何地图在这种照明下的帧率都可能非常糟糕,所以不要以这种方式发布地图!
光源类型
光源有几种不同的类型,光的消散方式也不同。你可以在左侧工具栏中使用Hammer中的实体工具将光源放置到地图中——很方便,它有一个样式化的灯泡图标。
放置后,在3D视图中选择实体,并在右下角的'对象属性'中设置颜色、亮度、最大距离等。
- 环境光——light_environment
- 来自天空的虚拟阳光——无限远的光源
- 不随距离衰减
- 通常在地图上只有一个
- 在workshop_examples/prefabs/environment_settings文件夹里提供超级有用的预制件
- 点光源——light_omni
- 单一的全方位光源
- 有点像灯泡!
- 聚光灯——light_spot
- 来自特定点的定向光源
- 就像一个真实世界的聚光灯,令人惊讶!
- 正交光 - light_ortho
- 需要在实体类弹出菜单中选择'light_ortho'来放置
- 介于环境光和聚光灯之间
- 光是平行的,但只存在于特定的长方体内
- 与环境光一样,不会随距离衰减
- 适用于有限的天窗和其他特殊用途等
直接和间接照明
光源实际上由两个部分组成:
- 直接照明——基于光源视线的主要组件
- 间接照明——光线从一个表面反射到另一个表面的次要组成部分——可能相当微妙,但极大地增加了视觉真实感
如何渲染直接照明是这里的主要选择。
默认为烘焙灯光索引
- 选择时将“直接照明(Direct Lighting)”设置为“烘焙(Baked)”,并选中“烘焙光照索引(Baked Light Indexing)”(默认设置)
- 默认情况下,间接照明烘焙到光照贴图(对于静态曲面)和光探测体积(对于动态对象)
- 优点:
- 直接照明具有镜面反射(闪亮)组件
- 可以从动态对象(角色、物理道具等)投射阴影
- 静态几何体阴影上的真实接触强化【使用光源半径(Light Source Radius)/太阳散射角(SunSpreadAngle)】
- 直接照明可以闪烁、频闪、打开和关闭(给灯光实体一个目标名称,并通过实体I/O告诉它该怎么做)
- 渲染成本相对低!
- 缺点:
- 一次最多可以有4个索引光照射在一个表面上,否则会发生奇怪的事情(最好保持为一个或两个,以使渲染成本更低)
- 在Hammer的3D视图右上方预览菜单里——将“工具可视化模式(Tools Visualization Mode)”设置为“烘焙照明复杂性(Baked Lighting Complexity)”
- 黑色是在那个表面上的零索引光,红色1,橙色2,黄色3,白色4,青色是“不要那样做”
- 光源不能移动
- 一次只能有一个索引光源从动态对象上投射阴影
- 如果你想要一个完全可开关的灯,间接组件不能被打开或关闭——将它设置为“None”
- 地图中索引灯光的总数是有限制的,尽管它相当高,而且在正常使用中不太可能达到
- 一次最多可以有4个索引光照射在一个表面上,否则会发生奇怪的事情(最好保持为一个或两个,以使渲染成本更低)
完全烘焙直接照明
- 在“直接照明(Direct Lighting)”设置为“烘焙(Baked)”且未选中“烘焙光照索引(Baked Light Indexing)”的情况下选择
- 优点:
- 基本上渲染0成本!
- 直接组件存储在光照贴图和光探测容器中,与间接组件相同
- 静态几何体阴影上的真实接触强化【使用光源半径(Light Source Radius)/太阳散射角(SunSpreadAngle)】
- 基本上渲染0成本!
- 缺点:
- 直接照明没有镜面反射组件
- 明亮的表面看起来干燥而粗糙
- 不要从方向光照贴图中获得方向性
- 不能从动态对象上投射阴影
- 光源不能移动
- 不能打开或关闭灯光,不能闪烁或频闪
- 直接照明没有镜面反射组件
每像素直接照明
- 选择“直接光(Direct Light)”设置为“每像素(Per Pixel)”
- 渲染成本可能非常高;通常只用于特殊场合(如玩家手电筒)或非常小的光源(弹药夹,树脂等)。
- 只有在你知道自己在做什么的情况下才用这些
- 查看附加的地图中的示例以了解更多细节
- 优点:
- 可以有高质量的高光组件(尽管在上面列出的小光源下高光会被关闭)
- 可以有完全动态的阴影(同样,对于那些小光源会关闭)
- 可以移动光源,例如用玩家手电筒!
- 直接照明可以闪烁,频闪,打开和关闭
- 缺点:
- 渲染成本可能非常高!
- 无接触硬化【光源半径(Light Source Radius)/太阳散射角(SunSpreadAngle)不起作用】
- 如果启用,间接组件仍然被烘焙到光照贴图等(大多数情况下切换到“None”)
- 外预览照明,我们只支持
- 在视图中启用带阴影的单个light_spot或light_ortho
- 最多8个禁用阴影和镜面反射和完全线性衰减的light_omnis
间接照明
为了对动态对象进行适当的间接照明,你需要放置Light Probe Volume and Cubemap实体——env_combined_light_probe_volume
- 你想要每个中心都是一个典型的,在那个特定的空间里的平均视图,可以是一个房间,走廊,户外场景等等
- 对于一个简单的长方体房间,你应该把它放在正中央,大约眼睛的高度
- 扩展体积边界(红色、绿色和蓝色3D箭头)以完全包围该体积
- 反射是通过盒子投影的立方体贴图完成的——想象一个盒子的每个面都有一个纹理
- 在这个简单的长方体房间中,你希望体积边界与墙、地板和天花板相交
- 因此,反射就是那个房间的简化版,具有正确的视深度和一切
- 体积可以旋转和重叠——可以设置优先级,使一个覆盖另一个(例如,一个黑暗的玩家可进入的盒子,周围有阳光照射)
- 必要时,应将hammer的几何体分解为单独的网格
- 每个网格只能接收来自一个立方体贴图的反射
- 不要担心在过程中有效地添加额外的几何体、顶点等——所有奇特的网格解剖东西都在后台以可见性和类似的方式进行。
- 所有可到达的区域都应该用光探测容器包围起来——如果你有动态物体突然闪得很亮或者什么的,你可能漏掉了这一点

Lightmap Density
Lightmap density describes how dense the lightmap texture is on a particular face. This is necessary to control because giving all faces a uniform density would be inefficient to calculate.
You can preview lightmap density in-game with the command mat_luxels 1
.
Lightmap Player Spaces
Lightmaps are textures containing precalculated lighting information for static surfaces. You want these to be as high resolution as possible for the best visual fidelity. You can ensure the compilation process automatically prioritizes player-accessible stuff by placing lightmap player space volumes - quite simply, these are Hammer meshes with materials/tools/toolslightmapres.vmat on them. The closer a surface is to one of these meshes, the more lightmap texels are assigned to it.
The general gist of it is - place these lightmap player space volumes within areas the player can get to. Inaccessible areas - things behind fences, high up on walls, distant buildings - will thus be assigned fewer and fewer lightmap texels depending on how far away they are.
The net result is apparently seamless lightmap detail!
Lightmap Resolution Bias
When editing a face, you can control the Lightmap Resolution Bias. This is a value that will influence how large the lightmap texture space is for that face relative to the rest of the map.
Bias scales by powers of 2. A lightmap bias of +1 will make the texture resolution 2x what it would be normally, +2 is 4x resolution, etc. Negative bias will lower the lightmap resolution.
Be wary of adjusting this option by accident, as a single face with a very large lightmap bias can effectively destroy the lightmap density for the rest of your map! If you have bad shadows and your console is reporting 'Mesh with material <x> is extremely large in lightmap (x.x%)' with a large value, this may be why. You can go to the coordinates listed in console to get an idea of where these faces may be, and use mat_luxels 1
in game to find suspiciously dense faces.
Basic summary of lighting:
- Use indexed lights for the key lights in your scene - sunlight, major light sources etc. that you want specular from, switchable / flickering lights etc.
- The 'Baked Lighting Complexity' visualization mode will show you how things are looking in terms of rendering cost
- Ideally, you want things black, red and a bit of orange and yellow
- Use fully baked lights for visually less important things - fill lights, things in background, dimmer light sources
- Use per-pixel lights only for very particular reasons - if in doubt, don't.
Volumetric Fog
Half-Life: Alyx uses a voxel-based system for having the atmosphere react to light sources - hazy light flares, glare, mist, shafts of light coming through windows - all are possible using this unified volumetric fog system.
To enable it in your map, place a env_volumetric_fog_controller entity. The default settings are generally okay.
You can set volume-specific fog with env_volumetric_fog_volume entities - drag out the volume bounds in a similar way to light probe volumes. For harder edges to a fog volume, select the fog volume entity and set 'Falloff Exponent' to a low value such as zero.
Next, you need to set light sources to contribute to volumetric fog. Select a light entity, then set 'Volumetric Fogging' to 'Baked'. You can tweak the fog intensity with 'Fog contribution strength'
One drawback is that for outdoors areas, sunlit things will suddenly look darker. By default, atmosphere will absorb light. Normally we'd fix this by enabling volumetric fogging on the sunlight, but unfortunately environment lights (such as that sunlight) can't contribute to volumetric fog, for various technical reasons.
It's possible to locally fake stuff with a fog volume set to a low fog strength (to minimize atmospheric darkening), then a light_ortho with volumetric fog enabled, a low brightness, high fog contribution strength, and angles and color set to the same as the sunlight light_environment.
You can have many light sources with volumetric fogging enabled - additional rendering cost per light source is relatively minor. For light_omnis, set as many as you can to 'Baked: No shadows' or graphics programmers will be sad.
Look at shipping maps for more examples!
Building Lighting
Preview Baked Lighting
For an in-Hammer preview of how your map will look when fully compiled, make use of the Preview Baked Lighting : Bake All Lighting menu. This will perform some lower-quality calculations which will help you iterate on the overall look and visual feel of your map - it'll let you continue working on the map beyond this point, but remember to re-bake preview lighting whenever you make any significant changes so that you'll get an up-to-date representation of how everything will look.


Building Lightmaps
Once you're happy with how your map will look, you can now compile the thing. Press F9 to bring up the Map Builder dialogue box. The default settings for Full Compile will generate 1k lightmaps - this should be relatively fast for a simple map, just taking a few minutes. To build cubemaps for proper reflections on metallic surfaces, remember to check the Build cubemaps on load checkbox.
For a release version of your map, select the Final Compile option. This will generate higher-quality lightmaps at 2k resolution. This may take a little while on a typical home PC!