예제 #1
0
        // Initialize the color for the new particles.
        private void InitColor(EntityManager mgr, Entity emitter, NativeArray <Entity> newParticles)
        {
            bool hasInitialColor  = mgr.HasComponent <EmitterInitialColor>(emitter);
            bool hasLifetimeColor = mgr.HasComponent <LifetimeColor>(emitter);

            if (!hasInitialColor && !hasLifetimeColor)
            {
                return;
            }

            if (hasLifetimeColor)
            {
                var lifetimeColor = mgr.GetComponentData <LifetimeColor>(emitter);

                if (hasInitialColor)
                {
                    // LifetimeColor and EmitterInitialColor are present.

                    var initialColor = mgr.GetComponentData <EmitterInitialColor>(emitter);

                    foreach (var particle in newParticles)
                    {
                        var renderer    = mgr.GetComponentData <Sprite2DRenderer>(particle);
                        var randomColor = InterpolationService.EvaluateCurveColor(
                            mgr, m_rand.NextFloat(), initialColor.curve);
                        var color = renderer.color * randomColor;
                        mgr.AddComponentData(particle, new ParticleLifetimeColor()
                        {
                            initialColor = color
                        });
                    }
                }
                else
                {
                    // Only LifetimeColor is present.
                    foreach (var particle in newParticles)
                    {
                        var rendererColor = mgr.GetComponentData <Sprite2DRenderer>(particle).color;
                        mgr.AddComponentData(particle, new ParticleLifetimeColor()
                        {
                            initialColor = rendererColor
                        });
                    }
                }
            }
            else if (hasInitialColor)
            {
                // Only EmitterInitialColor is present.

                var initialColor = mgr.GetComponentData <EmitterInitialColor>(emitter);

                foreach (var particle in newParticles)
                {
                    var renderer    = mgr.GetComponentData <Sprite2DRenderer>(particle);
                    var randomColor = InterpolationService.EvaluateCurveColor(mgr, m_rand.NextFloat(), initialColor.curve);
                    renderer.color = renderer.color * randomColor;
                    mgr.SetComponentData(particle, renderer);
                }
            }
        }
        private void calculate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MethodContext        context = this.GetMethodContext();
                IInterpolationMethod m       = InterpolationService.GetInterpolationMethod(method.Text);

                result.Text = m.Calculate(context).ToString();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
        public void GetInterpolation_OneDayOneRecord_ReturnMeasurmentValue()
        {
            var service = new InterpolationService();
            var data    = new List <Measurment>()
            {
                new Measurment {
                    Date = DateTime.Today, Value = 10
                }
            };

            var interpolations = service.GetInterpolationForInterval(DateTime.Today, DateTime.Today, TimeSpan.FromDays(1), data);
            var enumerable     = interpolations as Interpolation[] ?? interpolations.ToArray();

            Assert.Equal(1, enumerable.Count());
            Assert.Equal(10, enumerable.First().Value);
        }
예제 #4
0
        private void UpdateParticleColor(EntityManager mgr, float deltaTime)
        {
            Entities.ForEach((ref Particle cParticle, ref Sprite2DRenderer cRenderer, ref ParticleLifetimeColor cLifetimeColor, ref ParticleEmitterReference cEmitterRef) =>
            {
                if (!mgr.HasComponent <LifetimeColor>(cEmitterRef.emitter))
                {
                    return;
                }

                var lifetimeColor = mgr.GetComponentData <LifetimeColor>(cEmitterRef.emitter);

                float normalizedLife = cParticle.time / cParticle.lifetime;

                var color       = InterpolationService.EvaluateCurveColor(mgr, normalizedLife, lifetimeColor.curve);
                cRenderer.color = cLifetimeColor.initialColor * color;
            });
        }
예제 #5
0
        private void UpdateParticleScale(EntityManager mgr, float deltaTime)
        {
            Entities.ForEach((ref Particle cParticle, ref ParticleLifetimeScale cLifetimeScale, ref NonUniformScale cLocalScale, ref ParticleEmitterReference cEmitterRef) =>
            {
                if (!mgr.HasComponent <LifetimeScale>(cEmitterRef.emitter))
                {
                    return;
                }

                var lifetimeScale = mgr.GetComponentData <LifetimeScale>(cEmitterRef.emitter);

                float normalizedLife = cParticle.time / cParticle.lifetime;

                var scale         = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetimeScale.curve);
                cLocalScale.Value = cLifetimeScale.initialScale * scale;
            });
        }
