forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoidSchoolSpawnSystem.cs
More file actions
81 lines (71 loc) · 3.1 KB
/
Copy pathBoidSchoolSpawnSystem.cs
File metadata and controls
81 lines (71 loc) · 3.1 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
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine.Profiling;
namespace Boids
{
[RequireMatchingQueriesForUpdate]
[BurstCompile]
public partial struct BoidSchoolSpawnSystem : ISystem
{
private EntityQuery _boidQuery;
public void OnCreate(ref SystemState state)
{
_boidQuery = new EntityQueryBuilder(Allocator.Temp).WithAll<Boid, LocalTransform>().Build(ref state);
}
public void OnUpdate(ref SystemState state)
{
var localToWorldLookup = SystemAPI.GetComponentLookup<LocalToWorld>();
var ecb = new EntityCommandBuffer(Allocator.Temp);
var world = state.World.Unmanaged;
foreach (var (boidSchool, boidSchoolLocalToWorld, entity) in
SystemAPI.Query<RefRO<BoidSchool>, RefRO<LocalToWorld>>()
.WithEntityAccess())
{
var boidEntities =
CollectionHelper.CreateNativeArray<Entity, RewindableAllocator>(boidSchool.ValueRO.Count,
ref world.UpdateAllocator);
state.EntityManager.Instantiate(boidSchool.ValueRO.Prefab, boidEntities);
var setBoidLocalToWorldJob = new SetBoidLocalToWorld
{
LocalToWorldFromEntity = localToWorldLookup,
Entities = boidEntities,
Center = boidSchoolLocalToWorld.ValueRO.Position,
Radius = boidSchool.ValueRO.InitialRadius
};
state.Dependency = setBoidLocalToWorldJob.Schedule(boidSchool.ValueRO.Count, 64, state.Dependency);
state.Dependency.Complete();
ecb.DestroyEntity(entity);
}
ecb.Playback(state.EntityManager);
// TODO: all Prefabs are currently forced to TransformUsageFlags.Dynamic by default, which means boids get a LocalTransform
// they don't need. As a workaround, remove the component at spawn-time.
state.EntityManager.RemoveComponent<LocalTransform>(_boidQuery);
}
}
[BurstCompile]
struct SetBoidLocalToWorld : IJobParallelFor
{
[NativeDisableContainerSafetyRestriction] [NativeDisableParallelForRestriction]
public ComponentLookup<LocalToWorld> LocalToWorldFromEntity;
public NativeArray<Entity> Entities;
public float3 Center;
public float Radius;
public void Execute(int i)
{
var entity = Entities[i];
var random = new Random(((uint)(entity.Index + i + 1) * 0x9F6ABC1));
var dir = math.normalizesafe(random.NextFloat3() - new float3(0.5f, 0.5f, 0.5f));
var pos = Center + (dir * Radius);
var localToWorld = new LocalToWorld
{
Value = float4x4.TRS(pos, quaternion.LookRotationSafe(dir, math.up()), new float3(1.0f, 1.0f, 1.0f))
};
LocalToWorldFromEntity[entity] = localToWorld;
}
}
}