在现实世界中,光线会在远处消失,而且昏暗的光线比明亮的光线具有更低的有效范围。“衰减”一词是指光衰速率。除了 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 页面已发布
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.