原文地址:http://unity3d.com/learn/tutorials/modules/beginner/scripting/linear-interpolation

水平有限,翻译粗略,欢迎吐槽 :)

When making games it can sometimes be useful to linearly interpolate between two values. This is done with a function called Lerp. Linearly interpolating is finding a value that is some percentage between two given values. For example, we could linearly interpolate between the numbers 3 and 5 by 50% to get the number 4. This is because 4 is 50% of the way between 3 and 5.

制作游戏时,在两个值之间进行线性插值有时候很有用。Lerp函数能够实现该功能。线性插值就是要找出两个给定值之间位于一定百分比的某个值。举例来说,在数字3和数字5之间以50%进行线性插值能够得到数字4。这是因为数字4位于数字3到数字5的50%处。

In Unity there are several Lerp functions that can be used for different types. For the example we have just used, the equivalent would be the Mathf.Lerp function and would look like this:

在Unity中有若干个用于不同类型的Lerp函数。对于我们之前刚刚举得例子来说,等价的Mathf.Lerp函数看起来如下:

// In this case, result = 4
float result = Mathf.Lerp (3f, 5f, 0.5f);

The Mathf.Lerp function takes 3 float parameters: one representing the value to interpolate from; another representing the value to interpolate to and a final float representing how far to interpolate. In this case, the interpolation value is 0.5 which means 50%. If it was 0, the function would return the ‘from’ value and if it was 1 the function would return the ‘to’ value.

函数Mathf.Lerp有三个浮点型参数:一个表示插值的起始值,另一个表示插值的结束值,最后一个浮点数表示要插值多少。在上面的代码中,插值量是0.5,也就是50%。如果插值量是0,则函数就会返回起始值。如果插值量是1,则函数就会返回结束值。

Other examples of Lerp functions include Color.Lerp and Vector3.Lerp. These work in exactly the same way as Mathf.Lerp but the ‘from’ and ‘to’ values are of type Color and Vector3 respectively. The third parameter in each case is still a float representing how much to interpolate. The result of these functions is finding a colour that is some blend of two given colours and a vector that is some percentage of the way between the two given vectors.

其他Lerp函数的例子有Color.Lerp以及Vector3.Lerp。他们的工作方式和Mathf.Lerp函数几乎一致,除了起始值和结束值分别是Color和Vector3类型。他们的第三个参数仍旧是表示插值多少的一个浮点值。这些函数的结果是找出一个由两个给定颜色经过一定比例混合的颜色值以及两个给定向量之间位于一定百分比处的一个向量值。

Let’s look at another example:

让我们来看看另一个例子:

Vector3 from = new Vector3 (1f, 2f, 3f);
Vector3 to = new Vector3 (5f, 6f, 7f); // Here result = (4, 5, 6)
Vector3 result = Vector3.Lerp (from, to, 0.75f);

In this case the result is (4, 5, 6) because 4 is 75% of the way between 1 and 5; 5 is 75% of the way between 2 and 6 and 6 is 75% of the way between 3 and 7.

在上面的情况下结果将是(4, 5, 6),因为数字4位于数字1到数字5的75%处,数字5位于数字2到数字6的75%处,数字6位于数字3到数字7的75%处。

The same principle is applied when using Color.Lerp. In the Color struct, colours are represented by 4 floats representing red, blue, green and alpha. When using Lerp, these floats are interpolated just as with Mathf.Lerp and Vector3.Lerp.

当使用Color.Lerp时其原理也是一样的。在Color结构体中,颜色是由4个分别表示红,蓝,绿和alpha的浮点值构成的。当使用Lerp时,这些浮点值的插值方式就像Mathf.Lerp与Vector3.Lerp一样。

Under some circumstances Lerp functions can be used to smooth a value over time. Consider the following piece of code:

在一些情况下Lerp函数能够用于随时间流逝而平滑一个值。来看下面的代码:

void Update ()
{
light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f);
}

If the intensity of the light starts off at 0 then after the first update it will be set to 4. The next frame it will be set to 6, then to 7, then to 7.5 and so on. Thus over several frames, the lights intensity will tend towards 8 but the rate of it’s change will slow as it approaches its target. Note that this happens over the course of several frames. If we wanted this to not be frame rate dependent then we could use the following code:

如果光的强度从0开始,那么在第一次更新后它的值将是4。下一帧它的值将是6,接着是7,然后是7.5等等。因此在若干帧后,光的强度将会趋近于8,但是它的变化速率在接近目标值时将减慢。需要注意这个过程是发生在若干帧里的。如果我们想要这个过程不依赖帧率的话,那么我们可以使用下面的代码:

