protected override JobHandle OnUpdate(JobHandle inputDeps) { Unity.Mathematics.Random ran = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 100000)); // Copy entities to the native arrays var ctj = new CopyTransformsToJob() { positions = this.positions, rotations = this.rotations }; var ctjHandle = ctj.Schedule(this, inputDeps); cells.Clear(); var partitionJob = new PartitionSpaceJob() { positions = this.positions, cells = this.cells.ToConcurrent(), threedcells = bootstrap.threedcells, cellSize = bootstrap.cellSize, gridSize = bootstrap.gridSize }; var partitionHandle = partitionJob.Schedule(bootstrap.numBoids, 50, ctjHandle); // Count Neigthbours var cnj = new CountNeighboursJob() { positions = this.positions, rotations = this.rotations, neighbours = this.neighbours, maxNeighbours = bootstrap.totalNeighbours, cells = this.cells, cellSize = bootstrap.cellSize, gridSize = bootstrap.gridSize, usePatritioning = bootstrap.usePartitioning, neighbourDistance = bootstrap.neighbourDistance }; var cnjHandle = cnj.Schedule(this, partitionHandle); var seperationJob = new SeperationJob() { positions = SpineSystem.Instance.positions, spineLength = bootstrap.spineLength, spineOffset = bootstrap.spineLength / 2, maxNeighbours = this.maxNeighbours, random = ran, neighbours = this.neighbours, weight = bootstrap.seperationWeight }; var sjHandle = seperationJob.Schedule(this, cnjHandle); var alignmentJob = new AlignmentJob() { positions = this.positions, rotations = this.rotations, maxNeighbours = this.maxNeighbours, neighbours = this.neighbours, weight = bootstrap.alignmentWeight }; var ajHandle = alignmentJob.Schedule(this, sjHandle); var cohesionJob = new CohesionJob() { positions = this.positions, maxNeighbours = this.maxNeighbours, neighbours = this.neighbours, weight = bootstrap.cohesionWeight }; var cjHandle = cohesionJob.Schedule(this, ajHandle); var wanderJob = new WanderJob() { dT = Time.deltaTime * bootstrap.speed, random = ran, weight = bootstrap.wanderWeight }; var wjHandle = wanderJob.Schedule(this, cjHandle); var constrainJob = new ConstrainJob() { positions = this.positions, centre = bootstrap.constrainPosition, radius = bootstrap.radius, weight = bootstrap.constrainWeight }; var constrainHandle = constrainJob.Schedule(this, wjHandle); var fleeJob = new FleeJob() { positions = this.positions, enemyPos = Camera.main.transform.position, distance = bootstrap.fleeDistance, weight = bootstrap.fleeWeight }; var fleeHandle = fleeJob.Schedule(this, constrainHandle); var seekJob = new SeekJob() { positions = this.positions, targetPos = Camera.main.transform.position, weight = bootstrap.seekWeight }; var seekHandle = seekJob.Schedule(this, fleeHandle); // Integrate the forces var boidJob = new BoidJob() { positions = this.positions, rotations = this.rotations, speeds = this.speeds, dT = Time.deltaTime * bootstrap.speed, damping = 0.01f, limitUpAndDown = bootstrap.limitUpAndDown, banking = 0.01f }; var boidHandle = boidJob.Schedule(this, seekHandle); // Copy back to the entities var cfj = new CopyTransformsFromJob() { positions = this.positions, rotations = this.rotations }; return(cfj.Schedule(this, boidHandle)); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // Copy entities to the native arrays var ctj = new CopyTransformsToJob() { positions = this.positions, rotations = this.rotations }; var ctjHandle = ctj.Schedule(this, inputDeps); cells.Clear(); var partitionJob = new PartitionSpaceJob() { positions = this.positions, cells = this.cells.ToConcurrent(), cellSize = bootstrap.cellSize, gridSize = bootstrap.gridSize }; var partitionHandle = partitionJob.Schedule(bootstrap.numBoids, 50, ctjHandle); // Count Neigthbours var cnj = new CountNeighboursJob() { positions = this.positions, rotations = this.rotations, neighbours = this.neighbours, maxNeighbours = this.maxNeighbours, cells = this.cells, cellSize = bootstrap.cellSize, gridSize = bootstrap.gridSize, usePatritioning = bootstrap.usePartitioning, neighbourDistance = bootstrap.neighbourDistance }; var cnjHandle = cnj.Schedule(this, partitionHandle); var seperationJob = new SeperationJob() { positions = this.positions, maxNeighbours = this.maxNeighbours, neighbours = this.neighbours, weight = bootstrap.seperationWeight }; var sjHandle = seperationJob.Schedule(this, cnjHandle); var alignmentJob = new AlignmentJob() { positions = this.positions, rotations = this.rotations, maxNeighbours = this.maxNeighbours, neighbours = this.neighbours, weight = bootstrap.alignmentWeight }; var ajHandle = alignmentJob.Schedule(this, sjHandle); var cohesionJob = new CohesionJob() { positions = this.positions, maxNeighbours = this.maxNeighbours, neighbours = this.neighbours, weight = bootstrap.cohesionWeight }; var cjHandle = cohesionJob.Schedule(this, ajHandle); var ran = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 100000)); var wanderJob = new WanderJob() { dT = Time.deltaTime * bootstrap.speed, random = ran, weight = bootstrap.wanderWeight }; var wjHandle = wanderJob.Schedule(this, cjHandle); var constrainJob = new ConstrainJob() { positions = this.positions, centre = bootstrap.transform.position, radius = bootstrap.radius, weight = bootstrap.constrainWeight }; var constrainHandle = constrainJob.Schedule(this, wjHandle); var fleeJob = new FleeJob() { positions = this.positions, enemyPos = Camera.main.transform.position, distance = bootstrap.fleeDistance, weight = bootstrap.fleeWeight }; var fleeHandle = fleeJob.Schedule(this, constrainHandle); // Integrate the forces var boidJob = new BoidJob() { positions = this.positions, rotations = this.rotations, speeds = this.speeds, dT = Time.deltaTime * bootstrap.speed, damping = 0.01f, banking = 0.00f }; var boidHandle = boidJob.Schedule(this, fleeHandle); // Animate the head and tail var headJob = new HeadJob() { positions = this.positions, rotations = this.rotations, speeds = this.speeds, dT = Time.deltaTime * bootstrap.speed, amplitude = bootstrap.headAmplitude, frequency = bootstrap.animationFrequency, size = bootstrap.size }; var headHandle = headJob.Schedule(this, boidHandle);// Animate the head and tail var tailJob = new TailJob() { positions = this.positions, rotations = this.rotations, speeds = this.speeds, dT = Time.deltaTime * bootstrap.speed, amplitude = bootstrap.tailAmplitude, frequency = bootstrap.animationFrequency, size = bootstrap.size }; var tailHandle = tailJob.Schedule(this, headHandle); // Copy back to the entities var cfj = new CopyTransformsFromJob() { positions = this.positions, rotations = this.rotations }; return(cfj.Schedule(this, tailHandle)); }
public void Execute(ref Boid b) { int neighbourStartIndex = maxNeighbours * b.boidId; int neighbourCount = 0; if (usePatritioning) { int surroundingCellCount = (int)Mathf.Ceil(neighbourDistance / cellSize); // Are we looking above and below? int sliceSurrounding = threedcells ? surroundingCellCount : 0; for (int slice = -sliceSurrounding; slice <= sliceSurrounding; slice++) { for (int row = -surroundingCellCount; row <= surroundingCellCount; row++) { for (int col = -surroundingCellCount; col <= surroundingCellCount; col++) { Vector3 pos = positions[b.boidId] + new Vector3(col * cellSize, slice * cellSize, row * cellSize); int cell = PartitionSpaceJob.PositionToCell(pos, cellSize, gridSize); NativeMultiHashMapIterator <int> iterator; int boidId; if (cells.TryGetFirstValue(cell, out boidId, out iterator)) { do { if (boidId != b.boidId) { if (Vector3.Distance(positions[b.boidId], positions[boidId]) < neighbourDistance) { neighbours[neighbourStartIndex + neighbourCount] = boidId; neighbourCount++; if (neighbourCount == maxNeighbours) { b.taggedCount = neighbourCount; return; } } } } while (cells.TryGetNextValue(out boidId, ref iterator)); } } } } } else { for (int i = 0; i < positions.Length; i++) { if (i != b.boidId) { if (Vector3.Distance(positions[b.boidId], positions[i]) < neighbourDistance) { neighbours[neighbourStartIndex + neighbourCount] = i; neighbourCount++; if (neighbourCount == maxNeighbours) { break; } } } } } b.taggedCount = neighbourCount; }