예제 #1
0
        void SveltoECS()
        {
            //creates the _enginesRoot, for more information read my svelto.ECS articles
            _enginesRoot = new EnginesRoot(new UnityEntitySubmissionScheduler());
            //this will be used to build entities
            var entityFactory = _enginesRoot.GenerateEntityFactory();
            var random        = new Unity.Mathematics.Random(853);

            for (int i = 0; i < boidCount; ++i)
            {
                var initSpeed = param.initSpeed;

                //build entities in the BOIDS_GROUP exclusive group
                var entityStructInitializer = entityFactory.BuildEntity <BoidEntityDecriptor>(i, GAME_GROUPS.BOIDS_GROUP);
                var nextFloat3 = random.NextFloat3(1f);
                var float3     = random.NextFloat3Direction() * initSpeed;
                //init them with these default values
                entityStructInitializer.Init(new BoidEntityStruct()
                {
                    position = new SVector3 {
                        x = nextFloat3.x, y = nextFloat3.y, z = nextFloat3.z
                    },
                    rotation = new SVector4 {
                        x = 0, y = 0, z = 0, w = 1
                    },
                    velocity = new SVector3 {
                        x = float3.x, y = float3.y, z = float3.z
                    },
                    acceleration = new SVector3()
                });
            }

            //create a synchronization enumerator to be used inside the Svelto.Tasks
            ThreadSynchronizationSignal _signal = new ThreadSynchronizationSignal("name");

            //add the engines we are going to use
            _enginesRoot.AddEngine(new BoidsSyncronizationEngine(_unityECSGroup, _signal));
            _enginesRoot.AddEngine(new BoidsSimulationEngine(_signal, param, boidCount));
        }
예제 #2
0
        public BoidsSimulationEngine(ThreadSynchronizationSignal synchronizationSignal, Param param, int boidCount)
        {
            this.param = param;

            //Svelto.Tasks is based on the concept of tasks and runners to run them. Runners can be many. I may even
            //write a Unity Jobs runner one day. The MultiThreadRunner is the Svelto.Tasks runner to run tasks
            //on other threads. For more information please check my Svelto.Tasks articles.
            //this runner will run the main loop
            _runner = new MultiThreadRunner("BoidSimulationSystem", 1);
            //MultiThreadedParallelTaskCollection enables massive parallelism on the CPU. I will create a Svelto.Tasks
            //job for each boid. These jobs will be then spread on 16 threads on an I7.
            _parallelJobs = new MultiThreadedParallelTaskCollection("BoidsSimulationJobs", (uint)(System.Environment.ProcessorCount * 2), true);
            //Svelto Tasks Jobs must be struct, as they must be instantiated multiple times, the first one
            //will be used as blueprint fro the other ones
            var jobInstance = new BoidSimulationJob(this, boidCount);

            //Create boidCount jobs and add them
            _parallelJobs.Add(ref jobInstance, boidCount);

            //the Synchronization signal is used to send signals between threads and allow synchronizations
            _synchronizationSignal = synchronizationSignal;
            _boidCount             = boidCount;
        }
예제 #3
0
 public BoidsSyncronizationEngine(ComponentGroup unityEcSgroup, ThreadSynchronizationSignal synchronizationSignal)
 {
     _unityECSgroup         = unityEcSgroup;
     _synchronizationSignal = synchronizationSignal;
 }