コード例 #1
0
ファイル: ParticleForcesTest.cs プロジェクト: vvvv/stride
        public void Sorting()
        {
            var customFieldDesc = new ParticleFieldDescription <UInt32>("SomeField", 0);

            const int maxParticles = 4;
            var       pool         = new ParticlePool(0, maxParticles);

            const bool forceCreation = true;

            pool.FieldExists(ParticleFields.Position, forceCreation);      // Force creation of the position field
            pool.FieldExists(ParticleFields.RemainingLife, forceCreation); // Force creation of the life field
            pool.FieldExists(customFieldDesc, forceCreation);              // Force creation of the custom field we just declared

            // We can extract them before the tight loop on all living particles
            var posField    = pool.GetField(ParticleFields.Position);
            var lifeField   = pool.GetField(ParticleFields.RemainingLife);
            var customField = pool.GetField(customFieldDesc);

            // Ad 4 particles
            var particle1 = pool.AddParticle();
            var particle2 = pool.AddParticle();
            var particle3 = pool.AddParticle();
            var particle4 = pool.AddParticle();

            particle1.Set(customField, (uint)1);
            particle2.Set(customField, (uint)2);
            particle3.Set(customField, (uint)3);
            particle4.Set(customField, (uint)4);

            particle1.Set(lifeField, 0.4f);
            particle2.Set(lifeField, 0.8f);
            particle3.Set(lifeField, 0.2f);
            particle4.Set(lifeField, 0.6f);

            particle1.Set(posField, new Vector3(0, 0, 3));
            particle2.Set(posField, new Vector3(0, 0, 9));
            particle3.Set(posField, new Vector3(0, 0, 5));
            particle4.Set(posField, new Vector3(0, 0, 1));

            // Don't sort
            uint[] sortedNone = { 1, 2, 3, 4 }; // List of expected values
            {
                var i = 0;
                foreach (var particle in pool)
                {
                    Assert.Equal(sortedNone[i++], particle.Get(customField));
                }
            }

            // Sort by depth
            uint[] sortedDepth = { 4, 1, 3, 2 }; // List of expected values
            {
                var depthSorter = new ParticleSorterDepth(pool);
                var sortedList  = depthSorter.GetSortedList(new Vector3(0, 0, 1));

                var i = 0;
                foreach (var particle in sortedList)
                {
                    Assert.Equal(sortedDepth[i++], particle.Get(customField));
                }
            }

            // Sort by age
            uint[] sortedAge = { 2, 4, 1, 3 }; // List of expected values
            {
                var ageSorter  = new ParticleSorterAge(pool);
                var sortedList = ageSorter.GetSortedList(new Vector3(0, 0, 1));

                var i = 0;
                foreach (var particle in sortedList)
                {
                    Assert.Equal(sortedAge[i++], particle.Get(customField));
                }
            }
        }
コード例 #2
0
        public void Sorting()
        {
            var customFieldDesc = new ParticleFieldDescription<UInt32>("SomeField", 0);

            const int maxParticles = 4;
            var pool = new ParticlePool(0, maxParticles);

            const bool forceCreation = true;
            pool.FieldExists(ParticleFields.Position, forceCreation); // Force creation of the position field
            pool.FieldExists(ParticleFields.RemainingLife, forceCreation); // Force creation of the life field
            pool.FieldExists(customFieldDesc, forceCreation); // Force creation of the custom field we just declared

            // We can extract them before the tight loop on all living particles
            var posField = pool.GetField(ParticleFields.Position);
            var lifeField = pool.GetField(ParticleFields.RemainingLife);
            var customField = pool.GetField(customFieldDesc);

            // Ad 4 particles
            var particle1 = pool.AddParticle();
            var particle2 = pool.AddParticle();
            var particle3 = pool.AddParticle();
            var particle4 = pool.AddParticle();

            particle1.Set(customField, (uint)1);
            particle2.Set(customField, (uint)2);
            particle3.Set(customField, (uint)3);
            particle4.Set(customField, (uint)4);

            particle1.Set(lifeField, 0.4f);
            particle2.Set(lifeField, 0.8f);
            particle3.Set(lifeField, 0.2f);
            particle4.Set(lifeField, 0.6f);

            particle1.Set(posField, new Vector3(0, 0, 3));
            particle2.Set(posField, new Vector3(0, 0, 9));
            particle3.Set(posField, new Vector3(0, 0, 5));
            particle4.Set(posField, new Vector3(0, 0, 1));

            // Don't sort
            uint[] sortedNone = { 1, 2, 3, 4 }; // List of expected values
            {
                var i = 0;
                foreach (var particle in pool)
                {
                    Assert.That(particle.Get(customField), Is.EqualTo(sortedNone[i++]));
                }
            }

            // Sort by depth
            uint[] sortedDepth = { 4, 1, 3, 2 }; // List of expected values
            {
                var depthSorter = new ParticleSorterDepth(pool);
                var sortedList = depthSorter.GetSortedList(new Vector3(0, 0, 1));

                var i = 0;
                foreach (var particle in sortedList)
                {
                    Assert.That(particle.Get(customField), Is.EqualTo(sortedDepth[i++]));
                }
            }

            // Sort by age
            uint[] sortedAge = { 2, 4, 1, 3 }; // List of expected values
            {
                var ageSorter = new ParticleSorterAge(pool);
                var sortedList = ageSorter.GetSortedList(new Vector3(0, 0, 1));

                var i = 0;
                foreach (var particle in sortedList)
                {
                    Assert.That(particle.Get(customField), Is.EqualTo(sortedAge[i++]));
                }
            }

        }