-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathSetStateSystem.cs
191 lines (171 loc) · 6.02 KB
/
SetStateSystem.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
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
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Unity.Profiling.LowLevel.Unsafe;
namespace HelloCube.StateChange
{
public partial struct SetStateSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Hit>();
state.RequireForUpdate<Config>();
state.RequireForUpdate<ExecuteStateChange>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var config = SystemAPI.GetSingleton<Config>();
var hit = SystemAPI.GetSingleton<Hit>();
if (!hit.HitChanged)
{
#if UNITY_EDITOR
SystemAPI.GetSingletonRW<StateChangeProfilerModule.FrameData>().ValueRW.SetStatePerf = 0;
#endif
return;
}
var radiusSq = config.Radius * config.Radius;
var ecbSystem = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
state.Dependency.Complete();
var before = ProfilerUnsafeUtility.Timestamp;
if (config.Mode == Mode.VALUE)
{
new SetValueJob
{
RadiusSq = radiusSq,
Hit = hit.Value
}.ScheduleParallel();
}
else if (config.Mode == Mode.STRUCTURAL_CHANGE)
{
new AddSpinJob
{
RadiusSq = radiusSq,
Hit = hit.Value,
ECB = ecbSystem.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter()
}.ScheduleParallel();
new RemoveSpinJob
{
RadiusSq = radiusSq,
Hit = hit.Value,
ECB = ecbSystem.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter()
}.ScheduleParallel();
}
else if (config.Mode == Mode.ENABLEABLE_COMPONENT)
{
new EnableSpinJob
{
RadiusSq = radiusSq,
Hit = hit.Value,
ECB = ecbSystem.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter()
}.ScheduleParallel();
new DisableSpinJob
{
RadiusSq = radiusSq,
Hit = hit.Value,
}.ScheduleParallel();
}
state.Dependency.Complete();
var after = ProfilerUnsafeUtility.Timestamp;
#if UNITY_EDITOR
// profiling
var conversionRatio = ProfilerUnsafeUtility.TimestampToNanosecondsConversionRatio;
var elapsed = (after - before) * conversionRatio.Numerator / conversionRatio.Denominator;
SystemAPI.GetSingletonRW<StateChangeProfilerModule.FrameData>().ValueRW.SetStatePerf = elapsed;
#endif
}
}
[BurstCompile]
partial struct SetValueJob : IJobEntity
{
public float RadiusSq;
public float3 Hit;
void Execute(ref URPMaterialPropertyBaseColor color, ref Spin spin, in LocalTransform transform)
{
if (math.distancesq(transform.Position, Hit) <= RadiusSq)
{
color.Value = (Vector4)Color.red;
spin.IsSpinning = true;
}
else
{
color.Value = (Vector4)Color.white;
spin.IsSpinning = false;
}
}
}
[WithNone(typeof(Spin))]
[BurstCompile]
partial struct AddSpinJob : IJobEntity
{
public float RadiusSq;
public float3 Hit;
public EntityCommandBuffer.ParallelWriter ECB;
void Execute(Entity entity, ref URPMaterialPropertyBaseColor color, in LocalTransform transform,
[ChunkIndexInQuery] int chunkIndex)
{
// If cube is inside the hit radius.
if (math.distancesq(transform.Position, Hit) <= RadiusSq)
{
color.Value = (Vector4)Color.red;
ECB.AddComponent<Spin>(chunkIndex, entity);
}
}
}
[WithAll(typeof(Spin))]
[BurstCompile]
partial struct RemoveSpinJob : IJobEntity
{
public float RadiusSq;
public float3 Hit;
public EntityCommandBuffer.ParallelWriter ECB;
void Execute(Entity entity, ref URPMaterialPropertyBaseColor color, in LocalTransform transform,
[ChunkIndexInQuery] int chunkIndex)
{
// If cube is NOT inside the hit radius.
if (math.distancesq(transform.Position, Hit) > RadiusSq)
{
color.Value = (Vector4)Color.white;
ECB.RemoveComponent<Spin>(chunkIndex, entity);
}
}
}
[WithNone(typeof(Spin))]
[BurstCompile]
public partial struct EnableSpinJob : IJobEntity
{
public float RadiusSq;
public float3 Hit;
public EntityCommandBuffer.ParallelWriter ECB;
void Execute(Entity entity, ref URPMaterialPropertyBaseColor color, in LocalTransform transform,
[ChunkIndexInQuery] int chunkIndex)
{
// If cube is inside the hit radius.
if (math.distancesq(transform.Position, Hit) <= RadiusSq)
{
color.Value = (Vector4)Color.red;
ECB.SetComponentEnabled<Spin>(chunkIndex, entity, true);
}
}
}
[BurstCompile]
public partial struct DisableSpinJob : IJobEntity
{
public float RadiusSq;
public float3 Hit;
void Execute(Entity entity, ref URPMaterialPropertyBaseColor color, in LocalTransform transform,
EnabledRefRW<Spin> spinnerEnabled)
{
// If cube is NOT inside the hit radius.
if (math.distancesq(transform.Position, Hit) > RadiusSq)
{
color.Value = (Vector4)Color.white;
spinnerEnabled.ValueRW = false;
}
}
}
}