- // Calculate the integer coordinates
- int x0 = (x > 0.0 ? (int)x : (int)x - 1);
- int y0 = (y > 0.0 ? (int)y : (int)y - 1);
- // Calculate the remaining part of the coordinates
- double xl = x - (double)x0;
- double yl = y - (double)y0;
- // Calculate random cosine lookup table indices for the integer corners.
- // They are looked up as unit vector gradients from the lookup table.
- int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
- int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
- int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
- int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
- // Make a dot product for the gradients and the positions, to get the values
- double s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
- double u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
- double v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
- double w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
- // Interpolate between the values
- return biLinearInterpolation(s,u,v,w,xl,yl);
-}
-#endif
-
-#if 1
-double noise2d_gradient(double x, double y, int seed)
+ return vx * wx + vy * wy;
+}
+
+
+inline float linearInterpolation(float v0, float v1, float t)
+{
+ return v0 + (v1 - v0) * t;
+}
+
+
+inline float biLinearInterpolation(
+ float v00, float v10,
+ float v01, float v11,
+ float x, float y)
+{
+ float tx = easeCurve(x);
+ float ty = easeCurve(y);
+ float u = linearInterpolation(v00, v10, tx);
+ float v = linearInterpolation(v01, v11, tx);
+ return linearInterpolation(u, v, ty);
+}
+
+
+inline float biLinearInterpolationNoEase(
+ float v00, float v10,
+ float v01, float v11,
+ float x, float y)
+{
+ float u = linearInterpolation(v00, v10, x);
+ float v = linearInterpolation(v01, v11, x);
+ return linearInterpolation(u, v, y);
+}
+
+
+float triLinearInterpolation(
+ float v000, float v100, float v010, float v110,
+ float v001, float v101, float v011, float v111,
+ float x, float y, float z)
+{
+ float tx = easeCurve(x);
+ float ty = easeCurve(y);
+ float tz = easeCurve(z);
+ float u = biLinearInterpolationNoEase(v000, v100, v010, v110, tx, ty);
+ float v = biLinearInterpolationNoEase(v001, v101, v011, v111, tx, ty);
+ return linearInterpolation(u, v, tz);
+}
+
+float triLinearInterpolationNoEase(
+ float v000, float v100, float v010, float v110,
+ float v001, float v101, float v011, float v111,
+ float x, float y, float z)
+{
+ float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
+ float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
+ return linearInterpolation(u, v, z);
+}
+
+float noise2d_gradient(float x, float y, s32 seed, bool eased)