Skip to main content

๐Ÿ“ Jump Flood SDF - Real-Time Distance Field Computation

ยท 3 min read
Galidar
Founder, Galidar Studio

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โ€‹

  1. Seed Pass: Initialize pixels with shore/edge positions
  2. Jump Passes: Each pass propagates distance information with halving step sizes
  3. Final Pass: Complete distance field with sub-pixel accuracy

Complexityโ€‹

MethodComplexityReal-Time Viable
Brute ForceO(nยฒ)โŒ No
Jump FloodO(n log n)โœ… Yes
ApproximationO(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โ€‹

MaterialPurpose
JumpStepMaterialSingle propagation pass
FindEdgesMaterialEdge detection from seed
BlurEdgesMaterialOptional 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โ€‹

ModeDescription
RenderTargetReal-time updates each frame
BakedPre-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โ€‹

TechniqueBenefit
Lower ResolutionFaster computation
Baked ModeZero runtime cost
Reduced BlurFewer passes
Async CaptureNon-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:

  1. Shore Distance โ†’ Wave transformation trigger
  2. Gradient Direction โ†’ Wave approach angle
  3. Depth Estimation โ†’ Breaking intensity
  4. Surf Zone Width โ†’ Foam spread area

Resourcesโ€‹

  • Console Variables: r.Oceanology.JumpFlood.*
  • Debug Mode: Enable visualization in editor
  • Documentation: Technical deep-dive in docs