Mathf数学函数总结

2022-08-29 15:30:54 浏览数 (1)

**Mathf.Abs 绝对值** C# => static float Abs(float f); Description: Returns the absolute value of f. 返回f的绝对值。 Example: Debug.log(Mathf.Abs(-10)); --> 10 **Mathf.Acos 反余弦** C# => static float Acos(float f); Description: Returns the arc-cosine of f - the angle in radians whose cosine is f. **Mathf.Approximately 近似值** C# => static bool approximately (float a, float b) Description: Compares two floating point values if they are similar. 比较两个浮点数值,看它们是否非常接近。 Example: Debug.Log(Mathf.Approximately(1.0f, 10.0f / 10.0f)); --> true **Mathf.Asin 反正弦** C# => static float Asin(float f); Description: Returns the arc-sine of f - the angle in radians whose sine is f. **Mathf.Atan 反正切** C# => static float Atan(float f); Description: Returns the arc-tangent of f - the angle in radians whose tangent is f. **Mathf.Ceil 向上进位取整** C# => static float Ceil (float f) Description: Returns the smallest integer greater to or equal to f. 返回大于或等于f的最小整数。 Example: Debug.Log(Mathf.Ceil(10.2f)); --> 11 **Mathf.CeilToInt 向上进位取整** C# => static int CeilToInt(float f); **Mathf.Clamp 钳制** C# => static float Clamp(float value, float min, float max ) Description: Clamps a value between a minimum float and maximum float value. 限制value的值在min和max之间, 如果value小于min,返回min。如果value大于max,返回max,否则返回value Example: Debug.log(Mathf.Clamp(10, 1, 3)); -->3 **Mathf.Clamp01 钳制01** C# => static float Clamp01(float value); Description: Clamps value between 0 and 1 and returns value. 限制value在0,1之间并返回value。如果value小于0,返回0。如果value大于1,返回1,否则返回value 。 **Mathf.ClosestPowerOfTwo 最接近二次方** C# => static int CloestPowerOfTwo(int value) Description: Return the closet power of two value. 返回距离value最近的2的次方数。 Example: Debug.Log(Mathf.ClosestPowerOfTwo(7)); -->8 **Mathf.Cos 余弦** C# => static float Cos(float f); Description: Returns the cosine of angle f in radians. 返回由参数 f 指定的角的余弦值(介于 -1.0 与 1.0 之间的值)。 **Mathf.Deg2Rad 度转弧度** C# => static float Deg2Rad; Description: Degress to radians conversion constant(Read Only). 度到弧度的转化常量 Example: public float deg = 30.0f; private void Start() {float rad = deg * Mathf.Deg2Rad; } **Mathf.Rad2Deg 弧度转度** C# => static float Rad2Deg; Description: Radians to degress conversion constant(Read Only). 弧度到度的转化常量 Example: public float rad = 10.0f; private void Start() {float deg = rad * Mathf.Rad2Deg; } **Mathf.DeltaAngle 增量角** C# => static float DeltaAngle(float current, float target); Description: Calculates the shortest difference between two given angles. 计算给定的两个角之间最短的差异。 Example: Debug.Log(Mathf.DeltaAngle(1080, 90)); --> 90 **Mathf.Exp 指数** C# => static float Exp(float power) Description: Returns e raised to the specified power. 返回 e 的 power 次方的值。 **Mathf.Epsilon** C# => static float Epsilon **Mathf.Floor 向下舍位取整** C# => static float Floor(float t) Description: Returns the largest integer smaller to or equal to f. 返回小于或等于该数的最大整数。 Example: Debug.log(Mathf.Floor(-10.5f)); --> -11 **Mathf.FloorToInt 向下舍位取整** C# => static int Floor(floot f) **Mathf.GammaToLinearSpace 伽马转线性空间** C# => static float GammaToLinearSpace(float value) Description: Converts the given value from gamma to linear color space. 转换给定值从gamma到线性颜色空间。

**Mathf.LinearToGammaSpace 线性转伽马空间** C# => static float LinearToGammaSpace(float value); Description: Converts the given value from linear to gamma color space. 转换给定值从线性到伽马颜色空间。

**Mathf.Infinity 正无穷** C# => static float Infinity Description: A representation of positive infinity(Read Only). 表示正无穷,也就是无穷大。 Example: Physics.RayCast(Vector3.zero, Vector3.forward, Mathf.Infinity) **Mathf.NegativeInfinity 负无穷** A representation of negative infinity(Read Only). 表示负无穷,也就是无穷小。 **Mathf.InverseLerp 反插值** C# => static float InverseLerp(float from, float to ,float value) Description: Calculates the lerp parameter between of two values. 计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。 Example: Debug.log(Mathf.Inverselerp(5.0f, 10.0f, 8.0f)); --> 3/5 **Mathf.IsPowerOfTwo 是否是二次方** C# => static bool IsPowerOfTwo(int value) Description: Returns true if the value is power of two. 如果该值是2的次方,返回true。

**Mathf.Lerp 插值** C# => static float Lerp(float a, float b, float t) Description: Interpolates a towards b by t. t is clamped between 0 and 1. **Mathf.LerpAngle 插值角度** C# => static float LerpAngle(float a, float b, float t) Description: Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degress. **Mathf.Log 对数** C# => static float Log(float f, float p) Description: Returns the logarithm of a specified number in a specified base. Example: Debug.Log(Mathf.Log(6, 2)); --> 2.584963 **Mathf.Log10 对数10** C# => static float Log10(float f) Description: Returns the base 10 logarithm of a specified number. 返回指定值的对数,基数为10。 Example: Debug.log(Mathf.Log10(100)); --> 2 **Mathf.Max 最大值** C# => static float Max(float a, float b) C# => static float Max(params float[] values) Description: Returns the largest of two or more values. **Mathf.Min 最小值** C# => static float Min(float a, float b) C# => static float Min(params float[] values) Description: Returns the smallest of two or more values. **Mathf.MoveTowards 移向** C# => static flaot MoveTowards(flaot current, float target, float maxDelta) Description: Moves a value current towards target. This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta. 改变一个当前值向目标值靠近。这实际上和 Mathf.Lerp相同,但该函数将确保我们的速度不会超过maxDelta。 Example: public float currStrength; public float maxStrength; public float recoveryRate; private void Update() { currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime); }

**Mathf.MoveTowardsAngle 移向角度** C# => static float MoveTowardsAngle(float current, float target, float maxDelta) Description: Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees. 像MoveTowards,但是当它们环绕360度时确保插值正确。 Example: public float target = 270.0f; public float speed = 45.0f; private void Update() { float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime); transform.eulerAngles = new Vector3(0, angle, 0); } **Mathf.NextPowerOfTwo 最接近的二次方** C# => static int NextPowerOfTwo(int value) Description: Returns the next power of two value. 返回最接近的二次方的值。 Example: Debug.Log(Mathf.NextPowerOfTwo(7)); --> 8 **Mathf.PerlinNoise 柏林噪波** C# => static float PerlinNoise(float x, float y) Description: Generate 2D Perlin noise. float Value between 0.0 and 1.0. 生成二维柏林噪波。返回0.0 ~ 1.0的浮点数值。 柏林噪波是在2D平面浮点值生成的伪随机图案。此噪波不是由每个点的完全随机值构成,而是由逐渐增加和减少交错波 形图案值构成。此噪波可以作为基本纹理效果,但也可用于动画、生成地形高度图以及其他东西。 Example: public float heightScale = 1.0f; public float xScale = 1.0f; private void Update() { float height = heightScale * Mathf.PerlinNoise(Time.time * xScale, 0.0f); Vector3 pos = transform.position; pos.y = height; transform.position = pos; }

**Mathf.PI 圆周率** C# static float PI Description: The infamous 3.1415926... value(Read Only) **Mathf.PingPong 乒乓** C# => static float PingPong(float t, float length) Description: PingPongs the value t, so that it is never larger than length and never smaller than 0. 让数值t在 0到 length之间往返。t值永远不会大于length的值,也永远不会小于0。 Example: transform.position = new Vector3(Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z); **Mathf.Pow 幂** C# => static float Pow(float f, float p) Description: Returns f raised to power p. 计算并返回 f 的 p 次幂。 Example: Debug.Log(Mathf.Pow(2f, 3f)); --> 8 **Mathf.Repeat 重复** C# => static float Repeat(float t, float length) Description: Loops the value t, so that it is never larger than length and never smaller than 0. 循环t值,从来不会比length大,并且从不会小于0,取值在0~length之间。 Example: transform.position = new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y, transform.position.z); **Mathf.Round 四舍五入** C# => static float Round(float f) Description: Returns f rounded to the nearest integer. 返回浮点数 f 进行四舍五入最接近的整数。 Example: Debug.Log(Mathf.Round(10.7)); --> 11 **Mathf.RoundToInt 四舍五入** C# => static int Round(float f) **Mathf.Sign 数字符号** C# => static float Sign(float f) Description: Returns value is 1 when f is positive or zero, -1 when f is negative. 当 f 为正或为0返回1,为负返回-1。 Example: Debug.Log(Mathf.Sign(3.0f)); Debug.Log(Mathf.Sign(-3.0f)); --> 1 -1 **Mathf.SmoothDamp 平滑阻尼** C# => static float SmoothDamp(float current, float target, float currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime); Description: Gradually changes a value towards a desired goal over time. 随着时间的推移逐渐改变一个值到目标值。 这个值就像一个不会崩溃的弹簧减震器一样被平滑。这个函数可以用来平滑任何类型的值,位置,颜色,标量。 Parameters: current 当前的位置 target 目标位置 currentVelocity 当前速度,这个值在你访问这个函数的时候会被随时修改 smoothTime 要到达目标位置的近似时间,实际到达目标时要快一些 maxSpeed 可选参数,允许限制最大速度 deltaTime 可选参数,上次调用该函数到现在的时间 Example: public Transform target; public float smoothTime = 0.3f; public float yVelocity = 0.0f; private void Update() { float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime); transform.position = new Vector3(transform.position.x, newPosition, transform.position.z); } **Mathf.SmoothDampAngle 平滑阻尼角度** C# => static float SmoothDampAngle(float current, float target, float currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);

**Mathf.SmoothStep 平滑插值** C# => static float SmoothStep(float min, float max, float t); Description: Interpolates between min and max with smoothing at the limits. 在min和max之间平滑插值,类似于Lerp方法。然而,插值从开始处将逐渐加快,到结束处减慢。这通常用于创建渐变和其他过渡看起来比较自然的动画。 Example: public float minimum = 10.0f; public float maximum = 20.0f; public float duration = 5.0f; private float startTime; private void Start() { startTime = Time.time; } private void Update() { float t = (Time.time - startTime) / duration; transform.position = new Vector3(Mathf.SmoothStep(minimum, maximum, t), 0, 0); } **Mathf.Sqrt 平方根** C# => static float Sqrt(float f); Description: Returns square root of f. 计算并返回f的平方根 Example: Debug.Log(Mathf.Sqrt(9)); --> 3 **Mathf.Tan 正切** C# => static float Tan(float f); Description: Returns the tangent of angle f in radians. 返回由参数 f 指定的角的正切值 Example: Debug.Log(Mathf.Tan(45 * Mathf.Deg2Rad)); -->1

0 人点赞