Beispiel #1
0
        protected virtual float GetSpread(Random rand, float sparam, float time)
        {
            var s_min = MathHelper.DegreesToRadians(MinSpread.GetValue(sparam, 0));
            var s_max = MathHelper.DegreesToRadians(MaxSpread.GetValue(sparam, 0));

            return(rand.NextFloat(s_min, s_max));
        }
        protected override void SetParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4 transform, float sparam, float globaltime)
        {
            float w     = Width.GetValue(sparam, 0) / 2;
            float h     = Height.GetValue(sparam, 0) / 2;
            float d     = Depth.GetValue(sparam, 0) / 2;
            float s_min = MathHelper.DegreesToRadians(MinSpread.GetValue(sparam, 0));
            float s_max = MathHelper.DegreesToRadians(MaxSpread.GetValue(sparam, 0));

            var pos = new Vector3(
                FxRandom.NextFloat(-w, w),
                FxRandom.NextFloat(-h, h),
                FxRandom.NextFloat(-d, d)
                );
            var n = RandomInCone(s_min, s_max);
            //var tr = Transform.GetMatrix(sparam, globaltime);
            //var tr = Matrix4.Identity;
            //n = (tr * new Vector4(n.Normalized(), 0)).Xyz.Normalized();
            Vector3    translate;
            Quaternion rotate;

            if (DoTransform(reference, sparam, globaltime, out translate, out rotate))
            {
                pos += translate;
                n    = rotate * n;
            }
            var pr = pos;

            instance.Pool.Particles[idx].Position = pr;
            instance.Pool.Particles [idx].Normal  = n * Pressure.GetValue(sparam, 0);
        }
Beispiel #3
0
        protected override void SetParticle(int idx, ParticleEffect fx, ParticleEffectInstance instance, ref Matrix4 transform, float sparam)
        {
            float w     = Width.GetValue(sparam, 0) / 2;
            float h     = Height.GetValue(sparam, 0) / 2;
            float d     = Depth.GetValue(sparam, 0) / 2;
            float s_min = MathHelper.DegreesToRadians(MinSpread.GetValue(sparam, 0));
            float s_max = MathHelper.DegreesToRadians(MaxSpread.GetValue(sparam, 0));

            var pos = new Vector3(
                instance.Random.NextFloat(-w, w),
                instance.Random.NextFloat(-h, h),
                instance.Random.NextFloat(-d, d)
                );
            //var angle = instance.Random.NextFloat(s_min, s_max);

            /*var n = new Vector3(
             *      (float)(Math.Cos(angle)),
             *      1,
             *      (float)(Math.Sin(angle))
             * );*/
            var n  = RandomInCone(instance.Random, s_min, s_max);
            var tr = Transform.GetMatrix(sparam, 0);

            n = (tr * new Vector4(n.Normalized(), 0)).Xyz.Normalized();
            var pr = pos;

            instance.Particles[idx].Position = pr;
            instance.Particles [idx].Normal  = n * Pressure.GetValue(sparam, 0);
        }
        protected override void SetParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4x4 transform, float sparam, float globaltime)
        {
            var r_min = MinRadius.GetValue(sparam, 0);
            var r_max = MaxRadius.GetValue(sparam, 0);

            var radius = FxRandom.NextFloat(r_min, r_max);

            var p = new Vector3(
                FxRandom.NextFloat(-1, 1),
                FxRandom.NextFloat(-1, 1),
                FxRandom.NextFloat(-1, 1)
                );

            p.Normalize();
            var        n = p;
            Vector3    translate;
            Quaternion rotate;

            if (DoTransform(reference, sparam, globaltime, out translate, out rotate))
            {
                p += translate;
                n  = Vector3.Transform(n, rotate);
            }
            n *= Pressure.GetValue(sparam, 0);
            var pr = p * radius;

            instance.Pool.Particles[idx].Position = pr;
            instance.Pool.Particles[idx].Normal   = n;
        }
        protected override void SetParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4 transform, float sparam, float globaltime)
        {
            var r_min = MinRadius.GetValue(sparam, 0);
            var r_max = MaxRadius.GetValue(sparam, 0);

            var radius = instance.Random.NextFloat(r_min, r_max);

            var p = new Vector3(
                instance.Random.NextFloat(-1, 1),
                instance.Random.NextFloat(-1, 1),
                instance.Random.NextFloat(-1, 1)
                );

            p.Normalize();
            var direction = p;

            var tr = Transform.GetMatrix(sparam, globaltime);
            var n  = (tr * new Vector4(direction, 0)).Xyz.Normalized();

            n *= Pressure.GetValue(sparam, 0);
            var pr = p * radius;

            instance.Particles[idx].Position = pr;
            instance.Particles[idx].Normal   = n;
        }