예제 #6
0
        private void UpdateParticlePosition(EntityManager mgr, float deltaTime)
        {
            float normalizedLife      = 0.0f;
            bool  hasLifetimeVelocity = false;
            bool  hasLifetimeSpeed    = false;

            Entities.ForEach(
                (ref Particle cParticle, ref ParticleVelocity cVelocity, ref Translation cLocalPos,
                 ref ParticleEmitterReference cEmitterRef) =>
            {
                hasLifetimeVelocity = mgr.HasComponent <LifetimeVelocity>(cEmitterRef.emitter);
                hasLifetimeSpeed    = mgr.HasComponent <LifetimeSpeedMultiplier>(cEmitterRef.emitter);
                normalizedLife      = cParticle.time / cParticle.lifetime;

                if (hasLifetimeVelocity && hasLifetimeSpeed)
                {
                    var lifetimeVel   = mgr.GetComponentData <LifetimeVelocity>(cEmitterRef.emitter);
                    var lifetimeSpeed = mgr.GetComponentData <LifetimeSpeedMultiplier>(cEmitterRef.emitter);

                    var velocity = InterpolationService.EvaluateCurveFloat3(mgr, normalizedLife, lifetimeVel.curve);
                    var speed    = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetimeSpeed.curve);

                    cLocalPos.Value += (cVelocity.velocity + velocity) * speed * deltaTime;
                }
                else if (hasLifetimeVelocity)
                {
                    var lifetimeVel = mgr.GetComponentData <LifetimeVelocity>(cEmitterRef.emitter);

                    var velocity     = InterpolationService.EvaluateCurveFloat3(mgr, normalizedLife, lifetimeVel.curve);
                    cLocalPos.Value += (cVelocity.velocity + velocity) * deltaTime;
                }
                else if (hasLifetimeSpeed)
                {
                    var lifetimeSpeed = mgr.GetComponentData <LifetimeSpeedMultiplier>(cEmitterRef.emitter);

                    var speed        = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetimeSpeed.curve);
                    cLocalPos.Value += cVelocity.velocity * speed * deltaTime;
                }
                else
                {
                    cLocalPos.Value += cVelocity.velocity * deltaTime;
                }
            });
        }
예제 #7
0
        private void UpdateParticleScale(EntityManager mgr, float deltaTime)
        {
            Entities.ForEach((ref Particle cParticle, ref ParticleLifetimeScale cLifetimeScale, ref NonUniformScale cLocalScale, ref ParticleEmitterReference cEmitterRef) =>
            {
                var emitter      = cEmitterRef.emitter;
                var randomFactor = cLifetimeScale.randomFactor;

                if (mgr.HasComponent <LifetimeScale>(emitter))
                {
                    var lifetimeScale = mgr.GetComponentData <LifetimeScale>(emitter);

                    float normalizedLife = cParticle.time / cParticle.lifetime;

                    var scale = InterpolationService.EvaluateMinMaxCurve(mgr, normalizedLife, lifetimeScale.curve, randomFactor);
                    //Debug.Log($"UpdateParticleScale: {scale}, {cLifetimeScale.initialScale}");
                    cLocalScale.Value = cLifetimeScale.initialScale * scale;
                }
            });
        }
        public void GetInterpolation_IntervalBetweenTwoDates()
        {
            var service = new InterpolationService();
            var data    = new List <Measurment>()
            {
                new Measurment {
                    Date = DateTime.Today, Value = 10
                },
                new Measurment {
                    Date = DateTime.Today.AddDays(2), Value = 20
                },
            };

            var interpolations = service.GetInterpolationForInterval(DateTime.Today, DateTime.Today.AddDays(2), TimeSpan.FromDays(1), data);
            var enumerable     = interpolations as Interpolation[] ?? interpolations.ToArray();

            Assert.Collection(enumerable,
                              _ => Assert.Equal(10, _.Value),
                              _ => Assert.Equal(15, _.Value),
                              _ => Assert.Equal(20, _.Value)
                              );
        }
예제 #9
0
        private void UpdateParticleRotation(EntityManager mgr, float deltaTime)
        {
            Entities.ForEach(
                (ref Particle cParticle, ref Rotation cLocalRotation, ref ParticleAngularVelocity cAngularVelocity,
                 ref ParticleEmitterReference cEmitterRef) =>
            {
                if (mgr.HasComponent <LifetimeAngularVelocity>(cEmitterRef.emitter))
                {
                    var lifetime = mgr.GetComponentData <LifetimeAngularVelocity>(cEmitterRef.emitter);

                    float normalizedLife = cParticle.time / cParticle.lifetime;

                    var angularVelocity  = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetime.curve);
                    float angle          = (cAngularVelocity.angularVelocity + angularVelocity) * deltaTime;
                    cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.Euler(0, 0, angle));
                }
                else
                {
                    float angle          = cAngularVelocity.angularVelocity * deltaTime;
                    cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.Euler(0, 0, angle));
                }
            });
        }
