๐ Jump Flood SDF - Real-Time Distance Field Computation
The Jump Flood Algorithm (JFA) provides GPU-accelerated Signed Distance Field computation for shore detection, breaking waves, and foam generation.
Overviewโ
Traditional distance field computation requires O(nยฒ) operations. The Jump Flood Algorithm reduces this to O(n log n) by using a series of passes with decreasing step sizes, making real-time SDF computation practical for water simulation.
Algorithm Fundamentalsโ
How Jump Flood Worksโ
- Seed Pass: Initialize pixels with shore/edge positions
- Jump Passes: Each pass propagates distance information with halving step sizes
- Final Pass: Complete distance field with sub-pixel accuracy
Complexityโ
| Method | Complexity | Real-Time Viable |
|---|---|---|
| Brute Force | O(nยฒ) | โ No |
| Jump Flood | O(n log n) | โ Yes |
| Approximation | O(n) | โ ๏ธ Low Quality |
Implementationโ
UOceanologyJumpFloodComponent2Dโ
Core component for 2D distance field computation:
UCLASS(Blueprintable, BlueprintType)
class UOceanologyJumpFloodComponent2D : public UActorComponent
{
// Execute complete Jump Flood algorithm
UFUNCTION(BlueprintCallable)
void JumpFlood(
UTextureRenderTarget2D* SeedRT,
float SceneCaptureZ,
FLinearColor Curl,
bool UseDepth,
float ZxLocationT
);
// Single jump step for custom pipelines
UFUNCTION(BlueprintCallable)
UTextureRenderTarget2D* SingleJumpStep();
// Edge detection from seed texture
UFUNCTION(BlueprintCallable)
UTextureRenderTarget2D* FindEdges(
UTextureRenderTarget2D* SeedRT,
float CaptureZ,
FLinearColor Curl,
bool UseDepth,
float ZxLocationT
);
};
Material Pipelineโ
| Material | Purpose |
|---|---|
| JumpStepMaterial | Single propagation pass |
| FindEdgesMaterial | Edge detection from seed |
| BlurEdgesMaterial | Optional smoothing pass |
Render Target Configurationโ
Ping-Pong Buffersโ
The algorithm uses two render targets (RTA, RTB) for ping-pong rendering:
void AssignRenderTargets(
UTextureRenderTarget2D* InRTA,
UTextureRenderTarget2D* InRTB
);
Pass Calculationโ
Required passes = ceil(log2(max(width, height)))
For a 1024ร1024 texture: 10 passes
Use Casesโ
Shore Detectionโ
Generate distance-to-shore for:
- Breaking wave positioning
- Foam intensity gradients
- Shallow water effects
- Shoreline wetness
Wave Breaking Zonesโ
SDF enables accurate surf zone identification:
- Detect approaching shore
- Trigger wave transformation
- Scale breaking intensity
- Position foam generation
Flow Field Generationโ
Combine SDF with gradient computation:
- Water flow toward shore
- Current direction fields
- Velocity magnitude mapping
Water Textures Componentโ
UOceanologyWaterTexturesComponentโ
Integrates Jump Flood with the water rendering pipeline:
UPROPERTY(VisibleAnywhere, Transient)
UTextureRenderTarget2D* RT_JumpFlood = nullptr;
UPROPERTY(VisibleAnywhere, Transient)
UTextureRenderTarget2D* RT_SDF_Gradient = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TEnumAsByte<EOceanologyWaterTexturesMode> WaterTexturesMode;
Capture Modesโ
| Mode | Description |
|---|---|
| RenderTarget | Real-time updates each frame |
| Baked | Pre-computed for static shorelines |
Auto-Captureโ
UPROPERTY(EditAnywhere)
bool bAutoCaptureWithRenderTargets = true;
UPROPERTY(EditAnywhere)
float AutoCaptureDelay = 0.1f;
Optional Blur Passโ
Enable blur for smoother distance fields:
UPROPERTY(EditAnywhere)
bool UseBlur = false;
UPROPERTY(EditAnywhere)
int32 BlurPasses = 1;
Performanceโ
GPU Optimizedโ
- All computation on GPU via material draws
- Minimal CPU overhead
- Supports dynamic resolution scaling
Optimization Tipsโ
| Technique | Benefit |
|---|---|
| Lower Resolution | Faster computation |
| Baked Mode | Zero runtime cost |
| Reduced Blur | Fewer passes |
| Async Capture | Non-blocking updates |
Debug Visualizationโ
UFUNCTION(BlueprintCallable)
void FindEdges_Debug(
UTextureRenderTarget2D* SeedRT,
float CaptureZ,
FLinearColor Curl,
UTextureRenderTarget2D* DestRT,
float ZOffset
);
Visualize:
- Seed points
- Distance gradients
- Edge detection results
- Completed SDF
Integration with Breaking Wavesโ
The SDF provides critical data for breaking wave simulation:
- Shore Distance โ Wave transformation trigger
- Gradient Direction โ Wave approach angle
- Depth Estimation โ Breaking intensity
- Surf Zone Width โ Foam spread area
Resourcesโ
- Console Variables:
r.Oceanology.JumpFlood.* - Debug Mode: Enable visualization in editor
- Documentation: Technical deep-dive in docs