13THIRD_PARTY_INCLUDES_START
15THIRD_PARTY_INCLUDES_END
18#include <util/ue-header-guard-begin.h>
19#include "Components/ActorComponent.h"
20#include "Components/PrimitiveComponent.h"
21#include "Engine/StaticMeshActor.h"
22#include "Engine/TextureRenderTarget2D.h"
23#include "Engine/DataAsset.h"
24#include "Engine/StreamableManager.h"
25#include "Async/Future.h"
26#include "Misc/ScopeLock.h"
27#include <util/ue-header-guard-end.h>
29#include <unordered_map>
33#include "CustomTerrainPhysicsComponent.generated.h"
51 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HeightMapDataAsset)
53 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HeightMapDataAsset)
55 UPROPERTY(BlueprintReadWrite, Category = HeightMapDataAsset)
56 TArray<
float> HeightValues;
93 void InitializeTile(uint32_t TextureSize,
float AffectedRadius,
float ParticleSize,
float Depth,
97 void GetParticlesInBox(
const FOrientedBox& OBox, std::vector<FParticle*> &ParticlesInRadius);
121 void Init( uint32 NewTextureSize,
float NewAffectedRadius,
float ParticleDiameter,
122 float Depth,
float NewFloorHeight )
129 UE_LOG(LogCarla, Warning,
152 uint64_t
GetTileId(uint32_t Tile_X, uint32_t Tile_Y);
190 std::vector<uint64_t> Result;
191 for (
auto& Iter :
Map)
193 Result.emplace_back(Iter.first);
199 std::vector<uint64_t> Result;
202 Result.emplace_back(Iter.first);
207 std::unordered_map<uint64_t, FDenseTile>
Map;
230USTRUCT(BlueprintType)
235 UPROPERTY(EditAnywhere, BlueprintReadWrite)
236 FVector Force = FVector::ZeroVector;
237 UPROPERTY(EditAnywhere, BlueprintReadWrite)
238 FVector Location = FVector::ZeroVector;
241UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
242class CARLA_API UCustomTerrainPhysicsComponent : public UActorComponent
248 UCustomTerrainPhysicsComponent();
250 virtual void BeginPlay()
override;
251 virtual void EndPlay(
const EEndPlayReason::Type EndPlayReason)
override;
253 virtual void TickComponent(
float DeltaTime,
254 ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction);
259 UFUNCTION(BlueprintCallable)
260 void AddForces(
const TArray<FForceAtLocation> &Forces);
262 UFUNCTION(BlueprintCallable)
263 static void BuildLandscapeHeightMapDataAasset(ALandscapeProxy* Landscape,
264 int Resolution, FVector MapSize, FString AssetPath, FString AssetName);
266 UFUNCTION(BlueprintCallable)
267 float GetHeightAtLocation(ALandscapeProxy * Landscape, FVector Location);
269 UFUNCTION(BlueprintCallable)
270 TArray<FVector> GetParticlesInRadius(FVector
Position,
float Radius);
272 UFUNCTION(BlueprintCallable)
273 TArray<FVector> GetParticlesInTileRadius(FVector
Position,
float Radius);
275 UFUNCTION(BlueprintCallable)
276 FVector GetTileCenter(FVector
Position);
278 UFUNCTION(BlueprintCallable, Category=
"Tiles")
281 UFUNCTION(BlueprintCallable, Category=
"Texture")
284 UFUNCTION(BlueprintCallable, Category=
"Texture")
285 void UpdateTexture();
287 UFUNCTION(BlueprintCallable, Category=
"Texture")
288 void UpdateLoadedTextureDataRegions();
290 UPROPERTY(EditAnywhere, BlueprintReadWrite)
293 UFUNCTION(BlueprintCallable, Category=
"Texture")
294 void UpdateLargeTexture();
296 UFUNCTION(BlueprintCallable, Category=
"Texture")
297 void UpdateLargeTextureData();
299 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
300 UTexture2D* TextureToUpdate;
301 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
302 float MinDisplacement = -100;
303 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
304 float MaxDisplacement = 100;
306 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
307 UTexture2D* LargeTextureToUpdate;
309 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
310 UMaterialParameterCollection* MPC;
312 UPROPERTY(EditAnywhere, BlueprintReadWrite)
313 FString NeuralModelFile =
"";
315 UPROPERTY(EditAnywhere, BlueprintReadWrite)
316 FVector NextPositionToUpdate = FVector(0,0,0);
318 FVector LastUpdatedPosition;
319 FVector CachePosition;
323 UPROPERTY(EditAnywhere, BlueprintReadWrite)
324 float ForceMulFactor = 1.0;
325 UPROPERTY(EditAnywhere, BlueprintReadWrite)
326 float ParticleForceMulFactor = 1.0;
327 UPROPERTY(EditAnywhere, BlueprintReadWrite)
329 UPROPERTY(EditAnywhere, BlueprintReadWrite)
330 bool bUseSoilType =
false;
331 UPROPERTY(EditAnywhere)
332 bool NNVerbose =
false;
333 UPROPERTY(EditAnywhere, BlueprintReadWrite)
334 bool bUseLocalFrame =
false;
337 void RunNNPhysicsSimulation(
340 void SetUpParticleArrays(std::vector<FParticle*>& ParticlesIn,
341 TArray<float>& ParticlePosOut,
342 TArray<float>& ParticleVelOut,
343 const FTransform &WheelTransform);
345 TArray<float>& WheelPos,
346 TArray<float>& WheelOrientation,
347 TArray<float>& WheelLinearVelocity,
348 TArray<float>& WheelAngularVelocity);
349 void UpdateParticles(
350 std::vector<FParticle*> Particles, std::vector<float> Forces,
351 float DeltaTime,
const FTransform& WheelTransform);
352 void ApplyForcesToVehicle(
354 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3,
355 FVector TorqueWheel0, FVector TorqueWheel1, FVector TorqueWheel2, FVector TorqueWheel3);
356 void ApplyMeanAccelerationToVehicle(
358 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3);
359 void ApplyAccelerationToVehicle(
361 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3);
364 void LimitParticlesPerWheel(std::vector<FParticle*> &Particles);
365 void DrawParticles(UWorld* World, std::vector<FParticle*>& Particles,
366 FLinearColor Color = FLinearColor(1.f, 0.f, 0.f));
367 void DrawParticlesArray(UWorld* World, TArray<float>& ParticlesArray,
368 FLinearColor Color = FLinearColor(1.f, 0.f, 0.f));
369 void DrawOrientedBox(UWorld* World,
const TArray<FOrientedBox>& Boxes);
370 void DrawTiles(UWorld* World,
const std::vector<uint64_t>& TilesIds,
float Height = 0,
371 FLinearColor Color = FLinearColor(0.0,1.0,0.0,1.0));
372 void GenerateBenchmarkParticles(std::vector<FParticle>& BenchParticles,
373 std::vector<FParticle*> &ParticlesWheel0, std::vector<FParticle*> &ParticlesWheel1,
374 std::vector<FParticle*> &ParticlesWheel2, std::vector<FParticle*> &ParticlesWheel3,
375 FOrientedBox &BboxWheel0, FOrientedBox &BboxWheel1,
376 FOrientedBox &BboxWheel2, FOrientedBox &BboxWheel3);
378 void UpdateParticlesDebug(std::vector<FParticle*> Particles);
380 void OnLevelAddedToWorld(ULevel* InLevel, UWorld* InWorld);
382 void UpdateTilesHeightMaps(
const std::vector<FParticle*>& Particles);
383 void RemoveParticlesFromOrderedContainer(
const std::vector<FParticle*>& Particles);
384 void AddParticlesToOrderedContainer(
const std::vector<FParticle*>& Particles);
385 void FlagTilesToRedoOrderedContainer(
const std::vector<FParticle*>& Particles);
388 void AddForceToSingleWheel(USkeletalMeshComponent* SkeletalMeshComponent,
389 FVector WheelPosition, FVector WheelNormalForce);
391 UPROPERTY(EditAnywhere)
392 TArray<FForceAtLocation> ForcesToApply;
393 UPROPERTY(EditAnywhere)
394 TObjectPtr<UPrimitiveComponent> RootComponent;
395 UPROPERTY(EditAnywhere)
396 float RayCastRange = 10.0f;
399 UPROPERTY(EditAnywhere)
400 FVector WorldSize = FVector(200000,200000,0);
401 UPROPERTY(EditAnywhere)
402 bool DrawDebugInfo =
true;
403 UPROPERTY(EditAnywhere)
404 bool bUpdateParticles =
false;
406 UPROPERTY(EditAnywhere, Category=
"Tiles")
407 FVector TileRadius = FVector( 100, 100, 0 );
409 UPROPERTY(EditAnywhere, Category=
"Tiles")
410 FVector CacheRadius = FVector( 50, 50, 0 );
411 UPROPERTY(EditAnywhere, Category=
"Tiles")
412 bool bDrawLoadedTiles =
false;
413 UPROPERTY(EditAnywhere, Category=
"Tiles")
415 UPROPERTY(EditAnywhere, Category=
"Tiles")
416 bool bRemoveLandscapeColliders =
false;
419 UPROPERTY(EditAnywhere, Category=
"Tiles")
420 float TimeToTriggerCacheReload = 20.0f;
422 UPROPERTY(EditAnywhere, Category=
"Tiles")
423 float TimeToTriggerLoadTiles = 1.0f;
424 UPROPERTY(EditAnywhere, Category=
"Tiles")
425 float TimeToTriggerUnLoadTiles = 5.0f;
427 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
428 float TextureRadius = 4.0f;
430 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
431 float LargeTextureRadius = 50.0f;
433 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
434 float EffectMultiplayer = 10.0f;
436 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
438 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
439 TMap<TEnumAsByte<EDefResolutionType>, UTexture2D*> TexturesRes;
441 bool bVisualization =
false;
443 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
444 bool bUseDeformationPlane =
false;
445 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
446 TObjectPtr<UStaticMesh> DeformationPlaneMesh =
nullptr;
447 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
448 TObjectPtr<UMaterialInstance> DeformationPlaneMaterial =
nullptr;
449 UPROPERTY(VisibleAnywhere, Category=
"DeformationMesh")
450 TObjectPtr<AStaticMeshActor> DeformationPlaneActor =
nullptr;
453 TObjectPtr<UMaterialParameterCollectionInstance> MPCInstance;
455 UPROPERTY(EditAnywhere, Category=
"Forces")
456 float NormalForceIntensity = 100;
458 UPROPERTY(EditAnywhere)
459 float SearchRadius = 100;
460 UPROPERTY(EditAnywhere)
461 float ParticleDiameter = 2;
462 UPROPERTY(EditAnywhere)
463 float TerrainDepth = 40;
464 UPROPERTY(EditAnywhere)
465 TObjectPtr<AActor> FloorActor =
nullptr;
466 UPROPERTY(EditAnywhere)
467 bool bUseDynamicModel =
false;
468 UPROPERTY(EditAnywhere)
469 bool bUseCUDAModel =
false;
471 UPROPERTY(EditAnywhere)
472 float TireRadius = 33.0229f;
473 UPROPERTY(EditAnywhere)
474 float TireWidth = 21.21f;
475 UPROPERTY(EditAnywhere)
476 float BoxSearchForwardDistance = 114.39f;
477 UPROPERTY(EditAnywhere)
478 float BoxSearchLateralDistance = 31.815f;
479 UPROPERTY(EditAnywhere)
480 float BoxSearchDepthDistance = 20.f;
481 UPROPERTY(EditAnywhere)
482 bool bDisableVehicleGravity =
false;
483 UPROPERTY(EditAnywhere)
484 float MaxForceMagnitude = 1000000.f;
485 UPROPERTY(EditAnywhere)
486 float FloorHeight = 0.f;
487 UPROPERTY(EditAnywhere)
488 bool bUseImpulse =
false;
489 UPROPERTY(EditAnywhere)
490 bool bUseMeanAcceleration =
false;
491 UPROPERTY(EditAnywhere)
492 bool bShowForces =
true;
493 UPROPERTY(EditAnywhere)
495 UPROPERTY(EditAnywhere)
496 float MaxHeight = 10;
497 UPROPERTY(EditAnywhere)
499 UPROPERTY(EditAnywhere)
500 bool bDrawHeightMap =
false;
501 UPROPERTY(EditAnywhere)
502 FVector DrawStart = FVector(0);
503 UPROPERTY(EditAnywhere)
504 FVector DrawEnd = FVector(1000, 1000, 0);
505 UPROPERTY(EditAnywhere)
506 FVector DrawInterval = FVector(100,100,0);
507 UPROPERTY(EditAnywhere)
509 UPROPERTY(EditAnywhere)
510 FVector HeightMapScaleFactor = FVector(1, 1, 1);
511 UPROPERTY(EditAnywhere)
512 FVector HeightMapOffset = FVector(0, 0, 0);
513 UPROPERTY(EditAnywhere)
514 bool bBenchMark =
false;
515 UPROPERTY(EditAnywhere)
516 int MaxParticlesPerWheel = 6000;
518 UPROPERTY(EditAnywhere)
519 FVector Radius = FVector(10,10,10);
521 UPROPERTY(VisibleAnywhere)
522 FIntVector CurrentLargeMapTileId = FIntVector(-1,-1,0);
523 UPROPERTY(VisibleAnywhere)
524 TObjectPtr<ALargeMapManager> LargeMapManager =
nullptr;
529 UPROPERTY(Transient, DuplicateTransient)
530 TObjectPtr<UHeightMapDataAsset> ActiveHeightMap =
nullptr;
531 TSharedPtr<FStreamableHandle> PendingHeightMapHandle;
532 FIntVector PendingHeightMapTileId = FIntVector(-1, -1, 0);
534 void OnHeightMapLoaded();
537 TArray<TObjectPtr<ACarlaWheeledVehicle>> Vehicles;
540 TArray<uint8> LargeData;
545 TFuture<bool> IterationCompleted;
547 class FRunnableThread* Thread;
553 FTilesWorker(
class UCustomTerrainPhysicsComponent* TerrainComp, FVector NewPosition,
float NewRadiusX,
float NewRadiusY );
557 virtual uint32
Run()
override;
Base class for CARLA wheeled vehicles.
FCriticalSection Lock_GetTile
FIntVector GetVectorTileId(FDVector Position)
float GetHeight(FDVector Position)
FDenseTile & GetTile(uint32_t Tile_X, uint32_t Tile_Y)
FDenseTile & InitializeRegionInCache(uint64_t TileId)
FCriticalSection Lock_Position
FDenseTile & InitializeRegion(uint32_t Tile_X, uint32_t Tile_Y)
void Init(uint32 NewTextureSize, float NewAffectedRadius, float ParticleDiameter, float Depth, float NewFloorHeight)
std::vector< FParticle * > GetParticlesInTileRadius(FDVector Position, float Radius)
FCriticalSection Lock_CacheMap
void InitializeMap(UHeightMapDataAsset *DataAsset, FDVector Origin, FDVector MapSize, float Size, float ScaleZ)
std::vector< uint64_t > GetTileIdInCache()
FCriticalSection Lock_Map
uint64_t GetTileId(uint64_t TileId)
std::vector< uint64_t > GetIntersectingTiles(const FOrientedBox &OBox)
void UpdateMaps(FDVector Position, float RadiusX, float RadiusY, float CacheRadiusX, float CacheRadiusY)
std::vector< uint64_t > GetLoadedTilesInRange(FDVector Position, float Radius)
FCriticalSection Lock_Particles
std::unordered_map< uint64_t, FDenseTile > CacheMap
FSparseHighDetailMap(float ParticleDiameter=0.02f, float Depth=0.4f)
FDVector GetTilePosition(uint64_t TileId)
std::vector< FParticle * > GetParticlesInBox(const FOrientedBox &OBox)
std::vector< uint64_t > GetTileIdInMap()
uint64_t GetTileId(uint32_t Tile_X, uint32_t Tile_Y)
void Update(FVector Position, float RadiusX, float RadiusY)
std::vector< FParticle * > GetParticlesInRadius(FDVector Position, float Radius)
float GetTileSize() const
void UpdateHeightMap(UHeightMapDataAsset *DataAsset, FDVector Origin, FDVector MapSize, float Size, float ScaleZ)
std::unordered_map< uint64_t, FDenseTile > TilesToWrite
std::unordered_map< uint64_t, FDenseTile > Map
void GetParticlesInBox(const FOrientedBox &OBox, std::vector< FParticle * > &ParticlesInRadius)
std::vector< float > ParticlesHeightMap
bool bParticlesZOrderedInitialized
std::vector< std::multiset< float, std::greater< float > > > ParticlesZOrdered
std::vector< FParticle > Particles
void InitializeDataStructure()
void UpdateLocalHeightmap()
void InitializeTile(uint32_t TextureSize, float AffectedRadius, float ParticleSize, float Depth, FDVector TileOrigin, FDVector TileEnd, const FString &SavePath, const FHeightMapData &HeightMap)
void GetAllParticles(std::vector< FParticle * > &ParticlesInRadius)
std::vector< FParticle * > GetParticlesInRadius(FDVector Position, float Radius)
uint32_t PartialHeightMapSize
FDenseTile & operator=(FDenseTile &&Origin)
bool bHeightmapNeedToUpdate
float GetHeight(FDVector Position) const
void InitializeHeightmap(UHeightMapDataAsset *DataAsset, FDVector Size, FDVector Origin, FDVector Tile0, float ScaleZ)
std::vector< float > Pixels
virtual ~FTilesWorker() override
class UCustomTerrainPhysicsComponent * CustomTerrainComp
virtual uint32 Run() override
volatile bool bShouldContinue