Beispiel #6
0
        public override void Update(NodeReference reference, ParticleEffectInstance instance, TimeSpan delta, ref Matrix4 transform, float sparam)
        {
            if (reference.Paired.Count == 0)
            {
                return;
            }
            if (NodeLifeSpan < instance.GlobalTime)
            {
                return;
            }
            if (reference.Paired[0].Node.NodeLifeSpan < instance.GlobalTime)
            {
                return;
            }
            var maxCount = 1000;
            //var maxCount = MaxParticles == null ? int.MaxValue : (int)Math.Ceiling(MaxParticles.GetValue(sparam, 0f));
            var freq    = Frequency == null ? 0f : Frequency.GetValue(sparam, 0f);
            var spawnMs = freq <= 0 ? 0 : 1 / (double)freq;
            var state   = instance.GetEmitterState(reference);

            if (state.ParticleCount >= maxCount)
            {
                return;
            }
            int j = 0;

            if (spawnMs > 0)
            {
                //Spawn lots of particles
                var dt = Math.Min(delta.TotalSeconds, 1);                 //don't go crazy during debug pauses
                while (true)
                {
                    if (state.SpawnTimer < dt)
                    {
                        dt -= state.SpawnTimer;
                        state.SpawnTimer = spawnMs;
                    }
                    else
                    {
                        state.SpawnTimer -= dt;
                        break;
                    }
                    if (state.ParticleCount + 1 <= maxCount)
                    {
                        var idx = instance.GetNextFreeParticle();
                        if (idx == -1)
                        {
                            return;
                        }
                        j++;
                        state.ParticleCount++;
                        SpawnParticle(idx, reference, instance, ref transform, sparam, (float)instance.GlobalTime);
                        var app = (FxAppearance)reference.Paired[0].Node;
                        app.OnParticleSpawned(idx, instance.Pool.Particles[idx].Appearance, instance);
                    }
                }
            }
        }
Beispiel #7
0
 protected void SpawnParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4 transform, float sparam)
 {
     instance.Particles[idx].Active     = true;
     instance.Particles[idx].Emitter    = reference;
     instance.Particles[idx].Appearance = reference.Paired[0];
     instance.Particles[idx].TimeAlive  = 0f;
     instance.Particles[idx].LifeSpan   = InitLifeSpan.GetValue(sparam, 0f);
     SetParticle(idx, reference, instance, ref transform, sparam);
 }
Beispiel #8
0
 protected void SpawnParticle(int idx, ParticleEffect fx, ParticleEffectInstance instance, ref Matrix4 transform, float sparam)
 {
     instance.Particles[idx].Active     = true;
     instance.Particles[idx].Emitter    = this;
     instance.Particles[idx].Appearance = (FxAppearance)fx.Pairs[this][0];
     instance.Particles[idx].TimeAlive  = 0f;
     instance.Particles[idx].LifeSpan   = InitLifeSpan.GetValue(sparam, 0f);
     SetParticle(idx, fx, instance, ref transform, sparam);
 }
        protected float GetFrame(float globaltime, float sparam, ref Particle particle)
        {
            float frame = 0;

            if (UseCommonAnimation)
            {
                frame = CommonAnimation.GetValue(sparam, globaltime);
            }
            else
            {
                frame = Animation.GetValue(sparam, particle.TimeAlive / particle.LifeSpan);
            }
            return(MathHelper.Clamp(frame, 0, 1));
        }
