forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeSphereColliderRadiusBehaviour.cs
More file actions
90 lines (82 loc) · 3.21 KB
/
Copy pathChangeSphereColliderRadiusBehaviour.cs
File metadata and controls
90 lines (82 loc) · 3.21 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
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Transforms;
using UnityEngine;
using SphereCollider = Unity.Physics.SphereCollider;
public struct ChangeSphereColliderRadius : IComponentData
{
public float Min;
public float Max;
public float Target;
}
// Converted in PhysicsSamplesConversionSystem so Physics and Graphics conversion is over
public class ChangeSphereColliderRadiusBehaviour : MonoBehaviour//, IConvertGameObjectToEntity
{
[Range(0, 10)] public float Min = 0;
[Range(0, 10)] public float Max = 10;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData(entity, new ChangeSphereColliderRadius
{
Min = Min,
Max = Max,
Target = math.lerp(Min, Max, 0.5f),
});
// Physics and graphics representations of bodies can be largely independent.
// Positions and Rotations of each representation are associated through the BuildPhysicsWorld & ExportPhysicsWorld systems.
// As scale is generally baked for runtime performance, we specifically need to add a scale component here
// and will update both the graphical and physical scales in our own demo update system.
dstManager.AddComponentData(entity, new Scale
{
Value = 1.0f,
});
}
}
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class ChangeSphereColliderRadiusSystem : JobComponentSystem
{
private struct ChangeSphereColliderRadiusJob : IJobForEach<PhysicsCollider, ChangeSphereColliderRadius, Scale>
{
public unsafe void Execute(ref PhysicsCollider collider, ref ChangeSphereColliderRadius radius, ref Scale scale)
{
// make sure we are dealing with spheres
if (collider.ColliderPtr->Type != ColliderType.Sphere) return;
//
// tweak the physical representation of the sphere
// grab the sphere pointer
SphereCollider* scPtr = (SphereCollider*)collider.ColliderPtr;
float oldRadius = scPtr->Radius;
float newRadius = math.lerp(oldRadius, radius.Target, 0.05f);
// if we have reached the target radius get a new target
if (math.abs(newRadius - radius.Target) < 0.01f)
{
radius.Target = radius.Target == radius.Min ? radius.Max : radius.Min;
}
scPtr->Radius = newRadius;
//
// now tweak the graphical representation of the sphere
float oldScale = scale.Value;
float newScale = oldScale;
if (oldRadius == 0.0f)
{
// avoid the divide by zero errors.
newScale = newRadius;
}
else
{
newScale *= newRadius / oldRadius;
}
scale = new Scale() { Value = newScale };
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
JobHandle job = new ChangeSphereColliderRadiusJob().Schedule(this, inputDeps);
return job;
}
}