void Update ()
{
light.intensity = Mathf.Lerp(light.intensity, 8f, 0.5f * Time.deltaTime);
}

This would mean the change to intensity would happen per second instead of per frame.

这样就意味着强度的改变是依赖于每秒的,而不是每帧。

Please note that when smoothing a value it is often best to use the SmoothDamp function. Only use Lerp for smoothing if you are sure of the effect you want.

请注意,通常来说最好使用函数SmoothDamp来平滑一个值。只有在你确定函数Lerp能够达到你想要的效果时才去使用它。

Unity3D - LINEAR INTERPOLATION的更多相关文章

  1. 3.2 Piecewise Linear Interpolation(站点)

    Newton Interpolation: https://www.youtube.com/watch? v=EyRQXA56asI Piecewise Linear Interpolation: h ...

  2. 【转】线性插值(Linear Interpolation)基本原理

    转:https://blog.csdn.net/u010312937/article/details/82055431 今天在阅读大牛代码的时候,发现了Linear Interpolation一次,百 ...

  3. 线性插值(linear interpolation)

    线性插值通常用于:使用离散的样本来重建连续的信号.在计算机图形学中,这些样本可能是纹理.动画关键帧等. template <class T> T Lerp(T& a, T& ...

  4. [Unity3D] C# Basic : Gameplay Scripting

    教程:https://unity3d.com/cn/learn/tutorials/s/scripting 补充:http://www.runoob.com/csharp/csharp-inherit ...

  5. OpenCASCADE Interpolation - Lagrange

    OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simp ...

  6. Interpolation in MATLAB

    Mathematics     One-Dimensional Interpolation There are two kinds of one-dimensional interpolation i ...

  7. Unity 官方教程 学习

    Interface & Essentials Using the Unity Interface 1.Interface Overview https://unity3d.com/cn/lea ...

  8. OpenGL实现相机视频NV21格式转RGB格式

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  9. OpenGL核心之视差映射

    笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者;已出版书籍:<手把手教你¯的纹理坐标偏移T3来对fragment的纹理坐标进行位移.你能够看到随着深度 ...

随机推荐

  1. [POI2008]Station

    题目大意: 给定一棵n个结点的树,求一个点x作为根,使得所有结点到x的距离和最小. 思路: 树形DP. 首先考虑将1作为根的情况. 很显然我们可以用一遍O(n)的DFS预处理出每个结点所对应子树大小s ...

  2. CentOS 6.9系统时间和硬件时间设置(转)

    总结一下hwclock,这个容易晕: 1)/etc/sysconfig/clock 文件,只对 hwclock 命令有效,且只在系统启动和关闭的时候才有用(修改了其中的 UTC=true 到 UTC= ...

  3. 解决sqlite删除数据后,文件大小不变问题 转载

    原文地址:http://blog.csdn.net/yangchun1213/article/details/7656146   说了这么多,没进主题,我的主题是给Sqlite在删除数据后擦屁股. 大 ...

  4. mormot数据库连接+查询+序列为JSON

    mormot数据库连接+查询+序列为JSON uses SynDB,SynCommons, SynDBRemote, SynOleDB, SynDBMidasVCL, mORMotMidasVCL p ...

  5. C# 7 新特性-2

    在之前的C# 7 新特性博客中,我们谈到了Tuples,Record Type和Pattern Matching.这些都是C#新特性中最可能出现的.在本博客中,我们会提到更多的一些特性,虽然这些特性不 ...

  6. iOS:CocoaPods详解

    原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 一.什么是CocoaPods 1.为什么需要CocoaPods 在进行iOS ...

  7. javascript快速入门2--变量,小学生数学与简单的交互

    变量 对于变量的理解:变量是数据的代号.如同人的名字一样. var num;//在JavaScript中使用关键字var声明一个变量 在JavaScript中,使用上面的语法,就可以声明一个变量,以便 ...

  8. Vue组件进阶知识总结

    上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:“嘿,老哥,我开通了一个驿站,你 ...

  9. D3.js系列——交互式操作和布局

    一.图表交互操作 与图表的交互,指在图形元素上设置一个或多个监听器,当事件发生时,做出相应的反应. 交互,指的是用户输入了某种指令,程序接受到指令之后必须做出某种响应.对可视化图表来说,交互能使图表更 ...

  10. netty handlers模式

    netty的handler模式真的挺方便的,可以像插件一样随意的插入自己新增的功能而不用队系统进行大的变动. 下面我们来看一下这个模式是如何实现和运行的. 待续...