private void Awake() { Instance = this; EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; EntityArchetype boidArchetype = entityManager.CreateArchetype( typeof(BoidECSJobsFast), typeof(RenderMesh), typeof(RenderBounds), typeof(LocalToWorld) ); NativeArray <Entity> boidArray = new NativeArray <Entity>(boidAmount, Allocator.Temp); entityManager.CreateEntity(boidArchetype, boidArray); for (int i = 0; i < boidArray.Length; i++) { Unity.Mathematics.Random rand = new Unity.Mathematics.Random((uint)i + 1); entityManager.SetComponentData(boidArray[i], new LocalToWorld { Value = float4x4.TRS( RandomPosition(), RandomRotation(), new float3(1f)) }); entityManager.SetSharedComponentData(boidArray[i], new RenderMesh { mesh = sharedMesh, material = sharedMaterial, }); } boidArray.Dispose(); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (!controller) { controller = BoidControllerECSJobsFast.Instance; } if (controller) { int boidCount = boidGroup.CalculateEntityCount(); var cellIndices = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellBoidCount = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var boidPositions = new NativeArray <float3>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var boidHeadings = new NativeArray <float3>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var hashMap = new NativeMultiHashMap <int, int>(boidCount, Allocator.TempJob); var positionsAndHeadingsCopyJob = new CopyPositionsAndHeadingsInBuffer { boidPositions = boidPositions, boidHeadings = boidHeadings }; JobHandle positionsAndHeadingsCopyJobHandle = positionsAndHeadingsCopyJob.Schedule(boidGroup, inputDeps); quaternion randomHashRotation = quaternion.Euler( UnityEngine.Random.Range(-360f, 360f), UnityEngine.Random.Range(-360f, 360f), UnityEngine.Random.Range(-360f, 360f) ); float offsetRange = controller.boidPerceptionRadius / 2f; float3 randomHashOffset = new float3( UnityEngine.Random.Range(-offsetRange, offsetRange), UnityEngine.Random.Range(-offsetRange, offsetRange), UnityEngine.Random.Range(-offsetRange, offsetRange) ); var hashPositionsJob = new HashPositionsToHashMap { hashMap = hashMap.AsParallelWriter(), cellRotationVary = randomHashRotation, positionOffsetVary = randomHashOffset, cellRadius = controller.boidPerceptionRadius, }; JobHandle hashPositionsJobHandle = hashPositionsJob.Schedule(boidGroup, inputDeps); // Proceed when these two jobs have been completed JobHandle copyAndHashJobHandle = JobHandle.CombineDependencies( positionsAndHeadingsCopyJobHandle, hashPositionsJobHandle ); var mergeCellsJob = new MergeCellsJob { indicesOfCells = cellIndices, cellPositions = boidPositions, cellHeadings = boidHeadings, cellCount = cellBoidCount, }; JobHandle mergeCellsJobHandle = mergeCellsJob.Schedule(hashMap, 64, copyAndHashJobHandle); var moveJob = new MoveBoids { deltaTime = Time.DeltaTime, boidSpeed = controller.boidSpeed, separationWeight = controller.separationWeight, alignmentWeight = controller.alignmentWeight, cohesionWeight = controller.cohesionWeight, cageSize = controller.cageSize, cageAvoidDist = controller.avoidWallsTurnDist, cageAvoidWeight = controller.avoidWallsWeight, cellSize = controller.boidPerceptionRadius, cellIndices = cellIndices, positionSumsOfCells = boidPositions, headingSumsOfCells = boidHeadings, cellBoidCount = cellBoidCount, }; JobHandle moveJobHandle = moveJob.Schedule(boidGroup, mergeCellsJobHandle); moveJobHandle.Complete(); hashMap.Dispose(); inputDeps = moveJobHandle; boidGroup.AddDependency(inputDeps); return(inputDeps); } else { return(inputDeps); } }