Exemplo n.º 1
0
        public ParticleSystem(Game game, ParticleSettings settings, Texture2D texture, Effect effect, ICamera camera)
            : base(game)
        {
            this.settings       = settings;
            this.particleEffect = effect;
            this.camera         = camera;

            rand = new Random(settings.Seed);

            EffectParameterCollection parameters = particleEffect.Parameters;

            effectViewParameter          = parameters["View"];
            effectProjectionParameter    = parameters["Projection"];
            effectViewportScaleParameter = parameters["ViewportScale"];
            effectTimeParameter          = parameters["CurrentTime"];

            parameters["Duration"].SetValue((float)settings.Duration.TotalSeconds);
            parameters["DurationRandomness"].SetValue(settings.DurationRandomness);
            parameters["Gravity"].SetValue(settings.Gravity);
            parameters["EndVelocity"].SetValue(settings.EndVelocity);
            parameters["MinColor"].SetValue(settings.MinColor.ToVector4());
            parameters["MaxColor"].SetValue(settings.MaxColor.ToVector4());

            parameters["RotateSpeed"].SetValue(new Vector2(settings.MinRotateSpeed, settings.MaxRotateSpeed));
            parameters["StartSize"].SetValue(new Vector2(settings.MinStartSize, settings.MaxStartSize));
            parameters["EndSize"].SetValue(new Vector2(settings.MinEndSize, settings.MaxEndSize));

            parameters["Texture"].SetValue(texture);

            particles = new ParticleVertex[settings.MaxParticles * 4];

            for (int i = 0; i < settings.MaxParticles; i++)
            {
                particles[i * 4 + 0].Corner = new Vector2(-1, -1);
                particles[i * 4 + 1].Corner = new Vector2(1, -1);
                particles[i * 4 + 2].Corner = new Vector2(1, 1);
                particles[i * 4 + 3].Corner = new Vector2(-1, 1);
            }

            // Create a dynamic vertex buffer.
            vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, ParticleVertex.VertexDeclaration,
                                                   settings.MaxParticles * 4, BufferUsage.WriteOnly);

            ushort[] indices = new ushort[settings.MaxParticles * 6];

            for (int i = 0; i < settings.MaxParticles; i++)
            {
                indices[i * 6 + 0] = (ushort)(i * 4 + 0);
                indices[i * 6 + 1] = (ushort)(i * 4 + 1);
                indices[i * 6 + 2] = (ushort)(i * 4 + 2);

                indices[i * 6 + 3] = (ushort)(i * 4 + 0);
                indices[i * 6 + 4] = (ushort)(i * 4 + 2);
                indices[i * 6 + 5] = (ushort)(i * 4 + 3);
            }

            indexBuffer = new IndexBuffer(GraphicsDevice, typeof(ushort), indices.Length, BufferUsage.WriteOnly);

            indexBuffer.SetData(indices);
        }
Exemplo n.º 2
0
        public Thruster(Ship parent, Vector3 pos, Vector3 dir)
            : base(parent.Game)
        {
            if (pSys == null)
            {
                var pSet = new ParticleSettings()
                {
                    BlendState = BlendState.NonPremultiplied,
                    MaxParticles = 150,
                    Duration = TimeSpan.FromSeconds(0.5),
                    DurationRandomness = 0.1f,
                    EmitterVelocitySensitivity = 0,
                    MinVelocity = 1f,
                    MaxVelocity = 2f,
                    Gravity = Vector3.Zero,
                    EndVelocity = 0,
                    MinColor = Color.White,
                    MaxColor = Color.White,
                    MinRotateSpeed = -0.1f,
                    MaxRotateSpeed = 0.1f,
                    MinStartSize = 0.25f,
                    MaxStartSize = 0.35f,
                    MinEndSize = 0.5f,
                    MaxEndSize = 0.6f
                };

                var pTex = new Texture2D(GraphicsDevice, 5, 5);
                pTex.SetData(Enumerable.Repeat(Color.FromNonPremultiplied(0, 0, 255, 125), 25).ToArray());

                var pEff = Game.Content.Load<Effect>("Shaders/Particles");

                pSys = new ParticleSystem(Game, pSet, pTex, pEff, parent.Camera);
                Game.Components.Add(pSys);
            }

            this.parent = parent;
            this.pos = pos;
            this.Direction = dir;
            this.obj = new Primitive(GraphicsDevice, 0.1f, Primitives.Sphere) { Position = pos };
            parent.Mesh.PrimitiveObjects.Add(this.obj);
            emitter = new ParticleEmitter(pSys) { Position = parent.Position + pos, Direction = dir, ParticlesPerSecond = 3 };
        }
Exemplo n.º 3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            SpriteBatchHelpers.Initialize(this.GraphicsDevice);
            Billboard.Initialize(this.GraphicsDevice, Content.Load<Effect>("Shaders/Billboarding"));

            #region Particles
            pSet = new ParticleSettings()
            {
                BlendState = BlendState.Additive
                , MaxParticles = 100
                , Duration = TimeSpan.FromSeconds(1)
                , DurationRandomness = 1
                , EmitterVelocitySensitivity = 1
                , MinXVelocity = -0.2f
                , MaxXVelocity = 0.2f
                , MinYVelocity = -0.2f
                , MaxYVelocity = 0.2f
                , Gravity = Vector3.Zero
                , EndVelocity = 0
                , MinColor = Color.White
                , MaxColor = Color.White
                , MinRotateSpeed = -0.1f
                , MaxRotateSpeed = 0.1f
                , MinStartSize = 0.2f
                , MaxStartSize = 0.3f
                , MinEndSize = 1
                , MaxEndSize = 2
            };

            var pTex = new Texture2D(GraphicsDevice, 5, 5);
            pTex.SetData(Enumerable.Repeat(Color.FromNonPremultiplied(0, 0, 255, 125), 25).ToArray());

            var pEff = Content.Load<Effect>("Shaders/Particles");

            pSys = new ParticleSystem(this, pSet, pTex, pEff, camera);
            pEmi = new ParticleEmitter(pSys) { Position = Vector3.Zero, ParticlesPerSecond = 10 };
            pEmi = new ParticleEmitter(pSys) { Position = Vector3.UnitX, ParticlesPerSecond = 10 };
            pEmi = new ParticleEmitter(pSys) { Position = Vector3.UnitY, ParticlesPerSecond = 10 };
            pEmi = new ParticleEmitter(pSys) { Position = new Vector3(Vector2.One, 0), ParticlesPerSecond = 10 };
            #endregion
        }
Exemplo n.º 4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            this.Services.AddService(typeof(SpriteBatch), spriteBatch);
            DiagnosticsManager.Initialize(this);
            SpriteBatchHelpers.Initialize(GraphicsDevice);
            Primitives.Initialize(GraphicsDevice);
            Billboard.Initialize(GraphicsDevice, Content.Load<Effect>("Shaders/Billboarding"));

            // TODO: use this.Content to load your game content here
            CreateSpheres();

            ship = new Ship(this);
            followCam = new FollowCamera(camera, ship);

            lightningTexture = new LightningTexture(GraphicsDevice, 50, 100);

            #region Particles
            pSet = new ParticleSettings()
            {
                BlendState = BlendState.Additive,
                MaxParticles = 10,
                Duration = TimeSpan.FromSeconds(0.5),
                DurationRandomness = 0.1f,
                EmitterVelocitySensitivity = 0,
                MinVelocity = 1f,
                MaxVelocity = 2f,
                Gravity = Vector3.Zero,
                EndVelocity = 0,
                MinColor = Color.White,
                MaxColor = Color.White,
                MinRotateSpeed = -0.1f,
                MaxRotateSpeed = 0.1f,
                MinStartSize = 0.25f,
                MaxStartSize = 0.35f,
                MinEndSize = 0.5f,
                MaxEndSize = 0.6f
            };

            var pTex = new Texture2D(GraphicsDevice, 5, 5);
            pTex.SetData(Enumerable.Repeat(Color.FromNonPremultiplied(0, 0, 255, 125), 25).ToArray());

            var pEff = Content.Load<Effect>("Shaders/Particles");

            pSys = new ParticleSystem(this, pSet, pTex, pEff, camera);
            pEmi = new ParticleEmitter(pSys) { Position = Vector3.UnitX, ParticlesPerSecond = 10 };
            pEmi2 = new ParticleEmitter(pSys) { Position = -Vector3.UnitX, ParticlesPerSecond = 10 };
            //pEmi = new ParticleEmitter(pSys) { Position = Vector3.UnitY, ParticlesPerSecond = 10 };
            //pEmi = new ParticleEmitter(pSys) { Position = new Vector3(Vector2.One, 0), ParticlesPerSecond = 10 };
            #endregion

            base.LoadContent();
        }