forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridOnCubeTransformSystem.cs
More file actions
47 lines (41 loc) · 1.99 KB
/
Copy pathCartesianGridOnCubeTransformSystem.cs
File metadata and controls
47 lines (41 loc) · 1.99 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
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
[UpdateAfter(typeof(CartesianGridMoveForwardSystem))]
public unsafe partial class CartesianGridOnCubeTransformSystem : JobComponentSystem
{
EntityQuery m_GridQuery;
protected override void OnCreate()
{
m_GridQuery = GetEntityQuery(ComponentType.ReadOnly<CartesianGridOnCube>());
RequireForUpdate(m_GridQuery);
}
protected override JobHandle OnUpdate(JobHandle lastJobHandle)
{
var cartesianGridOnCube = GetSingleton<CartesianGridOnCube>();
var faceLocalToWorld = (float4x4*)cartesianGridOnCube.Blob.Value.FaceLocalToWorld.GetUnsafePtr();
// Transform from grid-space Translation and gridFace to LocalToWorld for GridCube
// - This is an example of overriding the transform system's default behavior.
// - CubeFace is in the LocalToWorld WriteGroup, so when it this component is present, it is required to be
// part of the query in order to write to LocalToWorld. Since the transform system doesn't know anything
// about CubeFace, it will never be present in those default transformations. So it can be handled custom
// here.
lastJobHandle = Entities
.WithName("CartesianGridOnCubeTransform")
.WithNativeDisableUnsafePtrRestriction(faceLocalToWorld)
.ForEach((ref LocalToWorld localToWorld,
in Translation translation,
in CartesianGridOnCubeFace cubeFace) =>
{
var cubeFaceIndex = cubeFace.Value;
var resultLocalToWorld = faceLocalToWorld[cubeFaceIndex];
resultLocalToWorld.c3 = math.mul(resultLocalToWorld, new float4(translation.Value, 1.0f));
localToWorld = new LocalToWorld
{
Value = resultLocalToWorld
};
}).Schedule(lastJobHandle);
return lastJobHandle;
}
}