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

- Cleaned code up a bit further.

- Removed call to MarkRenderStateDirty due to crash - not sure why it crashes but seems to run again now.
- Fixed wrongly committed DynamicLineMaterial.uasset which lead to all colors being white.
parent 065a6f64
Branches
No related tags found
No related merge requests found
No preview for this file type
......@@ -27,7 +27,8 @@ UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer&
SetCanEverAffectNavigation(false);
static ConstructorHelpers::FObjectFinder<UStaticMesh>LineMeshAsset(TEXT("StaticMesh'/InstancedMeshLineRendering/line_subdivided5.line_subdivided5'"));
// Use optimized line here for now.
static ConstructorHelpers::FObjectFinder<UStaticMesh>LineMeshAsset(TEXT("StaticMesh'/InstancedMeshLineRendering/optimized_line.optimized_line'"));
static ConstructorHelpers::FObjectFinder<UMaterial>LineMaterialAsset(TEXT("Material'/InstancedMeshLineRendering/DynamicLineMaterial.DynamicLineMaterial'"));
UStaticMesh* LineAsset = LineMeshAsset.Object;
UMaterial* LineMaterial = LineMaterialAsset.Object;
......@@ -89,22 +90,26 @@ void UGPUInstancedLineComponent::UpdateWholeTexture() const
FLinearColor UGPUInstancedLineComponent::GetLineColor(int32 LineId)
{
const float R = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats];
const float G = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
const float B = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
return { R, G, B };
const float* CustomDataColorPointer = &PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats];
return { *CustomDataColorPointer, *(CustomDataColorPointer + 1), *(CustomDataColorPointer + 2) };
}
int32 UGPUInstancedLineComponent::AddNewSegmentInstance(const FLinearColor& Color, float Width, int32 Index)
{
// Add with a dummy transform
const int32 InstanceId = AddInstance(FTransform::Identity);
InitializeCreatedSegmentInstance(InstanceId, Color, Width, Index);
return InstanceId;
}
void UGPUInstancedLineComponent::InitializeCreatedSegmentInstance(const int InstanceId, const FLinearColor& Color,
float Width, int32 Index)
{
SetCustomDataValue(InstanceId, 0, Color.R, false);
SetCustomDataValue(InstanceId, 1, Color.G, false);
SetCustomDataValue(InstanceId, 2, Color.B, false);
SetCustomDataValue(InstanceId, 3, Width / 2.0f, false);
SetCustomDataValue(InstanceId, 4, static_cast<float>(Index), false); // Segment Start
return InstanceId;
}
void UGPUInstancedLineComponent::UpdateTexture(const FIntPoint& StartIndex, int32 NumberOfPoints, uint8* SrcData, bool bMarkRenderStateDirty)
......@@ -122,8 +127,8 @@ void UGPUInstancedLineComponent::UpdateTexture(const FIntPoint& StartIndex, int3
});
// Probably not a good place to call this
if(bMarkRenderStateDirty)
MarkRenderStateDirty();
//if(bMarkRenderStateDirty)
// MarkRenderStateDirty();
}
void UGPUInstancedLineComponent::Init()
......@@ -526,33 +531,18 @@ bool UGPUInstancedLineComponent::ResizeTexture(int32 Width, int32 Height)
return true;
}
void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int32 NumberOfSegmentsPerLine,
TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths)
{
if (LinearLineData.Num() != 0 || LineMap.Num() != 0 || GetInstanceCount() != 0)
{
LinearLineData.Empty(); // Reset
LineMap.Empty();
//NextFreeId = 0;
CurrentTextureIndex = 0;
CurrentTextureMarker = FIntPoint(0, 0);
ClearInstances();
}
if (Points.Num() != NumberOfLines * (NumberOfSegmentsPerLine + 1))
void UGPUInstancedLineComponent::AddBulkInternal(int32 NumberOfLines, int32 NumberOfSegmentsPerLine,
const TArray<FLinearColor>& Colors, const TArray<float>& Widths)
{
UE_LOG(LogLineRendering, Fatal, TEXT("UGPUInstancedLineComponent::InitializeLinesInBulk : Inconsistent number of points and lines!"));
}
LinearLineData = MoveTemp(Points);
// Add the number of instances - sadly this requires the for loop
const bool UniformColor = Colors.Num() == 1;
const bool UniformWidth = Widths.Num() == 1;
TArray<FTransform> DummyTransforms;
DummyTransforms.SetNum(NumberOfLines * NumberOfSegmentsPerLine);
AddInstances(DummyTransforms, false);
for (int32 LineIndex = 0; LineIndex < NumberOfLines; ++LineIndex)
{
const FLinearColor Color = UniformColor ? Colors[0] : Colors[LineIndex];
......@@ -563,7 +553,9 @@ void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int3
for (int32 PointIndex = 0; PointIndex < NumberOfSegmentsPerLine; ++PointIndex)
{
const int32 InstanceId = AddNewSegmentInstance(Color, Width, CurrentTextureIndex);
//const int32 InstanceId = AddNewSegmentInstance(Color, Width, CurrentTextureIndex);
const int32 InstanceId = CurrentTextureIndex;
InitializeCreatedSegmentInstance(InstanceId, Color, Width, CurrentTextureIndex);
NewLineArray.IndexArray.Add({ InstanceId, LineIndex * (NumberOfSegmentsPerLine + 1) + PointIndex });
CurrentTextureIndex++;
......@@ -579,6 +571,31 @@ void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int3
UpdateWholeTexture();
}
void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int32 NumberOfSegmentsPerLine,
TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths)
{
if (LinearLineData.Num() != 0 || LineMap.Num() != 0 || GetInstanceCount() != 0)
{
LinearLineData.Empty(); // Reset
LineMap.Empty();
//NextFreeId = 0;
CurrentTextureIndex = 0;
CurrentTextureMarker = FIntPoint(0, 0);
ClearInstances();
}
if (Points.Num() != NumberOfLines * (NumberOfSegmentsPerLine + 1))
{
UE_LOG(LogLineRendering, Fatal, TEXT("UGPUInstancedLineComponent::InitializeLinesInBulk : Inconsistent number of points and lines!"));
}
LinearLineData = MoveTemp(Points);
AddBulkInternal(NumberOfLines, NumberOfSegmentsPerLine, Colors, Widths);
}
void UGPUInstancedLineComponent::InitializeLinesInBulkTesselate(int32 NumberOfLines, int32 NumberOfSegmentsPerLine,
TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths)
{
......@@ -623,36 +640,7 @@ void UGPUInstancedLineComponent::InitializeLinesInBulkTesselate(int32 NumberOfLi
}
// Add the number of instances - sadly this requires the for loop
const bool UniformColor = Colors.Num() == 1;
const bool UniformWidth = Widths.Num() == 1;
for (int32 LineIndex = 0; LineIndex < NumberOfLines; ++LineIndex)
{
const FLinearColor Color = UniformColor ? Colors[0] : Colors[LineIndex];
const float Width = UniformWidth ? Widths[0] : Widths[LineIndex];
const int32 Idx = LineMap.Add(FGPULineArray());
FGPULineArray& NewLineArray = LineMap[Idx];
NewLineArray.IndexArray.Reserve(NumberOfSegmentsPerLine + 1);
for (int32 PointIndex = 0; PointIndex < NumberOfSegmentsPerLine; ++PointIndex)
{
const int32 InstanceId = AddNewSegmentInstance(Color, Width, CurrentTextureIndex);
NewLineArray.IndexArray.Add({ InstanceId, LineIndex * (NumberOfSegmentsPerLine + 1) + PointIndex });
CurrentTextureIndex++;
MoveTextureMarker();
}
NewLineArray.IndexArray.Add({ -1, LineIndex * (NumberOfSegmentsPerLine + 1) + NumberOfSegmentsPerLine });
MoveTextureMarker();
CurrentTextureIndex++;
//NextFreeId++;
}
MarkRenderStateDirty();
UpdateWholeTexture();
AddBulkInternal(NumberOfLines, NumberOfSegmentsPerLine, Colors, Widths);
}
......
......@@ -132,8 +132,11 @@ private:
int32 AddNewSegmentInstance(const FLinearColor& Color, float Width, int32 Index);
void InitializeCreatedSegmentInstance(const int InstanceId, const FLinearColor& Color, float Width, int32 Index);
void UpdateTexture(const FIntPoint& StartIndex, int32 NumberOfPoints, uint8* SrcData, bool bMarkRenderStateDirty = true);
void AddBulkInternal(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, const TArray<FLinearColor>& Colors, const TArray<float>& Widths);
public:
void UpdateWholeTexture() const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment