Example #1
0
        public ParticleSystem(
            ContentManager contentManager,
            ParticleSystemDefinition definition,
            GetMatrixReferenceDelegate getWorldMatrix)
        {
            Definition = definition;

            _getWorldMatrix = getWorldMatrix;

            var maxParticles = CalculateMaxParticles();

            // If this system never emits any particles, there's no reason to fully initialise it.
            if (maxParticles == 0)
            {
                return;
            }

            _graphicsDevice = contentManager.GraphicsDevice;

            _particleMaterial = AddDisposable(new ParticleMaterial(contentManager, contentManager.EffectLibrary.Particle));

            _velocityType = VelocityTypeUtility.GetImplementation(Definition.VelocityType);
            _volumeType   = VolumeTypeUtility.GetImplementation(Definition.VolumeType);

            var texturePath = Path.Combine("Art", "Textures", Definition.ParticleName);
            var texture     = contentManager.Load <Texture>(texturePath);

            _particleMaterial.SetTexture(texture);

            var blendState = GetBlendState(Definition.Shader);

            _particleMaterial.PipelineState = new EffectPipelineState(
                RasterizerStateDescriptionUtility.DefaultFrontIsCounterClockwise,
                DepthStencilStateDescription.DepthOnlyLessEqualRead,
                blendState,
                RenderPipeline.GameOutputDescription);

            _initialDelay = Definition.InitialDelay.GetRandomInt();

            _startSizeRate = Definition.StartSizeRate.GetRandomFloat();
            _startSize     = 0;

            _colorKeyframes = new List <ParticleColorKeyframe>();

            if (Definition.Color1 != null)
            {
                _colorKeyframes.Add(new ParticleColorKeyframe(Definition.Color1));
            }

            void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous)
            {
                if (keyframe != null && keyframe.Time > previous.Time)
                {
                    _colorKeyframes.Add(new ParticleColorKeyframe(keyframe));
                }
            }

            addColorKeyframe(Definition.Color2, Definition.Color1);
            addColorKeyframe(Definition.Color3, Definition.Color2);
            addColorKeyframe(Definition.Color4, Definition.Color3);
            addColorKeyframe(Definition.Color5, Definition.Color4);
            addColorKeyframe(Definition.Color6, Definition.Color5);
            addColorKeyframe(Definition.Color7, Definition.Color6);
            addColorKeyframe(Definition.Color8, Definition.Color7);

            _particles = new Particle[maxParticles];
            for (var i = 0; i < _particles.Length; i++)
            {
                _particles[i].AlphaKeyframes = new List <ParticleAlphaKeyframe>();
                _particles[i].Dead           = true;
            }

            _deadList = new List <int>();
            _deadList.AddRange(Enumerable.Range(0, maxParticles));

            var numVertices = maxParticles * 4;

            _vertexBuffer = AddDisposable(contentManager.GraphicsDevice.ResourceFactory.CreateBuffer(
                                              new BufferDescription(
                                                  (uint)(ParticleVertex.VertexDescriptor.Stride * maxParticles * 4),
                                                  BufferUsage.VertexBuffer | BufferUsage.Dynamic)));

            _vertices = new ParticleVertex[numVertices];

            _indexBuffer = AddDisposable(CreateIndexBuffer(
                                             contentManager.GraphicsDevice,
                                             maxParticles,
                                             out _numIndices));

            State = ParticleSystemState.Active;
        }
Example #2
0
        protected override void Start()
        {
            base.Start();

            _particleMaterial = new ParticleMaterial(ContentManager.EffectLibrary.Particle);

            _velocityType = VelocityTypeUtility.GetImplementation(Definition.VelocityType);
            _volumeType   = VolumeTypeUtility.GetImplementation(Definition.VolumeType);

            var texturePath = Path.Combine("Art", "Textures", Definition.ParticleName);
            var texture     = ContentManager.Load <Texture>(texturePath);

            _particleMaterial.SetTexture(texture);

            var blendState = GetBlendState(Definition.Shader);

            _particleMaterial.PipelineState = new EffectPipelineState(
                RasterizerStateDescription.CullBackSolid,
                DepthStencilStateDescription.DepthRead,
                blendState);

            _initialDelay = Definition.InitialDelay.GetRandomInt();

            _startSizeRate = Definition.StartSizeRate.GetRandomFloat();
            _startSize     = 0;

            _colorKeyframes = new List <ParticleColorKeyframe>();

            if (Definition.Color1 != null)
            {
                _colorKeyframes.Add(new ParticleColorKeyframe(Definition.Color1));
            }

            void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous)
            {
                if (keyframe != null && keyframe.Time > previous.Time)
                {
                    _colorKeyframes.Add(new ParticleColorKeyframe(keyframe));
                }
            }

            addColorKeyframe(Definition.Color2, Definition.Color1);
            addColorKeyframe(Definition.Color3, Definition.Color2);
            addColorKeyframe(Definition.Color4, Definition.Color3);
            addColorKeyframe(Definition.Color5, Definition.Color4);
            addColorKeyframe(Definition.Color6, Definition.Color5);
            addColorKeyframe(Definition.Color7, Definition.Color6);
            addColorKeyframe(Definition.Color8, Definition.Color7);

            var maxParticles = CalculateMaxParticles();

            _particles = new Particle[maxParticles];
            for (var i = 0; i < _particles.Length; i++)
            {
                _particles[i].Dead = true;
            }

            _deadList = new List <int>();
            _deadList.AddRange(Enumerable.Range(0, maxParticles));

            _vertexBuffer = Buffer <ParticleVertex> .CreateDynamicArray(
                GraphicsDevice,
                maxParticles * 4,
                BufferBindFlags.VertexBuffer);

            _vertices = new ParticleVertex[_vertexBuffer.ElementCount];

            _indexBuffer = CreateIndexBuffer(GraphicsDevice, maxParticles);

            State = ParticleSystemState.Active;
        }