public void Calculate(float lifetime, float time, out ColorValue result)
                    {
                        float value = 1;

                        switch (Type)
                        {
                        case Component_ParticleColorMultiplierByTime.TypeEnum.Range:
                            value = MathEx.Lerp(Range.Minimum, Range.Maximum, MathEx.Saturate(time / lifetime));
                            break;

                        case Component_ParticleColorMultiplierByTime.TypeEnum.Curve:
                            value = Curve != null?Curve.CalculateValueByTime(time) : 0;

                            break;
                        }

                        result = ColorValue.One;
                        if (Channels.HasFlag(Component_ParticleColorMultiplierByTime.ChannelsEnum.Red))
                        {
                            result.Red *= value;
                        }
                        if (Channels.HasFlag(Component_ParticleColorMultiplierByTime.ChannelsEnum.Green))
                        {
                            result.Green *= value;
                        }
                        if (Channels.HasFlag(Component_ParticleColorMultiplierByTime.ChannelsEnum.Blue))
                        {
                            result.Blue *= value;
                        }
                        if (Channels.HasFlag(Component_ParticleColorMultiplierByTime.ChannelsEnum.Alpha))
                        {
                            result.Alpha *= value;
                        }
                    }
Example #2
0
 /// <summary>
 /// Performs linear interpolation of <see cref="ColorByte"/>.
 /// </summary>
 /// <param name="value1">Source <see cref="ColorByte"/>.</param>
 /// <param name="value2">Destination <see cref="ColorByte"/>.</param>
 /// <param name="amount">Interpolation factor.</param>
 /// <returns>Interpolated <see cref="ColorByte"/>.</returns>
 public static ColorByte Lerp(ColorByte value1, ColorByte value2, double amount)
 {
     amount = MathEx.Clamp(amount, 0, 1);
     return(new ColorByte(
                (int)MathEx.Lerp(value1.Red, value2.Red, amount),
                (int)MathEx.Lerp(value1.Green, value2.Green, amount),
                (int)MathEx.Lerp(value1.Blue, value2.Blue, amount),
                (int)MathEx.Lerp(value1.Alpha, value2.Alpha, amount)));
 }
                    public float Calculate(float lifetime, float time)
                    {
                        switch (Type)
                        {
                        case Component_ParticleSizeMultiplierByTime.TypeEnum.Range:
                            return(MathEx.Lerp(Range.Minimum, Range.Maximum, MathEx.Saturate(time / lifetime)));

                        case Component_ParticleSizeMultiplierByTime.TypeEnum.Curve:
                            return(Curve != null?Curve.CalculateValueByTime(time) : 1);
                        }

                        return(1);
                    }
Example #4
0
        //!!!!по сути времнно пока нет корректной толщины

        public static void AddLineSegmented(Simple3DRenderer renderer, Vector3 start, Vector3 end, int steps = -1)
        {
            //draw line segments so that there is no problem with the thickness of the lines
            var ray = new Ray(start, end - start);

            int steps2 = steps;

            if (steps2 < 0)
            {
                steps2 = (int)MathEx.Lerp(2, 10, MathEx.Saturate(Math.Pow(ray.Direction.Length() / 100, 1.3)));
            }

            for (int n = 0; n < steps2; n++)
            {
                var p0 = ray.GetPointOnRay((double)n / steps2);
                var p1 = ray.GetPointOnRay((double)(n + 1) / steps2);
                renderer.AddLine(p0, p1);
            }
        }
