예제 #1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        // This runs only if there exists a BoidControllerECSJobs instance.
        if (!controller)
        {
            controller = BoidControllerECSJobs.Instance;
        }
        if (controller)
        {
            EntityQuery boidQuery = GetEntityQuery(ComponentType.ReadOnly <BoidECSJobs>(), ComponentType.ReadOnly <LocalToWorld>());

            NativeArray <Entity>       entityArray       = boidQuery.ToEntityArray(Allocator.TempJob);
            NativeArray <LocalToWorld> localToWorldArray = boidQuery.ToComponentDataArray <LocalToWorld>(Allocator.TempJob);

            // These arrays get deallocated after job completion
            NativeArray <EntityWithLocalToWorld> boidArray = new NativeArray <EntityWithLocalToWorld>(entityArray.Length, Allocator.TempJob);
            NativeArray <float4x4> newBoidTransforms       = new NativeArray <float4x4>(entityArray.Length, Allocator.TempJob);

            for (int i = 0; i < entityArray.Length; i++)
            {
                boidArray[i] = new EntityWithLocalToWorld {
                    entity       = entityArray[i],
                    localToWorld = localToWorldArray[i]
                };
            }

            entityArray.Dispose();
            localToWorldArray.Dispose();

            BoidJob boidJob = new BoidJob {
                otherBoids           = boidArray,
                newBoidTransforms    = newBoidTransforms,
                boidPerceptionRadius = controller.boidPerceptionRadius,
                separationWeight     = controller.separationWeight,
                cohesionWeight       = controller.cohesionWeight,
                alignmentWeight      = controller.alignmentWeight,
                cageSize             = controller.cageSize,
                avoidWallsTurnDist   = controller.avoidWallsTurnDist,
                avoidWallsWeight     = controller.avoidWallsWeight,
                boidSpeed            = controller.boidSpeed,
                deltaTime            = Time.DeltaTime
            };
            BoidMoveJob boidMoveJob = new BoidMoveJob {
                newBoidTransforms = newBoidTransforms
            };

            JobHandle boidJobHandle = boidJob.Schedule(this, inputDeps);
            return(boidMoveJob.Schedule(this, boidJobHandle));
        }
        else
        {
            return(inputDeps);
        }
    }
예제 #2
0
    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));
    }
예제 #3
0
    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));
    }