Beispiel #10
0
        public override void Update(ParticleEffect fx, ParticleEffectInstance instance, TimeSpan delta, ref Matrix4 transform, float sparam)
        {
            if (!fx.Pairs.ContainsKey(this))
            {
                return;
            }
            var maxCount = MaxParticles == null ? int.MaxValue : (int)Math.Ceiling(MaxParticles.GetValue(sparam, 0f));
            var freq     = Frequency == null ? 0f : Frequency.GetValue(sparam, 0f);
            var spawnMs  = freq <= 0 ? 0 : 1 / (double)freq;
            var state    = instance.GetEmitterState(this);

            if (state.ParticleCount >= maxCount)
            {
                return;
            }
            if (spawnMs > 0)
            {
                //Spawn lots of particles
                var dt = Math.Min(delta.TotalSeconds, 1);                 //don't go crazy during debug pauses
                while (true)
                {
                    if (state.SpawnTimer < dt)
                    {
                        dt -= state.SpawnTimer;
                        state.SpawnTimer = spawnMs;
                    }
                    else
                    {
                        state.SpawnTimer -= dt;
                        break;
                    }
                    if (state.ParticleCount + 1 <= maxCount)
                    {
                        var idx = instance.GetNextFreeParticle();
                        if (idx == -1)
                        {
                            return;
                        }
                        state.ParticleCount++;
                        SpawnParticle(idx, fx, instance, ref transform, sparam);
                        instance.Particles[idx].Appearance.OnParticleSpawned(idx, instance);
                    }
                }
            }
        }
Beispiel #11
0
        protected override void SetParticle(int idx, ParticleEffect fx, ParticleEffectInstance instance, ref Matrix4 transform, float sparam)
        {
            var r_min = MinRadius.GetValue(sparam, 0);
            var r_max = MaxRadius.GetValue(sparam, 0);

            var   radius = instance.Random.NextFloat(r_min, r_max);
            float s_min  = MathHelper.DegreesToRadians(MinSpread.GetValue(sparam, 0));
            float s_max  = MathHelper.DegreesToRadians(MaxSpread.GetValue(sparam, 0));

            var direction = RandomInCone(instance.Random, s_min, s_max);
            var tr        = Transform.GetMatrix(sparam, 0);
            var n         = (tr * new Vector4(direction.Normalized(), 0)).Xyz.Normalized();
            var p         = n * radius;

            n *= Pressure.GetValue(sparam, 0);
            instance.Particles[idx].Position = p;
            instance.Particles[idx].Normal   = n;
        }
Beispiel #12
0
 protected void SpawnParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4 transform, float sparam, float globaltime)
 {
     instance.Particles[idx].Active      = true;
     instance.Particles[idx].Emitter     = reference;
     instance.Particles[idx].Appearance  = reference.Paired[0];
     instance.Particles[idx].TimeAlive   = 0f;
     instance.Particles[idx].LifeSpan    = InitLifeSpan.GetValue(sparam, 0f);
     instance.Particles[idx].Orientation = Quaternion.Identity;
     SetParticle(idx, reference, instance, ref transform, sparam, globaltime);
     if (reference.Paired[0].Parent == null)
     {
         instance.Particles[idx].Position = VectorMath.Transform(
             instance.Particles[idx].Position, transform);
         var len         = instance.Particles[idx].Normal.Length;
         var nr          = instance.Particles[idx].Normal.Normalized();
         var transformed = (transform * new Vector4(nr, 0)).Xyz.Normalized();
         instance.Particles[idx].Normal = transformed * len;
     }
 }
