ledcube3d/Source/ledcube3d/LedVolumeDisplayActor.cpp

1469 lines
47 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "LedVolumeDisplayActor.h"
#include "Components/HierarchicalInstancedStaticMeshComponent.h"
#include "Components/InstancedStaticMeshComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
#include "EngineUtils.h"
#include "Kismet/GameplayStatics.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "Camera/PlayerCameraManager.h"
#include "RotatingVoxelCubeActor.h"
#include "UObject/ConstructorHelpers.h"
#if WITH_EDITOR
#include "Editor.h"
#include "LevelEditorViewport.h"
#endif
namespace
{
const FName LedColorParameterName(TEXT("LedColor"));
const FName BrightnessParameterName(TEXT("Brightness"));
const FName GammaParameterName(TEXT("Gamma"));
const FName AnimationSpeedParameterName(TEXT("AnimationSpeed"));
const FName PatternModeParameterName(TEXT("PatternMode"));
const FName VoxelSpacingParameterName(TEXT("VoxelSpacing"));
const FName VolumeCenterParameterName(TEXT("VolumeCenter"));
const FName VolumeExtentParameterName(TEXT("VolumeExtent"));
const FName PixelRadiusFractionParameterName(TEXT("PixelRadiusFraction"));
const FName VoxelShapeModeParameterName(TEXT("VoxelShapeMode"));
FLinearColor ApplyBrightnessAndGamma(const FLinearColor& InColor, float Brightness, float Gamma)
{
const float SafeGamma = FMath::Max(0.001f, Gamma);
return FLinearColor(
FMath::Pow(FMath::Max(0.0f, InColor.R), SafeGamma) * Brightness,
FMath::Pow(FMath::Max(0.0f, InColor.G), SafeGamma) * Brightness,
FMath::Pow(FMath::Max(0.0f, InColor.B), SafeGamma) * Brightness,
InColor.A);
}
}
ALedVolumeDisplayActor::ALedVolumeDisplayActor()
{
PrimaryActorTick.bCanEverTick = true;
VoxelInstances = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("VoxelInstances"));
SetRootComponent(VoxelInstances);
VoxelInstances->SetCollisionEnabled(ECollisionEnabled::NoCollision);
VoxelInstances->SetCanEverAffectNavigation(false);
VoxelInstances->bCastDynamicShadow = false;
VoxelInstances->CastShadow = false;
VoxelInstances->NumCustomDataFloats = 4;
GpuVolumeProxy = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GpuVolumeProxy"));
GpuVolumeProxy->SetupAttachment(VoxelInstances);
GpuVolumeProxy->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GpuVolumeProxy->SetCanEverAffectNavigation(false);
GpuVolumeProxy->bCastDynamicShadow = false;
GpuVolumeProxy->CastShadow = false;
GpuVolumeProxy->SetHiddenInGame(true);
GpuVolumeProxy->SetVisibility(false);
GpuVoxelInstances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("GpuVoxelInstances"));
GpuVoxelInstances->SetupAttachment(VoxelInstances);
GpuVoxelInstances->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GpuVoxelInstances->SetCanEverAffectNavigation(false);
GpuVoxelInstances->bCastDynamicShadow = false;
GpuVoxelInstances->CastShadow = false;
GpuVoxelInstances->SetHiddenInGame(true);
GpuVoxelInstances->SetVisibility(false);
GpuVoxelInstances->NumCustomDataFloats = 0;
GpuVoxelInstances->SetCullDistances(0, 0);
PhysicalWireInstances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("PhysicalWireInstances"));
PhysicalWireInstances->SetupAttachment(VoxelInstances);
PhysicalWireInstances->SetCollisionEnabled(ECollisionEnabled::NoCollision);
PhysicalWireInstances->SetCanEverAffectNavigation(false);
PhysicalWireInstances->bCastDynamicShadow = false;
PhysicalWireInstances->CastShadow = false;
PhysicalWireInstances->SetHiddenInGame(true);
PhysicalWireInstances->SetVisibility(false);
PhysicalWireInstances->SetCullDistances(0, 0);
PhysicalCabinetInstances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("PhysicalCabinetInstances"));
PhysicalCabinetInstances->SetupAttachment(VoxelInstances);
PhysicalCabinetInstances->SetCollisionEnabled(ECollisionEnabled::NoCollision);
PhysicalCabinetInstances->SetCanEverAffectNavigation(false);
PhysicalCabinetInstances->bCastDynamicShadow = false;
PhysicalCabinetInstances->CastShadow = false;
PhysicalCabinetInstances->SetHiddenInGame(true);
PhysicalCabinetInstances->SetVisibility(false);
PhysicalCabinetInstances->SetCullDistances(0, 0);
PhysicalBottomFixerInstances = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("PhysicalBottomFixerInstances"));
PhysicalBottomFixerInstances->SetupAttachment(VoxelInstances);
PhysicalBottomFixerInstances->SetCollisionEnabled(ECollisionEnabled::NoCollision);
PhysicalBottomFixerInstances->SetCanEverAffectNavigation(false);
PhysicalBottomFixerInstances->bCastDynamicShadow = false;
PhysicalBottomFixerInstances->CastShadow = false;
PhysicalBottomFixerInstances->SetHiddenInGame(true);
PhysicalBottomFixerInstances->SetVisibility(false);
PhysicalBottomFixerInstances->SetCullDistances(0, 0);
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultSphereMesh(TEXT("/Engine/BasicShapes/Sphere.Sphere"));
if (DefaultSphereMesh.Succeeded())
{
VoxelInstances->SetStaticMesh(DefaultSphereMesh.Object);
}
static ConstructorHelpers::FObjectFinder<UMaterialInterface> DefaultLedMaterial(TEXT("/Game/LedVolume/Materials/M_LedVoxelGpu.M_LedVoxelGpu"));
if (DefaultLedMaterial.Succeeded())
{
LedMaterial = DefaultLedMaterial.Object;
}
else
{
static ConstructorHelpers::FObjectFinder<UMaterialInterface> LegacyLedMaterial(TEXT("/Game/LedVolume/Materials/M_LedVoxel.M_LedVoxel"));
if (LegacyLedMaterial.Succeeded())
{
LedMaterial = LegacyLedMaterial.Object;
}
}
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultPlaneMesh(TEXT("/Engine/BasicShapes/Plane.Plane"));
if (DefaultPlaneMesh.Succeeded())
{
GpuVolumeProxy->SetStaticMesh(DefaultPlaneMesh.Object);
}
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultCubeMesh(TEXT("/Engine/BasicShapes/Cube.Cube"));
if (DefaultCubeMesh.Succeeded())
{
GpuVoxelInstances->SetStaticMesh(DefaultCubeMesh.Object);
PhysicalCabinetInstances->SetStaticMesh(DefaultCubeMesh.Object);
}
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultCylinderMesh(TEXT("/Engine/BasicShapes/Cylinder.Cylinder"));
if (DefaultCylinderMesh.Succeeded())
{
PhysicalWireInstances->SetStaticMesh(DefaultCylinderMesh.Object);
PhysicalBottomFixerInstances->SetStaticMesh(DefaultCylinderMesh.Object);
}
static ConstructorHelpers::FObjectFinder<UMaterialInterface> DefaultGpuVolumeMaterial(TEXT("/Game/LedVolume/Materials/M_LedVolumeRaymarch.M_LedVolumeRaymarch"));
if (DefaultGpuVolumeMaterial.Succeeded())
{
GpuVolumeMaterial = DefaultGpuVolumeMaterial.Object;
}
static ConstructorHelpers::FObjectFinder<UMaterialInterface> DefaultCabinetMaterial(TEXT("/Game/LedVolume/Materials/M_PhysicalCabinet.M_PhysicalCabinet"));
if (DefaultCabinetMaterial.Succeeded())
{
CabinetMaterial = DefaultCabinetMaterial.Object;
}
static ConstructorHelpers::FObjectFinder<UMaterialInterface> DefaultWireMaterial(TEXT("/Game/LedVolume/Materials/M_PhysicalWire.M_PhysicalWire"));
if (DefaultWireMaterial.Succeeded())
{
WireMaterial = DefaultWireMaterial.Object;
}
static ConstructorHelpers::FObjectFinder<UMaterialInterface> DefaultFixerMaterial(TEXT("/Game/LedVolume/Materials/M_PhysicalFixer.M_PhysicalFixer"));
if (DefaultFixerMaterial.Succeeded())
{
BottomFixerMaterial = DefaultFixerMaterial.Object;
}
}
void ALedVolumeDisplayActor::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);
#if WITH_EDITOR
if (GeometryMode == ELedDisplayGeometryMode::PhysicalMatrix)
{
if (const UWorld* World = GetWorld(); World && !World->IsGameWorld())
{
SyncDimensionsFromLayoutProperties();
if (VoxelInstances)
{
VoxelInstances->ClearInstances();
VoxelInstances->SetVisibility(false);
}
if (GpuVoxelInstances)
{
GpuVoxelInstances->ClearInstances();
GpuVoxelInstances->SetVisibility(false);
}
if (PhysicalWireInstances)
{
PhysicalWireInstances->ClearInstances();
PhysicalWireInstances->SetVisibility(false);
}
if (PhysicalCabinetInstances)
{
PhysicalCabinetInstances->ClearInstances();
PhysicalCabinetInstances->SetVisibility(false);
}
if (PhysicalBottomFixerInstances)
{
PhysicalBottomFixerInstances->ClearInstances();
PhysicalBottomFixerInstances->SetVisibility(false);
}
CacheCurrentSettings();
return;
}
}
#endif
RebuildDisplay();
}
void ALedVolumeDisplayActor::BeginPlay()
{
Super::BeginPlay();
RebuildDisplay();
}
void ALedVolumeDisplayActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
ApplyRemoteControlledChanges();
#if WITH_EDITOR
MirrorEditorRemoteControlStateForPIE();
#endif
const UWorld* World = GetWorld();
const bool bIsEditorPreviewWorld = World && !World->IsGameWorld();
const bool bRunSourceAnimation = !bIsEditorPreviewWorld || bAnimateInEditor;
RunningTime += DeltaSeconds * AnimationSpeed;
if (IsGpuTestPatternActive())
{
UpdateMaterialParameters();
bColorsDirty = false;
}
else if (SourceMode == ELedVolumeSourceMode::TestPattern && bRunSourceAnimation)
{
UpdateTestPattern(DeltaSeconds, false);
}
else if (SourceMode == ELedVolumeSourceMode::SublevelVoxelizedScene && bRunSourceAnimation)
{
UpdateSublevelVoxelizedScene(DeltaSeconds);
}
if (bColorsDirty && !IsGpuTestPatternActive())
{
UpdateInstanceColors(false);
}
}
#if WITH_EDITOR
bool ALedVolumeDisplayActor::ShouldTickIfViewportsOnly() const
{
return true;
}
#endif
void ALedVolumeDisplayActor::RebuildDisplay()
{
SyncDimensionsFromLayoutProperties();
PreviewStride = FMath::Clamp(PreviewStride, 1, 16);
PixelSize = FMath::Max(0.01f, PixelSize);
Spacing = FMath::Max(0.01f, Spacing);
CabinetCountX = FMath::Clamp(CabinetCountX, 1, 64);
CabinetCountZ = FMath::Clamp(CabinetCountZ, 1, 64);
DotsPerCabinetX = FMath::Clamp(DotsPerCabinetX, 1, 16);
DotsPerCabinetZ = FMath::Clamp(DotsPerCabinetZ, 1, 16);
LedsPerString = FMath::Clamp(LedsPerString, 1, 256);
CabinetSizeMm = FMath::Max(1.0f, CabinetSizeMm);
DotPitchMm = FMath::Max(1.0f, DotPitchMm);
PhysicalLedSizeMm = FMath::Max(0.1f, PhysicalLedSizeMm);
WireThicknessMm = FMath::Max(0.1f, WireThicknessMm);
WireOpacity = FMath::Clamp(WireOpacity, 0.0f, 1.0f);
PhysicalJitterMm = FMath::Max(0.0f, PhysicalJitterMm);
WireSegmentsPerString = FMath::Clamp(WireSegmentsPerString, 1, 32);
RebuildGeometry();
UpdateMaterialParameters();
if (IsGpuTestPatternActive())
{
bColorsDirty = false;
}
else if (SourceMode == ELedVolumeSourceMode::TestPattern)
{
EnsureVoxelBuffer();
UpdateTestPattern(0.0f, true);
UpdateInstanceColors(true);
}
else
{
EnsureVoxelBuffer();
UpdateInstanceColors(true);
}
CacheCurrentSettings();
}
void ALedVolumeDisplayActor::ResizeDisplay(int32 NewWidth, int32 NewHeight, int32 NewDepth)
{
Width = FMath::Clamp(NewWidth, 1, 128);
Height = FMath::Clamp(NewHeight, 1, 128);
Depth = FMath::Clamp(NewDepth, 1, 128);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetWidth(int32 NewWidth)
{
Width = FMath::Clamp(NewWidth, 1, 128);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetHeight(int32 NewHeight)
{
Height = FMath::Clamp(NewHeight, 1, 128);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetDepth(int32 NewDepth)
{
Depth = FMath::Clamp(NewDepth, 1, 128);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetPixelSize(float NewPixelSize)
{
PixelSize = FMath::Max(0.01f, NewPixelSize);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetSpacing(float NewSpacing)
{
Spacing = FMath::Max(0.01f, NewSpacing);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetBrightness(float NewBrightness)
{
Brightness = FMath::Max(0.0f, NewBrightness);
UpdateMaterialParameters();
if (!IsGpuTestPatternActive())
{
MarkColorsDirty();
}
CacheCurrentSettings();
}
void ALedVolumeDisplayActor::SetSourceMode(ELedVolumeSourceMode NewSourceMode)
{
SourceMode = NewSourceMode;
UpdateMaterialParameters();
if (!IsGpuTestPatternActive())
{
MarkColorsDirty();
}
CacheCurrentSettings();
}
void ALedVolumeDisplayActor::SetTestPattern(ELedTestPattern NewPattern)
{
TestPattern = NewPattern;
UpdateMaterialParameters();
if (!IsGpuTestPatternActive())
{
MarkColorsDirty();
}
CacheCurrentSettings();
}
void ALedVolumeDisplayActor::SetSolidColor(FLinearColor NewColor)
{
SolidColor = NewColor;
UpdateMaterialParameters();
if (!IsGpuTestPatternActive())
{
MarkColorsDirty();
}
CacheCurrentSettings();
}
void ALedVolumeDisplayActor::SetPreviewStride(int32 NewPreviewStride)
{
PreviewStride = FMath::Clamp(NewPreviewStride, 1, 16);
RebuildDisplay();
}
void ALedVolumeDisplayActor::SetVoxelShape(ELedVoxelShape NewShape)
{
VoxelShape = NewShape;
RebuildDisplay();
}
void ALedVolumeDisplayActor::ClearVoxels()
{
EnsureVoxelBuffer();
for (FLinearColor& Color : VoxelBuffer)
{
Color = FLinearColor::Black;
}
MarkColorsDirty();
}
void ALedVolumeDisplayActor::SetVoxelColor(int32 X, int32 Y, int32 Z, FLinearColor Color)
{
if (!VoxelBuffer.IsValidIndex(ToBufferIndex(X, Y, Z)))
{
return;
}
VoxelBuffer[ToBufferIndex(X, Y, Z)] = Color;
MarkColorsDirty();
}
int64 ALedVolumeDisplayActor::GetRequestedVoxelCount() const
{
return Dimensions.NumVoxels();
}
int32 ALedVolumeDisplayActor::GetRenderedInstanceCount() const
{
if (IsGpuTestPatternActive())
{
return FMath::Clamp<int64>(Dimensions.NumVoxels(), 0, TNumericLimits<int32>::Max());
}
return VoxelInstances ? VoxelInstances->GetInstanceCount() : 0;
}
void ALedVolumeDisplayActor::ApplyRemoteControlledChanges()
{
if (HasLayoutChanged())
{
RebuildDisplay();
return;
}
if (HasAppearanceChanged())
{
UpdateMaterialParameters();
MarkColorsDirty();
CacheCurrentSettings();
}
if (HasSourceSettingsChanged())
{
UpdateMaterialParameters();
if (IsGpuTestPatternActive())
{
bColorsDirty = false;
}
else if (SourceMode == ELedVolumeSourceMode::TestPattern)
{
UpdateTestPattern(0.0f, true);
}
else
{
MarkColorsDirty();
}
CacheCurrentSettings();
}
}
#if WITH_EDITOR
bool ALedVolumeDisplayActor::MirrorEditorRemoteControlStateForPIE()
{
UWorld* World = GetWorld();
if (!GEditor || !World || World->WorldType != EWorldType::PIE)
{
return false;
}
ALedVolumeDisplayActor* EditorSource = CachedEditorSourceActor.Get();
if (!EditorSource)
{
if (UWorld* EditorWorld = GEditor->GetEditorWorldContext(false).World())
{
for (TActorIterator<ALedVolumeDisplayActor> It(EditorWorld); It; ++It)
{
ALedVolumeDisplayActor* Candidate = *It;
if (Candidate && Candidate != this && Candidate->GetActorLabel() == GetActorLabel())
{
EditorSource = Candidate;
CachedEditorSourceActor = Candidate;
break;
}
}
if (!EditorSource)
{
for (TActorIterator<ALedVolumeDisplayActor> It(EditorWorld); It; ++It)
{
ALedVolumeDisplayActor* Candidate = *It;
if (Candidate && Candidate != this)
{
EditorSource = Candidate;
CachedEditorSourceActor = Candidate;
break;
}
}
}
}
}
if (!EditorSource)
{
return false;
}
Width = EditorSource->Width;
Height = EditorSource->Height;
Depth = EditorSource->Depth;
PixelSize = EditorSource->PixelSize;
Spacing = EditorSource->Spacing;
PreviewStride = EditorSource->PreviewStride;
GeometryMode = EditorSource->GeometryMode;
CabinetCountX = EditorSource->CabinetCountX;
CabinetCountZ = EditorSource->CabinetCountZ;
DotsPerCabinetX = EditorSource->DotsPerCabinetX;
DotsPerCabinetZ = EditorSource->DotsPerCabinetZ;
LedsPerString = EditorSource->LedsPerString;
CabinetSizeMm = EditorSource->CabinetSizeMm;
DotPitchMm = EditorSource->DotPitchMm;
PhysicalLedSizeMm = EditorSource->PhysicalLedSizeMm;
WireThicknessMm = EditorSource->WireThicknessMm;
WireOpacity = EditorSource->WireOpacity;
PhysicalJitterMm = EditorSource->PhysicalJitterMm;
WireSegmentsPerString = EditorSource->WireSegmentsPerString;
PhysicalRandomSeed = EditorSource->PhysicalRandomSeed;
bShowCabinets = EditorSource->bShowCabinets;
bShowWires = EditorSource->bShowWires;
bShowBottomFixers = EditorSource->bShowBottomFixers;
CabinetMaterial = EditorSource->CabinetMaterial;
WireMaterial = EditorSource->WireMaterial;
BottomFixerMaterial = EditorSource->BottomFixerMaterial;
VoxelShape = EditorSource->VoxelShape;
CustomVoxelMesh = EditorSource->CustomVoxelMesh;
LedMaterial = EditorSource->LedMaterial;
GpuVolumeMaterial = EditorSource->GpuVolumeMaterial;
Brightness = EditorSource->Brightness;
Gamma = EditorSource->Gamma;
SourceMode = EditorSource->SourceMode;
TestPattern = EditorSource->TestPattern;
SolidColor = EditorSource->SolidColor;
AnimationSpeed = EditorSource->AnimationSpeed;
bAnimateInEditor = EditorSource->bAnimateInEditor;
bUseGpuTestPattern = EditorSource->bUseGpuTestPattern;
VolumeTextureFrames = EditorSource->VolumeTextureFrames;
TextureSliceFrames = EditorSource->TextureSliceFrames;
VoxelizedSublevel = EditorSource->VoxelizedSublevel;
CaptureBoundsExtent = EditorSource->CaptureBoundsExtent;
bUseActorBoundsAsCaptureVolume = EditorSource->bUseActorBoundsAsCaptureVolume;
SourceActorTag = EditorSource->SourceActorTag;
EmptyVoxelColor = EditorSource->EmptyVoxelColor;
MaxEditorInstances = EditorSource->MaxEditorInstances;
MaxRuntimeInstances = EditorSource->MaxRuntimeInstances;
ColorUpdatesPerFrame = EditorSource->ColorUpdatesPerFrame;
ApplyRemoteControlledChanges();
return true;
}
#endif
void ALedVolumeDisplayActor::CacheCurrentSettings()
{
CachedDimensions = Dimensions;
CachedWidth = Width;
CachedHeight = Height;
CachedDepth = Depth;
CachedPixelSize = PixelSize;
CachedSpacing = Spacing;
CachedPreviewStride = PreviewStride;
CachedGeometryMode = GeometryMode;
CachedCabinetCountX = CabinetCountX;
CachedCabinetCountZ = CabinetCountZ;
CachedDotsPerCabinetX = DotsPerCabinetX;
CachedDotsPerCabinetZ = DotsPerCabinetZ;
CachedLedsPerString = LedsPerString;
CachedCabinetSizeMm = CabinetSizeMm;
CachedDotPitchMm = DotPitchMm;
CachedPhysicalLedSizeMm = PhysicalLedSizeMm;
CachedWireThicknessMm = WireThicknessMm;
CachedWireOpacity = WireOpacity;
CachedPhysicalJitterMm = PhysicalJitterMm;
CachedWireSegmentsPerString = WireSegmentsPerString;
CachedPhysicalRandomSeed = PhysicalRandomSeed;
bCachedShowCabinets = bShowCabinets;
bCachedShowWires = bShowWires;
bCachedShowBottomFixers = bShowBottomFixers;
CachedCabinetMaterial = CabinetMaterial;
CachedWireMaterial = WireMaterial;
CachedBottomFixerMaterial = BottomFixerMaterial;
CachedVoxelShape = VoxelShape;
CachedCustomVoxelMesh = CustomVoxelMesh;
CachedLedMaterial = LedMaterial;
CachedGpuVolumeMaterial = GpuVolumeMaterial;
CachedBrightness = Brightness;
CachedGamma = Gamma;
CachedSourceMode = SourceMode;
CachedTestPattern = TestPattern;
CachedSolidColor = SolidColor;
CachedAnimationSpeed = AnimationSpeed;
bCachedUseGpuTestPattern = bUseGpuTestPattern;
}
bool ALedVolumeDisplayActor::HasLayoutChanged() const
{
return CachedDimensions.Width != Dimensions.Width
|| CachedDimensions.Height != Dimensions.Height
|| CachedDimensions.Depth != Dimensions.Depth
|| CachedWidth != Width
|| CachedHeight != Height
|| CachedDepth != Depth
|| !FMath::IsNearlyEqual(CachedPixelSize, PixelSize)
|| !FMath::IsNearlyEqual(CachedSpacing, Spacing)
|| CachedPreviewStride != PreviewStride
|| CachedGeometryMode != GeometryMode
|| CachedCabinetCountX != CabinetCountX
|| CachedCabinetCountZ != CabinetCountZ
|| CachedDotsPerCabinetX != DotsPerCabinetX
|| CachedDotsPerCabinetZ != DotsPerCabinetZ
|| CachedLedsPerString != LedsPerString
|| !FMath::IsNearlyEqual(CachedCabinetSizeMm, CabinetSizeMm)
|| !FMath::IsNearlyEqual(CachedDotPitchMm, DotPitchMm)
|| !FMath::IsNearlyEqual(CachedPhysicalLedSizeMm, PhysicalLedSizeMm)
|| !FMath::IsNearlyEqual(CachedWireThicknessMm, WireThicknessMm)
|| !FMath::IsNearlyEqual(CachedWireOpacity, WireOpacity)
|| !FMath::IsNearlyEqual(CachedPhysicalJitterMm, PhysicalJitterMm)
|| CachedWireSegmentsPerString != WireSegmentsPerString
|| CachedPhysicalRandomSeed != PhysicalRandomSeed
|| bCachedShowCabinets != bShowCabinets
|| bCachedShowWires != bShowWires
|| bCachedShowBottomFixers != bShowBottomFixers
|| CachedCabinetMaterial != CabinetMaterial
|| CachedWireMaterial != WireMaterial
|| CachedBottomFixerMaterial != BottomFixerMaterial
|| CachedVoxelShape != VoxelShape
|| CachedCustomVoxelMesh != CustomVoxelMesh
|| CachedLedMaterial != LedMaterial
|| CachedGpuVolumeMaterial != GpuVolumeMaterial;
}
void ALedVolumeDisplayActor::SyncDimensionsFromLayoutProperties()
{
Width = FMath::Clamp(Width, 1, 128);
Height = FMath::Clamp(Height, 1, 128);
Depth = FMath::Clamp(Depth, 1, 128);
if (GeometryMode == ELedDisplayGeometryMode::PhysicalMatrix)
{
Width = FMath::Clamp(CabinetCountX * DotsPerCabinetX, 1, 128);
Height = FMath::Clamp(LedsPerString, 1, 128);
Depth = FMath::Clamp(CabinetCountZ * DotsPerCabinetZ, 1, 128);
}
Dimensions.Width = Width;
Dimensions.Height = Height;
Dimensions.Depth = Depth;
}
bool ALedVolumeDisplayActor::HasAppearanceChanged() const
{
return !FMath::IsNearlyEqual(CachedBrightness, Brightness)
|| !FMath::IsNearlyEqual(CachedGamma, Gamma);
}
bool ALedVolumeDisplayActor::HasSourceSettingsChanged() const
{
return CachedSourceMode != SourceMode
|| CachedTestPattern != TestPattern
|| CachedSolidColor != SolidColor
|| !FMath::IsNearlyEqual(CachedAnimationSpeed, AnimationSpeed)
|| bCachedUseGpuTestPattern != bUseGpuTestPattern;
}
bool ALedVolumeDisplayActor::IsGpuTestPatternActive() const
{
return bUseGpuTestPattern && SourceMode == ELedVolumeSourceMode::TestPattern;
}
void ALedVolumeDisplayActor::UpdateGpuVolumeProxyTransform()
{
if (!GpuVolumeProxy || !IsGpuTestPatternActive())
{
return;
}
FVector CameraLocation = GetActorLocation() + FVector(-1000.0f, 0.0f, 400.0f);
bool bHasCamera = false;
if (const UWorld* World = GetWorld())
{
if (World->IsGameWorld())
{
if (const APlayerCameraManager* CameraManager = UGameplayStatics::GetPlayerCameraManager(World, 0))
{
CameraLocation = CameraManager->GetCameraLocation();
bHasCamera = true;
}
}
#if WITH_EDITOR
else if (GCurrentLevelEditingViewportClient)
{
CameraLocation = GCurrentLevelEditingViewportClient->GetViewLocation();
bHasCamera = true;
}
#endif
}
const FVector Center = GetActorLocation();
FVector ViewDirection = Center - CameraLocation;
if (!ViewDirection.Normalize())
{
ViewDirection = FVector::ForwardVector;
}
const FVector VolumeSize(
FMath::Max(Spacing, Dimensions.Width * Spacing),
FMath::Max(Spacing, Dimensions.Height * Spacing),
FMath::Max(Spacing, Dimensions.Depth * Spacing));
const float Radius = VolumeSize.Size() * 0.5f;
const float PlaneSize = FMath::Max(VolumeSize.GetMax() * 1.75f, Radius * 2.2f);
const FVector PlaneWorldLocation = Center - ViewDirection * (Radius + 1.0f);
const FRotator PlaneWorldRotation = FRotationMatrix::MakeFromZ(-ViewDirection).Rotator();
GpuVolumeProxy->SetWorldLocationAndRotation(PlaneWorldLocation, PlaneWorldRotation);
GpuVolumeProxy->SetWorldScale3D(FVector(PlaneSize / 100.0f, PlaneSize / 100.0f, 1.0f));
if (!bHasCamera)
{
GpuVolumeProxy->SetRelativeLocation(FVector::ZeroVector);
}
}
void ALedVolumeDisplayActor::RebuildGeometry()
{
if (!VoxelInstances)
{
return;
}
VoxelInstances->ClearInstances();
const bool bUsePhysicalMatrix = GeometryMode == ELedDisplayGeometryMode::PhysicalMatrix;
const bool bUseGpuGeometry = IsGpuTestPatternActive() || bUsePhysicalMatrix;
VoxelInstances->SetVisibility(!bUseGpuGeometry);
VoxelInstances->SetHiddenInGame(bUseGpuGeometry);
if (GpuVolumeProxy)
{
GpuVolumeProxy->SetVisibility(false);
GpuVolumeProxy->SetHiddenInGame(true);
}
if (GpuVoxelInstances)
{
GpuVoxelInstances->SetVisibility(bUseGpuGeometry);
GpuVoxelInstances->SetHiddenInGame(!bUseGpuGeometry);
}
if (PhysicalWireInstances)
{
PhysicalWireInstances->SetVisibility(bUsePhysicalMatrix && bShowWires);
PhysicalWireInstances->SetHiddenInGame(!(bUsePhysicalMatrix && bShowWires));
}
if (PhysicalCabinetInstances)
{
PhysicalCabinetInstances->SetVisibility(bUsePhysicalMatrix && bShowCabinets);
PhysicalCabinetInstances->SetHiddenInGame(!(bUsePhysicalMatrix && bShowCabinets));
}
if (PhysicalBottomFixerInstances)
{
PhysicalBottomFixerInstances->SetVisibility(bUsePhysicalMatrix && bShowBottomFixers);
PhysicalBottomFixerInstances->SetHiddenInGame(!(bUsePhysicalMatrix && bShowBottomFixers));
}
if (bUsePhysicalMatrix)
{
RebuildPhysicalMatrixInstances();
RenderedVoxelToBufferIndex.Reset();
LastRenderedColors.Reset();
bGeometryDirty = false;
bColorsDirty = false;
return;
}
if (IsGpuTestPatternActive())
{
RebuildGpuVoxelInstances();
RenderedVoxelToBufferIndex.Reset();
LastRenderedColors.Reset();
bGeometryDirty = false;
bColorsDirty = false;
return;
}
VoxelInstances->NumCustomDataFloats = 4;
VoxelInstances->SetStaticMesh(ResolveVoxelMesh());
if (LedMaterial)
{
VoxelInstances->SetMaterial(0, LedMaterial);
}
RenderedVoxelToBufferIndex.Reset();
LastRenderedColors.Reset();
const int32 InstanceLimit = GetActiveInstanceLimit();
const auto CountInstancesForStride = [this](int32 CandidateStride) -> int64
{
const int64 CountX = FMath::DivideAndRoundUp(Dimensions.Width, CandidateStride);
const int64 CountY = FMath::DivideAndRoundUp(Dimensions.Height, CandidateStride);
const int64 CountZ = FMath::DivideAndRoundUp(Dimensions.Depth, CandidateStride);
return CountX * CountY * CountZ;
};
int32 Stride = GetClampedPreviewStride();
while (Stride < 16 && CountInstancesForStride(Stride) > InstanceLimit)
{
++Stride;
}
if (Stride != GetClampedPreviewStride())
{
UE_LOG(LogTemp, Warning, TEXT("LED volume preview requested %lld instances, using effective stride %d to fit limit %d."),
static_cast<long long>(CountInstancesForStride(GetClampedPreviewStride())), Stride, InstanceLimit);
}
const float MeshBaseSize = 100.0f;
const float UniformScale = PixelSize / MeshBaseSize;
const FVector InstanceScale = VoxelShape == ELedVoxelShape::Lens
? FVector(UniformScale, UniformScale, UniformScale * 0.35f)
: FVector(UniformScale);
int32 AddedInstances = 0;
for (int32 Z = 0; Z < Dimensions.Depth; Z += Stride)
{
for (int32 Y = 0; Y < Dimensions.Height; Y += Stride)
{
for (int32 X = 0; X < Dimensions.Width; X += Stride)
{
if (AddedInstances >= InstanceLimit)
{
UE_LOG(LogTemp, Warning, TEXT("LED volume preview still reached instance limit %d. Increase PreviewStride or Max*Instances for more detail."), InstanceLimit);
LastRenderedColors.SetNum(RenderedVoxelToBufferIndex.Num());
bColorsDirty = true;
return;
}
const FVector Location = VoxelIndexToLocalPosition(X, Y, Z);
const FTransform InstanceTransform(FRotator::ZeroRotator, Location, InstanceScale);
VoxelInstances->AddInstance(InstanceTransform);
RenderedVoxelToBufferIndex.Add(ToBufferIndex(X, Y, Z));
++AddedInstances;
}
}
}
LastRenderedColors.SetNum(RenderedVoxelToBufferIndex.Num());
NextColorUpdateInstance = 0;
bGeometryDirty = false;
bColorsDirty = true;
}
void ALedVolumeDisplayActor::RebuildGpuVoxelInstances()
{
if (!GpuVoxelInstances)
{
return;
}
const int64 RequestedCount = Dimensions.NumVoxels();
if (RequestedCount <= 0 || RequestedCount > TNumericLimits<int32>::Max())
{
GpuVoxelInstances->ClearInstances();
return;
}
const float MeshBaseSize = 100.0f;
const float UniformScale = PixelSize / MeshBaseSize;
const FVector InstanceScale = VoxelShape == ELedVoxelShape::Lens
? FVector(UniformScale, UniformScale, UniformScale * 0.35f)
: FVector(UniformScale);
GpuVoxelInstances->SetStaticMesh(ResolveVoxelMesh());
if (LedMaterial)
{
GpuVoxelInstances->SetMaterial(0, LedMaterial);
GpuVoxelMID = nullptr;
}
GpuVoxelInstances->ClearInstances();
GpuVoxelInstances->PreAllocateInstancesMemory(static_cast<int32>(RequestedCount));
constexpr int32 BatchSize = 32768;
TArray<FTransform> Batch;
Batch.Reserve(BatchSize);
for (int32 Z = 0; Z < Dimensions.Depth; ++Z)
{
for (int32 Y = 0; Y < Dimensions.Height; ++Y)
{
for (int32 X = 0; X < Dimensions.Width; ++X)
{
Batch.Emplace(FRotator::ZeroRotator, VoxelIndexToLocalPosition(X, Y, Z), InstanceScale);
if (Batch.Num() == BatchSize)
{
GpuVoxelInstances->AddInstances(Batch, false, false, false);
Batch.Reset();
}
}
}
}
if (!Batch.IsEmpty())
{
GpuVoxelInstances->AddInstances(Batch, false, false, false);
}
}
void ALedVolumeDisplayActor::RebuildPhysicalMatrixInstances()
{
if (!GpuVoxelInstances)
{
return;
}
const int64 RequestedCount = Dimensions.NumVoxels();
if (RequestedCount <= 0 || RequestedCount > TNumericLimits<int32>::Max())
{
GpuVoxelInstances->ClearInstances();
return;
}
const float LedScale = (PhysicalLedSizeMm * 0.1f) / 100.0f;
const FVector LedInstanceScale = VoxelShape == ELedVoxelShape::Lens
? FVector(LedScale, LedScale, LedScale * 0.45f)
: FVector(LedScale);
GpuVoxelInstances->SetStaticMesh(ResolveVoxelMesh());
if (LedMaterial)
{
GpuVoxelInstances->SetMaterial(0, LedMaterial);
GpuVoxelMID = nullptr;
}
GpuVoxelInstances->ClearInstances();
GpuVoxelInstances->PreAllocateInstancesMemory(static_cast<int32>(RequestedCount));
constexpr int32 LedBatchSize = 32768;
TArray<FTransform> LedBatch;
LedBatch.Reserve(LedBatchSize);
for (int32 Z = 0; Z < Dimensions.Depth; ++Z)
{
for (int32 Y = 0; Y < Dimensions.Height; ++Y)
{
for (int32 X = 0; X < Dimensions.Width; ++X)
{
LedBatch.Emplace(FRotator::ZeroRotator, PhysicalLedLocalPosition(X, Y, Z), LedInstanceScale);
if (LedBatch.Num() == LedBatchSize)
{
GpuVoxelInstances->AddInstances(LedBatch, false, false, false);
LedBatch.Reset();
}
}
}
}
if (!LedBatch.IsEmpty())
{
GpuVoxelInstances->AddInstances(LedBatch, false, false, false);
}
if (PhysicalWireInstances)
{
PhysicalWireInstances->ClearInstances();
if (bShowWires)
{
if (WireMaterial)
{
PhysicalWireInstances->SetMaterial(0, WireMaterial);
WireMID = nullptr;
}
const int32 SegmentCount = FMath::Clamp(WireSegmentsPerString, 1, 32);
PhysicalWireInstances->PreAllocateInstancesMemory(Dimensions.Width * Dimensions.Depth * SegmentCount);
const float WireRadiusScale = (WireThicknessMm * 0.1f) / 100.0f;
for (int32 Z = 0; Z < Dimensions.Depth; ++Z)
{
for (int32 X = 0; X < Dimensions.Width; ++X)
{
for (int32 SegmentIndex = 0; SegmentIndex < SegmentCount; ++SegmentIndex)
{
const float AlphaA = static_cast<float>(SegmentIndex) / static_cast<float>(SegmentCount);
const float AlphaB = static_cast<float>(SegmentIndex + 1) / static_cast<float>(SegmentCount);
const FVector Start = PhysicalWireLocalPosition(X, AlphaA, Z);
const FVector End = PhysicalWireLocalPosition(X, AlphaB, Z);
const FVector Segment = End - Start;
const float Length = Segment.Size();
if (Length <= UE_KINDA_SMALL_NUMBER)
{
continue;
}
const FQuat Rotation = FQuat::FindBetweenNormals(FVector::UpVector, Segment / Length);
const FVector Location = (Start + End) * 0.5f;
const FVector Scale(WireRadiusScale, WireRadiusScale, Length / 100.0f);
PhysicalWireInstances->AddInstance(FTransform(Rotation, Location, Scale));
}
}
}
}
}
if (PhysicalCabinetInstances)
{
PhysicalCabinetInstances->ClearInstances();
if (bShowCabinets)
{
if (CabinetMaterial)
{
PhysicalCabinetInstances->SetMaterial(0, CabinetMaterial);
}
const float CabinetSizeCm = CabinetSizeMm * 0.1f;
const float ModuleSizeCm = CabinetSizeCm * 0.5f;
const float ThicknessCm = FMath::Max(2.0f, CabinetSizeCm * 0.055f);
const float TotalX = CabinetCountX * CabinetSizeCm;
const float TotalY = CabinetCountZ * CabinetSizeCm;
const FVector ModuleScale(ModuleSizeCm / 100.0f * 0.985f, ModuleSizeCm / 100.0f * 0.985f, ThicknessCm / 100.0f);
PhysicalCabinetInstances->PreAllocateInstancesMemory(CabinetCountX * CabinetCountZ * 4);
for (int32 CabinetZ = 0; CabinetZ < CabinetCountZ; ++CabinetZ)
{
for (int32 CabinetX = 0; CabinetX < CabinetCountX; ++CabinetX)
{
const float BaseX = CabinetX * CabinetSizeCm - TotalX * 0.5f + CabinetSizeCm * 0.5f;
const float BaseY = CabinetZ * CabinetSizeCm - TotalY * 0.5f + CabinetSizeCm * 0.5f;
for (int32 ModuleZ = 0; ModuleZ < 2; ++ModuleZ)
{
for (int32 ModuleX = 0; ModuleX < 2; ++ModuleX)
{
const FVector Location(
BaseX + (ModuleX == 0 ? -ModuleSizeCm : ModuleSizeCm) * 0.25f,
BaseY + (ModuleZ == 0 ? -ModuleSizeCm : ModuleSizeCm) * 0.25f,
ThicknessCm * 0.5f);
PhysicalCabinetInstances->AddInstance(FTransform(FRotator::ZeroRotator, Location, ModuleScale));
}
}
}
}
}
}
if (PhysicalBottomFixerInstances)
{
PhysicalBottomFixerInstances->ClearInstances();
if (bShowBottomFixers)
{
if (BottomFixerMaterial)
{
PhysicalBottomFixerInstances->SetMaterial(0, BottomFixerMaterial);
}
const float RadiusScale = FMath::Max((PhysicalLedSizeMm * 0.16f * 0.1f) / 100.0f, 0.005f);
const float HeightScale = FMath::Max((PhysicalLedSizeMm * 0.22f * 0.1f) / 100.0f, 0.006f);
PhysicalBottomFixerInstances->PreAllocateInstancesMemory(Dimensions.Width * Dimensions.Depth);
for (int32 Z = 0; Z < Dimensions.Depth; ++Z)
{
for (int32 X = 0; X < Dimensions.Width; ++X)
{
const FVector Bottom = PhysicalStringBottomLocalPosition(X, Z);
PhysicalBottomFixerInstances->AddInstance(FTransform(FRotator::ZeroRotator, Bottom - FVector(0.0f, 0.0f, PhysicalLedSizeMm * 0.03f), FVector(RadiusScale, RadiusScale, HeightScale)));
}
}
}
}
}
void ALedVolumeDisplayActor::EnsureVoxelBuffer()
{
const int64 NumVoxels64 = Dimensions.NumVoxels();
if (NumVoxels64 > TNumericLimits<int32>::Max())
{
return;
}
const int32 NumVoxels = static_cast<int32>(NumVoxels64);
if (VoxelBuffer.Num() != NumVoxels)
{
VoxelBuffer.SetNum(NumVoxels);
for (FLinearColor& Color : VoxelBuffer)
{
Color = SolidColor;
}
}
}
void ALedVolumeDisplayActor::UpdateMaterialParameters()
{
if (!VoxelInstances)
{
return;
}
for (int32 MaterialIndex = 0; MaterialIndex < VoxelInstances->GetNumMaterials(); ++MaterialIndex)
{
UMaterialInstanceDynamic* DynamicMaterial = VoxelInstances->CreateAndSetMaterialInstanceDynamic(MaterialIndex);
if (DynamicMaterial)
{
DynamicMaterial->SetScalarParameterValue(BrightnessParameterName, Brightness);
DynamicMaterial->SetScalarParameterValue(GammaParameterName, Gamma);
DynamicMaterial->SetVectorParameterValue(LedColorParameterName, SolidColor);
DynamicMaterial->SetScalarParameterValue(AnimationSpeedParameterName, AnimationSpeed);
DynamicMaterial->SetScalarParameterValue(PatternModeParameterName, static_cast<float>(static_cast<uint8>(TestPattern)));
DynamicMaterial->SetScalarParameterValue(VoxelSpacingParameterName, Spacing);
}
}
if (GpuVolumeProxy)
{
if (!GpuVolumeMID)
{
GpuVolumeMID = GpuVolumeProxy->CreateAndSetMaterialInstanceDynamic(0);
}
if (GpuVolumeMID)
{
const FVector VolumeExtent(
FMath::Max(Spacing * 0.5f, Dimensions.Width * Spacing * 0.5f),
FMath::Max(Spacing * 0.5f, Dimensions.Height * Spacing * 0.5f),
FMath::Max(Spacing * 0.5f, Dimensions.Depth * Spacing * 0.5f));
GpuVolumeMID->SetScalarParameterValue(BrightnessParameterName, Brightness);
GpuVolumeMID->SetScalarParameterValue(GammaParameterName, Gamma);
GpuVolumeMID->SetVectorParameterValue(LedColorParameterName, SolidColor);
GpuVolumeMID->SetScalarParameterValue(AnimationSpeedParameterName, AnimationSpeed);
GpuVolumeMID->SetScalarParameterValue(PatternModeParameterName, static_cast<float>(static_cast<uint8>(TestPattern)));
GpuVolumeMID->SetScalarParameterValue(VoxelSpacingParameterName, Spacing);
const FVector Center = GetActorLocation();
GpuVolumeMID->SetVectorParameterValue(VolumeCenterParameterName, FLinearColor(Center.X, Center.Y, Center.Z, 1.0f));
GpuVolumeMID->SetVectorParameterValue(VolumeExtentParameterName, FLinearColor(VolumeExtent.X, VolumeExtent.Y, VolumeExtent.Z, 1.0f));
GpuVolumeMID->SetScalarParameterValue(PixelRadiusFractionParameterName, FMath::Clamp(PixelSize / FMath::Max(Spacing, 0.001f), 0.02f, 1.0f));
GpuVolumeMID->SetScalarParameterValue(VoxelShapeModeParameterName, static_cast<float>(static_cast<uint8>(VoxelShape)));
}
}
if (GpuVoxelInstances)
{
if (!GpuVoxelMID)
{
GpuVoxelMID = GpuVoxelInstances->CreateAndSetMaterialInstanceDynamic(0);
}
if (GpuVoxelMID)
{
GpuVoxelMID->SetScalarParameterValue(BrightnessParameterName, Brightness);
GpuVoxelMID->SetScalarParameterValue(GammaParameterName, Gamma);
GpuVoxelMID->SetVectorParameterValue(LedColorParameterName, SolidColor);
GpuVoxelMID->SetScalarParameterValue(AnimationSpeedParameterName, AnimationSpeed);
GpuVoxelMID->SetScalarParameterValue(PatternModeParameterName, static_cast<float>(static_cast<uint8>(TestPattern)));
GpuVoxelMID->SetScalarParameterValue(VoxelSpacingParameterName,
GeometryMode == ELedDisplayGeometryMode::PhysicalMatrix ? DotPitchMm * 0.1f : Spacing);
}
}
if (PhysicalWireInstances)
{
if (!WireMID && WireMaterial)
{
WireMID = PhysicalWireInstances->CreateAndSetMaterialInstanceDynamic(0);
}
if (WireMID)
{
WireMID->SetScalarParameterValue(TEXT("WireOpacity"), WireOpacity);
WireMID->SetVectorParameterValue(TEXT("WireColor"), FLinearColor(0.92f, 0.92f, 0.86f, WireOpacity));
}
}
}
void ALedVolumeDisplayActor::UpdateTestPattern(float DeltaSeconds, bool bForceAll)
{
EnsureVoxelBuffer();
if (!RenderedVoxelToBufferIndex.IsEmpty())
{
const int32 InstanceCount = RenderedVoxelToBufferIndex.Num();
const int32 Budget = bForceAll ? InstanceCount : FMath::Clamp(ColorUpdatesPerFrame, 1, InstanceCount);
const int32 StartInstance = NextColorUpdateInstance;
for (int32 Step = 0; Step < Budget; ++Step)
{
const int32 InstanceIndex = bForceAll ? Step : (StartInstance + Step) % InstanceCount;
const int32 BufferIndex = RenderedVoxelToBufferIndex[InstanceIndex];
int32 X = 0;
int32 Y = 0;
int32 Z = 0;
FromBufferIndex(BufferIndex, X, Y, Z);
VoxelBuffer[BufferIndex] = SampleTestPattern(X, Y, Z);
}
}
MarkColorsDirty();
}
void ALedVolumeDisplayActor::UpdateSublevelVoxelizedScene(float DeltaSeconds)
{
EnsureVoxelBuffer();
for (const int32 BufferIndex : RenderedVoxelToBufferIndex)
{
int32 X = 0;
int32 Y = 0;
int32 Z = 0;
FromBufferIndex(BufferIndex, X, Y, Z);
const FVector LocalPosition = VoxelIndexToLocalPosition(X, Y, Z);
const FVector WorldPosition = GetActorTransform().TransformPosition(LocalPosition);
VoxelBuffer[BufferIndex] = SampleWorldVoxelColor(WorldPosition);
}
MarkColorsDirty();
}
void ALedVolumeDisplayActor::UpdateInstanceColors(bool bForceAll)
{
if (!VoxelInstances || RenderedVoxelToBufferIndex.IsEmpty())
{
bColorsDirty = false;
return;
}
const int32 InstanceCount = RenderedVoxelToBufferIndex.Num();
const int32 Budget = bForceAll ? InstanceCount : FMath::Clamp(ColorUpdatesPerFrame, 1, InstanceCount);
for (int32 Step = 0; Step < Budget; ++Step)
{
const int32 InstanceIndex = bForceAll ? Step : NextColorUpdateInstance;
const int32 BufferIndex = RenderedVoxelToBufferIndex[InstanceIndex];
const FLinearColor SourceColor = VoxelBuffer.IsValidIndex(BufferIndex) ? VoxelBuffer[BufferIndex] : FLinearColor::Black;
const FLinearColor OutputColor = ApplyBrightnessAndGamma(SourceColor, Brightness, Gamma);
VoxelInstances->SetCustomDataValue(InstanceIndex, 0, OutputColor.R, false);
VoxelInstances->SetCustomDataValue(InstanceIndex, 1, OutputColor.G, false);
VoxelInstances->SetCustomDataValue(InstanceIndex, 2, OutputColor.B, false);
VoxelInstances->SetCustomDataValue(InstanceIndex, 3, OutputColor.A, false);
if (LastRenderedColors.IsValidIndex(InstanceIndex))
{
LastRenderedColors[InstanceIndex] = OutputColor;
}
if (!bForceAll)
{
NextColorUpdateInstance = (NextColorUpdateInstance + 1) % InstanceCount;
}
}
VoxelInstances->MarkRenderStateDirty();
bColorsDirty = !bForceAll && Budget < InstanceCount;
if (!bColorsDirty)
{
NextColorUpdateInstance = 0;
}
}
void ALedVolumeDisplayActor::MarkColorsDirty()
{
bColorsDirty = true;
}
int32 ALedVolumeDisplayActor::GetClampedPreviewStride() const
{
return FMath::Clamp(PreviewStride, 1, 16);
}
int32 ALedVolumeDisplayActor::GetActiveInstanceLimit() const
{
return FMath::Clamp<int64>(Dimensions.NumVoxels(), 1, TNumericLimits<int32>::Max());
}
int32 ALedVolumeDisplayActor::ToBufferIndex(int32 X, int32 Y, int32 Z) const
{
if (X < 0 || X >= Dimensions.Width || Y < 0 || Y >= Dimensions.Height || Z < 0 || Z >= Dimensions.Depth)
{
return INDEX_NONE;
}
return X + Y * Dimensions.Width + Z * Dimensions.Width * Dimensions.Height;
}
void ALedVolumeDisplayActor::FromBufferIndex(int32 BufferIndex, int32& OutX, int32& OutY, int32& OutZ) const
{
const int32 SliceSize = Dimensions.Width * Dimensions.Height;
OutZ = BufferIndex / SliceSize;
const int32 Remainder = BufferIndex % SliceSize;
OutY = Remainder / Dimensions.Width;
OutX = Remainder % Dimensions.Width;
}
FVector ALedVolumeDisplayActor::VoxelIndexToLocalPosition(int32 X, int32 Y, int32 Z) const
{
const FVector CenterOffset(
(Dimensions.Width - 1) * Spacing * 0.5f,
(Dimensions.Depth - 1) * Spacing * 0.5f,
(Dimensions.Height - 1) * Spacing * 0.5f);
return FVector(
X * Spacing - CenterOffset.X,
Z * Spacing - CenterOffset.Y,
Y * Spacing - CenterOffset.Z);
}
FVector ALedVolumeDisplayActor::PhysicalStringTopLocalPosition(int32 X, int32 Z) const
{
const float PitchCm = DotPitchMm * 0.1f;
const FVector CenterOffset(
(Dimensions.Width - 1) * PitchCm * 0.5f,
(Dimensions.Depth - 1) * PitchCm * 0.5f,
0.0f);
const float StringJitterCm = PhysicalJitterMm * 0.1f * 0.18f;
const FVector Jitter = PhysicalStableJitter(X, 0, Z, StringJitterCm);
return FVector(
X * PitchCm - CenterOffset.X + Jitter.X,
Z * PitchCm - CenterOffset.Y + Jitter.Y,
0.0f);
}
FVector ALedVolumeDisplayActor::PhysicalStringBottomLocalPosition(int32 X, int32 Z) const
{
const float PitchCm = DotPitchMm * 0.1f;
const float DropCm = (Dimensions.Height - 1) * PitchCm;
const float StringJitterCm = PhysicalJitterMm * 0.1f * 0.52f;
const FVector Jitter = PhysicalStableJitter(X, Dimensions.Height + 17, Z, StringJitterCm);
return PhysicalStringTopLocalPosition(X, Z) + FVector(Jitter.X, Jitter.Y, -DropCm + Jitter.Z * 0.12f);
}
FVector ALedVolumeDisplayActor::PhysicalWireLocalPosition(int32 X, float Alpha, int32 Z) const
{
const FVector Top = PhysicalStringTopLocalPosition(X, Z);
const FVector Bottom = PhysicalStringBottomLocalPosition(X, Z);
const float ClampedAlpha = FMath::Clamp(Alpha, 0.0f, 1.0f);
const FVector Straight = FMath::Lerp(Top, Bottom, ClampedAlpha);
const float SwayAmountCm = PhysicalJitterMm * 0.1f * 0.46f;
const FVector Phase = PhysicalStableJitter(X, 911, Z, 1.0f);
const float WaveA = FMath::Sin((ClampedAlpha * 2.0f + Phase.X) * PI);
const float WaveB = FMath::Sin((ClampedAlpha * 3.0f + Phase.Y) * PI);
const float EndFade = FMath::Sin(ClampedAlpha * PI);
return Straight + FVector(WaveA * SwayAmountCm * EndFade, WaveB * SwayAmountCm * EndFade, 0.0f);
}
FVector ALedVolumeDisplayActor::PhysicalLedLocalPosition(int32 X, int32 Y, int32 Z) const
{
const float Alpha = Dimensions.Height > 1 ? static_cast<float>(Y) / static_cast<float>(Dimensions.Height - 1) : 0.0f;
const FVector OnWire = PhysicalWireLocalPosition(X, Alpha, Z);
const float BeadSlideCm = PhysicalJitterMm * 0.1f * 0.08f;
const FVector BeadJitter = PhysicalStableJitter(X, Y, Z, BeadSlideCm);
return OnWire + FVector(0.0f, 0.0f, BeadJitter.Z);
}
FVector ALedVolumeDisplayActor::PhysicalStableJitter(int32 X, int32 Y, int32 Z, float AmountCm) const
{
if (AmountCm <= 0.0f)
{
return FVector::ZeroVector;
}
auto Unit = [](uint32 Value) -> float
{
Value ^= Value >> 16;
Value *= 0x7feb352dU;
Value ^= Value >> 15;
Value *= 0x846ca68bU;
Value ^= Value >> 16;
return static_cast<float>(Value & 0x00ffffffU) / static_cast<float>(0x00ffffffU);
};
const uint32 Seed = static_cast<uint32>(PhysicalRandomSeed);
const uint32 Base = Seed
^ static_cast<uint32>(X * 73856093)
^ static_cast<uint32>(Y * 19349663)
^ static_cast<uint32>(Z * 83492791);
return FVector(
(Unit(Base ^ 0x1234abcdU) * 2.0f - 1.0f) * AmountCm,
(Unit(Base ^ 0x88ff0137U) * 2.0f - 1.0f) * AmountCm,
(Unit(Base ^ 0x45d9f3bU) * 2.0f - 1.0f) * AmountCm);
}
FLinearColor ALedVolumeDisplayActor::SampleTestPattern(int32 X, int32 Y, int32 Z) const
{
const float NX = Dimensions.Width > 1 ? static_cast<float>(X) / static_cast<float>(Dimensions.Width - 1) : 0.0f;
const float NY = Dimensions.Height > 1 ? static_cast<float>(Y) / static_cast<float>(Dimensions.Height - 1) : 0.0f;
const float NZ = Dimensions.Depth > 1 ? static_cast<float>(Z) / static_cast<float>(Dimensions.Depth - 1) : 0.0f;
switch (TestPattern)
{
case ELedTestPattern::Solid:
return SolidColor;
case ELedTestPattern::AxisGradient:
return FLinearColor(NX, NY, NZ, 1.0f);
case ELedTestPattern::MovingSphere:
{
const FVector P(NX - 0.5f, NY - 0.5f, NZ - 0.5f);
const FVector Center(
FMath::Sin(RunningTime * 0.91f) * 0.25f,
FMath::Cos(RunningTime * 0.73f) * 0.25f,
FMath::Sin(RunningTime * 0.57f) * 0.25f);
const float Distance = FVector::Distance(P, Center);
const float Intensity = FMath::Clamp(1.0f - Distance / 0.25f, 0.0f, 1.0f);
return FLinearColor(0.15f + Intensity, Intensity * 0.55f, Intensity * 0.1f, Intensity);
}
case ELedTestPattern::RainbowWave:
default:
{
const float Hue = FMath::Fmod((NX + NY + NZ) * 120.0f + RunningTime * 60.0f, 360.0f);
return FLinearColor::MakeFromHSV8(static_cast<uint8>(Hue / 360.0f * 255.0f), 255, 255);
}
}
}
FLinearColor ALedVolumeDisplayActor::SampleWorldVoxelColor(const FVector& WorldPosition) const
{
UWorld* World = GetWorld();
if (!World)
{
return EmptyVoxelColor;
}
for (TActorIterator<AActor> It(World); It; ++It)
{
const AActor* SourceActor = *It;
if (!SourceActor || SourceActor == this || !SourceActor->ActorHasTag(SourceActorTag))
{
continue;
}
TInlineComponentArray<UStaticMeshComponent*> MeshComponents;
SourceActor->GetComponents(MeshComponents);
for (const UStaticMeshComponent* MeshComponent : MeshComponents)
{
if (!MeshComponent || !MeshComponent->GetStaticMesh() || !MeshComponent->IsVisible())
{
continue;
}
FVector LocalMin = FVector::ZeroVector;
FVector LocalMax = FVector::ZeroVector;
MeshComponent->GetLocalBounds(LocalMin, LocalMax);
const FVector LocalPosition = MeshComponent->GetComponentTransform().InverseTransformPosition(WorldPosition);
if (LocalPosition.X >= LocalMin.X && LocalPosition.X <= LocalMax.X
&& LocalPosition.Y >= LocalMin.Y && LocalPosition.Y <= LocalMax.Y
&& LocalPosition.Z >= LocalMin.Z && LocalPosition.Z <= LocalMax.Z)
{
if (const ARotatingVoxelCubeActor* CubeSource = Cast<ARotatingVoxelCubeActor>(SourceActor))
{
return CubeSource->VoxelColor;
}
return SolidColor;
}
}
}
return EmptyVoxelColor;
}
UStaticMesh* ALedVolumeDisplayActor::ResolveVoxelMesh() const
{
if (VoxelShape == ELedVoxelShape::CustomMesh && CustomVoxelMesh)
{
return CustomVoxelMesh;
}
const TCHAR* MeshPath = TEXT("/Engine/BasicShapes/Sphere.Sphere");
if (VoxelShape == ELedVoxelShape::Cube)
{
MeshPath = TEXT("/Engine/BasicShapes/Cube.Cube");
}
UStaticMesh* Mesh = LoadObject<UStaticMesh>(nullptr, MeshPath);
return Mesh ? Mesh : VoxelInstances->GetStaticMesh().Get();
}