Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
HankunYu committed Nov 24, 2024
1 parent f59f311 commit bc4683b
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 17 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "com.hankun.utilis.editor",
"rootNamespace": "",
"references": [],
"references": [
"GUID:e8b70daea7d4844a1b9bf7448c1d0af9"
],
"includePlatforms": [
"Editor"
],
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions Assets/Packages/Utilis/Runtime/GizmosHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

266 changes: 266 additions & 0 deletions Assets/Packages/Utilis/Runtime/Utilis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/// <summary>
/// Functions from Hurricane Plugin
/// </summary>
public static class Utilis
{
public static float GetSign(this Axis axis)
{
return axis switch
{
Axis.X => 1f,
Axis.Y => 1f,
Axis.Z => 1f,
Axis.NegX => -1f,
Axis.NegY => -1f,
Axis.NegZ => -1f,
_ => throw new ArgumentOutOfRangeException($"Cannot get sign from axis, no case for axis={axis}")
};
}

public static Vector3 GetVector(this Axis axis)
{
return axis switch
{
Axis.X => Vector3.right,
Axis.Y => Vector3.up,
Axis.Z => Vector3.forward,
Axis.NegX => -Vector3.right,
Axis.NegY => -Vector3.up,
Axis.NegZ => -Vector3.forward,
_ => throw new ArgumentOutOfRangeException($"Cannot get vector from axis - no case for axis={axis}")
};
}

public static float GetVectorDimension(this Axis axis, Vector3 vector)
{
return axis switch
{
Axis.X => vector.x,
Axis.Y => vector.y,
Axis.Z => vector.z,
Axis.NegX => -vector.x,
Axis.NegY => -vector.y,
Axis.NegZ => -vector.z,
_ => throw new ArgumentOutOfRangeException(nameof(axis), axis, null)
};
}

public static Vector3 OrthogonalVector(this Vector3 v)
{
//////https://math.stackexchange.com/questions/137362/how-to-find-perpendicular-vector-to-another-vector
v.Normalize();

float x = v.x;
float y = v.y;
float z = v.z;

Vector3 v1 = new(0f, z, -y);
Vector3 v2 = new(-z, 0f, x);
Vector3 v3 = new(-y, x, 0f);
Vector3 largest = v1;

if (v2.sqrMagnitude > largest.sqrMagnitude)
{
largest = v2;
}

if (v3.sqrMagnitude > largest.sqrMagnitude)
{
largest = v3;
}

return largest;
}
/// <summary>
/// Returns the angle between two quaternions around a specified axis. Range is 0 to 360.
/// </summary>
/// <param name="qA"></param>
/// <param name="qB"></param>
/// <param name="axis"></param>
/// <returns></returns>
public static float GetAngleDifference(Quaternion qA, Quaternion qB, Vector3 axis)
{
Vector3 directionA = qA * axis.OrthogonalVector();
Vector3 directionB = qB * axis.OrthogonalVector();

Vector3 projectedA = Vector3.ProjectOnPlane(directionA, axis);
Vector3 projectedB = Vector3.ProjectOnPlane(directionB, axis);

float angle = Vector3.Angle(projectedA, projectedB);

Vector3 crossProduct = Vector3.Cross(projectedA, projectedB);
// convert -180 to 180 to 0 to 360
if (Vector3.Dot(crossProduct, axis) < 0)
{
angle = 360 - angle;
}

return angle;
}
public static Vector3 FindNearestPointOnLine(Vector3 origin, Vector3 end, Vector3 point)
{
//Get heading
Vector3 heading = (end - origin);
float magnitudeMax = heading.magnitude;
heading.Normalize();

//Do projection from the point but clamp it
Vector3 lhs = point - origin;
float dotP = Vector3.Dot(lhs, heading);
dotP = Mathf.Clamp(dotP, 0f, magnitudeMax);
return origin + heading * dotP;
}

public static List<Collider> GetColliders(this GameObject go, bool includedTriggers = false)
{
return go.GetComponentsInChildren<Collider>().Where(e => !e.isTrigger || includedTriggers).ToList();
}

public static IEnumerable<Collider> GetColliders(this Rigidbody rigidbody, bool includeTriggers = false)
{
return GetColliders(rigidbody, rigidbody.transform, includeTriggers);
}
private static IEnumerable<Collider> GetColliders(this Rigidbody rigidbody, Transform transform, bool includeTriggers = false)
{
Rigidbody rb = transform.GetComponent<Rigidbody>();
if (rb && rb != rigidbody)
yield break;

foreach (Collider c in transform.GetComponents<Collider>())
{
if (!c.enabled) continue;
if (!c.isTrigger || (c.isTrigger && includeTriggers))
yield return c;
}

foreach (Transform child in transform)
{
foreach (Collider c in GetColliders(rigidbody, child))
{
yield return c;
}
}
}

public static Bounds GetColliderBounds(this Rigidbody rb)
{
var bounds = new Bounds();
var first = true;
foreach (var collider in rb.GetColliders())
{
if (first)
{
first = false;
bounds = collider.bounds;
}
else
{
bounds.Encapsulate(collider.bounds);
}
}

return bounds;
}

public static Bounds GetColliderBounds(this Transform transform)
{
Collider[] colliders = transform.GetComponents<Collider>();
return colliders.GetColliderBounds();
}

public static Bounds GetColliderBounds(this GameObject go)
{
Collider[] colliders = go.GetComponents<Collider>();
return colliders.GetColliderBounds();
}

public static Bounds GetColliderBounds(this List<Collider> colliders)
{
Bounds bounds = new Bounds();
for (int i = 0; i < colliders.Count; i++)
{
Collider collider = colliders[i];
if (i == 0)
{
bounds = collider.bounds;
}
else
{
bounds.Encapsulate(collider.bounds);
}
}

return bounds;
}

public static Bounds GetColliderBounds(this Collider[] colliders)
{
Bounds bounds = new Bounds();
bool first = true;
for (int i = 0; i < colliders.Length; i++)
{
Collider collider = colliders[i];

if (!collider.enabled) continue;

if (first)
{
first = false;
bounds = collider.bounds;
}
else
{
bounds.Encapsulate(collider.bounds);
}
}

return bounds;
}

public static void DrawBounds(this Bounds bounds)
{
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;

var v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
var v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
var v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
var v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
var v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
var v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
var v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
var v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner


var color = Color.magenta;
Debug.DrawLine(v3FrontTopLeft, v3FrontTopRight, color);
Debug.DrawLine(v3FrontTopRight, v3FrontBottomRight, color);
Debug.DrawLine(v3FrontBottomRight, v3FrontBottomLeft, color);
Debug.DrawLine(v3FrontBottomLeft, v3FrontTopLeft, color);

Debug.DrawLine(v3BackTopLeft, v3BackTopRight, color);
Debug.DrawLine(v3BackTopRight, v3BackBottomRight, color);
Debug.DrawLine(v3BackBottomRight, v3BackBottomLeft, color);
Debug.DrawLine(v3BackBottomLeft, v3BackTopLeft, color);

Debug.DrawLine(v3FrontTopLeft, v3BackTopLeft, color);
Debug.DrawLine(v3FrontTopRight, v3BackTopRight, color);
Debug.DrawLine(v3FrontBottomRight, v3BackBottomRight, color);
Debug.DrawLine(v3FrontBottomLeft, v3BackBottomLeft, color);
}
}

