Example #1
0
		public GPUParticle(GPUParticleVBA vba, int index)
		{
			_vba = vba;
			_vbaIndex = index;
		}
		private void UnloadShader()
		{
			vertexBuffer?.Dispose();
			indexBuffer?.Dispose();
			particlePool = null;
			vboArray = null;
		}
		private void LoadShader()
		{
			UnloadShader();

			geometryCount = FloatMath.Ceiling(_config.SpawnRate * _config.ParticleLifetimeMax);

			// Create Particle Pool

			particlePool = new GPUParticle[geometryCount];
			vboArray = new GPUParticleVBA(geometryCount);
			for (int i = 0; i < geometryCount; i++)
			{
				particlePool[i] = new GPUParticle(vboArray, i)
				{
					StartPosition = Position,
					Random = new Vector4(FloatMath.GetRandom(), FloatMath.GetRandom(), FloatMath.GetRandom(), FloatMath.GetRandom()),
					StartTimeOffset = Config.SpawnDelay * i
				};
				
				InitializeParticle(particlePool[i], i, geometryCount);

				particlePool[i].UpdateVBO();
			}
			
			// Create VertexBuffer and IndexBuffer

			vertexBuffer = new VertexBuffer(Owner.GraphicsDevice, GPUParticleVBO.VertexDeclaration, geometryCount * 4, BufferUsage.WriteOnly);
			vertexBuffer.SetData(vboArray.Data);

			if (geometryCount * 6 < short.MaxValue)
			{
				short[] indices = new short[geometryCount * 6];
				for (short i = 0; i < geometryCount; i++)
				{
					// TR triangle
					indices[i * 6 + 0] = (short)(i * 4 + 0);
					indices[i * 6 + 1] = (short)(i * 4 + 2);
					indices[i * 6 + 2] = (short)(i * 4 + 1);

					// BL triangle
					indices[i * 6 + 3] = (short)(i * 4 + 0);
					indices[i * 6 + 4] = (short)(i * 4 + 3);
					indices[i * 6 + 5] = (short)(i * 4 + 2);
				}
				indexBuffer = new IndexBuffer(Owner.GraphicsDevice, typeof(short), geometryCount * 6, BufferUsage.WriteOnly);
				indexBuffer.SetData(indices);
			}
			else
			{
				int[] indices = new int[geometryCount * 6];
				for (int i = 0; i < geometryCount; i++)
				{
					// TR triangle
					indices[i * 6 + 0] = (i * 4 + 0);
					indices[i * 6 + 1] = (i * 4 + 2);
					indices[i * 6 + 2] = (i * 4 + 1);

					// BL triangle
					indices[i * 6 + 3] = (i * 4 + 0);
					indices[i * 6 + 4] = (i * 4 + 3);
					indices[i * 6 + 5] = (i * 4 + 2);
				}
				indexBuffer = new IndexBuffer(Owner.GraphicsDevice, typeof(int), geometryCount * 6, BufferUsage.WriteOnly);
				indexBuffer.SetData(indices);
			}


			// Load effect

			particleEffect = Owner.Game.Content.Load<Effect>("shaders/SAMParticleEffect").Clone();

			// World Config

			parameterOffset = particleEffect.Parameters["Offset"]; 
			parameterVirtualViewport = particleEffect.Parameters["VirtualViewport"];
			parameterCurrentTime = particleEffect.Parameters["CurrentTime"];

			// Particle Config

			particleEffect.Parameters["Texture"]?.SetValue(Config.Texture.Texture);
			particleEffect.Parameters["TextureProjection"]?.SetValue(Config.Texture.GetShaderProjectionMatrix().ToMatrix());

			particleEffect.Parameters["ParticleLifetimeMin"]?.SetValue(Config.ParticleLifetimeMin);
			particleEffect.Parameters["ParticleLifetimeMax"]?.SetValue(Config.ParticleLifetimeMax);
			particleEffect.Parameters["ParticleRespawnTime"]?.SetValue(Config.ParticleRespawnTime);

			particleEffect.Parameters["ParticleSpawnAngleMin"]?.SetValue(Config.ParticleSpawnAngleMin);
			particleEffect.Parameters["ParticleSpawnAngleMax"]?.SetValue(Config.ParticleSpawnAngleMax);
			particleEffect.Parameters["ParticleSpawnAngleIsRandom"]?.SetValue(Config.ParticleSpawnAngleIsRandom);
			particleEffect.Parameters["FixedParticleSpawnAngle"]?.SetValue(Config.FixedParticleSpawnAngle);

			particleEffect.Parameters["ParticleVelocityMin"]?.SetValue(Config.ParticleVelocityMin);
			particleEffect.Parameters["ParticleVelocityMax"]?.SetValue(Config.ParticleVelocityMax);

			particleEffect.Parameters["ParticleAlphaInitial"]?.SetValue(Config.ParticleAlphaInitial);
			particleEffect.Parameters["ParticleAlphaFinal"]?.SetValue(Config.ParticleAlphaFinal);

			particleEffect.Parameters["ParticleSizeInitialMin"]?.SetValue(Config.ParticleSizeInitialMin);
			particleEffect.Parameters["ParticleSizeInitialMax"]?.SetValue(Config.ParticleSizeInitialMax);

			particleEffect.Parameters["ParticleSizeFinalMin"]?.SetValue(Config.ParticleSizeFinalMin);
			particleEffect.Parameters["ParticleSizeFinalMax"]?.SetValue(Config.ParticleSizeFinalMax);

			particleEffect.Parameters["ColorInitial"]?.SetValue(Config.ColorInitial.ToVector4(Config.ParticleAlphaInitial));
			particleEffect.Parameters["ColorFinal"]?.SetValue(Config.ColorFinal.ToVector4(Config.ParticleAlphaInitial));
		}
Example #4
0
 public GPUParticle(GPUParticleVBA vba, int index)
 {
     _vba      = vba;
     _vbaIndex = index;
 }