游戏中的线性插值

文章目录

线性插值

线性插值是指找到两个给定值之间的某个百分比值。

L e r p = a + ( b − a ) ∗ t , t ∈ 0 , 1 Lerp = a + (b - a) * t,t∈0,1 Lerp=a+(b−a)∗t,t∈0,1

例如,我们可以在数字3和5之间线性插值50%,得到数字4。这是因为4在3和5之间占了50%。


Unity中通过 Lerp 的函数实现。包括Mathf.LerpColor.LerpVector3.Lerp

Mathf.Lerp

Mathf.Lerp 会自动将插值因子 t 限制在 0, 1 区间内。而 Mathf.LerpUnclamped 则允许 t 超出范围。

c# 复制代码
float result = Mathf.Lerp (3f, 5f, 0.5f);
// result = 4

设 a = 3 a = 3 a=3, b = 4 b = 4 b=4,插值因子 t = 0.5 t = 0.5 t=0.5 ,则插值结果为:4

f ( t ) = a + ( b − a ) ∗ t = 3 + ( 4 − 3 ) ∗ 0.5 = 4 \begin{aligned} f(t) &= a+(b-a)*t\\&= 3+(4-3)*0.5\\&= 4 \end{aligned} f(t)=a+(b−a)∗t=3+(4−3)∗0.5=4

控制灯光强度

在某些情况下,Lerp函数可以用来随时间平滑一个值。

c# 复制代码
void Update ()
{
    light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f);
}

如果灯光强度是0,那么第一次更新后会被设置为4。

下一帧会先是6,然后7,再7.5,依此类推。

因此,在多个帧内,光的强度会趋近8,但随着接近目标,变化速度会减慢。

注意,这种情况发生在多个帧内。

如果我们希望这不依赖帧率,务必使用 Time.deltaTime 来缩放插值速度。

c# 复制代码
void Update ()
{
    light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f * Time.deltaTime);
}

这意味着强度的变化将是每秒发生 ,而不是每帧。

平滑值时通常最好使用SmoothDamp函数。只有确定想要的效果时才用Lerp平滑。


Vector3.Lerp

c# 复制代码
Vector3 from = new Vector3(1f, 2f, 3f);
Vector3 to = new Vector3(5f, 6f, 7f);

Vector3 result = Vector3.Lerp(from, to, 0.75f);
// result = (4, 5, 6)

在线性变换中, a ⃗ = 1 2 3 , b ⃗ = 5 6 7 , t = 0.75 \vec{a} = \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix}, \vec{b} = \begin{bmatrix}5 \\ 6 \\ 7\end{bmatrix}, t = 0.75 a = 123 ,b = 567 ,t=0.75,则插值结果为: 4 5 6 \begin{bmatrix}4 \\ 5 \\ 6\end{bmatrix} 456

向量插值的计算:

u ⃗ = a ⃗ + ( b ⃗ − a ⃗ ) ∗ t = 1 2 3 + ( 5 6 7 1 2 3 ) ∗ 0.75 = 1 2 3 + 4 4 4 ∗ 0.75 = 1 2 3 + 3 3 3 = 4 5 6 \begin{aligned}\vec{u} &= \vec{a} + (\vec{b} - \vec{a}) * t\\&= \begin{bmatrix}1 \\2 \\ 3\end{bmatrix} + (\begin{bmatrix}5 \\ 6 \\ 7\end{bmatrix} - \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix}) * 0.75\\&= \begin{bmatrix}1 \\2 \\ 3\end{bmatrix} +\begin{bmatrix}4 \\ 4 \\ 4\end{bmatrix} * 0.75\\&= \begin{bmatrix}1 \\2 \\ 3\end{bmatrix} +\begin{bmatrix}3 \\ 3 \\ 3\end{bmatrix}\\&= \begin{bmatrix}4 \\5 \\ 6\end{bmatrix}\end{aligned} u =a +(b −a )∗t= 123 +( 567 − 123 )∗0.75= 123 + 444 ∗0.75= 123 + 333 = 456

插值因子的变化:

物体的空间运动

控制移动开关

c# 复制代码
private float t = 1.1f;

public void BeginLerp()
{
    t = 0f;
}

固定时长平滑移动

c# 复制代码
public float lerpTime = 1f;

private void FixedUpdate()
{
    if (t < 1f)
    {
        t += Mathf.Clamp01(1f / lerpTime * Time.fixedDeltaTime);
        objectToLerp.transform.position = Vector3.Lerp(objectToLerp.transform.position, destinationTransform.position, t);
    }
}

移动到目的地


Color.Lerp

在颜色结构中,颜色由4个浮点表示,分别代表红、蓝、绿和阿尔法。使用 Lerp 时,每个浮点数会像对 Mathf.Lerp 一样进行插值。

颜色渐变

c# 复制代码
public Image background;
public List<Color> socials = new List<Color>();
public float frequency = 3f;
private float t = 1.1f;

public void BeginLerp()
{
    t = 0f;
}

private void Update()
{
    if (t < 1)
    {
        t = Mathf.Sin(Time.time * frequency);
        background.color = Color.Lerp(socials[0], socials[1], t);
    }
}

自定义要渐变的颜色

相关推荐
Jiamiren1 小时前
WEEX Labs 周度观察:AI 基础设施的“权力游戏”与硬核变现时代
人工智能·游戏
YakSue16 小时前
游戏项目中的程序化生成(PCG):五层工作的耦合问题
游戏
国际学术会议-杨老师1 天前
2026年游戏美学、动画生成与色彩科学国际会议(GEAAC 2026)
游戏·动画·色彩科学
qizayaoshuap1 天前
# [特殊字符] 猜数字 — 鸿蒙ArkTS游戏逻辑与交互反馈系统设计
游戏·华为·harmonyos
梅雅达编程笔记1 天前
编程启蒙|Scratch 转 Python 系列第10天:问答闯关游戏实战(AI题库管理+随机出题实战)
人工智能·python·游戏·青少年编程
weixin_439857542 天前
借助AI制作的贪吃蛇游戏(单HTML文件)
游戏·html·ai编程
快乐星空Maker2 天前
C++【生存游戏】开发:荒岛往事 第三期
开发语言·c++·游戏·编程语言
AA陈超2 天前
006 T03 — 蓝图操作指南
c++·游戏·架构·ue5·github·虚幻引擎
cd_949217212 天前
Unity游戏角色资产怎么快速制作?用V2Fun跑通生成、绑定和导入测试
游戏·unity·游戏引擎
tokenKe3 天前
Epic Games 发布下一代版本控制系统 Lore:被 HN 干到 1000+ 分的“游戏版 Git“
git·游戏