This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -44,13 +44,23 @@ namespace BITKit
var deltaRot = targetRotation * Quaternion.Inverse(transform.rotation);
deltaRot.ToAngleAxis(out var angle, out var axis);
var angleDelta = axis * (angle * Mathf.Deg2Rad);
angleDelta.x = MathV.TransientRotationAxis(angleDelta.x);
var torqueLocal = transform.InverseTransformDirection(angleDelta);
// torqueLocal = rigidbody.inertiaTensorRotation * torqueLocal;
// torqueLocal.Scale(rigidbody.inertiaTensor);
// torqueLocal = Quaternion.Inverse(rigidbody.inertiaTensorRotation) * torqueLocal;
var torque = transform.TransformDirection(torqueLocal);
return torque;
var minTorque = MathV.TransientRotationAxis(torque);
return new Vector3()
{
x = Mathf.Min(torque.x, minTorque.x),
y = Mathf.Min(torque.y, minTorque.y),
z = Mathf.Min(torque.z, minTorque.z),
};
}
public static Vector3 Multiply(params Vector3[] vectors)
{
@@ -175,6 +185,10 @@ namespace BITKit
return remainder > snapSize / 2 ? size + 1 : size;
}
}
public static float GetLength(this Vector2 self)
{
return Mathf.Max(Mathf.Abs(self.x), Mathf.Abs(self.y));
}
public static float GetLength(this Vector3 self)
{
return Mathf.Max(Mathf.Abs(self.x), Mathf.Abs(self.y), Mathf.Abs(self.z));
@@ -200,21 +214,19 @@ namespace BITKit
}
public static bool InRange(this Vector2Int self, Vector2Int other)
{
return self.x >= 0
&& self.y >= 0
&& other.x >= 0
&& other.y >= 0
&& other.x <= self.x
&& other.y <= self.y
&& (self + other).x >= 0
&& (self + other).y >= 0;
return self is { x: >= 0, y: >= 0 }
&& other is { x: >= 0, y: >= 0 }
&& other.x <= self.x
&& other.y <= self.y
&& (self + other).x >= 0
&& (self + other).y >= 0;
}
public static float TransientRotationAxis(this float transientAxis)
{
return (transientAxis %= 360) switch
{
var x when x > 180 => transientAxis - 360,
var x when x < -180 => transientAxis + 360,
> 180 => transientAxis - 360,
< -180 => transientAxis + 360,
_ => transientAxis,
};
}
@@ -297,6 +309,7 @@ namespace BITKit
}
public static T Random<T>(this T[] self)
{
if (self is null || self.Length is 0) return default;
return self[UnityEngine.Random.Range(0, self.Length)];
}
public static bool IsNullOrEmpty(this string self)
@@ -614,6 +627,10 @@ namespace BITKit
{
self.style.display = new(active ? DisplayStyle.Flex : DisplayStyle.None);
}
public static void SetVisible(this VisualElement self, bool visible)
{
self.style.visibility = new(visible ? Visibility.Visible : Visibility.Hidden);
}
public static float GetOpacity(this VisualElement self) => self.style.opacity.value;
public static void SetOpacity(this VisualElement self, float value) => self.style.opacity = new(value);
public static void ScrollToBottom(this ScrollView self)
@@ -623,50 +640,66 @@ namespace BITKit
}
public static async void ScrollToBottomAutomatic(this ScrollView self, float delay = 0.02f)
{
if (Math.Abs(self.verticalScroller.value - self.verticalScroller.highValue) < 0.01)
switch (true)
{
try
{
await Task.Delay(TimeSpan.FromSeconds(delay), BITApp.CancellationToken);
ScrollToBottom(self);
}
catch (OperationCanceledException) { }
catch (System.Exception)
{
throw;
}
case true when Math.Abs(self.verticalScroller.value - self.verticalScroller.highValue) < 0.01:
case true when self.verticalScroller.highValue < 0.01f:
case true when Math.Abs(self.verticalScroller.value - self.verticalScroller.highValue) < 0.01f:
try
{
await Task.Delay(TimeSpan.FromSeconds(delay), BITApp.CancellationToken);
ScrollToBottom(self);
}
catch (OperationCanceledException)
{
}
break;
}
}
public static bool IsVisible(this VisualElement self,Vector3 worldPosition)
{
var cameraTrans = Camera.main.transform;
return Vector3.Dot(cameraTrans.forward, worldPosition - cameraTrans.position) > 0;
}
public static Vector2 GetScreenPosition(this VisualElement self, Vector3 worldPosition)
{
var pos = RuntimePanelUtils
.CameraTransformWorldToPanel(self.panel, worldPosition, Camera.main);
pos.x -= self.layout.width / 2;
pos.y -= self.layout.height / 2;
return pos;
}
public static Vector2 GetPosition(this VisualElement self)
{
return new Vector2(self.style.left.value.value, self.style.top.value.value);
}
public static void SetPosition(this VisualElement self, Vector2 screenPosition)
{
self.style.right = new StyleLength(StyleKeyword.Auto);
self.style.bottom = new StyleLength(StyleKeyword.Auto);
self.style.position = Position.Absolute;
self.style.left = screenPosition.x;
self.style.top = screenPosition.y;
}
public static void SetPosition(this VisualElement self,Vector3 worldPosition)
{
try
{
var camera = Camera.main;
if (camera is null) return;
var cameraTrans = camera.transform;
var pos = RuntimePanelUtils
.CameraTransformWorldToPanel(self.panel, worldPosition, camera);
pos.x = (pos.x - self.layout.width / 2);
pos.y = (pos.y - self.layout.height / 2);
self.style.left = 0;
self.style.top = 0;
self.style.right = new StyleLength(StyleKeyword.Auto);
self.style.bottom = new StyleLength(StyleKeyword.Auto);
self.style.position = Position.Absolute;
// self.transform.position = pos;
self.style.left = pos.x;
self.style.top = pos.y;
self.SetOpacity(Vector3.Dot(cameraTrans.forward, worldPosition - cameraTrans.position) > 0 ? 1 : 0);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
var pos = self.GetScreenPosition(worldPosition);
var visible = self.IsVisible(worldPosition);
self.style.right = new StyleLength(StyleKeyword.Auto);
self.style.bottom = new StyleLength(StyleKeyword.Auto);
self.style.position = Position.Absolute;
// self.transform.position = pos;
self.style.left = pos.x;
self.style.top = pos.y;
self.SetOpacity(visible ? 1 : 0);
}
}
}