forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsteroidRenderSystem.cs
More file actions
94 lines (88 loc) · 3.55 KB
/
Copy pathAsteroidRenderSystem.cs
File metadata and controls
94 lines (88 loc) · 3.55 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
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
using Unity.Rendering;
using Unity.Burst;
namespace Asteroids.Client
{
[RequireMatchingQueriesForUpdate]
[WorldSystemFilter(WorldSystemFilterFlags.Presentation)]
[UpdateBefore(typeof(TransformSystemGroup))]
[BurstCompile]
public partial struct AsteroidRenderSystem : ISystem
{
private float m_Pulse;
private float m_PulseDelta;
private const float m_PulseMax = 1.2f;
private const float m_PulseMin = 0.8f;
ComponentLookup<PredictedGhostComponent> m_PredictedGhostComponentFromEntity;
ComponentLookup<URPMaterialPropertyBaseColor> m_URPMaterialPropertyBaseColorFromEntity;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
m_Pulse = 1;
m_PulseDelta = 1;
m_PredictedGhostComponentFromEntity = state.GetComponentLookup<PredictedGhostComponent>(true);
m_URPMaterialPropertyBaseColorFromEntity = state.GetComponentLookup<URPMaterialPropertyBaseColor>();
state.RequireForUpdate<AsteroidTagComponentData>();
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
// Should ideally not be a hard-coded value
float astrScale = 30;
m_Pulse += m_PulseDelta * SystemAPI.Time.DeltaTime;
if (m_Pulse > m_PulseMax)
{
m_Pulse = m_PulseMax;
m_PulseDelta = -m_PulseDelta;
}
else if (m_Pulse < m_PulseMin)
{
m_Pulse = m_PulseMin;
m_PulseDelta = -m_PulseDelta;
}
var pulse = m_Pulse;
m_PredictedGhostComponentFromEntity.Update(ref state);
m_URPMaterialPropertyBaseColorFromEntity.Update(ref state);
var scaleJob = new ScaleAsteroids
{
predictedFromEntity = m_PredictedGhostComponentFromEntity,
colorFromEntity = m_URPMaterialPropertyBaseColorFromEntity,
astrScale = astrScale,
pulse = pulse
};
state.Dependency = scaleJob.ScheduleParallel(state.Dependency);
}
[WithAll(typeof(AsteroidTagComponentData))]
[BurstCompile]
partial struct ScaleAsteroids : IJobEntity
{
[ReadOnly] public ComponentLookup<PredictedGhostComponent> predictedFromEntity;
[NativeDisableParallelForRestriction] public ComponentLookup<URPMaterialPropertyBaseColor> colorFromEntity;
public float astrScale;
public float pulse;
#if !ENABLE_TRANSFORM_V1
public void Execute(Entity ent, ref LocalTransform localTransform)
{
if (colorFromEntity.HasComponent(ent))
colorFromEntity[ent] = new URPMaterialPropertyBaseColor{Value = predictedFromEntity.HasComponent(ent) ? new float4(0,1,0,1) : new float4(1,1,1,1)};
localTransform.Scale = astrScale * pulse;
}
#else
public void Execute(Entity ent, ref NonUniformScale scale)
{
if (colorFromEntity.HasComponent(ent))
colorFromEntity[ent] = new URPMaterialPropertyBaseColor{Value = predictedFromEntity.HasComponent(ent) ? new float4(0,1,0,1) : new float4(1,1,1,1)};
scale.Value = new float3(astrScale * pulse);
}
#endif
}
}
}