forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounceSystem.cs
More file actions
59 lines (51 loc) · 1.82 KB
/
Copy pathBounceSystem.cs
File metadata and controls
59 lines (51 loc) · 1.82 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
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Samples.Common
{
public class BounceSystem : JobComponentSystem
{
struct BounceGroup
{
public ComponentDataArray<Position> positions;
public ComponentDataArray<Bounce> bounce;
public readonly int Length;
}
[Inject] private BounceGroup m_BounceGroup;
[BurstCompile]
struct BouncePosition : IJobParallelFor
{
public ComponentDataArray<Position> positions;
public ComponentDataArray<Bounce> bounce;
public float dt;
public void Execute(int i)
{
float t = bounce[i].t + (i*0.005f);
float st = math.sin(t);
float3 prevPosition = positions[i].Value;
Bounce prevBounce = bounce[i];
positions[i] = new Position
{
Value = prevPosition + new float3( st*prevBounce.height.x, st*prevBounce.height.y, st*prevBounce.height.z )
};
bounce[i] = new Bounce
{
t = prevBounce.t + (dt * prevBounce.speed),
height = prevBounce.height,
speed = prevBounce.speed
};
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var bouncePositionJob = new BouncePosition();
bouncePositionJob.positions = m_BounceGroup.positions;
bouncePositionJob.bounce = m_BounceGroup.bounce;
bouncePositionJob.dt = Time.deltaTime;
return bouncePositionJob.Schedule(m_BounceGroup.Length, 64, inputDeps);
}
}
}