예제 #10
0
        private void UpdateParticleRotation(EntityManager mgr, float deltaTime)
        {
            Entities.ForEach(
                (ref Particle cParticle, ref Rotation cLocalRotation, ref ParticleAngularVelocity cAngularVelocity,
                 ref ParticleEmitterReference cEmitterRef) =>
            {
                var emitter          = cEmitterRef.emitter;
                float normalizedLife = cParticle.time / cParticle.lifetime;
                var angularVelocity  = cAngularVelocity.angularVelocity;

                if (mgr.HasComponent <LifetimeAngularVelocity>(emitter))
                {
                    var module = mgr.GetComponentData <LifetimeAngularVelocity>(emitter);

                    var deltaAngularVelocity = InterpolationService.EvaluateMinMaxCurve(mgr, normalizedLife, module.curve);
                    angularVelocity         += deltaAngularVelocity;
                }
                if (mgr.HasComponent <RotationBySpeed>(emitter) && mgr.HasComponent <ParticleVelocity>(emitter))
                {
                    var velocity = mgr.GetComponentData <ParticleVelocity>(emitter);
                    var speed    = velocity.finalSpeed;
                    var module   = mgr.GetComponentData <RotationBySpeed>(emitter);
                    var mmc      = module.curve;
                    float deltaAngularVelocity = 0f;
                    if (mmc.mode == (int)MinMaxCurveMode.Constant || mmc.mode == (int)MinMaxCurveMode.TwoConstants)
                    {
                        deltaAngularVelocity  = InterpolationService.EvaluateMinMaxCurve(mgr, 1f, module.curve);
                        deltaAngularVelocity *= speed;
                    }
                    else
                    {
                        var min = module.range.start;
                        var max = module.range.end;
                        if (speed < min)
                        {
                            normalizedLife = 0;
                        }
                        else if (speed >= max)
                        {
                            normalizedLife = 1;
                        }
                        else
                        {
                            normalizedLife = (speed - min) / (max - min);
                        }
                        deltaAngularVelocity = InterpolationService.EvaluateMinMaxCurve(mgr, normalizedLife, module.curve);
                    }
                    angularVelocity += deltaAngularVelocity;
                }

                float angle          = (angularVelocity) * deltaTime;
                cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.AxisAngle(cAngularVelocity.axis, angle));

                //if (mgr.HasComponent<LifetimeAngularVelocity>(cEmitterRef.emitter))
                //{
                //    var lifetime = mgr.GetComponentData<LifetimeAngularVelocity>(cEmitterRef.emitter);

                //    float normalizedLife = cParticle.time / cParticle.lifetime;

                //    var angularVelocity = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetime.curve);
                //    float angle = (cAngularVelocity.angularVelocity + angularVelocity) * deltaTime;
                //    cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.AxisAngle(cAngularVelocity.axis, angle));
                //    //cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.Euler(0, 0, angle));
                //}
                //else
                //{
                //    float angle = cAngularVelocity.angularVelocity * deltaTime;
                //    cLocalRotation.Value = math.mul(cLocalRotation.Value, quaternion.Euler(0, 0, angle));
                //}
            });
        }
