forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalToWorld2DSystem.cs
More file actions
181 lines (163 loc) · 8.92 KB
/
Copy pathLocalToWorld2DSystem.cs
File metadata and controls
181 lines (163 loc) · 8.92 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
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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Assertions;
using Unity.Burst.Intrinsics;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;
using Unity.Transforms;
namespace HelloCube.CustomTransforms
{
// This system computes a transform matrix for each entity with a LocalTransform2D.
// For root-level / world-space entities with no Parent, the LocalToWorld can be
// computed directly from the entity's LocalTransform2D.
// For child entities, each unique hierarchy is traversed recursively, computing each child's LocalToWorld
// by composing its LocalTransform with its parent's transform.
[WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor)]
[UpdateInGroup(typeof(TransformSystemGroup))]
[UpdateAfter(typeof(ParentSystem))]
[BurstCompile]
public partial struct LocalToWorld2DSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<LocalTransform2D>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var rootsQuery = SystemAPI.QueryBuilder().WithAll<LocalTransform2D>().WithAllRW<LocalToWorld>()
.WithNone<Parent>().Build();
var parentsQuery = SystemAPI.QueryBuilder().WithAll<LocalTransform2D, Child>()
.WithAllRW<LocalToWorld>()
.WithNone<Parent>().Build();
var localToWorldWriteGroupMask = SystemAPI.QueryBuilder()
.WithAll<LocalTransform2D, Parent>()
.WithAllRW<LocalToWorld>().Build().GetEntityQueryMask();
// compute LocalToWorld for all root-level entities
var rootJob = new ComputeRootLocalToWorldJob
{
LocalTransform2DTypeHandleRO = SystemAPI.GetComponentTypeHandle<LocalTransform2D>(true),
PostTransformMatrixTypeHandleRO = SystemAPI.GetComponentTypeHandle<PostTransformMatrix>(true),
LocalToWorldTypeHandleRW = SystemAPI.GetComponentTypeHandle<LocalToWorld>(),
LastSystemVersion = state.LastSystemVersion,
};
state.Dependency = rootJob.ScheduleParallelByRef(rootsQuery, state.Dependency);
// compute LocalToWorld for all child entities
var childJob = new ComputeChildLocalToWorldJob
{
LocalToWorldWriteGroupMask = localToWorldWriteGroupMask,
ChildTypeHandle = SystemAPI.GetBufferTypeHandle<Child>(true),
ChildLookup = SystemAPI.GetBufferLookup<Child>(true),
LocalToWorldTypeHandleRW = SystemAPI.GetComponentTypeHandle<LocalToWorld>(),
LocalTransform2DLookup = SystemAPI.GetComponentLookup<LocalTransform2D>(true),
PostTransformMatrixLookup = SystemAPI.GetComponentLookup<PostTransformMatrix>(true),
LocalToWorldLookup = SystemAPI.GetComponentLookup<LocalToWorld>(),
LastSystemVersion = state.LastSystemVersion,
};
state.Dependency = childJob.ScheduleParallelByRef(parentsQuery, state.Dependency);
}
[BurstCompile]
unsafe struct ComputeRootLocalToWorldJob : IJobChunk
{
[ReadOnly] public ComponentTypeHandle<LocalTransform2D> LocalTransform2DTypeHandleRO;
[ReadOnly] public ComponentTypeHandle<PostTransformMatrix> PostTransformMatrixTypeHandleRO;
public ComponentTypeHandle<LocalToWorld> LocalToWorldTypeHandleRW;
public uint LastSystemVersion;
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
in v128 chunkEnabledMask)
{
Assert.IsFalse(useEnabledMask);
LocalTransform2D* chunk2DLocalTransforms =
(LocalTransform2D*)chunk.GetRequiredComponentDataPtrRO(ref LocalTransform2DTypeHandleRO);
if (chunk.DidChange(ref LocalTransform2DTypeHandleRO, LastSystemVersion) ||
chunk.DidChange(ref PostTransformMatrixTypeHandleRO, LastSystemVersion))
{
LocalToWorld* chunkLocalToWorlds =
(LocalToWorld*)chunk.GetRequiredComponentDataPtrRW(ref LocalToWorldTypeHandleRW);
PostTransformMatrix* chunkPostTransformMatrices =
(PostTransformMatrix*)chunk.GetComponentDataPtrRO(ref PostTransformMatrixTypeHandleRO);
if (chunkPostTransformMatrices != null)
{
for (int i = 0, chunkEntityCount = chunk.Count; i < chunkEntityCount; ++i)
{
chunkLocalToWorlds[i].Value = math.mul(chunk2DLocalTransforms[i].ToMatrix(),
chunkPostTransformMatrices[i].Value);
}
}
else
{
for (int i = 0, chunkEntityCount = chunk.Count; i < chunkEntityCount; ++i)
{
chunkLocalToWorlds[i].Value = chunk2DLocalTransforms[i].ToMatrix();
}
}
}
}
}
[BurstCompile]
unsafe struct ComputeChildLocalToWorldJob : IJobChunk
{
[NativeDisableContainerSafetyRestriction]
public ComponentLookup<LocalToWorld> LocalToWorldLookup;
[ReadOnly] public EntityQueryMask LocalToWorldWriteGroupMask;
[ReadOnly] public BufferTypeHandle<Child> ChildTypeHandle;
[ReadOnly] public BufferLookup<Child> ChildLookup;
public ComponentTypeHandle<LocalToWorld> LocalToWorldTypeHandleRW;
[ReadOnly] public ComponentLookup<LocalTransform2D> LocalTransform2DLookup;
[ReadOnly] public ComponentLookup<PostTransformMatrix> PostTransformMatrixLookup;
public uint LastSystemVersion;
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
in v128 chunkEnabledMask)
{
Assert.IsFalse(useEnabledMask);
bool updateChildrenTransform = chunk.DidChange(ref ChildTypeHandle, LastSystemVersion);
BufferAccessor<Child> chunkChildBuffers = chunk.GetBufferAccessor(ref ChildTypeHandle);
updateChildrenTransform = updateChildrenTransform ||
chunk.DidChange(ref LocalToWorldTypeHandleRW, LastSystemVersion);
LocalToWorld* chunkLocalToWorlds =
(LocalToWorld*)chunk.GetRequiredComponentDataPtrRO(ref LocalToWorldTypeHandleRW);
for (int i = 0, chunkEntityCount = chunk.Count; i < chunkEntityCount; i++)
{
var localToWorld = chunkLocalToWorlds[i].Value;
var children = chunkChildBuffers[i];
for (int j = 0, childCount = children.Length; j < childCount; j++)
{
ChildLocalToWorldFromTransformMatrix(localToWorld, children[j].Value, updateChildrenTransform);
}
}
}
void ChildLocalToWorldFromTransformMatrix(in float4x4 parentLocalToWorld, Entity childEntity,
bool updateChildrenTransform)
{
updateChildrenTransform = updateChildrenTransform
|| PostTransformMatrixLookup.DidChange(childEntity, LastSystemVersion)
|| LocalTransform2DLookup.DidChange(childEntity, LastSystemVersion);
float4x4 localToWorld;
if (updateChildrenTransform && LocalToWorldWriteGroupMask.MatchesIgnoreFilter(childEntity))
{
var localTransform2D = LocalTransform2DLookup[childEntity];
localToWorld = math.mul(parentLocalToWorld, localTransform2D.ToMatrix());
if (PostTransformMatrixLookup.HasComponent(childEntity))
{
localToWorld = math.mul(localToWorld, PostTransformMatrixLookup[childEntity].Value);
}
LocalToWorldLookup[childEntity] = new LocalToWorld { Value = localToWorld };
}
else
{
localToWorld = LocalToWorldLookup[childEntity].Value;
updateChildrenTransform = LocalToWorldLookup.DidChange(childEntity, LastSystemVersion);
}
if (ChildLookup.TryGetBuffer(childEntity, out DynamicBuffer<Child> children))
{
for (int i = 0, childCount = children.Length; i < childCount; i++)
{
ChildLocalToWorldFromTransformMatrix(localToWorld, children[i].Value, updateChildrenTransform);
}
}
}
}
}
}