forked from keenanwoodall/Deform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquashAndStretchDeformer.cs
130 lines (113 loc) · 3.19 KB
/
SquashAndStretchDeformer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using UnityEngine;
using Unity.Jobs;
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace Deform
{
[Deformer (Name = "Squash and Stretch", Description = "Squashes and stretches a mesh", XRotation = -90f, Type = typeof (SquashAndStretchDeformer))]
public class SquashAndStretchDeformer : Deformer, IFactor
{
public float Factor
{
get => factor;
set => factor = value;
}
public float Curvature
{
get => curvature;
set => curvature = Mathf.Clamp01 (value);
}
public float Top
{
get => top;
set => top = Mathf.Max (value, Bottom);
}
public float Bottom
{
get => bottom;
set => bottom = Mathf.Min (value, Top);
}
public Transform Axis
{
get
{
if (axis == null)
axis = transform;
return axis;
}
set => axis = value;
}
[SerializeField, HideInInspector] private float factor = 0f;
[SerializeField, HideInInspector] private float curvature = 1f;
[SerializeField, HideInInspector] private float top = 0.5f;
[SerializeField, HideInInspector] private float bottom = -0.5f;
[SerializeField, HideInInspector] private Transform axis;
public override DataFlags DataFlags => DataFlags.Vertices;
public override JobHandle Process (MeshData data, JobHandle dependency = default)
{
if (Factor == 0f || Top == Bottom)
return dependency;
var meshToAxis = DeformerUtils.GetMeshToAxisSpace (Axis, data.Target.GetTransform ());
return new LimitedBulgeJob
{
factor = Factor,
curvature = (Curvature >= 0f) ? Curvature + 1f : 1f / (-Curvature + 1f),
top = Top,
bottom = Bottom,
meshToAxis = meshToAxis,
axisToMesh = meshToAxis.inverse,
vertices = data.DynamicNative.VertexBuffer
}.Schedule (data.Length, DEFAULT_BATCH_COUNT, dependency);
}
[BurstCompile (CompileSynchronously = COMPILE_SYNCHRONOUSLY)]
private struct LimitedBulgeJob : IJobParallelFor
{
public float factor;
public float curvature;
public float top;
public float bottom;
public float4x4 meshToAxis;
public float4x4 axisToMesh;
public NativeArray<float3> vertices;
public void Execute (int index)
{
var range = abs (top - bottom);
var inverseRange = 1f / range;
var point = mul (meshToAxis, float4 (vertices[index], 1f));
var nDist = 0f;
if (point.z > top)
nDist = range * inverseRange;
else if (point.z < bottom)
nDist = (bottom - bottom) * inverseRange;
else
nDist = (point.z - bottom) * inverseRange;
var squashAmount = 0f;
var stretchAmount = 0f;
if (factor > 0f)
{
squashAmount = 1f / (curvature * factor + 1f);
stretchAmount = factor + 1f;
}
else
{
squashAmount = (curvature * -factor + 1f);
stretchAmount = -1f / (factor - 1f);
}
var f = 4f * (1f - squashAmount);
squashAmount = (((f * nDist) - f) * nDist) + 1f;
point.xy *= squashAmount;
if (point.z < bottom)
point.z += (stretchAmount - 1f) * bottom;
else if (point.z <= top)
point.z *= stretchAmount;
else if (point.z > top)
point.z += (stretchAmount - 1f) * top;
else
point.z *= stretchAmount;
vertices[index] = mul (axisToMesh, point).xyz;
}
}
}
}