Skip to content

Commit 805df90

Browse files
committed
early exits, saves ~15% performance for 2D worley noise
1 parent 43a6ec6 commit 805df90

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

sources/noise/cell.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ struct CellNoise2D:Noise
6161
// as “near” and “far” points. We call these points the *generating points*.
6262
// The sample point (example) has been marked with an ‘*’.
6363

64-
// A ------- far
64+
// A ------ far
6565
// | | |
6666
// |----+----|
6767
// | * | |
68-
// near ------- A
68+
// near ------ A
6969

7070
// The actual feature points never spawn outside of the unit square surrounding
7171
// their generating points. Therefore, the boundaries of the generating
@@ -111,6 +111,13 @@ struct CellNoise2D:Noise
111111
test(generating_point: far)
112112
}
113113

114+
// EARLY EXIT: if we have a point within 0.5 units, we don’t have to check
115+
// the outer kernel
116+
if r2_min < 0.5*0.5
117+
{
118+
return self.amplitude * r2_min
119+
}
120+
114121
// This is the part where shit hits the fan. (`inner` and `outer` are never
115122
// sampled directly, they are used for calculating the coordinates of the
116123
// generating point.)
@@ -152,6 +159,13 @@ struct CellNoise2D:Noise
152159
test(generating_point: (far.a, inner.b))
153160
}
154161

162+
// EARLY EXIT: if we have a point within 1 unit, we don’t have to check
163+
// the D points or the E points
164+
if r2_min < 1*1
165+
{
166+
return self.amplitude * r2_min
167+
}
168+
155169
// D points
156170
if (1.5 - nearpoint_disp.y) * (1.5 - nearpoint_disp.y) < r2_min
157171
{

0 commit comments

Comments
 (0)