This repository has been archived on 2025-09-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
XericLibrary-OLD/Runtime/MicroLibrary/MacroMath.cs
LiRuoChen a658cc424b 添加了更多的材质函数,部分与引擎新特性重合,这是为了能够脱离引擎在多个版本中更具可移植性。
添加了多种对程序流程控制与保全的脚本,而且现在可以更快速的构建一个具有线性关系的协程了。
  添加了一个支持多种模式的单维梯度控制器,类似unity的颜色梯度类型。
  添加了一个用于看向某个物体的旋转控制器。
  添加了一个加载宏。
  完善了批处理脚本流程。
2023-11-23 11:10:40 +08:00

242 lines
5.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Windows;
namespace XericLibrary.Runtime.MacroLibrary
{
/// <summary>
/// 数学库
/// </summary>
public static partial class MacroMath
{
#region
/// <summary>
/// 获取一个被特定随机数填充的矩阵,这通常是无意义的
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public static Matrix4x4 RandomMatrix4(float range)
{
return new Matrix4x4(
RandomVector4(range),
RandomVector4(range),
RandomVector4(range),
RandomVector4(range));
}
/// <summary>
/// 获取一个在指定坐标范围内的随机矢量(矩形范围)
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public static Vector4 RandomVector4(float range)
{
var rand = new System.Random();
range *= 100;
Func<float> GetNextRand = () =>
{
return rand.Next((int)-range, (int)range) * 0.01f;
};
return new Vector4(GetNextRand(), GetNextRand(), GetNextRand(), GetNextRand());
}
/// <summary>
/// 获取一个在指定坐标范围内的随机矢量(矩形范围)
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public static Vector3 RandomVector3(float range)
{
var rand = new System.Random();
range *= 100;
Func<float> GetNextRand = () =>
{
return rand.Next((int)-range, (int)range) * 0.01f;
};
return new Vector3(GetNextRand(), GetNextRand(), GetNextRand());
}
/// <summary>
/// 获取一个在指定坐标范围内的随机矢量(矩形范围)
/// </summary>
/// <param name="vec"></param>
/// <returns></returns>
public static Vector2 RandomVector2(float range)
{
var rand = new System.Random();
range *= 100;
Func<float> GetNextRand = () =>
{
return rand.Next((int)-range, (int)range) * 0.01f;
};
return new Vector2(GetNextRand(), GetNextRand());
}
/// <summary>
/// 哈希后处理
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MurmurFinalize(ulong index)
{
index ^= index >> 33;
index *= 0xff51afd7ed558ccd;
index ^= index >> 33;
index *= 0xc4ceb9fe1a85ec53;
index ^= index >> 33;
return index;
}
/// <summary>
/// 大范围随机
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong RandomNumber(ulong index)
{
index *= 1103515245;
index += 12345;
index *= 6364136223846793005UL;
index += 1442695040888963407UL;
index %= 18446744073709551615UL;
return index;
}
#endregion
#region
/// <summary>
/// pmod
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int PMod(int a, int b)
{
if(b <= 0)
return 0;
var mod = a % b;
return a < 0 ? (b + mod) % b : mod;
}
/// <summary>
/// pmod
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double PMod(double a, double b)
{
double z = a % b;
double w = (z < 0) ? -1 : 1;
z = (z < 0) ? -z : z;
if(w < 0)
return b - z;
else
return z;
}
/// <summary>
/// 获取两者的最小值,但不包括负数;
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float MinPositive(float a, float b)
=> Math.Min(Math.Max(0, a), Math.Max(0, b));
/// <summary>
/// 获取两者的最小值,但不包括负数;
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MinPositive(int a, int b)
=> Math.Min(Math.Max(0, a), Math.Max(0, b));
#endregion
#region
/// <summary>
/// 设置字
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetByteState(ref int target, int site, bool state)
{
if(state)
target |= 1 << site;
else
target &= 0xff - (1 << site);
}
/// <summary>
/// 设置字
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetByteState(ref int target, Enum site, bool state)
{
SetByteState(ref target, Convert.ToInt32(site), state);
}
/// <summary>
/// 检查字
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetByteState(int target, int site)
{
return (target >> site & 0x01) > 0;
}
/// <summary>
/// 检查字
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetByteState(int target, Enum site)
{
return GetByteState(target, Convert.ToInt32(site));
}
#endregion
#region
/// <summary>
/// 获取被从A到B的值填充的列表
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static List<int> GetListFormAToB(int a, int b)
{
var list = new List<int>();
bool way = a > b;
var step = way ? -1 : 1;
for(int i = a; (!way && i <= b) || (way && i >= b); i += step)
list.Add(i);
return list;
}
#endregion
}
}