Beispiel #13
0
        protected override void SetParticle(int idx, NodeReference reference, ParticleEffectInstance instance, ref Matrix4 transform, float sparam, float globaltime)
        {
            var r_min = MinRadius.GetValue(sparam, 0);
            var r_max = MaxRadius.GetValue(sparam, 0);

            var   radius = instance.Random.NextFloat(r_min, r_max);
            float s_min  = MathHelper.DegreesToRadians(MinSpread.GetValue(sparam, 0));
            float s_max  = MathHelper.DegreesToRadians(MaxSpread.GetValue(sparam, 0));

            var        n = RandomInCone(instance.Random, s_min, s_max);
            Vector3    translate;
            Quaternion rotate;

            if (DoTransform(reference, sparam, globaltime, out translate, out rotate))
            {
                n = rotate * n;
            }
            var p = n * radius + translate;

            n *= Pressure.GetValue(sparam, 0);
            instance.Particles[idx].Position = p;
            instance.Particles[idx].Normal   = n;
        }
        protected void HandleTexture(
            ResourceManager res,
            float globaltime,
            float sparam,
            ref Particle particle,
            out Texture2D tex2d,
            out Vector2 tl,
            out Vector2 tr,
            out Vector2 bl,
            out Vector2 br
            )
        {
            //Initial texcoords
            tl = new Vector2(0, 0);
            tr = new Vector2(1, 0);
            bl = new Vector2(0, 1);
            br = new Vector2(1, 1);
            if (Texture == null)
            {
                tex2d = res.NullTexture;
                return;
            }
            //Get the Texture2D
            if (_tex == null && _frame == null && _tex2D != null)
            {
                if (_tex2D == null || _tex2D.IsDisposed)
                {
                    _tex2D = res.FindTexture(Texture) as Texture2D;
                }
                tex2d = _tex2D;
            }
            else if (_tex == null)
            {
                if (res.TryGetShape(Texture, out _tex))
                {
                    _tex2D = (Texture2D)res.FindTexture(_tex.Texture);
                }
                else if (res.TryGetFrameAnimation(Texture, out _frame))
                {
                    _tex2D = res.FindTexture(Texture + "_0") as Texture2D;
                }
                else
                {
                    _tex2D = res.FindTexture(Texture) as Texture2D;
                }
            }
            if (_tex2D == null || _tex2D.IsDisposed)
            {
                if (_frame == null)
                {
                    _tex2D = res.FindTexture(_tex == null ? Texture : _tex.Texture) as Texture2D;
                }
                else
                {
                    _tex2D = res.FindTexture(Texture + "_0") as Texture2D;
                }
            }
            tex2d = _tex2D;
            if (tex2d == null)
            {
                tex2d = (Texture2D)res.FindTexture(ResourceManager.WhiteTextureName);
            }
            //Shape?
            if (_tex != null)
            {
                tl = new Vector2(_tex.Dimensions.X, _tex.Dimensions.Y);
                tr = new Vector2(_tex.Dimensions.X + _tex.Dimensions.Width, _tex.Dimensions.Y);
                bl = new Vector2(_tex.Dimensions.X, _tex.Dimensions.Y + _tex.Dimensions.Height);
                br = new Vector2(_tex.Dimensions.X + _tex.Dimensions.Width, _tex.Dimensions.Y + _tex.Dimensions.Height);
            }
            else if (_frame != null)
            {
                float frame = 0;
                if (UseCommonAnimation)
                {
                    frame = CommonAnimation.GetValue(sparam, globaltime);
                }
                else
                {
                    frame = Animation.GetValue(sparam, particle.TimeAlive / particle.LifeSpan);
                }
                frame = MathHelper.Clamp(frame, 0, 1);
                var frameNo = (int)Math.Floor(frame / _frame.FrameCount);
                var rect    = _frame.Frames[frameNo];
                tl = new Vector2(rect.X, rect.Y);
                tr = new Vector2(rect.X + rect.Width, rect.Y);
                bl = new Vector2(rect.X, rect.Y + rect.Height);
                br = new Vector2(rect.X + rect.Width, rect.Y + rect.Height);
            }

            //Flip
            if (FlipHorizontal)
            {
                tl.X = 1 - tl.X;
                tr.X = 1 - tl.X;
                bl.X = 1 - bl.X;
                br.X = 1 - br.X;
            }
            if (FlipVertical)
            {
                tl.Y = 1 - tl.Y;
                tr.Y = 1 - tr.Y;
                bl.Y = 1 - bl.Y;
                br.Y = 1 - br.Y;
            }
        }
