/// <summary>Sample 2D Perlin noise, tiled in both dimensions.</summary> /// <returns>The noise value.</returns> /// <param name="point">Sample point in 2D.</param> /// <param name="xOffset">X offset of the tiling domain.</param> /// <param name="yOffset">Y offset of the tiling domain.</param> /// <param name="frequency">Frequency of the noise.</param> public static float Sample2DTiledXY(Vector2 point, int xOffset, int yOffset, int frequency) { if (frequency == 0) { return(0f); } point.x *= frequency; point.y *= frequency; int ix0 = NoiseMath.FloorToInt(point.x); int iy0 = NoiseMath.FloorToInt(point.y); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tx1 = tx0 - 1f; float ty1 = ty0 - 1f; ix0 %= frequency; if (ix0 < 0) { ix0 += frequency; } iy0 %= frequency; if (iy0 < 0) { iy0 += frequency; } int ix1 = ((ix0 + 1) % frequency + xOffset) & NoiseMath.hashMask; int iy1 = ((iy0 + 1) % frequency + yOffset) & NoiseMath.hashMask; ix0 = (ix0 + xOffset) & NoiseMath.hashMask; iy0 = (iy0 + yOffset) & NoiseMath.hashMask; int h0 = NoiseMath.hash[ix0]; int h1 = NoiseMath.hash[ix1]; Vector2 g00 = NoiseMath.gradients2D[NoiseMath.hash[h0 + iy0] & NoiseMath.gradientsMask2D]; Vector2 g10 = NoiseMath.gradients2D[NoiseMath.hash[h1 + iy0] & NoiseMath.gradientsMask2D]; Vector2 g01 = NoiseMath.gradients2D[NoiseMath.hash[h0 + iy1] & NoiseMath.gradientsMask2D]; Vector2 g11 = NoiseMath.gradients2D[NoiseMath.hash[h1 + iy1] & NoiseMath.gradientsMask2D]; float v00 = NoiseMath.Dot(g00, tx0, ty0); float v10 = NoiseMath.Dot(g10, tx1, ty0); float v01 = NoiseMath.Dot(g01, tx0, ty1); float v11 = NoiseMath.Dot(g11, tx1, ty1); float a = v00; float b = v10 - v00; float c = v01 - v00; float d = v11 - v01 - v10 + v00; float tx = NoiseMath.Smooth(tx0); float ty = NoiseMath.Smooth(ty0); return((a + b * tx + (c + d * tx) * ty) * sqrt2); }
/// <summary>Sample 2D Perlin noise.</summary> /// <returns>The noise value.</returns> /// <param name="point">Sample point in 2D.</param> /// <param name="frequency">Frequency of the noise.</param> public static float Sample2D(Vector2 point, float frequency) { point.x *= frequency; point.y *= frequency; int ix0 = NoiseMath.FloorToInt(point.x); int iy0 = NoiseMath.FloorToInt(point.y); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tx1 = tx0 - 1f; float ty1 = ty0 - 1f; ix0 &= NoiseMath.hashMask; iy0 &= NoiseMath.hashMask; int ix1 = ix0 + 1; int iy1 = iy0 + 1; int h0 = NoiseMath.hash[ix0]; int h1 = NoiseMath.hash[ix1]; Vector2 g00 = NoiseMath.gradients2D[NoiseMath.hash[h0 + iy0] & NoiseMath.gradientsMask2D]; Vector2 g10 = NoiseMath.gradients2D[NoiseMath.hash[h1 + iy0] & NoiseMath.gradientsMask2D]; Vector2 g01 = NoiseMath.gradients2D[NoiseMath.hash[h0 + iy1] & NoiseMath.gradientsMask2D]; Vector2 g11 = NoiseMath.gradients2D[NoiseMath.hash[h1 + iy1] & NoiseMath.gradientsMask2D]; float v00 = NoiseMath.Dot(g00, tx0, ty0); float v10 = NoiseMath.Dot(g10, tx1, ty0); float v01 = NoiseMath.Dot(g01, tx0, ty1); float v11 = NoiseMath.Dot(g11, tx1, ty1); float a = v00; float b = v10 - v00; float c = v01 - v00; float d = v11 - v01 - v10 + v00; float tx = NoiseMath.Smooth(tx0); float ty = NoiseMath.Smooth(ty0); return((a + b * tx + (c + d * tx) * ty) * sqrt2); }
/// <summary>Sample 3D Perlin noise, tiled in the X and Y dimensions.</summary> /// <returns>The noise value.</returns> /// <param name="point">Sample point in 3D.</param> /// <param name="xOffset">X offset of the tiling domain.</param> /// <param name="yOffset">Y offset of the tiling domain.</param> /// <param name="zOffset">Z offset of the tiling domain.</param> /// <param name="frequency">Frequency of the noise.</param> public static float Sample3DTiledXYZ(Vector3 point, int xOffset, int yOffset, int zOffset, int frequency) { if (frequency == 0) { return(0f); } point.x *= frequency; point.y *= frequency; point.z *= frequency; int ix0 = NoiseMath.FloorToInt(point.x); int iy0 = NoiseMath.FloorToInt(point.y); int iz0 = NoiseMath.FloorToInt(point.z); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tz0 = point.z - iz0; float tx1 = tx0 - 1f; float ty1 = ty0 - 1f; float tz1 = tz0 - 1f; ix0 %= frequency; if (ix0 < 0) { ix0 += frequency; } iy0 %= frequency; if (iy0 < 0) { iy0 += frequency; } iz0 %= frequency; if (iz0 < 0) { iz0 += frequency; } int ix1 = ((ix0 + 1) % frequency + xOffset) & NoiseMath.hashMask; int iy1 = ((iy0 + 1) % frequency + yOffset) & NoiseMath.hashMask; int iz1 = ((iz0 + 1) % frequency + zOffset) & NoiseMath.hashMask; ix0 = (ix0 + xOffset) & NoiseMath.hashMask; iy0 = (iy0 + yOffset) & NoiseMath.hashMask; iz0 = (iz0 + zOffset) & NoiseMath.hashMask; int h0 = NoiseMath.hash[ix0]; int h1 = NoiseMath.hash[ix1]; int h00 = NoiseMath.hash[h0 + iy0]; int h10 = NoiseMath.hash[h1 + iy0]; int h01 = NoiseMath.hash[h0 + iy1]; int h11 = NoiseMath.hash[h1 + iy1]; Vector3 g000 = NoiseMath.gradients3D[NoiseMath.hash[h00 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g100 = NoiseMath.gradients3D[NoiseMath.hash[h10 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g010 = NoiseMath.gradients3D[NoiseMath.hash[h01 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g110 = NoiseMath.gradients3D[NoiseMath.hash[h11 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g001 = NoiseMath.gradients3D[NoiseMath.hash[h00 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g101 = NoiseMath.gradients3D[NoiseMath.hash[h10 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g011 = NoiseMath.gradients3D[NoiseMath.hash[h01 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g111 = NoiseMath.gradients3D[NoiseMath.hash[h11 + iz1] & NoiseMath.gradientsMask3D]; float v000 = NoiseMath.Dot(g000, tx0, ty0, tz0); float v100 = NoiseMath.Dot(g100, tx1, ty0, tz0); float v010 = NoiseMath.Dot(g010, tx0, ty1, tz0); float v110 = NoiseMath.Dot(g110, tx1, ty1, tz0); float v001 = NoiseMath.Dot(g001, tx0, ty0, tz1); float v101 = NoiseMath.Dot(g101, tx1, ty0, tz1); float v011 = NoiseMath.Dot(g011, tx0, ty1, tz1); float v111 = NoiseMath.Dot(g111, tx1, ty1, tz1); float a = v000; float b = v100 - v000; float c = v010 - v000; float d = v001 - v000; float e = v110 - v010 - v100 + v000; float f = v101 - v001 - v100 + v000; float g = v011 - v001 - v010 + v000; float h = v111 - v011 - v101 + v001 - v110 + v010 + v100 - v000; float tx = NoiseMath.Smooth(tx0); float ty = NoiseMath.Smooth(ty0); float tz = NoiseMath.Smooth(tz0); return(a + b * tx + (c + e * tx) * ty + (d + f * tx + (g + h * tx) * ty) * tz); }
/// <summary>Sample 3D Perlin noise.</summary> /// <returns>The noise value.</returns> /// <param name="point">Sample point in 3D.</param> /// <param name="frequency">Frequency of the noise.</param> public static float Sample3D(Vector3 point, float frequency) { point.x *= frequency; point.y *= frequency; point.z *= frequency; int ix0 = NoiseMath.FloorToInt(point.x); int iy0 = NoiseMath.FloorToInt(point.y); int iz0 = NoiseMath.FloorToInt(point.z); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tz0 = point.z - iz0; float tx1 = tx0 - 1f; float ty1 = ty0 - 1f; float tz1 = tz0 - 1f; ix0 &= NoiseMath.hashMask; iy0 &= NoiseMath.hashMask; iz0 &= NoiseMath.hashMask; int ix1 = ix0 + 1; int iy1 = iy0 + 1; int iz1 = iz0 + 1; int h0 = NoiseMath.hash[ix0]; int h1 = NoiseMath.hash[ix1]; int h00 = NoiseMath.hash[h0 + iy0]; int h10 = NoiseMath.hash[h1 + iy0]; int h01 = NoiseMath.hash[h0 + iy1]; int h11 = NoiseMath.hash[h1 + iy1]; Vector3 g000 = NoiseMath.gradients3D[NoiseMath.hash[h00 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g100 = NoiseMath.gradients3D[NoiseMath.hash[h10 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g010 = NoiseMath.gradients3D[NoiseMath.hash[h01 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g110 = NoiseMath.gradients3D[NoiseMath.hash[h11 + iz0] & NoiseMath.gradientsMask3D]; Vector3 g001 = NoiseMath.gradients3D[NoiseMath.hash[h00 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g101 = NoiseMath.gradients3D[NoiseMath.hash[h10 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g011 = NoiseMath.gradients3D[NoiseMath.hash[h01 + iz1] & NoiseMath.gradientsMask3D]; Vector3 g111 = NoiseMath.gradients3D[NoiseMath.hash[h11 + iz1] & NoiseMath.gradientsMask3D]; float v000 = NoiseMath.Dot(g000, tx0, ty0, tz0); float v100 = NoiseMath.Dot(g100, tx1, ty0, tz0); float v010 = NoiseMath.Dot(g010, tx0, ty1, tz0); float v110 = NoiseMath.Dot(g110, tx1, ty1, tz0); float v001 = NoiseMath.Dot(g001, tx0, ty0, tz1); float v101 = NoiseMath.Dot(g101, tx1, ty0, tz1); float v011 = NoiseMath.Dot(g011, tx0, ty1, tz1); float v111 = NoiseMath.Dot(g111, tx1, ty1, tz1); float a = v000; float b = v100 - v000; float c = v010 - v000; float d = v001 - v000; float e = v110 - v010 - v100 + v000; float f = v101 - v001 - v100 + v000; float g = v011 - v001 - v010 + v000; float h = v111 - v011 - v101 + v001 - v110 + v010 + v100 - v000; float tx = NoiseMath.Smooth(tx0); float ty = NoiseMath.Smooth(ty0); float tz = NoiseMath.Smooth(tz0); return(a + b * tx + (c + e * tx) * ty + (d + f * tx + (g + h * tx) * ty) * tz); }