Example #5
0
        /////////////////////////////////////////

        protected virtual void OnRenderProgressBar(UIProgressBar control, CanvasRenderer renderer)
        {
            var rect = control.GetScreenRectangle();

            renderer.AddQuad(rect, new ColorValue(0, 0, 0));

            if (control.Maximum.Value != 0)
            {
                double progress = control.Value.Value / control.Maximum.Value;
                if (progress > 0)
                {
                    var rect2 = rect;
                    rect2.Expand(-control.GetScreenOffsetByValue(new UIMeasureValueVector2(UIMeasure.Units, 4, 4)));
                    rect2.Right = MathEx.Lerp(rect2.Left, rect2.Right, progress);

                    renderer.AddQuad(rect2, new ColorValue(1, 1, 1));
                }
            }
        }
                public int GenerateValue(Random random)
                {
                    switch (Type)
                    {
                    case Component_ParticleEmitter.IntegerProperty.TypeEnum.Constant:
                        return(Constant);

                    case Component_ParticleEmitter.IntegerProperty.TypeEnum.Range:
                        return((int)MathEx.Lerp(Range.Minimum, Range.Maximum, random.NextFloat()));

                    case Component_ParticleEmitter.IntegerProperty.TypeEnum.Curve:
                        if (Curve != null)
                        {
                            var maxValue = Curve.Points[Curve.Points.Count - 1].Time;
                            return((int)Curve.CalculateValueByTime(random.Next(maxValue)));
                        }
                        break;
                    }

                    return(0);
                }
        Component_GroupOfObjects.Object[] UpdateOutputDefault(Component_Surface surface, Component_GroupOfObjects groupOfObjects)
        {
            var pointPositions  = GetPointPositions();
            var pointPositions2 = pointPositions.Select(a => a.ToVector2()).ToArray();
            var pointBounds     = GetPointPositionsBounds();

            var lines = new List <Line2>();

            for (int n = 0; n < pointPositions2.Length; n++)
            {
                lines.Add(new Line2(pointPositions2[n], pointPositions2[(n + 1) % pointPositions2.Length]));
            }

            var random = new Random(RandomSeed.Value);

            double GetFadeFactor(Vector2 point)
            {
                var minLength = double.MaxValue;

                foreach (var line in lines)
                {
                    var d = Math.Min((line.Start - point).Length(), (line.End - point).Length());

                    var projected = MathAlgorithms.ProjectPointToLine(line.Start, line.End, point);

                    Rectangle b = new Rectangle(line.Start);
                    b.Add(line.End);
                    if (b.Contains(projected))
                    {
                        var d2 = (projected - point).Length();
                        if (d2 < d)
                        {
                            d = d2;
                        }
                    }

                    if (d < minLength)
                    {
                        minLength = d;
                    }
                }

                if (minLength >= FadeLength)
                {
                    return(1);
                }
                return(minLength / FadeLength);
            }

            if (pointPositions.Length >= 3)
            {
                //calculate object count
                int count;
                {
                    double polygonArea;
                    {
                        var points = new List <Vector2>(pointPositions2);
                        points.Add(points[0]);

                        polygonArea = Math.Abs(points.Take(points.Count - 1)
                                               .Select((p, i) => (points[i + 1].X - p.X) * (points[i + 1].Y + p.Y))
                                               .Sum() / 2);
                    }

                    //!!!!среднее от всех групп
                    double minDistanceBetweenObjects;
                    {
                        var groups = surface.GetComponents <Component_SurfaceGroupOfElements>();
                        if (groups.Length != 0)
                        {
                            minDistanceBetweenObjects = 0;
                            foreach (var group in groups)
                            {
                                minDistanceBetweenObjects += group.MinDistanceBetweenObjects;
                            }
                            minDistanceBetweenObjects /= groups.Length;
                        }
                        else
                        {
                            minDistanceBetweenObjects = 1;
                        }
                    }

                    double radius     = minDistanceBetweenObjects / 2;
                    double objectArea = Math.PI * radius * radius;
                    if (objectArea < 0.1)
                    {
                        objectArea = 0.1;
                    }

                    double maxCount = polygonArea / objectArea;

                    count = (int)(Strength * (double)maxCount);
                    count = Math.Max(count, 0);
                }

                var data = new List <Component_GroupOfObjects.Object>(count);

                var scene = groupOfObjects.FindParent <Component_Scene>();
                if (scene != null)
                {
                    var destinationCachedBaseObjects = groupOfObjects.GetBaseObjects();

                    var pointContainerFindFreePlace = new PointContainer3D(pointBounds, 100);

                    for (int n = 0; n < count; n++)
                    {
                        surface.GetRandomVariation(new Component_Surface.GetRandomVariationOptions(), random, out var groupIndex, out var elementIndex, out var positionZ, out var rotation, out var scale);
                        var surfaceGroup = surface.GetGroup(groupIndex);

                        Vector3?position = null;

                        int counter = 0;
                        while (counter < 20)
                        {
                            Vector2 position2 = Vector2.Zero;
                            position2.X = MathEx.Lerp(pointBounds.Minimum.X, pointBounds.Maximum.X, random.NextFloat());
                            position2.Y = MathEx.Lerp(pointBounds.Minimum.Y, pointBounds.Maximum.Y, random.NextFloat());

                            if (MathAlgorithms.IsPointInPolygon(pointPositions2, position2))
                            {
                                double factor = 1;
                                if (FadeLength > 0)
                                {
                                    factor = GetFadeFactor(position2);
                                }

                                if (factor >= 1 || random.NextDouble() <= factor)
                                {
                                    var result = Component_Scene_Utility.CalculateObjectPositionZ(scene, groupOfObjects, pointBounds.GetCenter().Z, position2, destinationCachedBaseObjects);
                                    if (result.found || destinationCachedBaseObjects.Count == 0)
                                    {
                                        var p = new Vector3(position2, destinationCachedBaseObjects.Count != 0 ? result.positionZ : pointBounds.GetCenter().Z);

                                        //check by MinDistanceBetweenObjects
                                        if (surfaceGroup == null || !pointContainerFindFreePlace.Contains(new Sphere(p, surfaceGroup.MinDistanceBetweenObjects)))
                                        {
                                            //found place to create
                                            position = p;
                                            break;
                                        }
                                    }
                                }
                            }

                            counter++;
                        }

                        if (position != null)
                        {
                            var obj = new Component_GroupOfObjects.Object();
                            obj.Element          = 0;                   // (ushort)element.Index.Value;
                            obj.VariationGroup   = groupIndex;
                            obj.VariationElement = elementIndex;
                            obj.Flags            = Component_GroupOfObjects.Object.FlagsEnum.Enabled | Component_GroupOfObjects.Object.FlagsEnum.Visible;
                            obj.Position         = position.Value + new Vector3(0, 0, positionZ);
                            obj.Rotation         = rotation;
                            obj.Scale            = scale;
                            obj.Color            = ColorValue.One;
                            data.Add(obj);

                            //add to point container
                            pointContainerFindFreePlace.Add(ref obj.Position);
                        }
                    }
                }

                return(data.ToArray());
            }
            else
            {
                return(new Component_GroupOfObjects.Object[0]);
            }
        }
Example #8
0
 public double GetValueAngle()
 {
     return(MathEx.Lerp(AngleRange.Value.Minimum, AngleRange.Value.Maximum, GetValueFactor()));
 }