forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyJointLimitsAuthoring.cs
More file actions
194 lines (182 loc) · 8.71 KB
/
Copy pathModifyJointLimitsAuthoring.cs
File metadata and controls
194 lines (182 loc) · 8.71 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Authoring;
using Unity.Physics.Extensions;
using Unity.Physics.Systems;
using UnityEngine;
using LegacyJoint = UnityEngine.Joint;
using FloatRange = Unity.Physics.Math.FloatRange;
// stores an initial value and a pair of scalar curves to apply to relevant constraints on the joint
struct ModifyJointLimits : ISharedComponentData, IEquatable<ModifyJointLimits>
{
public PhysicsJoint InitialValue;
public ParticleSystem.MinMaxCurve AngularRangeScalar;
public ParticleSystem.MinMaxCurve LinearRangeScalar;
public bool Equals(ModifyJointLimits other) =>
AngularRangeScalar.Equals(other.AngularRangeScalar) && LinearRangeScalar.Equals(other.LinearRangeScalar);
public override bool Equals(object obj) => obj is ModifyJointLimits other && Equals(other);
public override int GetHashCode() =>
unchecked((AngularRangeScalar.GetHashCode() * 397) ^ LinearRangeScalar.GetHashCode());
}
// an authoring component to add to a GameObject with one or more Joint
public class ModifyJointLimitsAuthoring : MonoBehaviour
{
public ParticleSystem.MinMaxCurve AngularRangeScalar = new ParticleSystem.MinMaxCurve(
1f,
min: new AnimationCurve(
new Keyframe(0f, 0f, 0f, 0f),
new Keyframe(2f, -2f, 0f, 0f),
new Keyframe(4f, 0f, 0f, 0f)
)
{
preWrapMode = WrapMode.Loop,
postWrapMode = WrapMode.Loop
},
max: new AnimationCurve(
new Keyframe(0f, 1f, 0f, 0f),
new Keyframe(2f, -1f, 0f, 0f),
new Keyframe(4f, 1f, 0f, 0f)
)
{
preWrapMode = WrapMode.Loop,
postWrapMode = WrapMode.Loop
}
);
public ParticleSystem.MinMaxCurve LinearRangeScalar = new ParticleSystem.MinMaxCurve(
1f,
min: new AnimationCurve(
new Keyframe(0f, 1f, 0f, 0f),
new Keyframe(2f, 0.5f, 0f, 0f),
new Keyframe(4f, 1f, 0f, 0f)
)
{
preWrapMode = WrapMode.Loop,
postWrapMode = WrapMode.Loop
},
max: new AnimationCurve(
new Keyframe(0f, 0.5f, 0f, 0f),
new Keyframe(2f, 0f, 0f, 0f),
new Keyframe(4f, 0.5f, 0f, 0f)
)
{
preWrapMode = WrapMode.Loop,
postWrapMode = WrapMode.Loop
}
);
}
// after joints have been converted, find the entities they produced and add ModifyJointLimits to them
[UpdateAfter(typeof(EndJointConversionSystem))]
class ModifyJointLimitsConversionSystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
Entities.ForEach((ModifyJointLimitsAuthoring modifyJointLimits) =>
{
foreach (var jointAuthoringComponent in modifyJointLimits.GetComponents<Component>())
{
// apply modification to joints produced by legacy Joint and BaseJoint
if (jointAuthoringComponent as LegacyJoint == null && jointAuthoringComponent as BaseJoint == null)
continue;
var jointEntities = new NativeList<Entity>(16, Allocator.Temp);
World.GetOrCreateSystem<EndJointConversionSystem>().GetJointEntities(jointAuthoringComponent, jointEntities);
foreach (var jointEntity in jointEntities)
{
var angularModification = new ParticleSystem.MinMaxCurve(
multiplier: math.radians(modifyJointLimits.AngularRangeScalar.curveMultiplier),
min: modifyJointLimits.AngularRangeScalar.curveMin,
max: modifyJointLimits.AngularRangeScalar.curveMax
);
DstEntityManager.AddSharedComponentData(jointEntity, new ModifyJointLimits
{
InitialValue = DstEntityManager.GetComponentData<PhysicsJoint>(jointEntity),
AngularRangeScalar = angularModification,
LinearRangeScalar = modifyJointLimits.LinearRangeScalar
});
}
}
});
}
}
// apply an animated effect to the limits on supported types of joints
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(EndFramePhysicsSystem))]
class ModifyJointLimitsSystem : SystemBase
{
EndFramePhysicsSystem m_EndFramePhysicsSystem;
protected override void OnCreate() => m_EndFramePhysicsSystem = World.GetOrCreateSystem<EndFramePhysicsSystem>();
protected override void OnUpdate()
{
var time = (float)Time.ElapsedTime;
Dependency = JobHandle.CombineDependencies(Dependency, m_EndFramePhysicsSystem.GetOutputDependency());
Entities
.WithName("ModifyJointLimitsJob")
.WithoutBurst()
.ForEach((ref PhysicsJoint joint, in ModifyJointLimits modification) =>
{
var animatedAngularScalar = new FloatRange(
modification.AngularRangeScalar.curveMin.Evaluate(time),
modification.AngularRangeScalar.curveMax.Evaluate(time)
);
var animatedLinearScalar = new FloatRange(
modification.LinearRangeScalar.curveMin.Evaluate(time),
modification.LinearRangeScalar.curveMax.Evaluate(time)
);
// in each case, get relevant properties from the initial value based on joint type, and apply scalar
switch (joint.JointType)
{
// Custom type could be anything, so this demo just applies changes to all constraints
case JointType.Custom:
var constraints = modification.InitialValue.GetConstraints();
for (var i = 0; i < constraints.Length; i++)
{
var constraint = constraints[i];
var isAngular = constraint.Type == ConstraintType.Angular;
var scalar = math.select(animatedLinearScalar, animatedAngularScalar, isAngular);
var constraintRange = (FloatRange)(new float2(constraint.Min, constraint.Max) * scalar);
constraint.Min = constraintRange.Min;
constraint.Max = constraintRange.Max;
constraints[i] = constraint;
}
joint.SetConstraints(constraints);
break;
// other types have corresponding getters/setters to retrieve more meaningful data
case JointType.LimitedDistance:
var distanceRange = modification.InitialValue.GetLimitedDistanceRange();
joint.SetLimitedDistanceRange(distanceRange * (float2)animatedLinearScalar);
break;
case JointType.LimitedHinge:
var angularRange = modification.InitialValue.GetLimitedHingeRange();
joint.SetLimitedHingeRange(angularRange * (float2)animatedAngularScalar);
break;
case JointType.Prismatic:
var distanceOnAxis = modification.InitialValue.GetPrismaticRange();
joint.SetPrismaticRange(distanceOnAxis * (float2)animatedLinearScalar);
break;
// ragdoll joints are composed of two separate joints with different meanings
case JointType.RagdollPrimaryCone:
modification.InitialValue.GetRagdollPrimaryConeAndTwistRange(
out var maxConeAngle,
out var angularTwistRange
);
joint.SetRagdollPrimaryConeAndTwistRange(
maxConeAngle * animatedAngularScalar.Max,
angularTwistRange * (float2)animatedAngularScalar
);
break;
case JointType.RagdollPerpendicularCone:
var angularPlaneRange = modification.InitialValue.GetRagdollPerpendicularConeRange();
joint.SetRagdollPerpendicularConeRange(angularPlaneRange * (float2)animatedAngularScalar);
break;
// remaining types have no limits on their Constraint atoms to meaningfully modify
case JointType.BallAndSocket:
case JointType.Fixed:
case JointType.Hinge:
break;
}
}).Run();
}
}