Ejemplo n.º 1
0
        static void RayTest_PostProcess(Physics2DRayTestItem item, List <Physics2DRayTestItem.ResultItem> resultList)
        {
            if (resultList.Count == 0)
            {
                item.Result = Array.Empty <Physics2DRayTestItem.ResultItem>();
                return;
            }

            if (item.Mode == Physics2DRayTestItem.ModeEnum.OneClosest || item.Mode == Physics2DRayTestItem.ModeEnum.One)
            {
                item.Result = new Physics2DRayTestItem.ResultItem[] { resultList[0] };
                return;
            }

            if (item.Mode == Physics2DRayTestItem.ModeEnum.OneForEach)
            {
                item.Result = GetResultsOnePerShape(resultList);
                return;
            }

            //sort by distance in any other case
            CollectionUtility.SelectionSort(resultList, Compare);

            if (item.Mode == Physics2DRayTestItem.ModeEnum.All)
            {
                item.Result = resultList.ToArray();
                return;
            }

            //item.Mode == PhysicsRayTestItem.ModeEnum.OneClosestForEach
            item.Result = GetResultsOnePerShape(resultList);
        }
Ejemplo n.º 2
0
        public static SharpBgfx.VertexLayout CreateVertexDeclaration(this VertexElement[] elements, int forSource)
        {
            GetInfo(elements, out var vertexSize, out var holes);

            //!!!!
            if (holes)
            {
                Log.Fatal("VertexElement: CreateVertexDeclaration: holes. impl.");
            }

            //!!!!
            if (forSource != 0)
            {
                Log.Fatal("VertexElement: CreateVertexDeclaration: forSource != 0. impl.");
            }
            //if( element.Source == forSource )
            //{
            //}


            var result = new SharpBgfx.VertexLayout();

            result.Begin();

            var sorted = (VertexElement[])elements.Clone();

            CollectionUtility.SelectionSort(sorted, delegate(VertexElement element1, VertexElement element2)
            {
                if (element1.Offset < element2.Offset)
                {
                    return(-1);
                }
                if (element1.Offset > element2.Offset)
                {
                    return(1);
                }
                return(0);
            });

            foreach (var element in sorted)
            {
                element.GetBfgx(out var attribute, out var count, out var type, out var asInt);
                result.Add(attribute, count, type, false, asInt);
            }

            result.End();

            if (vertexSize != result.Stride)
            {
                Log.Fatal("VertexElement: CreateVertexDeclaration: vertexSize != result.Stride.");
            }

            return(result);
        }
Ejemplo n.º 3
0
        static void GenerateVideoModes()
        {
            //generate list
            videoModes = PlatformFunctionality.Get().GetVideoModes();

            //safe list
            if (videoModes.Count == 0)
            {
                videoModes.Add(new Vector2I(640, 480));
                videoModes.Add(new Vector2I(800, 600));
                videoModes.Add(new Vector2I(1024, 768));
                videoModes.Add(new Vector2I(1152, 864));
                videoModes.Add(new Vector2I(1280, 1024));
                videoModes.Add(new Vector2I(1600, 1200));
            }

            //sort
            CollectionUtility.SelectionSort(videoModes, delegate(Vector2I mode1, Vector2I mode2)
            {
                if (mode1.X < mode2.X)
                {
                    return(-1);
                }
                if (mode1.X > mode2.X)
                {
                    return(1);
                }
                if (mode1.Y < mode2.Y)
                {
                    return(-1);
                }
                if (mode1.Y > mode2.Y)
                {
                    return(1);
                }
                return(0);
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Convex Hull Graham algorithm.
        /// </summary>
        /// <param name="points"></param>
        /// <param name="epsilon"></param>
        /// <returns></returns>
        public static int[] GetFromPoints(IList <Vector2> points, double epsilon)
        {
            List <PointItem> list = new List <PointItem>(points.Count);

            for (int nPoint = 0; nPoint < points.Count; nPoint++)
            {
                Vector2 point = points[nPoint];

                //const double epsilon = .001;

                bool skip = false;
                for (int nPointItem = 0; nPointItem < list.Count; nPointItem++)
                {
                    PointItem listItem = list[nPointItem];
                    if (listItem.point.Equals(ref point, epsilon))
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                {
                    continue;
                }

                PointItem pointItem = new PointItem();
                pointItem.sourceIndex = nPoint;
                pointItem.point       = points[nPoint];
                list.Add(pointItem);
            }

            if (list.Count < 3)
            {
                int[] array = new int[points.Count];
                for (int n = 0; n < array.Length; n++)
                {
                    array[n] = n;
                }
                return(array);
            }

            //!!!!merge sort
            CollectionUtility.SelectionSort(list, delegate(PointItem item1, PointItem item2)
            {
                Vector2 i1 = item1.point;
                Vector2 i2 = item2.point;

                if (i1.X < i2.X || i1.X == i2.X && i1.Y < i2.Y)
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            });

            PointItem p1 = list[0];
            PointItem p2 = list[list.Count - 1];

            List <PointItem> up   = new List <PointItem>(points.Count);
            List <PointItem> down = new List <PointItem>(points.Count);

            up.Add(p1);
            down.Add(p1);

            for (int i = 1; i < list.Count; ++i)
            {
                if (i == list.Count - 1 || cw(p1.point, list[i].point, p2.point))
                {
                    while (up.Count >= 2 && !cw(up[up.Count - 2].point, up[up.Count - 1].point,
                                                list[i].point))
                    {
                        up.RemoveAt(up.Count - 1);
                    }
                    up.Add(list[i]);
                }
                if (i == list.Count - 1 || ccw(p1.point, list[i].point, p2.point))
                {
                    while (down.Count >= 2 && !ccw(down[down.Count - 2].point,
                                                   down[down.Count - 1].point, list[i].point))
                    {
                        down.RemoveAt(down.Count - 1);
                    }
                    down.Add(list[i]);
                }
            }

            List <int> result = new List <int>(points.Count);

            for (int i = 0; i < up.Count; i++)
            {
                result.Add(up[i].sourceIndex);
            }
            for (int i = down.Count - 2; i > 0; i--)
            {
                result.Add(down[i].sourceIndex);
            }

            return(result.ToArray());
        }
Ejemplo n.º 5
0
        static void UpdateChannels(double delta, bool update3DChannels)
        {
            List <SoundVirtualChannel> activeVirtualChannels;
            List <SoundRealChannel>    realChannels;

            if (update3DChannels)
            {
                activeVirtualChannels = activeVirtual3DChannels;
                realChannels          = real3DChannels;
            }
            else
            {
                activeVirtualChannels = activeVirtual2DChannels;
                realChannels          = real2DChannels;
            }

            //update virtual channels time
            if (delta != 0)
            {
                for (int n = 0; n < activeVirtualChannels.Count; n++)
                {
                    var virtualChannel = activeVirtualChannels[n];
                    if (!virtualChannel.IsTotalPaused())
                    {
                        virtualChannel.time += delta * virtualChannel.pitch;

                        var sound = virtualChannel.sound;
                        if (virtualChannel.time >= sound.Length)
                        {
                            if ((sound.Mode & SoundModes.Loop) != 0)
                            {
                                virtualChannel.time -= sound.Length;
                            }
                            else
                            {
                                if (virtualChannel.currentRealChannel == null || virtualChannel.time >= sound.Length + .1f)
                                {
                                    virtualChannel.Stop();
                                    n--;
                                }
                            }
                        }
                    }
                }
            }

            //calculate priority

            //!!!!slowly. часто обновляется может

            for (int n = 0; n < activeVirtualChannels.Count; n++)
            {
                var virtualChannel = activeVirtualChannels[n];
                var sound          = virtualChannel.sound;

                double priority = 0;

                if (virtualChannel.AttachedToScene == null || virtualChannel.AttachedToScene == listenerCurrentScene)
                {
                    if (virtualChannel.GetTotalVolume() != 0 && !virtualChannel.IsTotalPaused())
                    {
                        if (update3DChannels)
                        {
                            double distanceSqr = (virtualChannel.position - listenerPosition).LengthSquared();
                            priority = (100000000.0 - distanceSqr) * (virtualChannel.Priority + .00001);
                            //priority += 1.0f / distanceSqr + virtualChannel.Priority * 10000;
                        }
                        else
                        {
                            //if( ( sound.Mode & SoundModes.Stream ) != 0 )
                            //	priority += 10;
                            priority = (virtualChannel.Priority + .00001) * 10000000000;

                            //fix sorting jerking problem
                            priority += (double)sound.createIndex;
                        }

                        //if( ( sound.Mode & SoundModes.Mode3D ) == 0 )
                        //{
                        //	priority += 100000000;
                        //	if( ( sound.Mode & SoundModes.Stream ) != 0 )
                        //		priority += 100000000;
                        //}
                        //else
                        //{
                        //	double distanceSqr = ( virtualChannel.position - listenerPosition ).LengthSquared();

                        //	priority += ( 1000000.0 - distanceSqr ) + virtualChannel.priority * 10000;
                        //	//priority += 1.0f / distanceSqr + virtualChannel.Priority * 10000;
                        //}

                        ////only for small fix sorting jerking problem at the DataBuffer and Streams
                        //if( sound.Name == null || ( sound.Mode & SoundModes.Stream ) != 0 )
                        //	priority += (double)sound.createIndex;
                    }
                }

                virtualChannel.tempPriority = priority;
            }

            //Sort by priority
            CollectionUtility.SelectionSort(activeVirtualChannels, virtualChannelsTempPriorityComparer);
            //ListUtils.MergeSort( activeVirtualChannels, virtualChannelsTempPriorityComparer );

            //remove virtual channels from real channels
            for (int n = 0; n < activeVirtualChannels.Count; n++)
            {
                var virtualChannel = activeVirtualChannels[n];
                var realChannel    = virtualChannel.currentRealChannel;

                if (realChannel != null)
                {
                    if (n >= realChannels.Count || virtualChannel.tempPriority == 0)
                    {
                        realChannel.PreDetachVirtualChannel();
                        realChannel.currentVirtualChannel = null;
                        virtualChannel.currentRealChannel = null;
                    }
                }
            }

            //bind virtual channels to real channels
            var freeRealChannels = new Queue <SoundRealChannel>(realChannels.Count);

            foreach (var c in realChannels)
            {
                if (c.CurrentVirtualChannel == null)
                {
                    freeRealChannels.Enqueue(c);
                }
            }

            for (int n = 0; n < activeVirtualChannels.Count; n++)
            {
                //no free real channels
                if (freeRealChannels.Count == 0)
                {
                    break;
                }

                var virtualChannel = activeVirtualChannels[n];
                if (virtualChannel.currentRealChannel == null && virtualChannel.tempPriority != 0)
                {
                    var realChannel = freeRealChannels.Dequeue();

                    realChannel.currentVirtualChannel = virtualChannel;
                    virtualChannel.currentRealChannel = realChannel;
                    realChannel.PostAttachVirtualChannel();
                }
            }

            ////add virtual channels to real channels
            //int currentRealChannelIndex = 0;

            //for( int n = 0; n < activeVirtualChannels.Count; n++ )
            //{
            //	VirtualChannel virtualChannel = activeVirtualChannels[ n ];

            //	if( currentRealChannelIndex >= realChannels.Count )
            //		break;
            //	if( virtualChannel.currentRealChannel != null )
            //		continue;
            //	if( virtualChannel.tempPriority == 0 )
            //		continue;

            //	if( virtualChannel.Time >= virtualChannel.Sound.Length )
            //	{
            //		if( ( virtualChannel.Sound.Mode & SoundModes.Loop ) == 0 )
            //			continue;
            //		else
            //			virtualChannel.time -= virtualChannel.Sound.Length;
            //	}

            //	RealChannel realChannel = null;
            //	{
            //		while( realChannels[ currentRealChannelIndex ].CurrentVirtualChannel != null )
            //		{
            //			currentRealChannelIndex++;
            //			if( currentRealChannelIndex >= realChannels.Count )
            //				break;
            //		}

            //		if( currentRealChannelIndex < realChannels.Count )
            //		{
            //			realChannel = realChannels[ currentRealChannelIndex ];
            //			currentRealChannelIndex++;
            //		}
            //	}

            //	if( realChannel == null )
            //		break;

            //	if( realChannel.currentVirtualChannel != null )
            //		Log.Fatal( "SoundWorld: UpdateChannels: realChannel.currentVirtualChannel != null." );

            //	realChannel.currentVirtualChannel = virtualChannel;
            //	virtualChannel.currentRealChannel = realChannel;
            //	realChannel.PostAttachVirtualChannel();
            //}

            //update gain for 3d sound (calculate attenuation)
            if (update3DChannels)
            {
                for (int n = 0; n < real3DChannels.Count; n++)
                {
                    SoundRealChannel realChannel = real3DChannels[n];
                    if (realChannel.currentVirtualChannel != null)
                    {
                        //!!!!slowly enter to critical section for each real channel?
                        realChannel.UpdateVolume();
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public virtual void RenderUI(CanvasRenderer renderer, Component_RenderingPipeline.RenderSceneData.LightItem lightItem, Viewport viewport, double intensity)
        {
            var lightPosition = lightItem.Position;

            //!!!!override real position
            //{
            //	if( Sun.Instances.Count > 0 )
            //	{
            //		//get first sun entity on the map.
            //		Sun sun = Sun.Instances[ 0 ];

            //		Vec3 direction;
            //		if( sun.BillboardOverridePosition != Vec3.Zero )
            //			direction = sun.BillboardOverridePosition.GetNormalize();
            //		else
            //			direction = -sun.Rotation.GetForward();
            //		position = camera.Position + direction * 100000;

            //		return true;
            //	}

            //	position = Vec3.Zero;
            //	return false;
            //}

            if (viewport.CameraSettings.ProjectToScreenCoordinates(lightPosition, out var screenLightPosition))
            {
                //get enabled flares
                var flares = GetComponents <Component_LensFlare>(onlyEnabledInHierarchy: true);

                //sort by position
                CollectionUtility.SelectionSort(flares, delegate(Component_LensFlare f1, Component_LensFlare f2)
                {
                    if (f1.Position > f2.Position)
                    {
                        return(-1);
                    }
                    if (f1.Position < f2.Position)
                    {
                        return(1);
                    }
                    return(0);
                });

                //for( int n = 0; n < flares.Count; n++ )
                //	sortedFlares[ n ].tempSmoothIntensityFactor = cameraInfo.SmoothIntensityFactors[ n ];

                //render
                foreach (var flare in flares)
                {
                    var image = flare.Image.Value;

                    var flareVector   = screenLightPosition - new Vector2(0.5, 0.5);
                    var flarePosition = new Vector2(0.5, 0.5) + flareVector * flare.Position;
                    var size          = flare.Size.Value;
                    var flareSize     = new Vector2(size.X, size.Y * renderer.AspectRatio);
                    var rectangle     = new Rectangle(flarePosition - flareSize * 0.5, flarePosition + flareSize * 0.5);

                    var flareColor = Color.Value * flare.Color * new ColorValue(1, 1, 1, intensity);
                    // * new ColorValue( 1, 1, 1, item.tempSmoothIntensityFactor );

                    if (flareColor.Alpha > 0)
                    {
                        renderer.PushBlendingType(flare.Blending.Value);
                        renderer.AddQuad(rectangle, new RectangleF(0, 0, 1, 1), image, flareColor, true);
                        renderer.PopBlendingType();
                    }
                }
            }
        }