示例#1
0
    // Update is called once per frame
    void Update()
    {
        // Input.GetKeyDown(KeyCode.Joystick1Button6) ||
        if (Input.GetKeyDown(KeyCode.U) || Input.GetKeyDown(KeyCode.Joystick1Button6))
        {
            int      sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings;
            string[] scenes     = new string[sceneCount];
            string   sceneName  = SceneManager.GetActiveScene().name;
            int      i          = 0;
            for (i = 0; i < sceneCount; i++)
            {
                scenes[i] = System.IO.Path.GetFileNameWithoutExtension(UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i));
            }
            for (i = 0; i < sceneCount; i++)
            {
                if (sceneName == scenes[i])
                {
                    break;
                }
            }

            i = (i + 1) % scenes.Length;

            ew.BoidBootstrap bb = FindObjectOfType <ew.BoidBootstrap>();
            if (bb != null)
            {
                //bb.DestroyEntities();
            }

            SceneManager.LoadScene(scenes[i]);
        }
    }
示例#2
0
        protected override void OnUpdate()
        {
            NativeArray <Vector3>    positions = this.positions;
            NativeArray <Quaternion> rotations = this.rotations;
            BoidBootstrap            bootstrap = this.bootstrap;
            float dT                 = Time.DeltaTime;
            float bondDamping        = bootstrap.bondDamping;
            float angularBondDamping = bootstrap.angularDamping;

            var ctjHandle = Entities
                            .WithNativeDisableParallelForRestriction(positions)
                            .WithNativeDisableParallelForRestriction(rotations)
                            .ForEach((ref Translation p, ref Rotation r, ref Spine s) =>
            {
                positions[s.spineId] = p.Value;
                rotations[s.spineId] = r.Value;
            })
                            .ScheduleParallel(this.Dependency);

            var spineHandle = Entities
                              .WithNativeDisableParallelForRestriction(positions)
                              .WithNativeDisableParallelForRestriction(rotations)
                              .ForEach((ref Spine s, ref Translation p, ref Rotation r) =>
            {
                // Is it the root of a spine?
                if (s.parent == -1)
                {
                    return;
                }
                Vector3 wantedPosition = positions[s.parent] + rotations[s.parent] * s.offset;
                //p.Value = Vector3.Lerp(p.Value, wantedPosition, bondDamping * dT);

                // Clamp the distance
                Vector3 lerpedPosition = Vector3.Lerp(p.Value, wantedPosition, bondDamping * dT);
                Vector3 clampedOffset  = lerpedPosition - positions[s.parent];
                clampedOffset          = Vector3.ClampMagnitude(clampedOffset, s.offset.magnitude);
                //positions[s.spineId] = Vector3.Lerp(positions[s.spineId], wantedPosition, bondDamping * dT);
                positions[s.spineId]        = positions[s.parent] + clampedOffset;
                Vector3 myPos               = positions[s.spineId];
                Quaternion wantedQuaternion = Quaternion.LookRotation(positions[s.parent] - myPos);
                rotations[s.spineId]        = Quaternion.Slerp(rotations[s.spineId], wantedQuaternion, angularBondDamping * dT);
                //r.Value = Quaternion.Slerp(r.Value, wantedQuaternion, angularBondDamping * dT);
            }
                                       )
                              .ScheduleParallel(ctjHandle);

            var cfJHandle = Entities
                            .WithNativeDisableParallelForRestriction(positions)
                            .WithNativeDisableParallelForRestriction(rotations)
                            .ForEach((ref Translation p, ref Rotation r, ref Spine s) =>
            {
                p.Value = positions[s.spineId];
                r.Value = rotations[s.spineId];
            }
                                     )
                            .ScheduleParallel(spineHandle);

            this.Dependency = cfJHandle;
            return;
        }
示例#3
0
        void Start()
        {
            transform.position = new Vector3(0, 0, maxDist);
            targetPos          = transform.position;
            transform.rotation = Quaternion.LookRotation(-transform.position);

            cr = StartCoroutine(Automatic());
            bb = FindObjectOfType <BoidBootstrap>();
        }
示例#4
0
        protected override void OnCreate()
        {
            Instance  = this;
            bootstrap = GameObject.FindObjectOfType <BoidBootstrap>();

            positions = new NativeArray <Vector3>(MAX_SPINES, Allocator.Persistent);
            rotations = new NativeArray <Quaternion>(MAX_SPINES, Allocator.Persistent);
            numSpines = 0;

            Enabled = false;
        }