public enum Axis
{
X,
Y,
Z,
NegX,
NegY,
NegZ
}
2 changes: 2 additions & 0 deletions Assets/Packages/Utilis/Runtime/Utilis.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions Assets/Packages/Utilis/Runtime/com.hankun.utilis.asmdef
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
{
"name": "com.hankun.utilis"
}
"name": "com.hankun.utilis",
"rootNamespace": "",
"references": [
"GUID:e8b70daea7d4844a1b9bf7448c1d0af9"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
4 changes: 3 additions & 1 deletion Assets/Packages/Utilis/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "com.hankun.utilis",
"version": "1.0.3",
"version": "1.0.4",
"displayName": "Utilis",
"description": "A collection of useful utilities for Unity development.",
"unity": "2020.3",
"author": {
"name": "hankun",
"email": "kmcyd1@gmail.com"
},
"dependencies": {
}
}
13 changes: 7 additions & 6 deletions Assets/Settings/UniversalRenderPipelineGlobalSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ MonoBehaviour:
m_GeometryUnshadowShader: {fileID: 4800000, guid: 77774d9009bb81447b048c907d4c6273, type: 3}
m_FallOffLookup: {fileID: 2800000, guid: 5688ab254e4c0634f8d6c8e0792331ca, type: 3}
m_CopyDepthPS: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
m_DefaultCustomMaterial: {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2}
m_DefaultLitMaterial: {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2}
m_DefaultUnlitMaterial: {fileID: 2100000, guid: 9dfc825aed78fcd4ba02077103263b40, type: 2}
m_DefaultMaskMaterial: {fileID: 2100000, guid: 15d0c3709176029428a0da2f8cecf0b5, type: 2}
Expand Down Expand Up @@ -194,12 +195,12 @@ MonoBehaviour:
dilationShader: {fileID: 7200000, guid: 6bb382f7de370af41b775f54182e491d, type: 3}
subdivideSceneCS: {fileID: 7200000, guid: bb86f1f0af829fd45b2ebddda1245c22, type: 3}
voxelizeSceneShader: {fileID: 4800000, guid: c8b6a681c7b4e2e4785ffab093907f9e, type: 3}
traceVirtualOffsetCS: {fileID: -6772857160820960102, guid: ff2cbab5da58bf04d82c5f34037ed123, type: 3}
traceVirtualOffsetRT: {fileID: -5126288278712620388, guid: ff2cbab5da58bf04d82c5f34037ed123, type: 3}
skyOcclusionCS: {fileID: -6772857160820960102, guid: 5a2a534753fbdb44e96c3c78b5a6999d, type: 3}
skyOcclusionRT: {fileID: -5126288278712620388, guid: 5a2a534753fbdb44e96c3c78b5a6999d, type: 3}
renderingLayerCS: {fileID: -6772857160820960102, guid: 94a070d33e408384bafc1dea4a565df9, type: 3}
renderingLayerRT: {fileID: -5126288278712620388, guid: 94a070d33e408384bafc1dea4a565df9, type: 3}
traceVirtualOffsetCS: {fileID: 7200000, guid: 805f10d263aac4b4098c0279cd37a4f3, type: 3}
traceVirtualOffsetRT: {fileID: 4807578003741378534, guid: b60511f4aa1443f4d8c0b18f4fec92f4, type: 3}
skyOcclusionCS: {fileID: 7200000, guid: 798f52ec82fa04048a12826bbbbcf7b4, type: 3}
skyOcclusionRT: {fileID: 4807578003741378534, guid: dfaf42b38dd001f49a72d8102b709f29, type: 3}
renderingLayerCS: {fileID: 7200000, guid: a63c9cf933e3d8f41ae680a372784ebf, type: 3}
renderingLayerRT: {fileID: 4807578003741378534, guid: c2be09c936362eb49a58f08aeb30627a, type: 3}
- rid: 6852985685364965389
type: {class: ProbeVolumeGlobalSettings, ns: UnityEngine.Rendering, asm: Unity.RenderPipelines.Core.Runtime}
data:
Expand Down
10 changes: 5 additions & 5 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.8.18",
"version": "1.8.16",
"depth": 2,
"source": "registry",
"dependencies": {
Expand All @@ -27,12 +27,12 @@
"url": "https://packages.unity.com"
},
"com.unity.collections": {
"version": "2.5.1",
"version": "2.4.1",
"depth": 2,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.8.17",
"com.unity.test-framework": "1.4.5",
"com.unity.burst": "1.8.13",
"com.unity.test-framework": "1.4.3",
"com.unity.nuget.mono-cecil": "1.11.4",
"com.unity.test-framework.performance": "3.0.3"
},
Expand Down Expand Up @@ -102,7 +102,7 @@
"com.unity.burst": "1.8.14",
"com.unity.mathematics": "1.3.2",
"com.unity.ugui": "2.0.0",
"com.unity.collections": "2.4.3",
"com.unity.collections": "2.4.1",
"com.unity.modules.physics": "1.0.0",
"com.unity.modules.terrain": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 6000.0.25f1
m_EditorVersionWithRevision: 6000.0.25f1 (4859ab7b5a49)
m_EditorVersion: 6000.0.9f1
m_EditorVersionWithRevision: 6000.0.9f1 (1490908003ac)

0 comments on commit bc4683b

Please sign in to comment.