Converted SplinePlus to Mathematics Library
This commit is contained in:
+46
-56
@@ -12,7 +12,7 @@ namespace FrameJosh.SplineImporter
|
|||||||
|
|
||||||
public int resolution = 1;
|
public int resolution = 1;
|
||||||
|
|
||||||
public void Evaluate(int splineIndex, float anchor, float distance, out Vector3 position, out Quaternion rotation)
|
public void Evaluate(int splineIndex, float anchor, float distance, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
float t = anchor + (distance / splineContainer.Spline.GetLength());
|
float t = anchor + (distance / splineContainer.Spline.GetLength());
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ namespace FrameJosh.SplineImporter
|
|||||||
EvaluateSpline(splineIndex, t, out position, out rotation);
|
EvaluateSpline(splineIndex, t, out position, out rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Evaluate(float anchor, float distance, out Vector3 position, out Quaternion rotation)
|
public void Evaluate(float anchor, float distance, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
float t = anchor + (distance / splineContainer.CalculateLength());
|
float t = anchor + (distance / splineContainer.CalculateLength());
|
||||||
|
|
||||||
@@ -32,15 +32,15 @@ namespace FrameJosh.SplineImporter
|
|||||||
EvaluateSpline(t, out position, out rotation);
|
EvaluateSpline(t, out position, out rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GetNearestPoint(int splineIndex, Vector3 point, out Vector3 position, out Quaternion rotation, out float t)
|
public void GetNearestPoint(int splineIndex, float3 point, out float3 position, out quaternion rotation, out float t)
|
||||||
{
|
{
|
||||||
position = Vector3.zero;
|
position = float3.zero;
|
||||||
|
|
||||||
rotation = Quaternion.identity;
|
rotation = quaternion.identity;
|
||||||
|
|
||||||
t = 0;
|
t = 0;
|
||||||
|
|
||||||
float nearestDistance = Mathf.Infinity;
|
float nearestDistance = float.PositiveInfinity;
|
||||||
|
|
||||||
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
||||||
{
|
{
|
||||||
@@ -48,9 +48,9 @@ namespace FrameJosh.SplineImporter
|
|||||||
|
|
||||||
for (float j = 0; j <= resolutionScale; j++)
|
for (float j = 0; j <= resolutionScale; j++)
|
||||||
{
|
{
|
||||||
Evaluate(i, j / resolutionScale, 0, out Vector3 thisPosition, out Quaternion thisRotation);
|
Evaluate(i, j / resolutionScale, 0, out float3 thisPosition, out quaternion thisRotation);
|
||||||
|
|
||||||
float thisDistance = Vector3.Distance(point, thisPosition);
|
float thisDistance = math.distance(point, thisPosition);
|
||||||
|
|
||||||
if (thisDistance < nearestDistance)
|
if (thisDistance < nearestDistance)
|
||||||
{
|
{
|
||||||
@@ -66,23 +66,23 @@ namespace FrameJosh.SplineImporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GetNearestPoint(Vector3 point, out Vector3 position, out Quaternion rotation)
|
public void GetNearestPoint(float3 point, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
position = Vector3.zero;
|
position = float3.zero;
|
||||||
|
|
||||||
rotation = Quaternion.identity;
|
rotation = quaternion.identity;
|
||||||
|
|
||||||
float nearestDistance = Mathf.Infinity;
|
float nearestDistance = float.PositiveInfinity;
|
||||||
|
|
||||||
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
||||||
{
|
{
|
||||||
int resolutionScale = Mathf.CeilToInt(splineContainer.Splines[i].GetLength()) * resolution;
|
int resolutionScale = (int)math.ceil(splineContainer.Splines[i].GetLength()) * resolution;
|
||||||
|
|
||||||
for (float j = 0; j <= resolutionScale; j++)
|
for (float j = 0; j <= resolutionScale; j++)
|
||||||
{
|
{
|
||||||
Evaluate(i, j / resolutionScale, 0, out Vector3 thisPosition, out Quaternion thisRotation);
|
Evaluate(i, j / resolutionScale, 0, out float3 thisPosition, out quaternion thisRotation);
|
||||||
|
|
||||||
float thisDistance = Vector3.Distance(point, thisPosition);
|
float thisDistance = math.distance(point, thisPosition);
|
||||||
|
|
||||||
if (thisDistance < nearestDistance)
|
if (thisDistance < nearestDistance)
|
||||||
{
|
{
|
||||||
@@ -96,92 +96,82 @@ namespace FrameJosh.SplineImporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EvaluateSpline(int splineIndex, float t, out Vector3 position, out Quaternion rotation)
|
void EvaluateSpline(int splineIndex, float t, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
ScaledEvaluate(splineContainer, splineIndex, t, out float3 position1, out float3 tangent, out float3 upVector);
|
ScaledEvaluate(splineContainer, splineIndex, t, out float3 position1, out float3 tangent, out float3 upVector);
|
||||||
|
|
||||||
position = position1;
|
position = position1;
|
||||||
|
|
||||||
rotation = Quaternion.LookRotation(tangent, upVector);
|
rotation = quaternion.LookRotation(tangent, upVector);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EvaluateSpline(float t, out Vector3 position, out Quaternion rotation)
|
void EvaluateSpline(float t, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
splineContainer.Evaluate(t, out float3 position1, out float3 tangent, out float3 upVector);
|
splineContainer.Evaluate(t, out float3 position1, out float3 tangent, out float3 upVector);
|
||||||
|
|
||||||
position = position1;
|
position = position1;
|
||||||
|
|
||||||
rotation = Quaternion.LookRotation(tangent, upVector);
|
rotation = quaternion.LookRotation(tangent, upVector);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeformSpline(int splineIndex, float t, out Vector3 position, out Quaternion rotation)
|
void DeformSpline(int splineIndex, float t, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
int resolutionScale = Mathf.CeilToInt(splineContainer.Splines[splineIndex].GetLength()) * resolution;
|
int resolutionScale = (int)math.ceil(splineContainer.Splines[splineIndex].GetLength()) * resolution;
|
||||||
|
|
||||||
position = EvaluatePoint(splineIndex, t);
|
position = EvaluatePoint(splineIndex, t);
|
||||||
|
|
||||||
float t1 = Mathf.Clamp(t, 0, 1 - (1 / (float)resolutionScale));
|
float t1 = math.clamp(t, 0, 1 - (1 / (float)resolutionScale));
|
||||||
|
|
||||||
Vector3 position0 = EvaluatePoint(splineIndex, t1);
|
float3 position0 = EvaluatePoint(splineIndex, t1);
|
||||||
|
|
||||||
Vector3 position1 = EvaluatePoint(splineIndex, t1 + (1 / (float)resolutionScale));
|
float3 position1 = EvaluatePoint(splineIndex, t1 + (1 / (float)resolutionScale));
|
||||||
|
|
||||||
Vector3 difference = position1 - position0;
|
float3 difference = position1 - position0;
|
||||||
|
|
||||||
rotation = Mathf.Abs(Vector3.Dot(difference, Vector3.up)) > 0
|
rotation = quaternion.LookRotationSafe(difference, math.up());
|
||||||
? Quaternion.LookRotation(difference, Vector3.up)
|
|
||||||
: Quaternion.FromToRotation(Vector3.forward, difference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeformSpline(float t, out Vector3 position, out Quaternion rotation)
|
void DeformSpline(float t, out float3 position, out quaternion rotation)
|
||||||
{
|
{
|
||||||
int resolutionScale = Mathf.CeilToInt(splineContainer.CalculateLength()) * resolution;
|
int resolutionScale = (int)math.ceil(splineContainer.CalculateLength()) * resolution;
|
||||||
|
|
||||||
position = EvaluatePoint(t);
|
position = EvaluatePoint(t);
|
||||||
|
|
||||||
float t1 = Mathf.Clamp(t, 0, 1 - (1 / (float)resolutionScale));
|
float t1 = math.clamp(t, 0, 1 - (1 / (float)resolutionScale));
|
||||||
|
|
||||||
Vector3 position0 = EvaluatePoint(t1);
|
float3 position0 = EvaluatePoint(t1);
|
||||||
|
|
||||||
Vector3 position1 = EvaluatePoint(t1 + (1 / (float)resolutionScale));
|
float3 position1 = EvaluatePoint(t1 + (1 / (float)resolutionScale));
|
||||||
|
|
||||||
Vector3 difference = position1 - position0;
|
float3 difference = position1 - position0;
|
||||||
|
|
||||||
rotation = Mathf.Abs(Vector3.Dot(difference, Vector3.up)) > 0
|
rotation = quaternion.LookRotationSafe(difference, math.up());
|
||||||
? Quaternion.LookRotation(difference, Vector3.up)
|
|
||||||
: Quaternion.FromToRotation(Vector3.forward, difference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 EvaluatePoint(int splineIndex, float t)
|
float3 EvaluatePoint(int splineIndex, float t)
|
||||||
{
|
{
|
||||||
ScaledEvaluate(splineContainer, splineIndex, t, out float3 position, out _, out _);
|
ScaledEvaluate(splineContainer, splineIndex, t, out float3 position, out _, out _);
|
||||||
|
|
||||||
ScaledEvaluate(deformContainer, 0, position.x / deformContainer.Spline.GetLength(), out float3 deformPosition, out float3 deformTangent, out float3 deformUpVector);
|
ScaledEvaluate(deformContainer, 0, position.x / deformContainer.Spline.GetLength(), out float3 deformPosition, out float3 deformTangent, out float3 deformUpVector);
|
||||||
|
|
||||||
float3x3 deformMatrix = new()
|
float3 right = math.normalize(math.cross(deformTangent, deformUpVector));
|
||||||
{
|
|
||||||
c0 = (float3)Vector3.Normalize(Vector3.Cross(deformTangent, deformUpVector)),
|
|
||||||
c1 = (float3)Vector3.Normalize(deformUpVector),
|
|
||||||
c2 = (float3)Vector3.Normalize(deformTangent)
|
|
||||||
};
|
|
||||||
|
|
||||||
return deformPosition + (deformMatrix.c0 * position.z) + (deformMatrix.c1 * position.y);
|
float3 up = math.normalize(deformUpVector);
|
||||||
|
|
||||||
|
return deformPosition + (right * position.z) + (up * position.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 EvaluatePoint(float t)
|
float3 EvaluatePoint(float t)
|
||||||
{
|
{
|
||||||
splineContainer.Evaluate(t, out float3 position, out _, out _);
|
splineContainer.Evaluate(t, out float3 position, out _, out _);
|
||||||
|
|
||||||
ScaledEvaluate(deformContainer, 0, position.x / deformContainer.Spline.GetLength(), out float3 deformPosition, out float3 deformTangent, out float3 deformUpVector);
|
ScaledEvaluate(deformContainer, 0, position.x / deformContainer.Spline.GetLength(), out float3 deformPosition, out float3 deformTangent, out float3 deformUpVector);
|
||||||
|
|
||||||
float3x3 deformMatrix = new()
|
float3 right = math.normalize(math.cross(deformTangent, deformUpVector));
|
||||||
{
|
|
||||||
c0 = (float3)Vector3.Normalize(Vector3.Cross(deformTangent, deformUpVector)),
|
|
||||||
c1 = (float3)Vector3.Normalize(deformUpVector),
|
|
||||||
c2 = (float3)Vector3.Normalize(deformTangent)
|
|
||||||
};
|
|
||||||
|
|
||||||
return deformPosition + (deformMatrix.c0 * position.z) + (deformMatrix.c1 * position.y);
|
float3 up = math.normalize(deformUpVector);
|
||||||
|
|
||||||
|
return deformPosition + (right * position.z) + (up * position.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScaledEvaluate(SplineContainer splineContainer, int splineIndex, float t, out float3 position, out float3 tangent, out float3 upVector)
|
void ScaledEvaluate(SplineContainer splineContainer, int splineIndex, float t, out float3 position, out float3 tangent, out float3 upVector)
|
||||||
@@ -212,11 +202,11 @@ namespace FrameJosh.SplineImporter
|
|||||||
|
|
||||||
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
for (int i = 0; i < splineContainer.Splines.Count; i++)
|
||||||
{
|
{
|
||||||
Evaluate(i, 0, 0, out Vector3 position, out _);
|
Evaluate(i, 0, 0, out float3 position, out _);
|
||||||
|
|
||||||
Vector3 oldPosition = position;
|
float3 oldPosition = position;
|
||||||
|
|
||||||
int gizmoResolution = Mathf.CeilToInt(splineContainer.Splines[i].GetLength());
|
int gizmoResolution = (int)math.ceil(splineContainer.Splines[i].GetLength());
|
||||||
|
|
||||||
for (float j = 1; j <= gizmoResolution; j++)
|
for (float j = 1; j <= gizmoResolution; j++)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -413,8 +413,8 @@ Transform:
|
|||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 304474437}
|
m_GameObject: {fileID: 304474437}
|
||||||
m_LocalRotation: {x: 0, y: 0.905946, z: 0, w: -0.42339325}
|
m_LocalRotation: {x: 0, y: 0.905946, z: 0, w: -0.42339322}
|
||||||
m_LocalPosition: {x: 2.304097, y: 0, z: -2.9640856}
|
m_LocalPosition: {x: 2.3040967, y: 0, z: -2.9640856}
|
||||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_ConstrainProportionsScale: 0
|
m_ConstrainProportionsScale: 0
|
||||||
m_Children: []
|
m_Children: []
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace FrameJosh.SplineImporter.Samples
|
|||||||
{
|
{
|
||||||
if (!splinePlus) return;
|
if (!splinePlus) return;
|
||||||
|
|
||||||
splinePlus.Evaluate(anchor, distance, out Vector3 position, out Quaternion rotation);
|
splinePlus.Evaluate(anchor, distance, out float3 position, out quaternion rotation);
|
||||||
|
|
||||||
transform.position = position;
|
transform.position = position;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Unity.Mathematics;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace FrameJosh.SplineImporter.Samples
|
namespace FrameJosh.SplineImporter.Samples
|
||||||
@@ -12,7 +13,7 @@ namespace FrameJosh.SplineImporter.Samples
|
|||||||
{
|
{
|
||||||
if (!splinePlus) return;
|
if (!splinePlus) return;
|
||||||
|
|
||||||
splinePlus.GetNearestPoint(transform.position, out Vector3 position, out _);
|
splinePlus.GetNearestPoint(transform.position, out float3 position, out _);
|
||||||
|
|
||||||
Gizmos.DrawCube(position, Vector3.one * cubeSize);
|
Gizmos.DrawCube(position, Vector3.one * cubeSize);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user