Archived
添加了更多的材质函数,部分与引擎新特性重合,这是为了能够脱离引擎在多个版本中更具可移植性。
添加了多种对程序流程控制与保全的脚本,而且现在可以更快速的构建一个具有线性关系的协程了。 添加了一个支持多种模式的单维梯度控制器,类似unity的颜色梯度类型。 添加了一个用于看向某个物体的旋转控制器。 添加了一个加载宏。 完善了批处理脚本流程。
This commit is contained in:
@@ -145,5 +145,7 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,72 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
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>
|
||||
@@ -85,11 +150,22 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
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));
|
||||
@@ -162,7 +238,5 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#define _DEBUG_
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
/// <summary>
|
||||
/// 加载宏
|
||||
/// </summary>
|
||||
public static class MacroResources
|
||||
{
|
||||
#region 字段属性
|
||||
|
||||
public static readonly Dictionary<string, string> FilePathName = new ()
|
||||
{
|
||||
// 图片
|
||||
{ "Sprite","Sprite" },
|
||||
{ "Texture2D","Sprite" },
|
||||
// 声音
|
||||
{ "Audio","Audio"},
|
||||
// 字体
|
||||
{ "Font","Font"},
|
||||
// 脚本
|
||||
{ "MonoBehaviour","Script"},
|
||||
// 脚本
|
||||
{ "RuntimeAnimatorController","Animation"},
|
||||
// 材质
|
||||
{ "PhysicMaterial","Material"},
|
||||
{ "Material","Material"},
|
||||
{ "Shader","Material"},
|
||||
// 模型
|
||||
{ "Mesh","Mesh"},
|
||||
// 预制体
|
||||
{ "GameObject","Prefab"},
|
||||
// 场景
|
||||
{ "Scene","Scene"},
|
||||
};
|
||||
|
||||
private static System.Type CacheType;
|
||||
|
||||
private static UnityEngine.Object[] ResourceCache;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 加载方法
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个类型应该存在的根路径
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetFileRootPath(System.Type type, out string path)
|
||||
{
|
||||
var name = type.Name;
|
||||
|
||||
if(FilePathName.TryGetValue(name, out path))
|
||||
return true;
|
||||
#if _DEBUG_
|
||||
Debug.LogError($"在Resources中加载{name}时并未找到一个有效的根路径");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个类型应该存在的根路径
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetFileRootPath<T>(out string path)
|
||||
{
|
||||
return GetFileRootPath(typeof(T), out path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载一个给定名称的特定文件类型的文件
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public static bool LoadFile<T>(this string fileName, out T result)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
var type = typeof(T);
|
||||
if(!GetFileRootPath(type, out var path))
|
||||
{
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
if(CacheType != type)
|
||||
{
|
||||
CacheType = type;
|
||||
ResourceCache = Resources.LoadAll<T>(path);
|
||||
}
|
||||
result = ResourceCache.FirstOrDefault(a => a.name == fileName) as T;
|
||||
return result != null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 036eda606c33c8448b301e568b8e8d46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,363 @@
|
||||
using Codice.Client.BaseCommands.BranchExplorer;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using XericLibrary.Runtime.Type;
|
||||
|
||||
namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
/// <summary>
|
||||
/// 平滑宏
|
||||
/// </summary>
|
||||
public static partial class MacroSmooth
|
||||
{
|
||||
#region y=f(x)
|
||||
|
||||
public static double Linear(double k)
|
||||
{
|
||||
return k;
|
||||
}
|
||||
public static double QuadraticIn(double k)
|
||||
{
|
||||
return k * k;
|
||||
}
|
||||
public static double QuadraticOut(double k)
|
||||
{
|
||||
return k * (2 - k);
|
||||
}
|
||||
public static double QuadraticInOut(double k)
|
||||
{
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * k * k;
|
||||
}
|
||||
return -0.5 * (--k * (k - 2) - 1);
|
||||
}
|
||||
public static double CubicIn(double k)
|
||||
{
|
||||
return k * k * k;
|
||||
}
|
||||
public static double CubicOut(double k)
|
||||
{
|
||||
return --k * k * k + 1;
|
||||
}
|
||||
public static double CubicInOut(double k)
|
||||
{
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * k * k * k;
|
||||
}
|
||||
return 0.5 * ((k -= 2) * k * k + 2);
|
||||
}
|
||||
public static double QuarticIn(double k)
|
||||
{
|
||||
return k * k * k * k;
|
||||
}
|
||||
public static double QuarticOut(double k)
|
||||
{
|
||||
return 1 - --k * k * k * k;
|
||||
}
|
||||
public static double QuarticInOut(double k)
|
||||
{
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * k * k * k * k;
|
||||
}
|
||||
return -0.5 * ((k -= 2) * k * k * k - 2);
|
||||
}
|
||||
public static double QuinticIn(double k)
|
||||
{
|
||||
return k * k * k * k * k;
|
||||
}
|
||||
public static double QuinticOut(double k)
|
||||
{
|
||||
return --k * k * k * k * k + 1;
|
||||
}
|
||||
public static double QuinticInOut(double k)
|
||||
{
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * k * k * k * k * k;
|
||||
}
|
||||
return 0.5 * ((k -= 2) * k * k * k * k + 2);
|
||||
}
|
||||
public static double SinusoidalIn(double k)
|
||||
{
|
||||
return 1 - Math.Cos((k * Math.PI) / 2);
|
||||
}
|
||||
public static double SinusoidalOut(double k)
|
||||
{
|
||||
return Math.Sin((k * Math.PI) / 2);
|
||||
}
|
||||
public static double SinusoidalInOut(double k)
|
||||
{
|
||||
return 0.5 * (1 - Math.Cos(Math.PI * k));
|
||||
}
|
||||
public static double ExponentialIn(double k)
|
||||
{
|
||||
return k == 0 ? 0 : Math.Pow(1024, k - 1);
|
||||
}
|
||||
public static double ExponentialOut(double k)
|
||||
{
|
||||
return k == 1 ? 1 : 1 - Math.Pow(2, -10 * k);
|
||||
}
|
||||
public static double ExponentialInOut(double k)
|
||||
{
|
||||
if(k == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(k == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * Math.Pow(1024, k - 1);
|
||||
}
|
||||
return 0.5 * (-Math.Pow(2, -10 * (k - 1)) + 2);
|
||||
}
|
||||
public static double CircularIn(double k)
|
||||
{
|
||||
return 1 - Math.Sqrt(1 - k * k);
|
||||
}
|
||||
public static double CircularOut(double k)
|
||||
{
|
||||
return Math.Sqrt(1 - --k * k);
|
||||
}
|
||||
public static double CircularInOut(double k)
|
||||
{
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return -0.5 * (Math.Sqrt(1 - k * k) - 1);
|
||||
}
|
||||
return 0.5 * (Math.Sqrt(1 - (k -= 2) * k) + 1);
|
||||
}
|
||||
public static double ElasticIn(double k)
|
||||
{
|
||||
double s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
if(k == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(k == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if(a != 0 || a < 1)
|
||||
{
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
s = (p * Math.Asin(1 / a)) / (2 * Math.PI);
|
||||
}
|
||||
return -(
|
||||
a *
|
||||
Math.Pow(2, 10 * (k -= 1)) *
|
||||
Math.Sin(((k - s) * (2 * Math.PI)) / p)
|
||||
);
|
||||
}
|
||||
public static double ElasticOut(double k)
|
||||
{
|
||||
double s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
if(k == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(k == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if(a != 0 || a < 1)
|
||||
{
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
s = (p * Math.Asin(1 / a)) / (2 * Math.PI);
|
||||
}
|
||||
return (
|
||||
a * Math.Pow(2, -10 * k) * Math.Sin(((k - s) * (2 * Math.PI)) / p) + 1
|
||||
);
|
||||
}
|
||||
public static double ElasticInOut(double k)
|
||||
{
|
||||
double s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
if(k == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(k == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if(a != 0 || a < 1)
|
||||
{
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
s = (p * Math.Asin(1 / a)) / (2 * Math.PI);
|
||||
}
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return (
|
||||
-0.5 *
|
||||
(a *
|
||||
Math.Pow(2, 10 * (k -= 1)) *
|
||||
Math.Sin(((k - s) * (2 * Math.PI)) / p))
|
||||
);
|
||||
}
|
||||
return (
|
||||
a *
|
||||
Math.Pow(2, -10 * (k -= 1)) *
|
||||
Math.Sin(((k - s) * (2 * Math.PI)) / p) *
|
||||
0.5 +
|
||||
1
|
||||
);
|
||||
}
|
||||
// 在某一动画开始沿指示的路径进行动画处理前稍稍收回该动画的移动
|
||||
public static double BackIn(double k)
|
||||
{
|
||||
var s = 1.70158;
|
||||
return k * k * ((s + 1) * k - s);
|
||||
}
|
||||
public static double BackOut(double k)
|
||||
{
|
||||
var s = 1.70158;
|
||||
return --k * k * ((s + 1) * k + s) + 1;
|
||||
}
|
||||
public static double BackInOut(double k)
|
||||
{
|
||||
var s = 1.70158 * 1.525;
|
||||
if((k *= 2) < 1)
|
||||
{
|
||||
return 0.5 * (k * k * ((s + 1) * k - s));
|
||||
}
|
||||
return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
|
||||
}
|
||||
public static double BounceIn(double k)
|
||||
{
|
||||
return 1 - BounceOut(1 - k);
|
||||
}
|
||||
public static double BounceOut(double k)
|
||||
{
|
||||
if(k < 1 / 2.75)
|
||||
{
|
||||
return 7.5625 * k * k;
|
||||
}
|
||||
else if(k < 2 / 2.75)
|
||||
{
|
||||
return 7.5625 * (k -= 1.5 / 2.75) * k + 0.75;
|
||||
}
|
||||
else if(k < 2.5 / 2.75)
|
||||
{
|
||||
return 7.5625 * (k -= 2.25 / 2.75) * k + 0.9375;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 7.5625 * (k -= 2.625 / 2.75) * k + 0.984375;
|
||||
}
|
||||
}
|
||||
public static double BounceInOut(double k)
|
||||
{
|
||||
if(k < 0.5)
|
||||
{
|
||||
return BounceIn(k * 2) * 0.5;
|
||||
}
|
||||
return BounceIn(k * 2 - 1) * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 多函数一站式解决
|
||||
|
||||
/// <summary>
|
||||
/// 切换缓动模式
|
||||
/// </summary>
|
||||
/// <param name="k"></param>
|
||||
/// <param name="mode"></param>
|
||||
/// <returns></returns>
|
||||
public static double SwitchEasing(this double k, EasingMode mode)
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
default:
|
||||
case EasingMode.Linear:
|
||||
return Linear(k);
|
||||
case EasingMode.QuadraticIn:
|
||||
return QuadraticIn(k);
|
||||
case EasingMode.QuadraticOut:
|
||||
return QuadraticOut(k);
|
||||
case EasingMode.QuadraticInOut:
|
||||
return QuadraticInOut(k);
|
||||
case EasingMode.CubicIn:
|
||||
return CubicIn(k);
|
||||
case EasingMode.CubicOut:
|
||||
return CubicOut(k);
|
||||
case EasingMode.CubicInOut:
|
||||
return CubicInOut(k);
|
||||
case EasingMode.QuarticIn:
|
||||
return QuarticIn(k);
|
||||
case EasingMode.QuarticOut:
|
||||
return QuarticOut(k);
|
||||
case EasingMode.QuarticInOut:
|
||||
return QuarticInOut(k);
|
||||
case EasingMode.QuinticIn:
|
||||
return QuinticIn(k);
|
||||
case EasingMode.QuinticOut:
|
||||
return QuinticOut(k);
|
||||
case EasingMode.QuinticInOut:
|
||||
return QuinticInOut(k);
|
||||
case EasingMode.SinusoidalIn:
|
||||
return SinusoidalIn(k);
|
||||
case EasingMode.SinusoidalOut:
|
||||
return SinusoidalOut(k);
|
||||
case EasingMode.SinusoidalInOut:
|
||||
return SinusoidalInOut(k);
|
||||
case EasingMode.ExponentialIn:
|
||||
return ExponentialIn(k);
|
||||
case EasingMode.ExponentialOut:
|
||||
return ExponentialOut(k);
|
||||
case EasingMode.ExponentialInOut:
|
||||
return ExponentialInOut(k);
|
||||
case EasingMode.CircularIn:
|
||||
return CircularIn(k);
|
||||
case EasingMode.CircularOut:
|
||||
return CircularOut(k);
|
||||
case EasingMode.CircularInOut:
|
||||
return CircularInOut(k);
|
||||
case EasingMode.ElasticIn:
|
||||
return ElasticIn(k);
|
||||
case EasingMode.ElasticOut:
|
||||
return ElasticOut(k);
|
||||
case EasingMode.ElasticInOut:
|
||||
return ElasticInOut(k);
|
||||
case EasingMode.BackIn:
|
||||
return BackIn(k);
|
||||
case EasingMode.BounceIn:
|
||||
return BounceIn(k);
|
||||
case EasingMode.BounceOut:
|
||||
return BounceOut(k);
|
||||
case EasingMode.BounceInOut:
|
||||
return BounceInOut(k);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f28db5d54680eac40b6a17c9bcf4e8bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
@@ -9,8 +10,7 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
public static partial class MacroMath
|
||||
{
|
||||
|
||||
#region 文本
|
||||
#region 名称文本
|
||||
|
||||
/// <summary>
|
||||
/// 获取此处的物体名称,如果带有(clone)后缀,则省略这个后缀
|
||||
@@ -39,6 +39,28 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
|
||||
#endregion
|
||||
|
||||
#region 转文本
|
||||
|
||||
/// <summary>
|
||||
/// 将一个数值四舍五入后转为一个文本,并保留特定的小数点位,如果这个小数位为0则省略不写
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="decimals">目标小数位</param>
|
||||
/// <returns></returns>
|
||||
public static string RoundToString(this double value, int decimals)
|
||||
{
|
||||
if(value == 0)
|
||||
return "0";
|
||||
return Math.Round(value, decimals).ToString();
|
||||
}
|
||||
|
||||
public static string RoundToString(this float value, int decimals)
|
||||
{
|
||||
return ((double)value).RoundToString(decimals);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 区域与语言
|
||||
|
||||
#if false
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
/// <summary>
|
||||
/// 矢量宏库
|
||||
/// </summary>
|
||||
[Obsolete("与扩展库放在一起,不要单独出现")]
|
||||
public static class MacroVector3
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取与世界正交轴最近的一条轴线
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static Vector3 GetWorldNormalAxis(Vector3 vector)
|
||||
{
|
||||
Vector3[] axes = { Vector3.right, Vector3.up, Vector3.forward };
|
||||
float minAngle = float.MaxValue;
|
||||
Vector3 minAngleAxis = Vector3.zero;
|
||||
|
||||
foreach(Vector3 axis in axes)
|
||||
{
|
||||
float dot = Vector3.Dot(vector, axis);
|
||||
float angle = Mathf.Abs(dot);
|
||||
if(angle < minAngle)
|
||||
{
|
||||
minAngle = angle;
|
||||
minAngleAxis = axis * Mathf.Sign(dot);
|
||||
}
|
||||
}
|
||||
|
||||
return minAngleAxis;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25b8c78062bd7454a944e8f73f7f17b5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,6 +9,8 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
public static class Vector3Extend
|
||||
{
|
||||
#region 四则运算
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Add(this Vector3 a, Vector3 b)
|
||||
=> new Vector3(
|
||||
@@ -105,6 +107,9 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
a.z / b
|
||||
);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 扩展运算
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Xonly(this Vector3 a)
|
||||
@@ -174,6 +179,9 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
public static Vector3 Floor(this Vector3 vec)
|
||||
=> new Vector3((int)vec.x, (int)vec.y, (int)vec.z);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 高级运算
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Min(this Vector3 a, Vector3 b)
|
||||
@@ -242,6 +250,9 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 特殊运算
|
||||
|
||||
/// <summary>
|
||||
/// 将这个向量投影到一个给定坐标的向上的平面上,同时变换到平面坐标
|
||||
@@ -294,11 +305,37 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
/// <returns></returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3 Orthogonalization(this Vector3 vec)
|
||||
=> new Vector3(
|
||||
(float)Math.Round(vec.x),
|
||||
(float)Math.Round(vec.y),
|
||||
(float)Math.Round(vec.z)
|
||||
);
|
||||
{
|
||||
Vector3[] axes = { Vector3.right, Vector3.up, Vector3.forward };
|
||||
float minAngle = float.MaxValue;
|
||||
Vector3 minAngleAxis = Vector3.zero;
|
||||
|
||||
foreach(Vector3 axis in axes)
|
||||
{
|
||||
float dot = Vector3.Dot(vec, axis);
|
||||
float angle = Mathf.Abs(dot);
|
||||
if(angle < minAngle)
|
||||
{
|
||||
minAngle = angle;
|
||||
minAngleAxis = axis * Mathf.Sign(dot);
|
||||
}
|
||||
}
|
||||
|
||||
return minAngleAxis;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取与世界正交轴最近的一条轴线
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("可以使用Orthogonalization代替")]
|
||||
public static Vector3 GetWorldNormalAxis(this Vector3 vec)
|
||||
{
|
||||
return vec.Orthogonalization();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user