游戏中的线性插值

文章目录

线性插值

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

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.Lerp、 Color.Lerp 和 Vector3.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);
    }
}

自定义要渐变的颜色

相关推荐
圣捷游戏14 小时前
教育游戏开发需要准备什么?一文讲清从0到1的完整准备清单(圣捷游戏15年经验分享)
游戏·游戏开发
学长毕设1 天前
基于SpringBoot的游戏攻略社区交流系统-附源码
spring boot·后端·游戏
一只大鸽子1 天前
游戏开发笔记7-Godot RPG游戏基础-场景切换、全局脚本
笔记·游戏·godot
赤生云电竞2 天前
云游戏边缘节点部署实践:机房选址与网络拓扑选型
网络·游戏·php
芝麻粒儿2 天前
Scratch少儿编程案例-鼠标控制练习
游戏·青少年编程·计算机外设·scratch·少儿编程
科技小登2 天前
DDoS防护选型测评:高防CDN、高防IP与SDK游戏盾怎么做同口径POC?
tcp/ip·游戏·ddos
xx z3 天前
打开游戏提示 d3dx9_43.dll 缺失怎么办(DirectX 修复步骤)
windows·游戏·工具·软件
珍猪吃饲料3 天前
消息丢了、重复消费了,MQ 这套账该怎么算
游戏
芝麻粒儿4 天前
Scratch少儿编程案例-多人联机与IO游戏(1)
游戏·青少年编程·scratch·少儿编程
赤生云电竞4 天前
云游戏边缘节点部署:机房选址与网络拓扑优化实践 —— 从80ms时延预算看节点落位与Spine-Leaf组网
服务器·游戏·spine