TheRedPixel

Unreal C++ L2 Spherical Harmonics

Jan 27th, 2026
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | Source Code | 0 0
  1. /************************
  2. ***********Header***********
  3. *************************/
  4. USTRUCT()
  5. struct FSH9
  6. {
  7.     GENERATED_BODY()
  8.  
  9.     FVector Coeffs[9];
  10.  
  11.     FSH9()
  12.     {
  13.         for (int32 i = 0; i < 9; ++i)
  14.             Coeffs[i] = FVector::ZeroVector;
  15.     }
  16. };
  17.  
  18. USTRUCT()
  19. struct FApproxLight
  20. {
  21.     GENERATED_BODY()
  22.  
  23.     FVector Direction;
  24.     FVector Color;
  25.     FVector Ambient;
  26. };
  27.  
  28. UCLASS()
  29. class ASHLightingVisualizerActor : public AActor
  30. {
  31.     GENERATED_BODY()
  32.  
  33. public:
  34.     ASHLightingVisualizerActor();
  35.  
  36.     virtual void BeginPlay() override;
  37.     virtual void Tick(float DeltaTime) override;
  38.  
  39. protected:
  40.     //*** SH math ***
  41.     void EvalSHBasis(const FVector& Dir, float OutBasis[9]);
  42.     FVector EvalSHIrradiance(const FSH9& SH, const FVector& Normal);
  43.     FApproxLight ExtractDirectionalLight(const FSH9& SH);
  44.  
  45.     //*** Environment ***
  46.     FVector SampleAnalyticSky(const FVector& Dir);
  47.     FSH9 ProjectEnvironmentToSH();
  48.  
  49.     //*** Visualization ***
  50.     void DrawVisualization();
  51.    
  52.     void Settings();
  53.  
  54. protected:
  55.     UPROPERTY(EditAnywhere, Category="SH")
  56.     int32 SampleThetaSteps = 32;
  57.  
  58.     UPROPERTY(EditAnywhere, Category="SH")
  59.     int32 SamplePhiSteps = 64;
  60.  
  61.     UPROPERTY(EditAnywhere, Category="Visualization")
  62.     float DebugScale = 200.0f;
  63.  
  64.     UPROPERTY(EditAnywhere, Category="Visualization")
  65.     int32 NormalGridSize = 8;
  66.  
  67.     UPROPERTY(EditAnywhere, Category="Visualization")
  68.     bool bShowDirectionalApprox = true;
  69.  
  70. private:
  71.     FSH9 CachedSH;
  72. };
  73.  
  74. /************************
  75. ***********CPP***********
  76. *************************/
  77.  
  78. void ASHLightingVisualizerActor::BeginPlay()
  79. {
  80.     Super::BeginPlay();
  81.     CachedSH = ProjectEnvironmentToSH();
  82. }
  83.  
  84. void ASHLightingVisualizerActor::Tick(float DeltaTime)
  85. {
  86.     Super::Tick(DeltaTime);
  87.     DrawVisualization();
  88. }
  89.  
  90. void ASHLightingVisualizerActor::EvalSHBasis(const FVector& Dir, float OutBasis[9])
  91. {
  92.     const float x = Dir.X;
  93.     const float y = Dir.Y;
  94.     const float z = Dir.Z;
  95.  
  96.     OutBasis[0] = 0.282095f;
  97.  
  98.     OutBasis[1] = 0.488603f * y;
  99.     OutBasis[2] = 0.488603f * z;
  100.     OutBasis[3] = 0.488603f * x;
  101.  
  102.     OutBasis[4] = 1.092548f * x * y;
  103.     OutBasis[5] = 1.092548f * y * z;
  104.     OutBasis[6] = 0.315392f * (3.0f * z * z - 1.0f);
  105.     OutBasis[7] = 1.092548f * x * z;
  106.     OutBasis[8] = 0.546274f * (x * x - y * y);
  107. }
  108.  
  109. FVector ASHLightingVisualizerActor::EvalSHIrradiance(const FSH9& SH, const FVector& Normal)
  110. {
  111.     float Basis[9];
  112.     EvalSHBasis(Normal, Basis);
  113.  
  114.     FVector Result = FVector::ZeroVector;
  115.     for (int i = 0; i < 9; ++i)
  116.     {
  117.         Result += SH.Coeffs[i] * Basis[i];
  118.     }
  119.  
  120.     return Result;
  121. }
  122.  
  123. FApproxLight ASHLightingVisualizerActor::ExtractDirectionalLight(const FSH9& SH)
  124. {
  125.     FApproxLight L;
  126.  
  127.     // Ambient (l = 0)
  128.     L.Ambient = SH.Coeffs[0] * 0.282095f;
  129.  
  130.     // l = 1 band
  131.     FVector L1(
  132.         SH.Coeffs[3].X,
  133.         SH.Coeffs[1].X,
  134.         SH.Coeffs[2].X
  135.     );
  136.  
  137.     float Intensity = L1.Size();
  138.     L.Direction = L1.GetSafeNormal();
  139.     L.Color = FVector(Intensity * 1.023326f);
  140.  
  141.     return L;
  142. }
  143.  
  144. FVector ASHLightingVisualizerActor::SampleAnalyticSky(const FVector& Dir)
  145. {
  146.     // Simple sky + sun lobe
  147.     const float SunDot = FMath::Max(Dir.Z, 0.0f);
  148.     const FVector SkyColor(0.2f, 0.4f, 0.8f);
  149.     const FVector SunColor(4.0f, 3.8f, 3.5f);
  150.  
  151.     return SkyColor + SunColor * FMath::Pow(SunDot, 64.0f);
  152. }
  153.  
  154. FSH9 ASHLightingVisualizerActor::ProjectEnvironmentToSH()
  155. {
  156.     FSH9 SH;
  157.     float WeightSum = 0.0f;
  158.  
  159.     for (int32 t = 0; t < SampleThetaSteps; ++t)
  160.     {
  161.         const float Theta = PI * (t + 0.5f) / SampleThetaSteps;
  162.  
  163.         for (int32 p = 0; p < SamplePhiSteps; ++p)
  164.         {
  165.             const float Phi = 2.0f * PI * (p + 0.5f) / SamplePhiSteps;
  166.  
  167.             FVector Dir(
  168.                 FMath::Sin(Theta) * FMath::Cos(Phi),
  169.                 FMath::Sin(Theta) * FMath::Sin(Phi),
  170.                 FMath::Cos(Theta)
  171.             );
  172.  
  173.             float Basis[9];
  174.             EvalSHBasis(Dir, Basis);
  175.  
  176.             FVector Radiance = SampleAnalyticSky(Dir);
  177.             const float Weight = FMath::Sin(Theta);
  178.  
  179.             for (int i = 0; i < 9; ++i)
  180.                 SH.Coeffs[i] += Radiance * Basis[i] * Weight;
  181.  
  182.             WeightSum += Weight;
  183.         }
  184.     }
  185.  
  186.     const float Scale = 4.0f * PI / WeightSum;
  187.     for (int i = 0; i < 9; ++i)
  188.         SH.Coeffs[i] *= Scale;
  189.    
  190.     return SH;
  191. }
  192.  
  193. void ASHLightingVisualizerActor::DrawVisualization()
  194. {
  195.     const FVector Origin = GetActorLocation();
  196.     const FApproxLight Approx = ExtractDirectionalLight(CachedSH);
  197.     const UWorld* W = GetWorld();
  198.     for (int32 y = 0; y < NormalGridSize; ++y)
  199.     {
  200.         for (int32 x = 0; x < NormalGridSize; ++x)
  201.         {
  202.             const float u = (x / static_cast<float>(NormalGridSize - 1)) * 2.0f - 1.0f;
  203.             const float v = (y / static_cast<float>(NormalGridSize - 1)) * 2.0f - 1.0f;
  204.  
  205.             FVector Normal(u, 0.0f, v);
  206.             if (!Normal.Normalize())
  207.                 continue;
  208.  
  209.             FVector Pos = Origin + FVector(u * 300.0f, 0.0f, v * 300.0f);
  210.  
  211.             FVector SHLight = EvalSHIrradiance(CachedSH, Normal);
  212.             DrawDebugLine(
  213.                 W,
  214.                 Pos,
  215.                 Pos + Normal * SHLight.Size() * DebugScale,
  216.                 FColor::Green,
  217.                 false, 0.0f, 0, 2.0f
  218.             );
  219.  
  220.             if (bShowDirectionalApprox)
  221.             {
  222.                 const float NdotL = FMath::Max(FVector::DotProduct(Normal, Approx.Direction), 0.0f);
  223.                 FVector ApproxLight = Approx.Ambient + Approx.Color * NdotL;
  224.  
  225.                 DrawDebugLine(
  226.                     W,
  227.                     Pos,
  228.                     Pos + Normal * ApproxLight.Size() * DebugScale,
  229.                     FColor::Red,
  230.                     false, 0.0f, 0, 2.0f
  231.                 );
  232.             }
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment