Unity3D - LINEAR INTERPOLATION

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

水平有限,翻譯粗略,歡迎吐槽 :)ide

 

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%處。ui

 

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:this

在Unity中有若干個用於不一樣類型的Lerp函數。對於咱們以前剛剛舉得例子來講,等價的Mathf.Lerp函數看起來以下:spa

// 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,則函數就會返回結束值。3d

 

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.code

其餘Lerp函數的例子有Color.Lerp以及Vector3.Lerp。他們的工做方式和Mathf.Lerp函數幾乎一致,除了起始值和結束值分別是Color和Vector3類型。他們的第三個參數仍舊是表示插值多少的一個浮點值。這些函數的結果是找出一個由兩個給定顏色通過必定比例混合的顏色值以及兩個給定向量之間位於必定百分比處的一個向量值。blog

 

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可以達到你想要的效果時纔去使用它。

相關文章
相關標籤/搜索