예제 #11
0
        private void UpdateParticlePosition(EntityManager mgr, float deltaTime)
        {
            float normalizedLife = 0.0f;

            Entities.ForEach(
                (ref Particle cParticle, ref ParticleVelocity cVelocity, ref Translation cLocalPos,
                 ref ParticleEmitterReference cEmitterRef) =>
            {
                var emitter      = cEmitterRef.emitter;
                normalizedLife   = cParticle.time / cParticle.lifetime;
                var randomFactor = cVelocity.randomFactor;
                //Debug.Log($"UpdateParticlePosition: {randomFactor}");

                var velocity   = cVelocity.velocity;  // float3.zero;
                var speedMulti = cVelocity.speedMultiplier;

                if (mgr.HasComponent <LifetimeVelocity>(emitter))
                {
                    var lifetimeVel   = mgr.GetComponentData <LifetimeVelocity>(emitter);
                    var deltaVelocity = InterpolationService.EvaluateMinMaxCurve3(mgr, normalizedLife, lifetimeVel.curveX, lifetimeVel.curveY, lifetimeVel.curveZ, randomFactor);
                    //Debug.Log($"UpdateParticlePosition: {deltaVelocity}, {lifetimeVel.curveX}, {lifetimeVel.curveY}, {lifetimeVel.curveZ}");

                    //var deltaVelocity = InterpolationService.EvaluateCurveFloat3(mgr, normalizedLife, lifetimeVel.curve);

                    if (cVelocity.isWorld)
                    {
                        deltaVelocity = math.mul(cVelocity.startRotation, deltaVelocity);
                    }

                    velocity += deltaVelocity;
                }
                if (mgr.HasComponent <LifetimeForce>(emitter))
                {
                    var lifetimeVel   = mgr.GetComponentData <LifetimeForce>(emitter);
                    var deltaVelocity = InterpolationService.EvaluateMinMaxCurve3(mgr, normalizedLife, lifetimeVel.curveX, lifetimeVel.curveY, lifetimeVel.curveZ, randomFactor);

                    deltaVelocity = deltaVelocity * deltaTime;
                    if (cVelocity.isWorld && lifetimeVel.spaceType == 0)
                    {
                        deltaVelocity = math.mul(cVelocity.startRotation, deltaVelocity);
                    }
                    else if (!cVelocity.isWorld && lifetimeVel.spaceType == 1)
                    {
                        deltaVelocity = math.mul(cVelocity.startRotationInverse, deltaVelocity);
                    }

                    velocity           += deltaVelocity;
                    cVelocity.velocity += deltaVelocity;
                }
                if (mgr.HasComponent <LifetimeSpeedMultiplier>(emitter))
                {
                    var lifetimeSpeed = mgr.GetComponentData <LifetimeSpeedMultiplier>(emitter);
                    var sm            = InterpolationService.EvaluateMinMaxCurve(mgr, normalizedLife, lifetimeSpeed.curve, randomFactor);
                    velocity         *= sm;
                }
                if (mgr.HasComponent <LifetimeLimitVelocity>(emitter))
                {
                    var lifetimeLimitVel = mgr.GetComponentData <LifetimeLimitVelocity>(emitter);
                    var limitSpeed       = InterpolationService.EvaluateMinMaxCurve(mgr, normalizedLife, lifetimeLimitVel.curve, randomFactor);
                    var speed            = math.length(velocity);
                    var speedResult      = speed * speedMulti;
                    if (speedResult >= limitSpeed)
                    {
                        speedResult = math.lerp(speedResult, limitSpeed, lifetimeLimitVel.dampen);
                        speedMulti  = speedResult / speed;
                        cVelocity.speedMultiplier = speedMulti;
                    }
                    //Debug.Log($"{velocity}, {speed}, {speedMulti}, {speedResult}, {limitSpeed}");
                }

                //velocity = (cVelocity.velocity + velocity) * speed;
                velocity        *= speedMulti;
                cLocalPos.Value += velocity * deltaTime;

                cVelocity.finalVelocity = velocity;
                cVelocity.finalSpeed    = math.length(velocity);

                //if (hasLifetimeVelocity && hasLifetimeSpeed)
                //{
                //    var lifetimeVel = mgr.GetComponentData<LifetimeVelocity>(cEmitterRef.emitter);
                //    var lifetimeSpeed = mgr.GetComponentData<LifetimeSpeedMultiplier>(cEmitterRef.emitter);

                //    var velocity = InterpolationService.EvaluateCurveFloat3(mgr, normalizedLife, lifetimeVel.curve);
                //    var speed = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetimeSpeed.curve);

                //    if(cVelocity.isWorld)
                //        velocity = math.mul(cVelocity.startRotation, velocity);

                //    velocity = (cVelocity.velocity + velocity) * speed;

                //    //Debug.Log($"{velocity}, {speed}, {cVelocity.velocity}");
                //    cLocalPos.Value += velocity * deltaTime;
                //}
                //else if (hasLifetimeVelocity)
                //{
                //    var lifetimeVel = mgr.GetComponentData<LifetimeVelocity>(cEmitterRef.emitter);

                //    var velocity = InterpolationService.EvaluateCurveFloat3(mgr, normalizedLife, lifetimeVel.curve);

                //    if (cVelocity.isWorld)
                //        velocity = math.mul(cVelocity.startRotation, velocity);

                //    cLocalPos.Value += (cVelocity.velocity + velocity) * deltaTime;
                //}
                //else if (hasLifetimeSpeed)
                //{
                //    var lifetimeSpeed = mgr.GetComponentData<LifetimeSpeedMultiplier>(cEmitterRef.emitter);

                //    var speed = InterpolationService.EvaluateCurveFloat(mgr, normalizedLife, lifetimeSpeed.curve);
                //    cLocalPos.Value += cVelocity.velocity * speed * deltaTime;
                //}
                //else
                //{
                //    cLocalPos.Value += cVelocity.velocity * deltaTime;
                //}
            });
        }