forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionSystem.cs
More file actions
116 lines (101 loc) · 4.76 KB
/
Copy pathCollisionSystem.cs
File metadata and controls
116 lines (101 loc) · 4.76 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
using Unity.Burst;
using Unity.Burst.Intrinsics;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
namespace HelloCube.CrossQuery
{
public partial struct CollisionSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ExecuteCrossQuery>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var boxQuery = SystemAPI.QueryBuilder()
.WithAll<LocalTransform, DefaultColor, URPMaterialPropertyBaseColor>().Build();
#if false
// More complex solution, but it avoids creating temporary copies of the box components
new CollisionJob
{
LocalTransformTypeHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(true),
DefaultColorTypeHandle = SystemAPI.GetComponentTypeHandle<DefaultColor>(true),
BaseColorTypeHandle = SystemAPI.GetComponentTypeHandle<URPMaterialPropertyBaseColor>(),
EntityTypeHandle = SystemAPI.GetEntityTypeHandle(),
OtherChunks = boxQuery.ToArchetypeChunkArray(state.WorldUpdateAllocator)
}.ScheduleParallel(boxQuery, state.Dependency).Complete();
#else
// Simple solution, but it requires creating temporary copies of all box translations and entity IDs
var boxTransforms = boxQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);
var boxEntities = boxQuery.ToEntityArray(Allocator.Temp);
foreach (var (transform, defaultColor, color, entity) in
SystemAPI.Query<RefRO<LocalTransform>, RefRO<DefaultColor>,
RefRW<URPMaterialPropertyBaseColor>>()
.WithEntityAccess())
{
// reset color of the box to its default
color.ValueRW.Value = defaultColor.ValueRO.Value;
// change the color if this box intersects another
for (int i = 0; i < boxTransforms.Length; i++)
{
var otherEnt = boxEntities[i];
var otherTrans = boxTransforms[i];
// A box should not intersect with itself, so we check if the other entity's id matches the current entity's id.
if (entity != otherEnt && math.distancesq(transform.ValueRO.Position, otherTrans.Position) < 1)
{
color.ValueRW.Value.y = 0.5f; // set green channel
break;
}
}
}
#endif
}
}
[BurstCompile]
public struct CollisionJob : IJobChunk
{
[ReadOnly] public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
[ReadOnly] public ComponentTypeHandle<DefaultColor> DefaultColorTypeHandle;
public ComponentTypeHandle<URPMaterialPropertyBaseColor> BaseColorTypeHandle;
[ReadOnly] public EntityTypeHandle EntityTypeHandle;
[ReadOnly] public NativeArray<ArchetypeChunk> OtherChunks;
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
in v128 chunkEnabledMask)
{
var transforms = chunk.GetNativeArray(ref LocalTransformTypeHandle);
var defaultColors = chunk.GetNativeArray(ref DefaultColorTypeHandle);
var baseColors = chunk.GetNativeArray(ref BaseColorTypeHandle);
var entities = chunk.GetNativeArray(EntityTypeHandle);
for (int i = 0; i < transforms.Length; i++)
{
var transform = transforms[i];
var baseColor = baseColors[i];
var entity = entities[i];
// reset to default color
baseColor.Value = defaultColors[i].Value;
for (int j = 0; j < OtherChunks.Length; j++)
{
var otherChunk = OtherChunks[j];
var otherTranslations = otherChunk.GetNativeArray(ref LocalTransformTypeHandle);
var otherEntities = otherChunk.GetNativeArray(EntityTypeHandle);
for (int k = 0; k < otherChunk.Count; k++)
{
var otherTranslation = otherTranslations[k];
var otherEntity = otherEntities[k];
if (entity != otherEntity && math.distancesq(transform.Position, otherTranslation.Position) < 1)
{
baseColor.Value.y = 0.5f; // set green channel
break;
}
}
}
baseColors[i] = baseColor;
}
}
}
}