示例#5
0
        protected override void OnUpdate()
        {
            physicsWorld = World.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>();

            BoidBootstrap bootstrap = this.bootstrap;

            ComponentTypeHandle <Wander>            wTHandle   = GetComponentTypeHandle <Wander>();
            ComponentTypeHandle <Boid>              bTHandle   = GetComponentTypeHandle <Boid>();
            ComponentTypeHandle <Translation>       ttTHandle  = GetComponentTypeHandle <Translation>();
            ComponentTypeHandle <Rotation>          rTHandle   = GetComponentTypeHandle <Rotation>();
            ComponentTypeHandle <Seperation>        sTHandle   = GetComponentTypeHandle <Seperation>();
            ComponentTypeHandle <Cohesion>          cTHandle   = GetComponentTypeHandle <Cohesion>();
            ComponentTypeHandle <Alignment>         aTHandle   = GetComponentTypeHandle <Alignment>();
            ComponentTypeHandle <Constrain>         conTHandle = GetComponentTypeHandle <Constrain>();
            ComponentTypeHandle <LocalToWorld>      ltwTHandle = GetComponentTypeHandle <LocalToWorld>();
            ComponentTypeHandle <ObstacleAvoidance> oaTHandle  = GetComponentTypeHandle <ObstacleAvoidance>();

            float deltaTime = Time.DeltaTime * bootstrap.speed;

            Unity.Mathematics.Random random = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(1, 100000));

            CamPosition = positions[0];
            CamRotation = rotations[0];

            //DrawGizmos();

            // Copy entities to the native arrays
            var copyToNativeJob = new CopyTransformsToNativeJob()
            {
                positions             = this.positions,
                rotations             = this.rotations,
                boidTypeHandle        = bTHandle,
                translationTypeHandle = ttTHandle,
                rotationTypeHandle    = rTHandle
            };

            var copyToNativeHandle = copyToNativeJob.ScheduleParallel(translationsRotationsQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, copyToNativeHandle);

            var oaJob = new ObstacleAvoidanceJob()
            {
                boidTypeHandle              = bTHandle,
                translationTypeHandle       = ttTHandle,
                ltwTypeHandle               = ltwTHandle,
                obstacleAvoidanceTypeHandle = oaTHandle,
                rotationTypeHandle          = rTHandle,
                collisionWorld              = physicsWorld.PhysicsWorld.CollisionWorld
            };

            var oaJobHandle = oaJob.ScheduleParallel(obstacleQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, oaJobHandle);

            if (bootstrap.usePartitioning)
            {
                cells.Clear();
                var partitionJob = new PartitionSpaceJob()
                {
                    positions   = this.positions,
                    cells       = this.cells.AsParallelWriter(),
                    threedcells = bootstrap.threedcells,
                    cellSize    = bootstrap.cellSize,
                    gridSize    = bootstrap.gridSize
                };

                var partitionHandle = partitionJob.Schedule(bootstrap.numBoids, 50, Dependency);
                Dependency = JobHandle.CombineDependencies(Dependency, partitionHandle);
            }
            var countNeighbourJob = new CountNeighboursJob()
            {
                positions             = this.positions,
                rotations             = this.rotations,
                neighbours            = this.neighbours,
                maxNeighbours         = bootstrap.totalNeighbours,
                cells                 = this.cells,
                cellSize              = bootstrap.cellSize,
                gridSize              = bootstrap.gridSize,
                usePartitioning       = bootstrap.usePartitioning,
                neighbourDistance     = bootstrap.neighbourDistance,
                boidTypeHandle        = bTHandle,
                translationTypeHandle = ttTHandle,
                rotationTypeHandle    = rTHandle
            };
            var cnjHandle = countNeighbourJob.ScheduleParallel(translationsRotationsQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, cnjHandle);

            var constrainJob = new ConstrainJob()
            {
                positions           = this.positions,
                boidTypeHandle      = bTHandle,
                constrainTypeHandle = conTHandle,
                weight = bootstrap.constrainWeight,
                centre = bootstrap.transform.position,
                radius = bootstrap.radius
            };

            var conjHandle = constrainJob.ScheduleParallel(constrainQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, conjHandle);


            var seperationJob = new SeperationJob()
            {
                positions             = this.positions,
                maxNeighbours         = this.maxNeighbours,
                random                = random,
                neighbours            = this.neighbours,
                weight                = bootstrap.seperationWeight,
                seperationTypeHandle  = sTHandle,
                translationTypeHandle = ttTHandle,
                boidTypeHandle        = bTHandle,
            };

            var sjHandle = seperationJob.ScheduleParallel(seperationQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, sjHandle);

            var cohesionJob = new CohesionJob()
            {
                positions          = this.positions,
                maxNeighbours      = this.maxNeighbours,
                neighbours         = this.neighbours,
                weight             = bootstrap.cohesionWeight,
                cohesionTypeHandle = cTHandle,
                boidTypeHandle     = bTHandle,
            };

            var cjHandle = cohesionJob.ScheduleParallel(cohesionQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, cjHandle);

            var alignmentJob = new AlignmentJob()
            {
                rotations           = this.rotations,
                maxNeighbours       = this.maxNeighbours,
                neighbours          = this.neighbours,
                weight              = bootstrap.alignmentWeight,
                alignmentTypeHandle = aTHandle,
                boidTypeHandle      = bTHandle,
            };

            var ajHandle = alignmentJob.ScheduleParallel(alignmentQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, ajHandle);

            var wanderJob = new WanderJob()
            {
                wanderTypeHandle      = wTHandle,
                translationTypeHandle = ttTHandle,
                rotationTypeHandle    = rTHandle,
                boidTypeHandle        = bTHandle,
                dT     = deltaTime,
                ran    = random,
                weight = bootstrap.wanderWeight
            };

            var wanderJobHandle = wanderJob.ScheduleParallel(wanderQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, wanderJobHandle);

            var boidJob = new BoidJob()
            {
                positions             = this.positions,
                rotations             = this.rotations,
                speeds                = this.speeds,
                dT                    = deltaTime,
                limitUpAndDown        = bootstrap.limitUpAndDown,
                banking               = 0.01f,
                damping               = 0.01f,
                wanderTypeHandle      = wTHandle,
                boidTypeHandle        = bTHandle,
                translationTypeHandle = ttTHandle,
                seperationTypeHandle  = sTHandle,
                rotationTypeHandle    = rTHandle,
                cohesionTypeHandle    = cTHandle,
                alignmentTypeHandle   = aTHandle,
                constrainTypeHandle   = conTHandle,
                obstacleTypeHandle    = oaTHandle
            };

            var boidJobHandle = boidJob.ScheduleParallel(boidQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, boidJobHandle);

            var copyFromNativeJob = new CopyTransformsFromNativeJob()
            {
                positions             = this.positions,
                rotations             = this.rotations,
                boidTypeHandle        = bTHandle,
                translationTypeHandle = ttTHandle,
                rotationTypeHandle    = rTHandle
            };

            var copyFromNativeHandle = copyFromNativeJob.ScheduleParallel(translationsRotationsQuery, 1, Dependency);

            Dependency = JobHandle.CombineDependencies(Dependency, copyFromNativeHandle);

            return;
        }
示例#6
0
        protected override void OnCreate()
        {
            Instance   = this;
            bootstrap  = GameObject.FindObjectOfType <BoidBootstrap>();
            Enabled    = false;
            neighbours = new NativeArray <int>(BoidBootstrap.MAX_BOIDS * BoidBootstrap.MAX_NEIGHBOURS, Allocator.Persistent);
            positions  = new NativeArray <Vector3>(BoidBootstrap.MAX_BOIDS, Allocator.Persistent);
            rotations  = new NativeArray <Quaternion>(BoidBootstrap.MAX_BOIDS, Allocator.Persistent);
            speeds     = new NativeArray <float>(BoidBootstrap.MAX_BOIDS, Allocator.Persistent); // Needed for the animations
            cells      = new NativeMultiHashMap <int, int>(BoidBootstrap.MAX_BOIDS, Allocator.Persistent);

            translationsRotationsQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>()
                }
            });

            wanderQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Wander>()
                }
            });

            boidQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Wander>(),
                    ComponentType.ReadOnly <Seperation>(),
                    ComponentType.ReadOnly <Alignment>(),
                    ComponentType.ReadOnly <Cohesion>(),
                    ComponentType.ReadOnly <Constrain>()
                }
            });

            seperationQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Seperation>()
                }
            });

            alignmentQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Alignment>()
                }
            });

            cohesionQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <Rotation>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Cohesion>()
                }
            });

            constrainQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <Constrain>(),
                    ComponentType.ReadOnly <Boid>()
                }
            });

            obstacleQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[] {
                    ComponentType.ReadOnly <ObstacleAvoidance>(),
                    ComponentType.ReadOnly <Boid>(),
                    ComponentType.ReadOnly <Translation>(),
                    ComponentType.ReadOnly <LocalToWorld>()
                }
            });

            Enabled = false;

            physicsWorld   = World.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>();
            collisionWorld = physicsWorld.PhysicsWorld.CollisionWorld;

            // Register the gizmos callback
            //MyGizmo.OnDrawGizmos(DrawGizmos);
        }
示例#7
0
 protected override void OnStartRunning()
 {
     bootstrap = GameObject.FindObjectOfType <BoidBootstrap>();
 }
 protected override void OnCreate()
 {
     Instance  = this;
     bootstrap = GameObject.FindObjectOfType <BoidBootstrap>();
     Enabled   = false;
 }