protected override JobHandle OnUpdate(JobHandle inputDeps) { var neighbors = new NeighborsDetectionJob { prodThresh = math.cos(math.radians(Bootstrap.Param.neighborFov)), distThresh = Bootstrap.Param.neighborDistance, neighborsFromEntity = GetBufferFromEntity <NeighborsEntityBuffer>(false), positionFromEntity = GetComponentDataFromEntity <Position>(true), entities = group.GetEntityArray(), }; var wall = new WallJob { scale = Bootstrap.Param.wallScale * 0.5f, thresh = Bootstrap.Param.wallDistance, weight = Bootstrap.Param.wallWeight, }; var separation = new SeparationJob { separationWeight = Bootstrap.Param.separationWeight, neighborsFromEntity = GetBufferFromEntity <NeighborsEntityBuffer>(true), positionFromEntity = GetComponentDataFromEntity <Position>(true), }; var alignment = new AlignmentJob { alignmentWeight = Bootstrap.Param.alignmentWeight, neighborsFromEntity = GetBufferFromEntity <NeighborsEntityBuffer>(true), velocityFromEntity = GetComponentDataFromEntity <Velocity>(true), }; var cohesion = new CohesionJob { cohesionWeight = Bootstrap.Param.cohesionWeight, neighborsFromEntity = GetBufferFromEntity <NeighborsEntityBuffer>(true), positionFromEntity = GetComponentDataFromEntity <Position>(true), }; var move = new MoveJob { dt = Time.deltaTime, minSpeed = Bootstrap.Param.minSpeed, maxSpeed = Bootstrap.Param.maxSpeed, }; inputDeps = neighbors.Schedule(this, inputDeps); inputDeps = wall.Schedule(this, inputDeps); inputDeps = separation.Schedule(this, inputDeps); inputDeps = alignment.Schedule(this, inputDeps); inputDeps = cohesion.Schedule(this, inputDeps); inputDeps = move.Schedule(this, inputDeps); return(inputDeps); }
void IEcsRunSystem.Run() { if (_filter.EntitiesCount == 0) { return; } if (FullBackground) { if (!CanRead) { jobHandle.Complete(); CanRead = true; } } var neighbors = new NeighborsDetectionJob { prodThresh = NeighborFov, distThresh = NeighborDistance, velocities = BoidEntityData.velocities, positions = BoidEntityData.positions, neighborsFromEntity = BoidEntityData.neighbors, entitiesCount = EntitiesCount }; var wall = new WallJob { scale = WallScale, thresh = WallDistance, weight = WallWeight, positions = BoidEntityData.positions, accelerations = BoidEntityData.accelerations, _right = new Float3(1, 0, 0), _up = new Float3(0, 1, 0), _fwd = new Float3(0, 0, 1), _left = new Float3(-1, 0, 0), _down = new Float3(0, -1, 0), _back = new Float3(0, 0, -1) }; var separation = new SeparationJob { separationWeight = SeparationWeight, entitiesCount = EntitiesCount, neighborsFromEntity = BoidEntityData.neighbors, positions = BoidEntityData.positions, accelerations = BoidEntityData.accelerations }; var alignment = new AlignmentJob { alignmentWeight = AlignmentWeight, entitiesCount = EntitiesCount, neighborsFromEntity = BoidEntityData.neighbors, velocities = BoidEntityData.velocities, accelerations = BoidEntityData.accelerations }; var cohesion = new CohesionJob { cohesionWeight = CohesionWeight, entitiesCount = EntitiesCount, neighborsFromEntity = BoidEntityData.neighbors, positions = BoidEntityData.positions, accelerations = BoidEntityData.accelerations }; var move = new MoveJob { dt = _timeSystem.DeltaTime, minSpeed = MinSpeed, maxSpeed = MaxSpeed, scale = BoidScale, positions = BoidEntityData.positions, velocities = BoidEntityData.velocities, accelerations = BoidEntityData.accelerations, matrices = BoidsVisualisationSystem._nativeMatrices }; jobHandle = neighbors.Schedule(EntitiesCount, NumBoidsPerJob); jobHandle = wall.Schedule(EntitiesCount, NumBoidsPerJob, jobHandle); jobHandle = separation.Schedule(EntitiesCount, NumBoidsPerJob, jobHandle); jobHandle = alignment.Schedule(EntitiesCount, NumBoidsPerJob, jobHandle); jobHandle = cohesion.Schedule(EntitiesCount, NumBoidsPerJob, jobHandle); jobHandle = move.Schedule(EntitiesCount, NumBoidsPerJob, jobHandle); JobHandle.ScheduleBatchedJobs(); if (FullBackground) { CanRead = false; } else { jobHandle.Complete(); CanRead = true; } }
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); // Count Neigthbours var cnj = new CountNeighboursJob() { positions = this.positions, rotations = this.rotations, neighbours = this.neighbours, maxNeighbours = this.maxNeighbours, neighbourDistance = bootstrap.neighbourDistance }; var cnjHandle = cnj.Schedule(this, ctjHandle); 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)); }
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)); }