77#include " Kismet/GameplayStatics.h"
88#include " GameFramework/RotatingMovementComponent.h"
99#include < exception>
10- #include < regex>
1110#include " common/common_utils/Utils.hpp"
1211#include " Components/StaticMeshComponent.h"
1312#include " EngineUtils.h"
1413#include " UObjectIterator.h"
14+ // #include "Runtime/Foliage/Public/FoliageType.h"
1515#include " Kismet/KismetStringLibrary.h"
1616#include " Engine/Engine.h"
1717
@@ -23,8 +23,6 @@ parameters -> camel_case
2323*/
2424
2525
26- typedef common_utils::Utils Utils;
27-
2826bool UAirBlueprintLib::log_messages_hidden = false ;
2927
3028void UAirBlueprintLib::LogMessageString (const std::string &prefix, const std::string &suffix, LogDebugLevel level, float persist_sec)
@@ -128,31 +126,86 @@ void UAirBlueprintLib::FindAllActor(const UObject* context, TArray<AActor*>& fou
128126 UGameplayStatics::GetAllActorsOfClass (context == nullptr ? GEngine : context, T::StaticClass (), foundActors);
129127}
130128
131- std::string UAirBlueprintLib::GetMeshName (UMeshComponent* mesh)
129+ template <typename T>
130+ void UAirBlueprintLib::InitializeObjectStencilID (T* mesh, bool ignore_existing)
131+ {
132+ std::string mesh_name = GetMeshName (mesh);
133+ if (mesh_name == " " || common_utils::Utils::startsWith (mesh_name, " Default_" )) {
134+ // common_utils::Utils::DebugBreak();
135+ return ;
136+ }
137+ FString name (mesh_name.c_str ());
138+ int hash = 5 ;
139+ int max_len = name.Len () - name.Len () / 4 ; // remove training numerical suffixes
140+ if (max_len < 3 )
141+ max_len = name.Len ();
142+ for (int idx = 0 ; idx < max_len; ++idx) {
143+ hash += UKismetStringLibrary::GetCharacterAsNumber (name, idx);
144+ }
145+ if (ignore_existing || mesh->CustomDepthStencilValue == 0 ) { // if value is already set then don't bother
146+ SetObjectStencilID (mesh, hash % 256 );
147+ }
148+ }
149+
150+ template <typename T>
151+ void UAirBlueprintLib::SetObjectStencilID (T* mesh, int object_id)
152+ {
153+ mesh->SetCustomDepthStencilValue (object_id);
154+ mesh->SetRenderCustomDepth (true );
155+ // mesh->SetVisibility(false);
156+ // mesh->SetVisibility(true);
157+ }
158+
159+ void UAirBlueprintLib::SetObjectStencilID (ALandscapeProxy* mesh, int object_id)
160+ {
161+ mesh->CustomDepthStencilValue = object_id;
162+ mesh->bRenderCustomDepth = true ;
163+ }
164+
165+ template <class T >
166+ std::string UAirBlueprintLib::GetMeshName (T* mesh)
167+ {
168+ if (mesh->GetOwner ())
169+ return std::string (TCHAR_TO_UTF8 (*(mesh->GetOwner ()->GetName ())));
170+ else
171+ return " " ; // std::string(TCHAR_TO_UTF8(*(UKismetSystemLibrary::GetDisplayName(mesh))));
172+ }
173+
174+ std::string UAirBlueprintLib::GetMeshName (ALandscapeProxy* mesh)
132175{
133- return std::string ( TCHAR_TO_UTF8 (*(mesh->GetOwner () ? mesh->GetOwner ()->GetName () :
134- " " ))); // UKismetSystemLibrary::GetDisplayName(mesh)
176+ return std::string (TCHAR_TO_UTF8 (*(mesh->GetName ())));
135177}
136178
137179void UAirBlueprintLib::InitializeMeshStencilIDs ()
138180{
139181 for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
140182 {
141- UMeshComponent *mesh = *comp;
142- mesh->SetRenderCustomDepth (true );
143- std::string mesh_name = GetMeshName (mesh);
144- if (mesh_name == " " )
145- continue ;
146- FString name (mesh_name.c_str ());
147- int hash = 0 ;
148- for (int idx = 0 ; idx < name.Len () && idx < 3 ; ++idx) {
149- hash += UKismetStringLibrary::GetCharacterAsNumber (name, idx);
150- }
151- mesh->CustomDepthStencilValue = hash % 256 ;
152- mesh->MarkRenderStateDirty ();
183+ InitializeObjectStencilID (*comp);
184+ }
185+ // for (TObjectIterator<UFoliageType> comp; comp; ++comp)
186+ // {
187+ // InitializeObjectStencilID(*comp);
188+ // }
189+ for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
190+ {
191+ InitializeObjectStencilID (*comp);
153192 }
154193}
155194
195+ template <typename T>
196+ void UAirBlueprintLib::SetObjectStencilIDIfMatch (T* mesh, int object_id, const std::string& mesh_name, bool is_name_regex,
197+ const std::regex& name_regex, int & changes)
198+ {
199+ std::string comp_mesh_name = GetMeshName (mesh);
200+ if (comp_mesh_name == " " )
201+ return ;
202+ bool is_match = (!is_name_regex && (comp_mesh_name == mesh_name))
203+ || (is_name_regex && std::regex_match (comp_mesh_name, name_regex));
204+ if (is_match) {
205+ ++changes;
206+ SetObjectStencilID (mesh, object_id);
207+ }
208+ }
156209bool UAirBlueprintLib::SetMeshStencilID (const std::string& mesh_name, int object_id,
157210 bool is_name_regex)
158211{
@@ -164,27 +217,11 @@ bool UAirBlueprintLib::SetMeshStencilID(const std::string& mesh_name, int object
164217 int changes = 0 ;
165218 for (TObjectIterator<UMeshComponent> comp; comp; ++comp)
166219 {
167- UMeshComponent *mesh = *comp;
168-
169- std::string comp_mesh_name = GetMeshName (mesh);
170- if (comp_mesh_name == " " )
171- continue ;
172-
173- bool is_match = (!is_name_regex && (comp_mesh_name == mesh_name))
174- || (is_name_regex && std::regex_match (comp_mesh_name, name_regex));
175-
176- if (is_match) {
177- ++changes;
178- // mesh->SetRenderCustomDepth(false);
179- // mesh->SetRenderCustomDepth(true);
180- mesh->CustomDepthStencilValue = object_id;
181- // mesh->SetVisibility(false);
182- // mesh->SetVisibility(true);
183- mesh->MarkRenderStateDirty ();
184-
185- // if (! is_name_regex)
186- // return true;
187- }
220+ SetObjectStencilIDIfMatch (*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
221+ }
222+ for (TObjectIterator<ALandscapeProxy> comp; comp; ++comp)
223+ {
224+ SetObjectStencilIDIfMatch (*comp, object_id, mesh_name, is_name_regex, name_regex, changes);
188225 }
189226
190227 return changes > 0 ;
@@ -269,7 +306,7 @@ void UAirBlueprintLib::FollowActor(AActor* follower, const AActor* followee, con
269306 float dist = (follower->GetActorLocation () - next_location).Size ();
270307 float offset_dist = offset.Size ();
271308 float dist_offset = (dist - offset_dist) / offset_dist;
272- float lerp_alpha = Utils::clip ((dist_offset*dist_offset) * 0 .01f + 0 .01f , 0 .0f , 1 .0f );
309+ float lerp_alpha = common_utils:: Utils::clip ((dist_offset*dist_offset) * 0 .01f + 0 .01f , 0 .0f , 1 .0f );
273310 next_location = FMath::Lerp (follower->GetActorLocation (), next_location, lerp_alpha);
274311 follower->SetActorLocation (next_location);
275312
0 commit comments