Version: 2023.1
光照贴图接缝缝合
Realtime Global Illumination using Enlighten

自定义衰减

在现实世界中,光线会在远处消失,而且昏暗的光线比明亮的光线具有更低的有效范围。“衰减”一词是指光衰速率。除了 Unity 的默认衰减光照行为外,还可以使用自定义衰减设置。

渐进式光照贴图 (Progressive Lightmapper) 提供自定义衰减预设,您可以通过脚本来实现这些预设。请参阅表下方的图像以查看这些预设的工作原理的可视化表示,并参阅图像下方的代码示例来了解此功能的用法示例。

属性 功能
InverseSquared 应用距离平方倒数法衰减模型。意味着,光照强度与位置到光源的距离的平方成反比减小。有关更多信息,请参阅 Wikipedia:平方反比定律 (Inverse-square law)。此选项在物理上是最准确的。
InverseSquaredNoRangeAttenuation 应用距离平方倒数法衰减模型,但没有平滑范围衰减。此选项的原理与 InverseSquared 此相同,但光照系统不考虑准时光源(即点光源和聚光灯)的范围参数的衰减。
Legacy 应用二次衰减模型。该模型将光衰减基于光源的范围。随着光线远离光源,强度会减弱,但衰减会有非常明显且不自然的下降,视觉效果也不太现实。
Linear 应用线性衰减模型。在此模型中,衰减与距光源的距离成反比,且衰减以固定的速率从光源开始减弱。
每个自定义衰减预设的视觉效果示例
每个自定义衰减预设的视觉效果示例

Note: The code example below only works with the Progressive Lightmapper when you use Baked or Mixed lights. To use the code example with realtime lights, use Enlighten Realtime Global Illumination.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.GlobalIllumination;
using UnityEngine.SceneManagement;
[ExecuteInEditMode]
public class ExtractFalloff : MonoBehaviour
{
    public void OnEnable()
    {
        Lightmapping.RequestLightsDelegate testDel = (Light[] requests, Unity.Collections.NativeArray<LightDataGI> lightsOutput) =>
        {
            DirectionalLight dLight = new DirectionalLight();
            PointLight point = new PointLight();
            SpotLight spot = new SpotLight();
            RectangleLight rect = new RectangleLight();
            DiscLight disc = new DiscLight();
            Cookie cookie = new Cookie();
            LightDataGI ld = new LightDataGI();
            
            for (int i = 0; i < requests.Length; i++)
            {
                Light l = requests[i];
                switch (l.type)
                {
                    case UnityEngine.LightType.Directional: LightmapperUtils.Extract(l, ref dLight); LightmapperUtils.Extract(l, out cookie); ld.Init(ref dLight, ref cookie); break;
                    case UnityEngine.LightType.Point: LightmapperUtils.Extract(l, ref point); LightmapperUtils.Extract(l, out cookie); ld.Init(ref point, ref cookie); break;
                    case UnityEngine.LightType.Spot: LightmapperUtils.Extract(l, ref spot); LightmapperUtils.Extract(l, out cookie); ld.Init(ref spot, ref cookie); break;
                    case UnityEngine.LightType.Area: LightmapperUtils.Extract(l, ref rect); LightmapperUtils.Extract(l, out cookie); ld.Init(ref rect, ref cookie); break;
                    case UnityEngine.LightType.Disc: LightmapperUtils.Extract(l, ref disc); LightmapperUtils.Extract(l, out cookie); ld.Init(ref disc, ref cookie); break;
                    default: ld.InitNoBake(l.GetInstanceID()); break;
                }
                ld.cookieID = l.cookie?.GetInstanceID() ?? 0;
                ld.falloff = FalloffType.InverseSquared;
                lightsOutput[i] = ld;
            }
        };
        Lightmapping.SetDelegate(testDel);
    }
    void OnDisable()
    {
        Lightmapping.ResetDelegate();
    }
}

Note: All code in the example above is necessary for the custom falloff to work; however, you can change InverseSquared in the line ld.falloff = FalloffType.InverseSquared; to use any of the presets described above.


2018.1 版中添加了渐进光照贴图 NewIn20181

2018–03–28 页面已发布

光照贴图接缝缝合
Realtime Global Illumination using Enlighten