小白_Unity引擎_Mathf

Ceil

1         //向上取值,向大取值
2         Debug.Log(Mathf.Ceil(0.1f)); //1
3         Debug.Log(Mathf.Ceil(0.9f));//1
4         Debug.Log(Mathf.Ceil(-0.1f));//0
5         Debug.Log(Mathf.Ceil(-0.9f));//0

Floor

1         //向下取值,向小取值
2         Debug.Log(Mathf.Floor(0.1f)); //0
3         Debug.Log(Mathf.Floor(0.9f));//0
4         Debug.Log(Mathf.Floor(-0.1f));//-1
5         Debug.Log(Mathf.Floor(-0.9f));//-1

Round 四舍五入

 1         //四舍五入
 2         Debug.Log(Mathf.Round(0.1f)); //0
 3         Debug.Log(Mathf.Round(0.9f));//1
 4         Debug.Log(Mathf.Round(-0.1f));//0
 5         Debug.Log(Mathf.Round(-0.9f));//-1
 6 
 7         //如果 遇到 0.5 时候会不一样, 结果看前一位
 8         /// 正数  偶数 --》  -0.5
 9         /// 负数  偶数 ---》  +0.5
10         /// 正数  奇数 --》  +0.5
11         /// 负数  奇数 --》  -0.5
12         Debug.Log(Mathf.Round(0.5f)); //1
13         Debug.Log(Mathf.Round(1.5f));//2
14         Debug.Log(Mathf.Round(-0.5f));//0
15         Debug.Log(Mathf.Round(-1.5f));//-2

Camp限制

 1         //Camp(value, min ,max)
 2         //限制:限制value的值在min 和 max之间,如果value小于min,返回min。
 3         //如果value大于max,返回max,返回max,否则返回value
 4         Debug.Log(Mathf.Clamp(12, 10, 20));//12
 5         Debug.Log(Mathf.Clamp(5, 10, 20));//10
 6         Debug.Log(Mathf.Clamp(25, 10, 20));//20
 7         //限制 0-1 之间,如果小于0返回0 ,如果大于1返回1,否则返回value
 8         Debug.Log(Mathf.Clamp01(0.1f));//0.1
 9         Debug.Log(Mathf.Clamp01(-0.1f));//0
10         Debug.Log(Mathf.Clamp01(2f));//1

插值

 1         //插值
 2         //第三个参数t :表示一个百分数,0-1,如果 t= 0.5f,那么返回值就从50%开始
 3         //1.第三个参数如果是固定值,则返回值是固定值根据参数大小而改变
 4         //2.第三个参数必须是0-1之间如果 <= 0 ,返回第一参数值,如果参数 >= 1 返回第2个参数
 5         // Mathf.Lerp(a, b, c)
 6         //原理 返回值 =  (b - a)*c + a ;
 7         Debug.Log(Mathf.Lerp(1, 100, Time.time));
 8         Debug.Log(Mathf.Lerp(1, 100, 0.5f));
 9         //物体匀速运动
10         obj.transform.position = new Vector3(Mathf.Lerp(0, 15, Time.time), 0, 0);
11 
12         //Mathf.LerpAngle(10, 100, Time.time);
13         obj.transform.eulerAngles = new Vector3(0, Mathf.LerpAngle(10, 100, Time.time), 0);

反插值

        //反插值
        Debug.Log( Mathf.InverseLerp(10, 100, Time.time));

阻尼运动

 1         //阻尼运动  
 2         float target = 5f;
 3         float curretVeloctity = 0f; //当前速度
 4         float smothTime = 1f;//平滑系数 = 1s
 5         float mManSpeed = Mathf.Infinity; //x轴的最大速度
 6         this.transform.position = new Vector3(
 7             Mathf.SmoothDamp(transform.position.x, target, ref curretVeloctity,smothTime,mManSpeed),
 8             transform.position.y,
 9             transform.position.z
10             );

Repeat 、 PingPong

1         //返回值 0-3 不停的重复
2         float f = Mathf.Repeat(Time.time, 3);
3         transform.localScale = Vector3.one * f;
4 
5         //返回值 0 - 3- 0 - 3 往返的返回值
6         float f2 = Mathf.PingPong(Time.time, 3);
7         transform.localScale = Vector3.one * f2;

颜色曲线

1         //颜色的改变是一个曲线的增长
2         Mathf.GammaToLinearSpace(); 
3         //颜色的增长是线性的增长
4         Mathf.LinearToGammaSpace();
5         /// 视觉效果上 GammaSpace 图显示效果要好
6         /// LinearSpace 要比 GammmaSpace 更容易计算
7         /// 因此, 往往处理颜色方面信息时, 需要用所讲的2个转换函数把Gamma Space -》Linear Space,
8         /// 然后对Linear Space进行计算后,再转换为GammaSpace
1         //颜色的改变是一个曲线的增长
2         Mathf.GammaToLinearSpace(); 
3         //颜色的增长是线性的增长
4         Mathf.LinearToGammaSpace();
5         /// 视觉效果上 GammaSpace 图显示效果要好
6         /// LinearSpace 要比 GammmaSpace 更容易计算
7         /// 因此, 往往处理颜色方面信息时, 需要用所讲的2个转换函数把Gamma Space -》Linear Space,
8         /// 然后对Linear Space进行计算后,再转换为GammaSpace

        PerlinNoise噪声图 ;返回的随机数之间不会有太大的变化,而是有一定的过渡效果
        Mathf.PerlinNoise();

 1     public Material mat;
 2     public Vector3 color;
 3 
 4     void Start()
 5     {
 6         mat = gameObject.GetComponent<MeshRenderer>().material;
 7         color = new Vector3(
 8             Random.Range(0f, 1f),
 9              Random.Range(0f, 1f),
10               Random.Range(0f, 1f));
11     }
12 
13     void Update()
14     {
15         float cx = Mathf.PerlinNoise(Time.time, color.x);
16         float cy = Mathf.PerlinNoise(Time.time, color.y);
17         float cz = Mathf.PerlinNoise(Time.time, color.z);
18 
19         mat.color = new Color(cx, cy, cz);
20     }
原文地址:https://www.cnblogs.com/CeasarH/p/9285217.html