Beispiel #15
0
        public override void Update(NodeReference reference, ParticleEffectInstance instance, double delta, ref Matrix4x4 transform, float sparam)
        {
            if (reference.Paired.Count == 0)
            {
                return;
            }
            if (NodeLifeSpan < instance.GlobalTime)
            {
                return;
            }
            //if (reference.Paired[0].Node.NodeLifeSpan < instance.GlobalTime) return;
            var maxCount = MaxParticles == null ? int.MaxValue : (int)Math.Ceiling(MaxParticles.GetValue(sparam, (float)instance.GlobalTime));
            var freq     = Frequency == null ? 0f : Frequency.GetValue(sparam, (float)instance.GlobalTime);
            var spawnMs  = freq <= 0 ? 0 : 1 / (double)freq;
            int j        = 0;
            var count    = instance.ParticleCounts[reference.EmitterIndex];

            if (spawnMs > 0)
            {
                if (instance.SpawnTimers[reference.EmitterIndex] > spawnMs)
                {
                    instance.SpawnTimers[reference.EmitterIndex] = spawnMs;
                }
                //Spawn lots of particles
                var dt = Math.Min(delta, 1);                 //don't go crazy during debug pauses
                while (true)
                {
                    if (instance.SpawnTimers[reference.EmitterIndex] < dt)
                    {
                        dt -= instance.SpawnTimers[reference.EmitterIndex];
                        instance.SpawnTimers[reference.EmitterIndex] = spawnMs;
                    }
                    else
                    {
                        instance.SpawnTimers[reference.EmitterIndex] -= dt;
                        break;
                    }
                    if (count < maxCount)
                    {
                        var idx = instance.GetNextFreeParticle();
                        if (idx == -1)
                        {
                            return;
                        }
                        j++;
                        SpawnParticle(idx, reference, instance, ref transform, sparam, (float)instance.GlobalTime);
                        var app = (FxAppearance)reference.Paired[0].Node;
                        app.OnParticleSpawned(idx, instance.Pool.Particles[idx].Appearance, instance);
                        //Simulate time already alive (TODO fix the time loop properly)
                        instance.Pool.Particles[idx].TimeAlive = (float)dt;
                        instance.Pool.Particles[idx].Position += instance.Pool.Particles[idx].Normal * (float)dt;
                        count++;
                    }
                }
            }
            else
            {
                instance.SpawnTimers[reference.EmitterIndex] = 0;
            }
            instance.ParticleCounts[reference.EmitterIndex] = count;
        }
Beispiel #16
0
 protected void HandleTexture(
     ResourceManager res,
     float globaltime,
     float sparam,
     ref Particle particle,
     out Texture2D tex2d,
     out Vector2 tl,
     out Vector2 tr,
     out Vector2 bl,
     out Vector2 br
     )
 {
     //Initial texcoords
     tl = new Vector2(0, 0);
     tr = new Vector2(1, 0);
     bl = new Vector2(0, 1);
     br = new Vector2(1, 1);
     //Get texture
     GetTexture2D(res, out tex2d);
     //Shape?
     if (_tex != null)
     {
         tl = new Vector2(_tex.Dimensions.X, _tex.Dimensions.Y);
         tr = new Vector2(_tex.Dimensions.X + _tex.Dimensions.Width, _tex.Dimensions.Y);
         bl = new Vector2(_tex.Dimensions.X, _tex.Dimensions.Y + _tex.Dimensions.Height);
         br = new Vector2(_tex.Dimensions.X + _tex.Dimensions.Width, _tex.Dimensions.Y + _tex.Dimensions.Height);
     }
     else if (_frame != null)
     {
         float frame = 0;
         if (UseCommonAnimation)
         {
             frame = CommonAnimation.GetValue(sparam, globaltime);
         }
         else
         {
             frame = Animation.GetValue(sparam, particle.TimeAlive / particle.LifeSpan);
         }
         frame = MathHelper.Clamp(frame, 0, 1);
         var frameNo = (int)Math.Floor(frame / _frame.FrameCount);
         var rect    = _frame.Frames[frameNo];
         tl = new Vector2(rect.X, rect.Y);
         tr = new Vector2(rect.X + rect.Width, rect.Y);
         bl = new Vector2(rect.X, rect.Y + rect.Height);
         br = new Vector2(rect.X + rect.Width, rect.Y + rect.Height);
     }
     //Flip
     if (FlipHorizontal)
     {
         tl.X = 1 - tl.X;
         tr.X = 1 - tl.X;
         bl.X = 1 - bl.X;
         br.X = 1 - br.X;
     }
     if (FlipVertical)
     {
         tl.Y = 1 - tl.Y;
         tr.Y = 1 - tr.Y;
         bl.Y = 1 - bl.Y;
         br.Y = 1 - br.Y;
     }
 }