internal ParticleSystem( FXParticleSystemTemplate template, AssetLoadContext loadContext, GetMatrixReferenceDelegate getWorldMatrix) : this(template, loadContext) { _getWorldMatrix = getWorldMatrix; }
public ParticleSystem( ContentManager contentManager, FXParticleSystemTemplate template, GetMatrixReferenceDelegate getWorldMatrix) { Template = template; _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; _renderItemConstantsBufferVS = AddDisposable(new ConstantBuffer <MeshShaderResources.RenderItemConstantsVS>(_graphicsDevice)); _velocityType = Template.EmissionVelocity; _volumeType = Template.EmissionVolume; var texturePath = Path.Combine("Art", "Textures", Template.ParticleName); var texture = contentManager.Load <Texture>(texturePath); _particleResourceSet = AddDisposable(contentManager.ShaderResources.Particle.CreateParticleResoureSet( _renderItemConstantsBufferVS.Buffer, texture)); _shaderSet = contentManager.ShaderResources.Particle.ShaderSet; _pipeline = contentManager.ShaderResources.Particle.GetCachedPipeline(Template.Shader); _initialDelay = Template.InitialDelay.GetRandomInt(); _startSizeRate = Template.StartSizeRate.GetRandomFloat(); _startSize = 0; _colorKeyframes = new List <ParticleColorKeyframe>(); var colors = Template.Colors; if (colors.Color1 != null) { _colorKeyframes.Add(new ParticleColorKeyframe(colors.Color1)); } void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous) { if (keyframe != null && keyframe.Time > previous.Time) { _colorKeyframes.Add(new ParticleColorKeyframe(keyframe)); } } addColorKeyframe(colors.Color2, colors.Color1); addColorKeyframe(colors.Color3, colors.Color2); addColorKeyframe(colors.Color4, colors.Color3); addColorKeyframe(colors.Color5, colors.Color4); addColorKeyframe(colors.Color6, colors.Color5); addColorKeyframe(colors.Color7, colors.Color6); addColorKeyframe(colors.Color8, colors.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)(ParticleShaderResources.ParticleVertex.VertexDescriptor.Stride * maxParticles * 4), BufferUsage.VertexBuffer | BufferUsage.Dynamic))); _vertices = new ParticleShaderResources.ParticleVertex[numVertices]; _indexBuffer = AddDisposable(CreateIndexBuffer( contentManager.GraphicsDevice, maxParticles, out _numIndices)); State = ParticleSystemState.Active; _beforeRender = (cl, context) => { // Only update once we know this particle system is visible on screen. Update(cl, context.GameTime); if (_worldMatrixChanged) { _renderItemConstantsBufferVS.Update(cl); } cl.SetGraphicsResourceSet(1, _particleResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }
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; }