Exemplo n.º 1
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));
    }
Exemplo n.º 2
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));
    }