Reduce Spline Importer to a minimal amount of features.

This commit is contained in:
Alexander Filippov 2024-01-15 23:36:26 +01:00
parent b8f5dd18cf
commit b04afbf039
22 changed files with 7 additions and 1568 deletions

View File

@ -136,7 +136,7 @@ namespace FrameJosh.SplineImporter
{
public TextAsset splineData;
public float scale = 1;
public float3 scale = new (1f, 1f, 1f);
public static Vector3 PositionToVector(Position position)
{

View File

@ -1,151 +0,0 @@
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Splines;
namespace FrameJosh.SplineImporter
{
public class SplinePlus : MonoBehaviour
{
public SplineContainer splineContainer;
public SplineContainer deformContainer;
public float resolution = 1;
public void Evaluate(int splineIndex, float anchor, float distance, out Vector3 position, out Quaternion rotation)
{
EvaluateSpline(splineContainer.Splines[splineIndex], deformContainer ? deformContainer.Spline : null, anchor, distance, resolution, out float3 position1, out float3 tangent, out float3 upVector);
position = splineContainer.transform.TransformPoint(position1);
rotation = splineContainer.transform.rotation
* Quaternion.LookRotation(tangent, upVector);
}
public void GetNearestPoint(int splineIndex, Vector3 point, out Vector3 position, out Quaternion rotation, out float t)
{
if (deformContainer)
{
SplineUtility.GetNearestPoint(deformContainer.Spline, point, out _, out float t1);
deformContainer.Spline.Evaluate(t1, out float3 nearest, out float3 tangent, out float3 upVector);
float3 difference = (float3)point - nearest;
float3x3 matrix = new()
{
c0 = math.normalize(math.cross(upVector, tangent)),
c1 = math.normalize(upVector),
c2 = math.normalize(tangent),
};
float3 offset = new(math.dot(difference, matrix.c2),
math.dot(difference, matrix.c1),
-math.dot(difference, matrix.c0));
float distance = math.clamp(t1, 0, 1) * deformContainer.Spline.GetLength();
point = new float3(distance, 0, 0) + offset;
SplineUtility.GetNearestPoint(splineContainer.Splines[splineIndex], point, out _, out t);
DeformSpline(splineContainer.Spline, deformContainer.Spline, t, resolution, out float3 position1, out float3 tangent1, out float3 upVector1);
position = position1;
rotation = Quaternion.LookRotation(tangent1, upVector1);
}
else
{
SplineUtility.GetNearestPoint(splineContainer.Splines[splineIndex], point, out float3 position1, out t);
position = position1;
SplineUtility.Evaluate(splineContainer.Splines[splineIndex], t, out _, out float3 tangent, out float3 upVector);
rotation = Quaternion.LookRotation(tangent, upVector);
}
t = math.clamp(t, 0, 1);
}
static void EvaluateSpline(ISpline spline, ISpline deform, float anchor, float distance, float resolution, out float3 position, out float3 tangent, out float3 upVector)
{
float t = anchor + (distance / spline.GetLength());
if (deform != null)
DeformSpline(spline, deform, t, resolution, out position, out tangent, out upVector);
else
spline.Evaluate(t, out position, out tangent, out upVector);
}
static void DeformSpline(ISpline spline, ISpline deform, float t, float resolution, out float3 position, out float3 tangent, out float3 upVector)
{
float resolutionScale = math.ceil(spline.GetLength() * resolution);
spline.Evaluate(t, out float3 position1, out _, out _);
position = EvaluatePoint(deform, position1);
float t1 = math.clamp(t, 0, 1 - (1 / (float)resolutionScale));
spline.Evaluate(t1, out float3 position2, out _, out _);
float3 point0 = EvaluatePoint(deform, position2);
spline.Evaluate(t1 + (1 / resolutionScale), out float3 position3, out _, out _);
float3 point1 = EvaluatePoint(deform, position3);
tangent = point1 - point0;
upVector = math.up();
}
static float3 EvaluatePoint(ISpline deform, float3 point)
{
deform.Evaluate(point.x / deform.GetLength(), out float3 deformPosition, out float3 deformTangent, out float3 deformUpVector);
float3 right = math.normalize(math.cross(deformTangent, deformUpVector));
float3 up = math.normalize(deformUpVector);
float3 forward = math.normalize(deformTangent);
return deformPosition
+ (forward * (math.max(point.x - deform.GetLength(), 0) + math.min(point.x, 0)))
+ (right * point.z)
+ (up * point.y);
}
void OnDrawGizmosSelected()
{
if (!splineContainer || !deformContainer) return;
Gizmos.color = Color.green;
for (int i = 0; i < splineContainer.Splines.Count; i++)
{
Evaluate(0, 0, 0, out Vector3 position, out _);
float3 oldPosition = position;
int gizmoResolution = (int)math.ceil(splineContainer.Splines[i].GetLength());
for (float j = 1; j <= gizmoResolution; j++)
{
Evaluate(0, j / gizmoResolution, 0, out position, out _);
Gizmos.DrawLine(oldPosition, position);
oldPosition = position;
}
}
}
void Reset()
{
splineContainer = GetComponentInChildren<SplineContainer>();
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 41eeb6f8f41efdb4eb29d6db19d12b6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: dad66e5ce81d3da46a6f24be534d3c90
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,21 +0,0 @@
In the Spline Debug scene, there are 6 important objects:
1. Spline
- A curved spline going from (0, 0, 0) to (10, 0, -5)
2. Deform
- A curved spline used to deform Spline
3. Spline Plus
- An object with the SplinePlus component
- This object is used to deform spline Spline along spline Deform
- The resulting spline is rendered in green with Gizmos enabled
4. Evaluate
- Renders a cube gizmo along each of the above splines at a given distance from a given anchor point
5. Nearest Point
- Renders a cube gizmo at the nearest point along the deformed spline from the Spline Plus object
6. Spline Debug
- An instantiated Blender file including a tube warped around the deformed spline using Blender's Curve modifier

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 4f23fad103e2d8548be04a5c255ce66b
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -1,109 +0,0 @@
fileFormatVersion: 2
guid: b93c3e7374b43344ca6fa176d935f2bd
ModelImporter:
serializedVersion: 22200
internalIDToNameTable: []
externalObjects: {}
materials:
materialImportMode: 2
materialName: 0
materialSearch: 1
materialLocation: 1
animations:
legacyGenerateAnimations: 4
bakeSimulation: 0
resampleCurves: 1
optimizeGameObjects: 0
removeConstantScaleCurves: 0
motionNodeName:
rigImportErrors:
rigImportWarnings:
animationImportErrors:
animationImportWarnings:
animationRetargetingWarnings:
animationDoRetargetingWarnings: 0
importAnimatedCustomProperties: 0
importConstraints: 0
animationCompression: 1
animationRotationError: 0.5
animationPositionError: 0.5
animationScaleError: 0.5
animationWrapMode: 0
extraExposedTransformPaths: []
extraUserProperties: []
clipAnimations: []
isReadable: 0
meshes:
lODScreenPercentages: []
globalScale: 1
meshCompression: 0
addColliders: 0
useSRGBMaterialColor: 1
sortHierarchyByName: 1
importPhysicalCameras: 1
importVisibility: 1
importBlendShapes: 1
importCameras: 1
importLights: 1
nodeNameCollisionStrategy: 1
fileIdsGeneration: 2
swapUVChannels: 0
generateSecondaryUV: 0
useFileUnits: 1
keepQuads: 0
weldVertices: 1
bakeAxisConversion: 1
preserveHierarchy: 0
skinWeightsMode: 0
maxBonesPerVertex: 4
minBoneWeight: 0.001
optimizeBones: 1
meshOptimizationFlags: -1
indexFormat: 0
secondaryUVAngleDistortion: 8
secondaryUVAreaDistortion: 15.000001
secondaryUVHardAngle: 88
secondaryUVMarginMethod: 1
secondaryUVMinLightmapResolution: 40
secondaryUVMinObjectScale: 1
secondaryUVPackMargin: 4
useFileScale: 1
strictVertexDataChecks: 0
tangentSpace:
normalSmoothAngle: 60
normalImportMode: 0
tangentImportMode: 3
normalCalculationMode: 4
legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 0
blendShapeNormalImportMode: 1
normalSmoothingSource: 0
referencedClips: []
importAnimation: 1
humanDescription:
serializedVersion: 3
human: []
skeleton: []
armTwist: 0.5
foreArmTwist: 0.5
upperLegTwist: 0.5
legTwist: 0.5
armStretch: 0.05
legStretch: 0.05
feetSpacing: 0
globalScale: 1
rootMotionBoneName:
hasTranslationDoF: 0
hasExtraRoot: 0
skeletonHasParents: 1
lastHumanDescriptionAvatarSource: {instanceID: 0}
autoGenerateAvatarMappingIfUnspecified: 1
animationType: 2
humanoidOversampling: 1
avatarSetup: 0
addHumanoidExtraRootOnlyWhenUsingAvatar: 1
importBlendShapeDeformPercent: 1
remapMaterialsIfMaterialImportModeIsNone: 0
additionalBone: 0
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: d3d151e577ee0984bbf49dac54834606
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 44ea138cdbdb30745b195f3459bdf8c9
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,51 +0,0 @@
using Unity.Mathematics;
using UnityEngine;
namespace FrameJosh.SplineImporter.Samples
{
public class SplineEvaluateDebug : MonoBehaviour
{
[SerializeField] SplinePlus splinePlus;
[SerializeField] float anchor;
[SerializeField] float distance;
[SerializeField] float cubeSize;
[SerializeField] float matrixSize;
void OnDrawGizmos()
{
if (!splinePlus) return;
splinePlus.Evaluate(0, anchor, distance, out Vector3 position, out Quaternion rotation);
transform.position = position;
transform.rotation = rotation;
Gizmos.DrawCube(position, Vector3.one * cubeSize);
splinePlus.splineContainer.Evaluate(anchor + (distance / splinePlus.splineContainer.Spline.GetLength()), out float3 position1, out _, out _);
Gizmos.DrawCube(position1, Vector3.one * cubeSize);
splinePlus.deformContainer.Evaluate(position1.x / splinePlus.deformContainer.Spline.GetLength(), out float3 deformPosition, out _, out _);
Gizmos.DrawCube(deformPosition, Vector3.one * cubeSize);
Gizmos.color = Color.green;
Gizmos.DrawRay(position, transform.up * matrixSize);
Gizmos.color = Color.red;
Gizmos.DrawRay(position, transform.right * matrixSize);
Gizmos.color = Color.blue;
Gizmos.DrawRay(position, transform.forward * matrixSize);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b96504a617d74094faa656840a73e7a9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,34 +0,0 @@
using UnityEngine;
namespace FrameJosh.SplineImporter.Samples
{
public class SplineNearestPointDebug : MonoBehaviour
{
[SerializeField] SplinePlus splinePlus;
[SerializeField] float cubeSize;
[SerializeField] float matrixSize;
void OnDrawGizmos()
{
if (!splinePlus) return;
splinePlus.GetNearestPoint(0, transform.position, out Vector3 position, out Quaternion rotation, out _);
Gizmos.DrawCube(position, Vector3.one * cubeSize);
Gizmos.color = Color.green;
Gizmos.DrawRay(position, (Quaternion)rotation * Vector3.up * matrixSize);
Gizmos.color = Color.red;
Gizmos.DrawRay(position, (Quaternion)rotation * Vector3.right * matrixSize);
Gizmos.color = Color.blue;
Gizmos.DrawRay(position, (Quaternion)rotation * Vector3.forward * matrixSize);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 0760258fbb4f0b5418d9a88014403d0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8b9cd3cbddbe4e44abf819fdd6da81ee
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,45 +0,0 @@
{
"splines": [
{
"controlPoints": [
{
"position": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"handleL": {
"x": -9.999999046325684,
"y": -1.5099578831723193e-06,
"z": 0.0
},
"handleR": {
"x": 9.999999046325684,
"y": 1.5099578831723193e-06,
"z": 0.0
},
"tilt": 0.0
},
{
"position": {
"x": 10.000001907348633,
"y": -9.999998092651367,
"z": 0.0
},
"handleL": {
"x": 1.9073486328125e-06,
"y": -10.000001907348633,
"z": 0.0
},
"handleR": {
"x": 20.000001907348633,
"y": -9.999994277954102,
"z": 0.0
},
"tilt": 0.0
}
],
"closed": false
}
]
}

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 22decb6838ce8a34a883a7ad6c7c3335
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,45 +0,0 @@
{
"splines": [
{
"controlPoints": [
{
"position": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"handleL": {
"x": -1.0,
"y": -8.742277657347586e-08,
"z": 0.0
},
"handleR": {
"x": 1.0,
"y": 8.742277657347586e-08,
"z": 0.0
},
"tilt": -0.0
},
{
"position": {
"x": 10.0,
"y": -5.0,
"z": 0.0
},
"handleL": {
"x": 9.0,
"y": -5.0,
"z": 0.0
},
"handleR": {
"x": 11.0,
"y": -5.0,
"z": 0.0
},
"tilt": -0.0
}
],
"closed": false
}
]
}

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: f3719dd04e0fe40438bb9578e9d53804
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,12 +1,12 @@
{
"name": "com.josh.spline-importer",
"version": "1.0.0",
"name": "ru.shazbot.spline-importer",
"version": "2.0.0",
"displayName": "Spline Importer",
"description": "Import and export splines between Blender and Unity",
"unity": "2022.1",
"description": "Import and export splines between Blender and Unity.",
"unity": "2021.3",
"dependencies": {
"com.unity.mathematics": "1.0.0",
"com.unity.splines": "1.0.0"
"com.unity.mathematics": "1.2.6",
"com.unity.splines": "2.5.1"
},
"author": {
"name": "Josh",