Skip to content
Snippets Groups Projects
Commit 935b73ff authored by David Gilbert's avatar David Gilbert :bug:
Browse files

- Moved Init function from BeginPlay to PostLoad and OnComponentCreated. This...

- Moved Init function from BeginPlay to PostLoad and OnComponentCreated. This means the Init function is going to be called a multitude of times unfortunately. Seems to work so far.
- Added basic support for lines in the editor.
parent b92841eb
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -68,6 +68,41 @@ void UGPUInstancedLineComponent::UpdateWholeTexture() ...@@ -68,6 +68,41 @@ void UGPUInstancedLineComponent::UpdateWholeTexture()
}); });
} }
void UGPUInstancedLineComponent::Init()
{
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::Init Creating Texture"));
PositionTexture = UTexture2D::CreateTransient(TextureWidth, TextureHeight, PF_A32B32G32R32F);
// Allocate the texture RHI
PositionTexture->UpdateResource();
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::Init Creating MID"));
DynamicLineMaterial = CreateAndSetMaterialInstanceDynamic(0);// UMaterialInstanceDynamic::Create(LineMat, this, "DynamicLineMaterialMID");
if(DynamicLineMaterial)
{
DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture);
DynamicLineMaterial->SetScalarParameterValue("TextureWidth", TextureWidth);
}
else
UE_LOG(LogTemp, Error, TEXT("UGPUInstancedLineComponent::Init DynamicLineMaterial was nullptr"));
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::Init Resetting Data"));
LineMap.Empty();
LinearLineData.Empty();
NextFreeId = 0;
CurrentTextureIndex = 0;
CurrentTextureMarker = FIntPoint(0, 0);
ClearInstances();
bIsInitialized = true;
for (FEditorLineData& EditorLine : EditorLines)
{
AddLineFromEditorData(EditorLine);
}
}
FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(const FIntPoint& StartIndex, FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(const FIntPoint& StartIndex,
int32 NumberOfPoints, int32& NumberOfRegionsOut) int32 NumberOfPoints, int32& NumberOfRegionsOut)
{ {
...@@ -132,48 +167,20 @@ void UGPUInstancedLineComponent::BeginPlay() ...@@ -132,48 +167,20 @@ void UGPUInstancedLineComponent::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
//if (PositionTexture == nullptr) //Init();
if(!bIsInitialized)
{ {
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::BeginPlay Creating Texture")); UE_LOG(LogTemp, Fatal, TEXT("UGPUInstancedLineComponent::BeginPlay: FATAL ERROR - Component is not initialized on BeginPlay!"));
PositionTexture = UTexture2D::CreateTransient(TextureWidth, TextureHeight, PF_A32B32G32R32F); // TODO Hardcoded values
// Allocate the texture RHI
PositionTexture->UpdateResource();
} }
//if (DynamicLineMaterial == nullptr)
{
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::BeginPlay Creating MID"));
//UMaterial* LineMat = LoadObject<UMaterial>(GetTransientPackage(), TEXT("Material'/InstancedMeshLineRendering/DynamicLineMaterial.DynamicLineMaterial'"));
DynamicLineMaterial = CreateAndSetMaterialInstanceDynamic(0);// UMaterialInstanceDynamic::Create(LineMat, this, "DynamicLineMaterialMID");
//GetStaticMesh()->SetMaterial(0, DynamicLineMaterial);
DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture);
DynamicLineMaterial->SetScalarParameterValue("TextureWidth", TextureWidth);
}
// TODO
LineMap.Empty();
LinearLineData.Empty();
NextFreeId = 0;
CurrentTextureIndex = 0;
CurrentTextureMarker = FIntPoint(0, 0);
ClearInstances();
} }
// Doesn't do anything yet, just as a testing function to see execution order on Unreal startup. // Doesn't do anything yet, just as a testing function to see execution order on Unreal startup.
void UGPUInstancedLineComponent::PostInitProperties() void UGPUInstancedLineComponent::PostInitProperties()
{ {
Super::PostInitProperties(); Super::PostInitProperties();
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::PostInitProperties"));
if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
{
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::PostInitProperties IF")); UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::PostInitProperties IF"));
}
} }
void UGPUInstancedLineComponent::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) void UGPUInstancedLineComponent::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent)
...@@ -226,6 +233,9 @@ void UGPUInstancedLineComponent::PostLoad() ...@@ -226,6 +233,9 @@ void UGPUInstancedLineComponent::PostLoad()
if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
{ {
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::PostLoad IF")); UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::PostLoad IF"));
if (!bIsInitialized)
Init();
} }
} }
...@@ -237,6 +247,9 @@ void UGPUInstancedLineComponent::OnComponentCreated() ...@@ -237,6 +247,9 @@ void UGPUInstancedLineComponent::OnComponentCreated()
if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
{ {
UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::OnComponentCreated IF")); UE_LOG(LogTemp, Display, TEXT("UGPUInstancedLineComponent::OnComponentCreated IF"));
if (!bIsInitialized)
Init();
} }
} }
...@@ -323,6 +336,14 @@ int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector>& Line, FLinearCo ...@@ -323,6 +336,14 @@ int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector>& Line, FLinearCo
return LineId; return LineId;
} }
int32 UGPUInstancedLineComponent::AddLineFromEditorData(const FEditorLineData& LineData)
{
const TArray<FEditorPoint>* EditorLineDataPtr = &LineData.Points;
const TArray<FVector>* FVectorLineDataPtr = reinterpret_cast<const TArray<FVector>*>(EditorLineDataPtr);
return AddLine(*FVectorLineDataPtr, LineData.Color, LineData.Width);
}
// Add a point to the end of the specified line // Add a point to the end of the specified line
bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point)
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Components/HierarchicalInstancedStaticMeshComponent.h" #include "Components/InstancedStaticMeshComponent.h"
#include "GPUInstancedLineComponent.generated.h" #include "GPUInstancedLineComponent.generated.h"
/** /**
...@@ -21,7 +21,7 @@ typedef TArray<GPULineIndices> GPULineArray; ...@@ -21,7 +21,7 @@ typedef TArray<GPULineIndices> GPULineArray;
// Maps a LineId to a LineArray. // Maps a LineId to a LineArray.
typedef TMap<int32, GPULineArray> GPULinesMap; typedef TMap<int32, GPULineArray> GPULinesMap;
USTRUCT() USTRUCT(BlueprintType)
struct FEditorPoint struct FEditorPoint
{ {
GENERATED_USTRUCT_BODY() GENERATED_USTRUCT_BODY()
...@@ -40,7 +40,7 @@ struct FEditorPoint ...@@ -40,7 +40,7 @@ struct FEditorPoint
}; };
USTRUCT() USTRUCT(BlueprintType)
struct FEditorLineData struct FEditorLineData
{ {
GENERATED_USTRUCT_BODY() GENERATED_USTRUCT_BODY()
...@@ -86,8 +86,20 @@ private: ...@@ -86,8 +86,20 @@ private:
void UpdateWholeTexture(); void UpdateWholeTexture();
void Init();
FUpdateTextureRegion2D* CalculateTextureRegions(const FIntPoint& StartIndex, int32 NumberOfPoints, int32& NumberOfRegionsOut); FUpdateTextureRegion2D* CalculateTextureRegions(const FIntPoint& StartIndex, int32 NumberOfPoints, int32& NumberOfRegionsOut);
GPULinesMap LineMap;
TArray<FVector4> LinearLineData;
int32 NextFreeId = 0;
int32 CurrentTextureIndex;
FIntPoint CurrentTextureMarker;
bool bIsInitialized = false;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
...@@ -97,7 +109,6 @@ protected: ...@@ -97,7 +109,6 @@ protected:
virtual void OnComponentCreated() override; virtual void OnComponentCreated() override;
virtual void BeginDestroy() override; virtual void BeginDestroy() override;
public: public:
UGPUInstancedLineComponent(const FObjectInitializer& ObjectInitializer); UGPUInstancedLineComponent(const FObjectInitializer& ObjectInitializer);
...@@ -124,6 +135,16 @@ public: ...@@ -124,6 +135,16 @@ public:
UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent")
int32 AddLine(const TArray<FVector>& Line, FLinearColor Color, float Width = 1.0); int32 AddLine(const TArray<FVector>& Line, FLinearColor Color, float Width = 1.0);
/**
* Adds a line and returns the respective ID of the line, which can be used to identify it for updating, modifying and removing.
*
* @param LineData [in] The Line data stored in a FEditorLineData struct.
*
* @return int32 Id of the line, can be used to access it later on.
*/
UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent")
int32 AddLineFromEditorData(const FEditorLineData& LineData);
/** /**
* Adds a point at the end of the specified line. * Adds a point at the end of the specified line.
* *
...@@ -276,16 +297,7 @@ public: ...@@ -276,16 +297,7 @@ public:
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, SkipSerialization) UPROPERTY(BlueprintReadOnly, VisibleAnywhere, SkipSerialization)
UMaterialInstanceDynamic* DynamicLineMaterial; UMaterialInstanceDynamic* DynamicLineMaterial;
UPROPERTY(EditAnywhere, SkipSerialization, DisplayName = "Lines", meta = (MakeEditWidget = true, EditFixedOrder)) UPROPERTY(EditAnywhere, DisplayName = "Lines", meta = (MakeEditWidget = true, EditFixedOrder))
TArray<FEditorLineData> EditorLines; TArray<FEditorLineData> EditorLines;
private:
GPULinesMap LineMap;
TArray<FVector4> LinearLineData;
int32 NextFreeId = 0;
int32 CurrentTextureIndex;
FIntPoint CurrentTextureMarker;
}; };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment