Ue4中的框选函数

 1 void AHUD::GetActorsInSelectionRectangle(TSubclassOf<class AActor> ClassFilter, const FVector2D& FirstPoint, const FVector2D& SecondPoint, TArray<AActor*>& OutActors, bool bIncludeNonCollidingComponents, bool bActorMustBeFullyEnclosed)
 2 {
 3     // Because this is a HUD function it is likely to get called each tick,
 4     // so make sure any previous contents of the out actor array have been cleared!
 5     OutActors.Empty();
 6 
 7     //Create Selection Rectangle from Points
 8     FBox2D SelectionRectangle(0);
 9 
10     //This method ensures that an appropriate rectangle is generated, 
11     //        no matter what the coordinates of first and second point actually are.
12     SelectionRectangle += FirstPoint;
13     SelectionRectangle += SecondPoint;
14 
15 
16     //The Actor Bounds Point Mapping
17     const FVector BoundsPointMapping[8] =
18     {
19         FVector(1, 1, 1),
20         FVector(1, 1, -1),
21         FVector(1, -1, 1),
22         FVector(1, -1, -1),
23         FVector(-1, 1, 1),
24         FVector(-1, 1, -1),
25         FVector(-1, -1, 1),
26         FVector(-1, -1, -1)
27     };
28 
29     //~~~
30 
31     //For Each Actor of the Class Filter Type
32     for (TActorIterator<AActor> Itr(GetWorld(), ClassFilter); Itr; ++Itr)
33     {
34         AActor* EachActor = *Itr;
35 
36         //Get Actor Bounds                //casting to base class, checked by template in the .h
37         const FBox EachActorBounds = Cast<AActor>(EachActor)->GetComponentsBoundingBox(bIncludeNonCollidingComponents); /* All Components? */
38 
39         //Center
40         const FVector BoxCenter = EachActorBounds.GetCenter();
41 
42         //Extents
43         const FVector BoxExtents = EachActorBounds.GetExtent();
44 
45         // Build 2D bounding box of actor in screen space
46         FBox2D ActorBox2D(0);
47         for (uint8 BoundsPointItr = 0; BoundsPointItr < 8; BoundsPointItr++)
48         {
49             // Project vert into screen space.
50             const FVector ProjectedWorldLocation = Project(BoxCenter + (BoundsPointMapping[BoundsPointItr] * BoxExtents));
51             // Add to 2D bounding box
52             ActorBox2D += FVector2D(ProjectedWorldLocation.X, ProjectedWorldLocation.Y);
53         }
54 
55         //Selection Box must fully enclose the Projected Actor Bounds
56         if (bActorMustBeFullyEnclosed)
57         {
58             if(SelectionRectangle.IsInside(ActorBox2D))
59             {
60                 OutActors.Add(Cast<AActor>(EachActor));
61             }
62         }
63         //Partial Intersection with Projected Actor Bounds
64         else
65         {
66             if (SelectionRectangle.Intersect(ActorBox2D))
67             {
68                 OutActors.Add(Cast<AActor>(EachActor));
69             }
70         }
71     }
72 }
原文地址:https://www.cnblogs.com/blueroses/p/5601823.html