Пример #1
0
        /// <summary>
        /// If the particle pool has changed the sorter must also be updated to reflect those changes
        /// </summary>
        private void PoolChangedNotification()
        {
            if (SortingPolicy == EmitterSortingPolicy.None || pool.ParticleCapacity <= 0)
            {
                ParticleSorter = new ParticleSorterDefault(pool);
                return;
            }

            if (SortingPolicy == EmitterSortingPolicy.ByDepth)
            {
                GetSortIndex <Vector3> sortByDepth = value =>
                {
                    var depth = Vector3.Dot(depthSortVector, value);
                    return(depth);
                };

                ParticleSorter = new ParticleSorterCustom <Vector3>(pool, ParticleFields.Position, sortByDepth);
                return;
            }

            if (SortingPolicy == EmitterSortingPolicy.ByAge)
            {
                GetSortIndex <float> sortByAge = value => { return(-value); };

                ParticleSorter = new ParticleSorterCustom <float>(pool, ParticleFields.Life, sortByAge);
                return;
            }

            // Default - no sorting
            ParticleSorter = new ParticleSorterDefault(pool);
        }
Пример #2
0
        public ParticleSorterCustom(ParticlePool pool, ParticleFieldDescription <T> fieldDesc, GetSortIndex <T> getIndex) : base(pool)
        {
            particleList           = new SortedParticle[pool.ParticleCapacity];
            currentLivingParticles = 0;

            this.fieldDesc = fieldDesc;
            this.getIndex  = getIndex;
        }
Пример #3
0
        /// <summary>
        /// If the particle pool has changed the sorter must also be updated to reflect those changes
        /// </summary>
        private void PoolChangedNotification()
        {
            if (SortingPolicy == EmitterSortingPolicy.None || pool.ParticleCapacity <= 0)
            {
                ParticleSorter = new ParticleSorterDefault(pool);
                return;
            }

            if (SortingPolicy == EmitterSortingPolicy.ByDepth)
            {
                GetSortIndex <Vector3> sortByDepth = value =>
                {
                    var depth = Vector3.Dot(depthSortVector, value);
                    return(depth);
                };

                ParticleSorter = new ParticleSorterCustom <Vector3>(pool, ParticleFields.Position, sortByDepth);
                return;
            }

            if (SortingPolicy == EmitterSortingPolicy.ByAge)
            {
                GetSortIndex <float> sortByAge = value => { return(-value); };

                ParticleSorter = new ParticleSorterCustom <float>(pool, ParticleFields.Life, sortByAge);
                return;
            }

            if (SortingPolicy == EmitterSortingPolicy.ByOrder)
            {
                // This sorting policy doesn't check if you actually have a Order field.
                // The ParticleSorterCustom will just skip sorting the particles if the field is invalid
                GetSortIndex <uint> sortByOrder = value => BitConverter.ToSingle(BitConverter.GetBytes(value), 0) * -1f;

                ParticleSorter = new ParticleSorterCustom <uint>(pool, ParticleFields.Order, sortByOrder);
                return;
            }

            // Default - no sorting
            ParticleSorter = new ParticleSorterDefault(pool);
        }
Пример #4
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
            {
                GetSortIndex <Vector3> sortByDepth = value => value.Z;

                var depthSorter = new ParticleSorterCustom <Vector3>(pool, ParticleFields.Position, sortByDepth);
                depthSorter.Sort();

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

            // Sort by age
            uint[] sortedAge = { 3, 1, 4, 2 }; // List of expected values
            {
                GetSortIndex <float> sortByAge = value => { return(value); };

                var ageSorter = new ParticleSorterCustom <float>(pool, ParticleFields.Life, sortByAge);
                ageSorter.Sort();

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