Mathf類

Mathf類還是有比較多的東東,今天花時間看了一下,記錄如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Mathf類的靜態(tài)變量和靜態(tài)方法
public class MathfTest : MonoBehaviour {

//// Use this for initialization
void Start()
{
    //靜態(tài)變量
    Debug.Log("角度變弧度:" + Mathf.Deg2Rad);
    Debug.Log("弧度變角度:" + Mathf.Rad2Deg);
    Debug.Log("圓周率:" + Mathf.PI);
    Debug.Log("無限小的小數(shù)(正數(shù)):" + Mathf.Epsilon);
    Debug.Log("無限大的數(shù)(正數(shù)):" + Mathf.Infinity);
    Debug.Log("無限小的數(shù)(負數(shù))" + Mathf.NegativeInfinity);

    //靜態(tài)方法
    Debug.Log("返回參數(shù)的符號,正數(shù)和0返回1,負數(shù)返回-1:"+Mathf.Sign(-10)+":::"+Mathf.Sign(10));
    Debug.Log("求絕對值:" + Mathf.Abs(-10));  //print 10 
    Debug.Log("五舍六入(float類型):"+Mathf.Round(10.5f)+":::"+Mathf.Round(10.6f));
    Debug.Log("五舍六入(int類型):" + Mathf.RoundToInt(-10.5f) + ":::" + Mathf.RoundToInt(-10.6f));
    Debug.Log("比較兩個浮點數(shù)的相似度:" + Mathf.Approximately(2.0f, 5.0f));  //print true/false
    Debug.Log("向上取整(返回float類型):" + Mathf.Ceil(0.6f));
    Debug.Log("向上取整(返回int類型):" + Mathf.CeilToInt(5.3f));
    Debug.Log("向下取整(返回float類型):" + Mathf.Floor(0.6f));
    Debug.Log("向下取整(返回int類型):" + Mathf.FloorToInt(1.6f));
    Debug.Log("限定方法:" + Mathf.Clamp(5, 10, 12)); //將第一個參數(shù)限定在第二個參數(shù)和第三個參數(shù)之間
    Debug.Log("限定方法01:" + Mathf.Clamp01(0.5f)); //將第一個參數(shù)限定在0-1之間
    Debug.Log("返回距離value最近的2的次方數(shù)。:" + Mathf.ClosestPowerOfTwo(5)); //此時應該返回4
    Debug.Log("檢測是否為2的N次方數(shù):"+Mathf.IsPowerOfTwo(8));  //返回true/false
    Debug.Log("輸出下一個最近的2的N次方數(shù):"+Mathf.NextPowerOfTwo(129));
    Debug.Log("計算兩個角度的最短距離:" + Mathf.DeltaAngle(1080, 90));  //1080即0度  所以二者之間最短距離為90-0 = 90度
    //對數(shù)和次方數(shù)
    Debug.Log("計算參數(shù)a的b次方:"+Mathf.Pow(6f, 2f));
    Debug.Log("計算平方根:" + Mathf.Sqrt(4));
    Debug.Log("e的N次方:" + Mathf.Exp(6));  //e:自然常數(shù)e(約為2.71828)
    Debug.Log("對數(shù)運算:" + Mathf.Log(6, 2)); // 以6位底,2的對數(shù)
    Debug.Log("對數(shù)運算(只有一個參數(shù)):" + Mathf.Log(6)); // 當只有一個參數(shù)時,默認以e為底
    Debug.Log("以10為底的對數(shù)運算:" + Mathf.Log10(100));
    //插值運算部分(先加速后減速運動)
    Debug.Log("反插值(返回參數(shù)c在參數(shù)ab之間的比例):" + Mathf.InverseLerp(2f, 8f, 5f));  //5在2和8之間的比例為0.5
    Debug.Log("插值(每次返回參數(shù)c在參數(shù)ab之間的比例值):"+Mathf.Lerp(1f,10f,0.8f));  //print 8.2   (10-1)*0.8 = 7.2+1 = 8.2
    Debug.Log("針對角度的插值運算:" + Mathf.LerpAngle(0.0f,90.0f,0.5f));  ///返回45度
    Debug.Log("線性插值在a和b之間,不受t的限制,可以跳到ab范圍的外面去:" + Mathf.LerpUnclamped(0.5f, 9.0f, 1.6f));
    //勻速移動
    Debug.Log("從起點按比例增加一直到終點為止:" + Mathf.MoveTowards(1.0f, 10.0f, 2.0f));  //第一次返回1+2=3
    Debug.Log("角度勻速增加:"+Mathf.MoveTowardsAngle(30.0f,90.0f,10.0f));
    //來回運動
    Debug.Log("參數(shù)a在長度之間來回變化"+Mathf.PingPong(1.0f,10.0f));  // 此處參數(shù)t在1-10之間來回變化(需要放在update里面)
    Debug.Log("來回循環(huán)" + Mathf.Repeat(1.0f, 5.0f));  //在1-5之間來回循環(huán)   和Mathf.PingPong的差異 暫時不清楚

    //Perlin Noise的特點是:返回值隨著x,y坐標的移動,是連續(xù)且平緩的變化的隨機值,這點和Random是完全不同的,而且這個特點非常重要,應用范圍很廣??梢韵胂?,把x或者y的值隨著時間去改變,會得到動態(tài)變化的一系列數(shù)值。
    Debug.Log("隨著兩個參數(shù)的變化會生成一張變化的圖像"+Mathf.PerlinNoise(0.5f,0.5f));

    //返回最大值和最小值(可以使用int&float類型),可以從兩個值或者數(shù)組中獲取最值
    Debug.Log("返回最大值:"+Mathf.Max(2.0f,3.6f));
    int[] i = { 5,9,6,3,45,8,};
    Debug.Log("返回最大值:" + Mathf.Max(i));
    Debug.Log("返回最小值:" + Mathf.Min(2.0f, 3.6f));
    int[] ii = { 5, 9, 6, 3, 45, 8, };
    Debug.Log("返回最小值:" + Mathf.Min(ii));

    //平滑插值
    Debug.Log("平滑插值:和lerp類似,在最小和最大值之間的插值,并在限制處漸入漸出" + Mathf.SmoothStep(1.0f,10.0f,2.0f));
    //隨著時間的推移逐漸改變一個給定的角度到期望的角度
    //Debug.Log("平滑阻尼角度:"+ Mathf.SmoothDampAngle(current: float, target: float, ref currentVelocity : float, smoothTime: float, maxSpeed: float = Mathf.Infinity, deltaTime: float = Time.deltaTime));
    //平滑阻尼:隨著時間的推移逐漸改變一個值到期望值。
    //Mathf.SmoothDamp(current: float, target: float, ref currentVelocity : float, smoothTime: float, maxSpeed: float = Mathf.Infinity, deltaTime: float = Time.deltaTime)

    //Unity5.6支持的一個方法,通過溫度單位開爾文來表示RGB
    //print(Mathf.CorrelatedColorTemperatureToRGB(float kelvin))   kelvin取值1000-40000
    //5.6支持的方法   將給定值從伽瑪(sRGB)轉換為線性顏色空間。
    //Mathf.GammaToLinearSpace(float value);
    //5.6支持的方法   將給定的值從線性轉換為伽瑪 (sRGB) 顏色空間。
    //Mathf.LinearToGammaSpace(float value);

    //正反余弦,正弦,正切等
    //反余弦值 反正弦值  反正切值  反正切2   注意參數(shù)都是弧度
    print(Mathf.Acos(1f));
    print(Mathf.Asin(1f));
    print(Mathf.Atan(1f));
    print(Mathf.Atan2(1f, 2f));
    //余弦  正弦  正切    注意參數(shù)都是弧度,可以考慮通過Mathf的靜態(tài)變量來切換弧度和角度
    print(Mathf.Cos(0.5f));
    print(Mathf.Sin(Mathf.Deg2Rad * 30));  //求sin(30)  即0.5
    print(Mathf.Tan(0.5f));
}


// Update is called once per frame
void Update () {
    
}
}

