Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************
- ***********Header***********
- *************************/
- USTRUCT()
- struct FSH9
- {
- GENERATED_BODY()
- FVector Coeffs[9];
- FSH9()
- {
- for (int32 i = 0; i < 9; ++i)
- Coeffs[i] = FVector::ZeroVector;
- }
- };
- USTRUCT()
- struct FApproxLight
- {
- GENERATED_BODY()
- FVector Direction;
- FVector Color;
- FVector Ambient;
- };
- UCLASS()
- class ASHLightingVisualizerActor : public AActor
- {
- GENERATED_BODY()
- public:
- ASHLightingVisualizerActor();
- virtual void BeginPlay() override;
- virtual void Tick(float DeltaTime) override;
- protected:
- //*** SH math ***
- void EvalSHBasis(const FVector& Dir, float OutBasis[9]);
- FVector EvalSHIrradiance(const FSH9& SH, const FVector& Normal);
- FApproxLight ExtractDirectionalLight(const FSH9& SH);
- //*** Environment ***
- FVector SampleAnalyticSky(const FVector& Dir);
- FSH9 ProjectEnvironmentToSH();
- //*** Visualization ***
- void DrawVisualization();
- void Settings();
- protected:
- UPROPERTY(EditAnywhere, Category="SH")
- int32 SampleThetaSteps = 32;
- UPROPERTY(EditAnywhere, Category="SH")
- int32 SamplePhiSteps = 64;
- UPROPERTY(EditAnywhere, Category="Visualization")
- float DebugScale = 200.0f;
- UPROPERTY(EditAnywhere, Category="Visualization")
- int32 NormalGridSize = 8;
- UPROPERTY(EditAnywhere, Category="Visualization")
- bool bShowDirectionalApprox = true;
- private:
- FSH9 CachedSH;
- };
- /************************
- ***********CPP***********
- *************************/
- void ASHLightingVisualizerActor::BeginPlay()
- {
- Super::BeginPlay();
- CachedSH = ProjectEnvironmentToSH();
- }
- void ASHLightingVisualizerActor::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- DrawVisualization();
- }
- void ASHLightingVisualizerActor::EvalSHBasis(const FVector& Dir, float OutBasis[9])
- {
- const float x = Dir.X;
- const float y = Dir.Y;
- const float z = Dir.Z;
- OutBasis[0] = 0.282095f;
- OutBasis[1] = 0.488603f * y;
- OutBasis[2] = 0.488603f * z;
- OutBasis[3] = 0.488603f * x;
- OutBasis[4] = 1.092548f * x * y;
- OutBasis[5] = 1.092548f * y * z;
- OutBasis[6] = 0.315392f * (3.0f * z * z - 1.0f);
- OutBasis[7] = 1.092548f * x * z;
- OutBasis[8] = 0.546274f * (x * x - y * y);
- }
- FVector ASHLightingVisualizerActor::EvalSHIrradiance(const FSH9& SH, const FVector& Normal)
- {
- float Basis[9];
- EvalSHBasis(Normal, Basis);
- FVector Result = FVector::ZeroVector;
- for (int i = 0; i < 9; ++i)
- {
- Result += SH.Coeffs[i] * Basis[i];
- }
- return Result;
- }
- FApproxLight ASHLightingVisualizerActor::ExtractDirectionalLight(const FSH9& SH)
- {
- FApproxLight L;
- // Ambient (l = 0)
- L.Ambient = SH.Coeffs[0] * 0.282095f;
- // l = 1 band
- FVector L1(
- SH.Coeffs[3].X,
- SH.Coeffs[1].X,
- SH.Coeffs[2].X
- );
- float Intensity = L1.Size();
- L.Direction = L1.GetSafeNormal();
- L.Color = FVector(Intensity * 1.023326f);
- return L;
- }
- FVector ASHLightingVisualizerActor::SampleAnalyticSky(const FVector& Dir)
- {
- // Simple sky + sun lobe
- const float SunDot = FMath::Max(Dir.Z, 0.0f);
- const FVector SkyColor(0.2f, 0.4f, 0.8f);
- const FVector SunColor(4.0f, 3.8f, 3.5f);
- return SkyColor + SunColor * FMath::Pow(SunDot, 64.0f);
- }
- FSH9 ASHLightingVisualizerActor::ProjectEnvironmentToSH()
- {
- FSH9 SH;
- float WeightSum = 0.0f;
- for (int32 t = 0; t < SampleThetaSteps; ++t)
- {
- const float Theta = PI * (t + 0.5f) / SampleThetaSteps;
- for (int32 p = 0; p < SamplePhiSteps; ++p)
- {
- const float Phi = 2.0f * PI * (p + 0.5f) / SamplePhiSteps;
- FVector Dir(
- FMath::Sin(Theta) * FMath::Cos(Phi),
- FMath::Sin(Theta) * FMath::Sin(Phi),
- FMath::Cos(Theta)
- );
- float Basis[9];
- EvalSHBasis(Dir, Basis);
- FVector Radiance = SampleAnalyticSky(Dir);
- const float Weight = FMath::Sin(Theta);
- for (int i = 0; i < 9; ++i)
- SH.Coeffs[i] += Radiance * Basis[i] * Weight;
- WeightSum += Weight;
- }
- }
- const float Scale = 4.0f * PI / WeightSum;
- for (int i = 0; i < 9; ++i)
- SH.Coeffs[i] *= Scale;
- return SH;
- }
- void ASHLightingVisualizerActor::DrawVisualization()
- {
- const FVector Origin = GetActorLocation();
- const FApproxLight Approx = ExtractDirectionalLight(CachedSH);
- const UWorld* W = GetWorld();
- for (int32 y = 0; y < NormalGridSize; ++y)
- {
- for (int32 x = 0; x < NormalGridSize; ++x)
- {
- const float u = (x / static_cast<float>(NormalGridSize - 1)) * 2.0f - 1.0f;
- const float v = (y / static_cast<float>(NormalGridSize - 1)) * 2.0f - 1.0f;
- FVector Normal(u, 0.0f, v);
- if (!Normal.Normalize())
- continue;
- FVector Pos = Origin + FVector(u * 300.0f, 0.0f, v * 300.0f);
- FVector SHLight = EvalSHIrradiance(CachedSH, Normal);
- DrawDebugLine(
- W,
- Pos,
- Pos + Normal * SHLight.Size() * DebugScale,
- FColor::Green,
- false, 0.0f, 0, 2.0f
- );
- if (bShowDirectionalApprox)
- {
- const float NdotL = FMath::Max(FVector::DotProduct(Normal, Approx.Direction), 0.0f);
- FVector ApproxLight = Approx.Ambient + Approx.Color * NdotL;
- DrawDebugLine(
- W,
- Pos,
- Pos + Normal * ApproxLight.Size() * DebugScale,
- FColor::Red,
- false, 0.0f, 0, 2.0f
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment