@@ -26,25 +26,34 @@ UDetectionComponent::UDetectionComponent()
26
26
void UDetectionComponent::BeginPlay ()
27
27
{
28
28
Super::BeginPlay ();
29
- scene_capture_component_2D_ = CastChecked<USceneCaptureComponent2D>(GetAttachParent ());
29
+ scene_capture_component_2D_ = Cast<USceneCaptureComponent2D>(GetAttachParent ());
30
+ if (!scene_capture_component_2D_)
31
+ {
32
+ // we get re-parented to USceneComponent when saving Take Recorder videos
33
+ this ->Deactivate ();
34
+ }
30
35
object_filter_ = FObjectFilter ();
31
36
}
32
37
33
- void UDetectionComponent::TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
38
+ void UDetectionComponent::TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
34
39
{
35
40
Super::TickComponent (DeltaTime, TickType, ThisTickFunction);
36
41
}
37
42
38
- const TArray<FDetectionInfo>& UDetectionComponent::getDetections ()
43
+ const TArray<FDetectionInfo> & UDetectionComponent::getDetections ()
39
44
{
40
45
cached_detections_.Empty ();
41
46
42
- for (TActorIterator<AActor> actor_itr (GetWorld ()); actor_itr; ++actor_itr) {
43
- AActor* actor = *actor_itr;
44
- if (object_filter_.matchesActor (actor)) {
45
- if (FVector::Distance (actor->GetActorLocation (), GetComponentLocation ()) <= max_distance_to_camera_) {
47
+ for (TActorIterator<AActor> actor_itr (GetWorld ()); actor_itr; ++actor_itr)
48
+ {
49
+ AActor *actor = *actor_itr;
50
+ if (object_filter_.matchesActor (actor))
51
+ {
52
+ if (FVector::Distance (actor->GetActorLocation (), GetComponentLocation ()) <= max_distance_to_camera_)
53
+ {
46
54
FBox2D box_2D_out;
47
- if (texture_target_ && calcBoundingFromViewInfo (actor, box_2D_out)) {
55
+ if (texture_target_ && calcBoundingFromViewInfo (actor, box_2D_out))
56
+ {
48
57
FDetectionInfo detection;
49
58
detection.Actor = actor;
50
59
detection.Box2D = box_2D_out;
@@ -63,7 +72,7 @@ const TArray<FDetectionInfo>& UDetectionComponent::getDetections()
63
72
return cached_detections_;
64
73
}
65
74
66
- bool UDetectionComponent::calcBoundingFromViewInfo (AActor* actor, FBox2D& box_out)
75
+ bool UDetectionComponent::calcBoundingFromViewInfo (AActor * actor, FBox2D & box_out)
67
76
{
68
77
FVector origin;
69
78
FVector extend ;
@@ -74,7 +83,7 @@ bool UDetectionComponent::calcBoundingFromViewInfo(AActor* actor, FBox2D& box_ou
74
83
bool is_in_camera_view = false ;
75
84
76
85
// get render target for texture size
77
- FRenderTarget* render_target = texture_target_->GameThread_GetRenderTargetResource ();
86
+ FRenderTarget * render_target = texture_target_->GameThread_GetRenderTargetResource ();
78
87
79
88
// initialize viewinfo for projection matrix
80
89
FMinimalViewInfo info;
@@ -113,16 +122,19 @@ bool UDetectionComponent::calcBoundingFromViewInfo(AActor* actor, FBox2D& box_ou
113
122
FPlane (0 , 1 , 0 , 0 ),
114
123
FPlane (0 , 0 , 0 , 1 ));
115
124
116
- if (scene_capture_component_2D_->bUseCustomProjectionMatrix ) {
125
+ if (scene_capture_component_2D_->bUseCustomProjectionMatrix )
126
+ {
117
127
projection_data.ProjectionMatrix = scene_capture_component_2D_->CustomProjectionMatrix ;
118
128
}
119
- else {
129
+ else
130
+ {
120
131
projection_data.ProjectionMatrix = info.CalculateProjectionMatrix ();
121
132
}
122
133
projection_data.SetConstrainedViewRectangle (screen_rect);
123
134
124
135
// Project Points to pixels and get the corner pixels
125
- for (FVector& point : points) {
136
+ for (FVector &point : points)
137
+ {
126
138
FVector2D Pixel (0 , 0 );
127
139
FSceneView::ProjectWorldToScreen ((point), screen_rect, projection_data.ComputeViewProjectionMatrix (), Pixel);
128
140
is_in_camera_view |= (Pixel != screen_rect.Min ) && (Pixel != screen_rect.Max ) && screen_rect.Contains (FIntPoint (Pixel.X , Pixel.Y ));
@@ -136,13 +148,17 @@ bool UDetectionComponent::calcBoundingFromViewInfo(AActor* actor, FBox2D& box_ou
136
148
// If actor in camera view - check if it's actually visible or hidden
137
149
// Check against 8 extend points
138
150
bool is_visible = false ;
139
- if (is_in_camera_view) {
151
+ if (is_in_camera_view)
152
+ {
140
153
FHitResult result;
141
154
bool is_world_hit;
142
- for (FVector& point : points) {
155
+ for (FVector &point : points)
156
+ {
143
157
is_world_hit = GetWorld ()->LineTraceSingleByChannel (result, GetComponentLocation (), point, ECC_WorldStatic);
144
- if (is_world_hit) {
145
- if (result.GetActor () == actor) {
158
+ if (is_world_hit)
159
+ {
160
+ if (result.GetActor () == actor)
161
+ {
146
162
is_visible = true ;
147
163
break ;
148
164
}
@@ -151,12 +167,16 @@ bool UDetectionComponent::calcBoundingFromViewInfo(AActor* actor, FBox2D& box_ou
151
167
152
168
// If actor in camera view but didn't hit any point out of 8 extend points,
153
169
// check against 10 random points
154
- if (!is_visible) {
155
- for (int i = 0 ; i < 10 ; i++) {
170
+ if (!is_visible)
171
+ {
172
+ for (int i = 0 ; i < 10 ; i++)
173
+ {
156
174
FVector point = UKismetMathLibrary::RandomPointInBoundingBox (origin, extend );
157
175
is_world_hit = GetWorld ()->LineTraceSingleByChannel (result, GetComponentLocation (), point, ECC_WorldStatic);
158
- if (is_world_hit) {
159
- if (result.GetActor () == actor) {
176
+ if (is_world_hit)
177
+ {
178
+ if (result.GetActor () == actor)
179
+ {
160
180
is_visible = true ;
161
181
break ;
162
182
}
@@ -187,11 +207,12 @@ FRotator UDetectionComponent::getRelativeRotation(FVector in_location, FRotator
187
207
return relative_object_transform.Rotator ();
188
208
}
189
209
190
- void UDetectionComponent::addMeshName (const std::string& mesh_name)
210
+ void UDetectionComponent::addMeshName (const std::string & mesh_name)
191
211
{
192
212
FString name (mesh_name.c_str ());
193
213
194
- if (!object_filter_.wildcard_mesh_names_ .Contains (name)) {
214
+ if (!object_filter_.wildcard_mesh_names_ .Contains (name))
215
+ {
195
216
object_filter_.wildcard_mesh_names_ .Add (name);
196
217
}
197
218
}
@@ -204,4 +225,4 @@ void UDetectionComponent::setFilterRadius(const float radius_cm)
204
225
void UDetectionComponent::clearMeshNames ()
205
226
{
206
227
object_filter_.wildcard_mesh_names_ .Empty ();
207
- }
228
+ }
0 commit comments