注意:有三種不同的運動

    //插值運算部分(先加速后減速運動)
    Debug.Log("反插值(返回參數(shù)c在參數(shù)ab之間的比例):" + Mathf.InverseLerp(2f, 8f, 5f));  //5在2和8之間的比例為0.5
    Debug.Log("插值(每次返回參數(shù)c在參數(shù)ab之間的比例值):"+Mathf.Lerp(1f,10f,0.8f));  //print 8.2   (10-1)*0.8 = 7.2+1 = 8.2
    Debug.Log("針對角度的插值運算:" + Mathf.LerpAngle(0.0f,90.0f,0.5f));  ///返回45度
    Debug.Log("線性插值在a和b之間,不受t的限制,可以跳到ab范圍的外面去:" + Mathf.LerpUnclamped(0.5f, 9.0f, 1.6f));
    //勻速移動
    Debug.Log("從起點按比例增加一直到終點為止:" + Mathf.MoveTowards(1.0f, 10.0f, 2.0f));  //第一次返回1+2=3
    Debug.Log("角度勻速增加:"+Mathf.MoveTowardsAngle(30.0f,90.0f,10.0f));
    //來回運動
    Debug.Log("參數(shù)a在長度之間來回變化"+Mathf.PingPong(1.0f,10.0f));  // 此處參數(shù)t在1-10之間來回變化(需要放在update里面)
    Debug.Log("來回循環(huán)" + Mathf.Repeat(1.0f, 5.0f));  //在1-5之間來回循環(huán)   和Mathf.PingPong的差異 暫時不清楚
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容