Something went wrong on our end
Select Git revision
-
Christoph von Oy authoredChristoph von Oy authored
MagicWand.h 3.73 KiB
#pragma once
#include "CoreMinimal.h"
#include "MagicWandSelectionManager.h"
#include "ProceduralMeshComponent.h"
#include "MetaCastBaseline.h"
#include "Generators/MarchingCubes.h"
#include "Templates/Atomic.h"
#include "MagicWand.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UMagicWand : public UMetaCastBaseline
{
GENERATED_BODY()
// MAGIC WAND
TAtomic<bool> AbortMagicWand = false;
TSharedPtr<FSelectionManager> CurrentSelection;
TMap<float, TSharedPtr<FSelectionManager>> SelectionCache;
TArray<float> SortedProximityRanges;
FVector SeedPointPositionLocal;
FDensityField* MyDensityField;
float CountSelectionTime = 0;
bool IsMagicWandInitialized = false;
// THREADING
mutable FCriticalSection ProceduralMeshGuard;
mutable FCriticalSection SelectionCacheLock;
mutable FCriticalSection ThreadNumberLock;
mutable FThreadSafeCounter NumberThreads = 0;
// VISUALIZATION
bool IsMarchingCubesRunning = false;
bool AbortMarchingCubes = false;
float MarchingCubeSize = 0;
// REFERENCES
UPROPERTY()
UWorld* World;
UPROPERTY()
ULineBatchComponent* MyLineBatchComponent;
UPROPERTY()
UProceduralMeshComponent* ProceduralMesh;
UPROPERTY()
UProceduralMeshComponent* MarchingCubeMesh;
// USER EDITABLE PROPERTIES
UPROPERTY(EditAnywhere)
float MagicWandUpdatesPerSecond = 3;
UPROPERTY(EditAnywhere)
float ThresholdDistanceScaling = 20;
UPROPERTY(EditAnywhere)
float MinThreshold = 0.1;
UPROPERTY(EditAnywhere)
float MaxThreshold = 8;
UPROPERTY(EditAnywhere)
UMaterialInterface* SelectionVolumeMat;
UPROPERTY(EditAnywhere)
int MaxThreadCount = 100;
UPROPERTY(EditAnywhere)
bool UpdateMagicWandDuringDrag = false;
public:
int32 SeedPointIndex;
// INITIALIZATION
UMagicWand();
virtual void BeginPlay() override;
void InitReferences();
void InitProceduralMesh() const;
// INPUT HANDLING
virtual void SelectionEndAction() override;
virtual void SelectionStartAction() override;
virtual void HandleMetaEraseReleased(const FInputActionInstance& Instance) override;
virtual void HandleUndoAction(const FInputActionInstance& Instance) override;
virtual void HandleRedoAction(const FInputActionInstance& Instance) override;
virtual void EraseParticles(const FVector& InputPosition) override;
// VISUALIZATION
void GenerateVoxelMeshWithCubes(const TArray<int32> Voxels) const;
void GenerateVoxelMeshSmooth(const TSet<int32>& Voxels);
// MAGIC WAND SELECTION
void InitMagicWandSelection();
void PerformMagicWandSelection(const float ProximityThreshold);
void FindSeed(const FVector& InputPosition);
void ExpandFromAllPointsInQueue(TQueue<int32>* ProcessQueue, const float ProximityThreshold, int ThreadLoad);
void ExpandFromPoint(TQueue<int32>* ProcessQueue, const float ProximityThreshold, int& ThreadLoad, const FVector& ExpansionPoint, const FVoxelPointLookupTable* MyVoxelPointLookupTable) const;
void FinishSelectionThread(const float ProximityThreshold);
void CreateNewExpansionThread(TQueue<int32>* ProcessQueue, int& NumberToProcess, const float ProximityThreshold);
void CacheSelectionResults(const float ProximityThreshold, const TSharedPtr<FSelectionManager>& SelectionResults);
TSharedPtr<FSelectionManager> GetSelectionCacheResultCopy(float ProximityThreshold);
// TICK
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void SelectParticles(const FVector& InputPosition) override;
void DrawPersistentLine(const FVector& Start, const FVector& End, const FColor& Color, float Duration, float Thickness) const;
void GenerateCylinderBetweenPoints(const FVector& Start, const FVector& End, const float Radius, const int32 NumSides, const FColor Color, const int MeshSectionIndex) const;
};