9#include "Components/ActorComponent.h"
10#include "Components/PrimitiveComponent.h"
11#include "Engine/StaticMeshActor.h"
12#include "Misc/ScopeLock.h"
16#include "Engine/TextureRenderTarget2D.h"
18#include "Engine/DataAsset.h"
19#include "Async/Future.h"
21THIRD_PARTY_INCLUDES_START
23THIRD_PARTY_INCLUDES_END
26#include <unordered_map>
28#include "Misc/ScopeLock.h"
31#include "CustomTerrainPhysicsComponent.generated.h"
49 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HeightMapDataAsset)
51 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HeightMapDataAsset)
53 UPROPERTY(BlueprintReadWrite, Category = HeightMapDataAsset)
54 TArray<
float> HeightValues;
91 void InitializeTile(uint32_t TextureSize,
float AffectedRadius,
float ParticleSize,
float Depth,
95 void GetParticlesInBox(
const FOrientedBox& OBox, std::vector<FParticle*> &ParticlesInRadius);
119 void Init( uint32 NewTextureSize,
float NewAffectedRadius,
float ParticleDiameter,
120 float Depth,
float NewFloorHeight )
127 UE_LOG(LogCarla, Warning,
150 uint64_t
GetTileId(uint32_t Tile_X, uint32_t Tile_Y);
188 std::vector<uint64_t> Result;
189 for (
auto& Iter :
Map)
191 Result.emplace_back(Iter.first);
197 std::vector<uint64_t> Result;
200 Result.emplace_back(Iter.first);
205 std::unordered_map<uint64_t, FDenseTile>
Map;
228USTRUCT(BlueprintType)
233 UPROPERTY(EditAnywhere, BlueprintReadWrite)
235 UPROPERTY(EditAnywhere, BlueprintReadWrite)
239UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
240class CARLA_API UCustomTerrainPhysicsComponent : public UActorComponent
246 UCustomTerrainPhysicsComponent();
248 virtual void BeginPlay()
override;
249 virtual void EndPlay(
const EEndPlayReason::Type EndPlayReason)
override;
251 virtual void TickComponent(
float DeltaTime,
252 ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction);
257 UFUNCTION(BlueprintCallable)
258 void AddForces(
const TArray<FForceAtLocation> &Forces);
260 UFUNCTION(BlueprintCallable)
261 static void BuildLandscapeHeightMapDataAasset(ALandscapeProxy* Landscape,
262 int Resolution, FVector MapSize, FString AssetPath, FString AssetName);
264 UFUNCTION(BlueprintCallable)
265 float GetHeightAtLocation(ALandscapeProxy * Landscape, FVector Location);
267 UFUNCTION(BlueprintCallable)
268 TArray<FVector> GetParticlesInRadius(FVector
Position,
float Radius);
270 UFUNCTION(BlueprintCallable)
271 TArray<FVector> GetParticlesInTileRadius(FVector
Position,
float Radius);
273 UFUNCTION(BlueprintCallable)
274 FVector GetTileCenter(FVector
Position);
276 UFUNCTION(BlueprintCallable, Category=
"Tiles")
279 UFUNCTION(BlueprintCallable, Category=
"Texture")
282 UFUNCTION(BlueprintCallable, Category=
"Texture")
283 void UpdateTexture();
285 UFUNCTION(BlueprintCallable, Category=
"Texture")
286 void UpdateLoadedTextureDataRegions();
288 UPROPERTY(EditAnywhere, BlueprintReadWrite)
291 UFUNCTION(BlueprintCallable, Category=
"Texture")
292 void UpdateLargeTexture();
294 UFUNCTION(BlueprintCallable, Category=
"Texture")
295 void UpdateLargeTextureData();
297 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
298 UTexture2D* TextureToUpdate;
299 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
300 float MinDisplacement = -100;
301 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
302 float MaxDisplacement = 100;
304 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
305 UTexture2D* LargeTextureToUpdate;
307 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=
"MaterialParameters")
308 UMaterialParameterCollection* MPC;
310 UPROPERTY(EditAnywhere, BlueprintReadWrite)
311 FString NeuralModelFile =
"";
313 UPROPERTY(EditAnywhere, BlueprintReadWrite)
314 FVector NextPositionToUpdate = FVector(0,0,0);
316 FVector LastUpdatedPosition;
317 FVector CachePosition;
321 UPROPERTY(EditAnywhere, BlueprintReadWrite)
322 float ForceMulFactor = 1.0;
323 UPROPERTY(EditAnywhere, BlueprintReadWrite)
324 float ParticleForceMulFactor = 1.0;
325 UPROPERTY(EditAnywhere, BlueprintReadWrite)
327 UPROPERTY(EditAnywhere, BlueprintReadWrite)
328 bool bUseSoilType =
false;
329 UPROPERTY(EditAnywhere)
330 bool NNVerbose =
false;
331 UPROPERTY(EditAnywhere, BlueprintReadWrite)
332 bool bUseLocalFrame =
false;
335 void RunNNPhysicsSimulation(
338 void SetUpParticleArrays(std::vector<FParticle*>& ParticlesIn,
339 TArray<float>& ParticlePosOut,
340 TArray<float>& ParticleVelOut,
341 const FTransform &WheelTransform);
343 TArray<float>& WheelPos,
344 TArray<float>& WheelOrientation,
345 TArray<float>& WheelLinearVelocity,
346 TArray<float>& WheelAngularVelocity);
347 void UpdateParticles(
348 std::vector<FParticle*> Particles, std::vector<float> Forces,
349 float DeltaTime,
const FTransform& WheelTransform);
350 void ApplyForcesToVehicle(
352 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3,
353 FVector TorqueWheel0, FVector TorqueWheel1, FVector TorqueWheel2, FVector TorqueWheel3);
354 void ApplyMeanAccelerationToVehicle(
356 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3);
357 void ApplyAccelerationToVehicle(
359 FVector ForceWheel0, FVector ForceWheel1, FVector ForceWheel2, FVector ForceWheel3);
362 void LimitParticlesPerWheel(std::vector<FParticle*> &Particles);
363 void DrawParticles(UWorld* World, std::vector<FParticle*>& Particles,
364 FLinearColor Color = FLinearColor(1.f, 0.f, 0.f));
365 void DrawParticlesArray(UWorld* World, TArray<float>& ParticlesArray,
366 FLinearColor Color = FLinearColor(1.f, 0.f, 0.f));
367 void DrawOrientedBox(UWorld* World,
const TArray<FOrientedBox>& Boxes);
368 void DrawTiles(UWorld* World,
const std::vector<uint64_t>& TilesIds,
float Height = 0,
369 FLinearColor Color = FLinearColor(0.0,1.0,0.0,1.0));
370 void GenerateBenchmarkParticles(std::vector<FParticle>& BenchParticles,
371 std::vector<FParticle*> &ParticlesWheel0, std::vector<FParticle*> &ParticlesWheel1,
372 std::vector<FParticle*> &ParticlesWheel2, std::vector<FParticle*> &ParticlesWheel3,
373 FOrientedBox &BboxWheel0, FOrientedBox &BboxWheel1,
374 FOrientedBox &BboxWheel2, FOrientedBox &BboxWheel3);
376 void UpdateParticlesDebug(std::vector<FParticle*> Particles);
378 void OnLevelAddedToWorld(ULevel* InLevel, UWorld* InWorld);
380 void UpdateTilesHeightMaps(
const std::vector<FParticle*>& Particles);
381 void RemoveParticlesFromOrderedContainer(
const std::vector<FParticle*>& Particles);
382 void AddParticlesToOrderedContainer(
const std::vector<FParticle*>& Particles);
383 void FlagTilesToRedoOrderedContainer(
const std::vector<FParticle*>& Particles);
386 void AddForceToSingleWheel(USkeletalMeshComponent* SkeletalMeshComponent,
387 FVector WheelPosition, FVector WheelNormalForce);
389 UPROPERTY(EditAnywhere)
390 TArray<FForceAtLocation> ForcesToApply;
391 UPROPERTY(EditAnywhere)
393 UPROPERTY(EditAnywhere)
394 float RayCastRange = 10.0f;
397 UPROPERTY(EditAnywhere)
398 FVector WorldSize = FVector(200000,200000,0);
399 UPROPERTY(EditAnywhere)
400 bool DrawDebugInfo =
true;
401 UPROPERTY(EditAnywhere)
402 bool bUpdateParticles =
false;
404 UPROPERTY(EditAnywhere, Category=
"Tiles")
405 FVector TileRadius = FVector( 100, 100, 0 );
407 UPROPERTY(EditAnywhere, Category=
"Tiles")
408 FVector CacheRadius = FVector( 50, 50, 0 );
409 UPROPERTY(EditAnywhere, Category=
"Tiles")
410 bool bDrawLoadedTiles =
false;
411 UPROPERTY(EditAnywhere, Category=
"Tiles")
413 UPROPERTY(EditAnywhere, Category=
"Tiles")
414 bool bRemoveLandscapeColliders =
false;
417 UPROPERTY(EditAnywhere, Category=
"Tiles")
418 float TimeToTriggerCacheReload = 20.0f;
420 UPROPERTY(EditAnywhere, Category=
"Tiles")
421 float TimeToTriggerLoadTiles = 1.0f;
422 UPROPERTY(EditAnywhere, Category=
"Tiles")
423 float TimeToTriggerUnLoadTiles = 5.0f;
425 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
426 float TextureRadius = 4.0f;
428 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
429 float LargeTextureRadius = 50.0f;
431 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
432 float EffectMultiplayer = 10.0f;
434 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
436 UPROPERTY(EditAnywhere, Category=
"MaterialParameters")
437 TMap<TEnumAsByte<EDefResolutionType>, UTexture2D*> TexturesRes;
439 bool bVisualization =
false;
441 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
442 bool bUseDeformationPlane =
false;
443 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
444 UStaticMesh* DeformationPlaneMesh =
nullptr;
445 UPROPERTY(EditAnywhere, Category=
"DeformationMesh")
446 UMaterialInstance* DeformationPlaneMaterial =
nullptr;
447 UPROPERTY(VisibleAnywhere, Category=
"DeformationMesh")
448 AStaticMeshActor* DeformationPlaneActor =
nullptr;
451 UMaterialParameterCollectionInstance* MPCInstance;
453 UPROPERTY(EditAnywhere, Category=
"Forces")
454 float NormalForceIntensity = 100;
456 UPROPERTY(EditAnywhere)
457 float SearchRadius = 100;
458 UPROPERTY(EditAnywhere)
459 float ParticleDiameter = 2;
460 UPROPERTY(EditAnywhere)
461 float TerrainDepth = 40;
462 UPROPERTY(EditAnywhere)
463 AActor *FloorActor =
nullptr;
464 UPROPERTY(EditAnywhere)
465 bool bUseDynamicModel =
false;
466 UPROPERTY(EditAnywhere)
467 bool bUseCUDAModel =
false;
469 UPROPERTY(EditAnywhere)
470 float TireRadius = 33.0229f;
471 UPROPERTY(EditAnywhere)
472 float TireWidth = 21.21f;
473 UPROPERTY(EditAnywhere)
474 float BoxSearchForwardDistance = 114.39f;
475 UPROPERTY(EditAnywhere)
476 float BoxSearchLateralDistance = 31.815f;
477 UPROPERTY(EditAnywhere)
478 float BoxSearchDepthDistance = 20.f;
479 UPROPERTY(EditAnywhere)
480 bool bDisableVehicleGravity =
false;
481 UPROPERTY(EditAnywhere)
482 float MaxForceMagnitude = 1000000.f;
483 UPROPERTY(EditAnywhere)
484 float FloorHeight = 0.f;
485 UPROPERTY(EditAnywhere)
486 bool bUseImpulse =
false;
487 UPROPERTY(EditAnywhere)
488 bool bUseMeanAcceleration =
false;
489 UPROPERTY(EditAnywhere)
490 bool bShowForces =
true;
491 UPROPERTY(EditAnywhere)
493 UPROPERTY(EditAnywhere)
494 float MaxHeight = 10;
495 UPROPERTY(EditAnywhere)
497 UPROPERTY(EditAnywhere)
498 bool bDrawHeightMap =
false;
499 UPROPERTY(EditAnywhere)
500 FVector DrawStart = FVector(0);
501 UPROPERTY(EditAnywhere)
502 FVector DrawEnd = FVector(1000, 1000, 0);
503 UPROPERTY(EditAnywhere)
504 FVector DrawInterval = FVector(100,100,0);
505 UPROPERTY(EditAnywhere)
507 UPROPERTY(EditAnywhere)
508 FVector HeightMapScaleFactor = FVector(1, 1, 1);
509 UPROPERTY(EditAnywhere)
510 FVector HeightMapOffset = FVector(0, 0, 0);
511 UPROPERTY(EditAnywhere)
512 bool bBenchMark =
false;
513 UPROPERTY(EditAnywhere)
514 int MaxParticlesPerWheel = 6000;
516 UPROPERTY(EditAnywhere)
517 FVector Radius = FVector(10,10,10);
519 UPROPERTY(VisibleAnywhere)
520 FIntVector CurrentLargeMapTileId = FIntVector(-1,-1,0);
521 UPROPERTY(VisibleAnywhere)
524 TArray<ACarlaWheeledVehicle*> Vehicles;
527 TArray<uint8> LargeData;
532 TFuture<bool> IterationCompleted;
534 class FRunnableThread* Thread;
540 FTilesWorker(
class UCustomTerrainPhysicsComponent* TerrainComp, FVector NewPosition,
float NewRadiusX,
float NewRadiusY );
544 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
FTilesWorker(class UCustomTerrainPhysicsComponent *TerrainComp, FVector NewPosition, float NewRadiusX, float NewRadiusY)
volatile bool bShouldContinue