private static float Perlin3D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int ix0 = Mathf.FloorToInt(point.x); int iy0 = Mathf.FloorToInt(point.y); int iz0 = Mathf.FloorToInt(point.z); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tz0 = point.z - iz0; float tx1 = tx0 - 1; float ty1 = ty0 - 1; float tz1 = tz0 - 1; ix0 &= HASH_LENGTH; iy0 &= HASH_LENGTH; iz0 &= HASH_LENGTH; int ix1 = ix0 + 1; int iy1 = iy0 + 1; int iz1 = iz0 + 1; int h0 = m_hash[ix0]; int h1 = m_hash[ix1]; int h00 = m_hash[h0 + iy0]; int h10 = m_hash[h1 + iy0]; int h01 = m_hash[h0 + iy1]; int h11 = m_hash[h1 + iy1]; Vector3 g000 = m_gradients3D[m_hash[h00 + iz0] & GRADIENT_MASK_3D]; Vector3 g100 = m_gradients3D[m_hash[h10 + iz0] & GRADIENT_MASK_3D]; Vector3 g010 = m_gradients3D[m_hash[h01 + iz0] & GRADIENT_MASK_3D]; Vector3 g110 = m_gradients3D[m_hash[h11 + iz0] & GRADIENT_MASK_3D]; Vector3 g001 = m_gradients3D[m_hash[h00 + iz1] & GRADIENT_MASK_3D]; Vector3 g101 = m_gradients3D[m_hash[h10 + iz1] & GRADIENT_MASK_3D]; Vector3 g011 = m_gradients3D[m_hash[h01 + iz1] & GRADIENT_MASK_3D]; Vector3 g111 = m_gradients3D[m_hash[h11 + iz1] & GRADIENT_MASK_3D]; float v000 = Dot(g000, tx0, ty0, tz0); float v100 = Dot(g100, tx1, ty0, tz0); float v010 = Dot(g010, tx0, ty1, tz0); float v110 = Dot(g110, tx1, ty1, tz0); float v001 = Dot(g001, tx0, ty0, tz1); float v101 = Dot(g101, tx1, ty0, tz1); float v011 = Dot(g011, tx0, ty1, tz1); float v111 = Dot(g111, tx1, ty1, tz1); float xLerp1 = interpolateMethod(v000, v100, tx0); float xLerp2 = interpolateMethod(v010, v110, tx0); float xLerp3 = interpolateMethod(v001, v101, tx0); float xLerp4 = interpolateMethod(v011, v111, tx0); float yLerp1 = interpolateMethod(xLerp1, xLerp2, ty0); float yLerp2 = interpolateMethod(xLerp3, xLerp4, ty0); float sample = interpolateMethod(yLerp1, yLerp2, tz0); return(sample); }
private static float TRIANGLES_TO_SQUARES = 0.3660254037844f; //(Mathf.Sqrt(3f) - 1f) / 2f private static float Simplex2D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; float skew = (point.x + point.y) * TRIANGLES_TO_SQUARES; float sx = point.x + skew; float sy = point.y + skew; int ix = Mathf.FloorToInt(sx); int iy = Mathf.FloorToInt(sy); float value = Simplex2DFunction(point, ix, iy); value += Simplex2DFunction(point, ix + 1, iy + 1); if (sx - ix >= sy - iy) { value += Simplex2DFunction(point, ix + 1, iy); } else { value += Simplex2DFunction(point, ix, iy + 1); } return(value * (8f * 2f / HASH_LENGTH) - 1f); }
private static float Value2D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int ix0 = Mathf.FloorToInt(point.x); int iy0 = Mathf.FloorToInt(point.y); float tx = point.x - ix0; float ty = point.y - iy0; ix0 &= HASH_LENGTH; iy0 &= HASH_LENGTH; int ix1 = ix0 + 1; int iy1 = iy0 + 1; int h0 = m_hash[ix0]; int h1 = m_hash[ix1]; int h00 = m_hash[h0 + iy0]; int h10 = m_hash[h1 + iy0]; int h01 = m_hash[h0 + iy1]; int h11 = m_hash[h1 + iy1]; float xLerp1 = interpolateMethod(h00, h10, tx); float xLerp2 = interpolateMethod(h01, h11, tx); float sample = interpolateMethod(xLerp1, xLerp2, ty); return(sample * (2f / HASH_LENGTH) - 1f); }
private static float Simplex1D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int ix = Mathf.FloorToInt(point.x); float value = Simplex1DFunction(point, ix); value += Simplex1DFunction(point, ix + 1); return(value * (2f / HASH_LENGTH) - 1f); }
private static float FractalSum(Vector3 point, NoiseMethodDelegate noiseMethod, InterpolateMethodDelegate interpolateMethod, float frequency, int octave, float lacunarity, float persistence) { float sum = noiseMethod(point, interpolateMethod, frequency); float amplitude = 1f; float maxValue = 1f; for (int i = 1; i < octave; i++) { frequency *= lacunarity; amplitude *= persistence; maxValue += amplitude; sum += noiseMethod(point, interpolateMethod, frequency) * amplitude; } return(sum / maxValue); }
private static float Value1D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int i0 = Mathf.FloorToInt(point.x); float t = point.x - i0; i0 &= HASH_LENGTH; int i1 = i0 + 1; int h0 = m_hash[i0]; int h1 = m_hash[i1]; float sample = interpolateMethod(h0, h1, t); return(sample * (2f / HASH_LENGTH) - 1f); }
private static float Perlin1D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int i0 = Mathf.FloorToInt(point.x); float t0 = point.x - i0; float t1 = t0 - 1f; i0 &= HASH_LENGTH; int i1 = i0 + 1; float g0 = m_gradients1D[m_hash[i0] & GRADIENT_MASK_1D]; float g1 = m_gradients1D[m_hash[i1] & GRADIENT_MASK_1D]; float v0 = g0 * t0; float v1 = g1 * t1; float sample = interpolateMethod(v0, v1, t0); return(sample * 2f); }
private static float Perlin2D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; int ix0 = Mathf.FloorToInt(point.x); int iy0 = Mathf.FloorToInt(point.y); float tx0 = point.x - ix0; float ty0 = point.y - iy0; float tx1 = tx0 - 1; float ty1 = ty0 - 1; ix0 &= HASH_LENGTH; iy0 &= HASH_LENGTH; int ix1 = ix0 + 1; int iy1 = iy0 + 1; int h0 = m_hash[ix0]; int h1 = m_hash[ix1]; Vector2 g00 = m_gradients2D[m_hash[h0 + iy0] & GRADIENT_MASK_2D]; Vector2 g10 = m_gradients2D[m_hash[h1 + iy0] & GRADIENT_MASK_2D]; Vector2 g01 = m_gradients2D[m_hash[h0 + iy1] & GRADIENT_MASK_2D]; Vector2 g11 = m_gradients2D[m_hash[h1 + iy1] & GRADIENT_MASK_2D]; float v00 = Dot(g00, tx0, ty0); float v10 = Dot(g10, tx1, ty0); float v01 = Dot(g01, tx0, ty1); float v11 = Dot(g11, tx1, ty1); float xLerp1 = interpolateMethod(v00, v10, tx0); float xLerp2 = interpolateMethod(v01, v11, tx0); float sample = interpolateMethod(xLerp1, xLerp2, ty0); return(sample * m_sqrt2); }
private static float Simplex3D(Vector3 point, InterpolateMethodDelegate interpolateMethod, float frequency) { point *= frequency; float skew = (point.x + point.y + point.z) * TETRAHEDRA_TO_CUBE; float sx = point.x + skew; float sy = point.y + skew; float sz = point.z + skew; int ix = Mathf.FloorToInt(sx); int iy = Mathf.FloorToInt(sy); int iz = Mathf.FloorToInt(sz); float value = Simplex3DFunction(point, ix, iy, iz); value += Simplex3DFunction(point, ix + 1, iy + 1, iz + 1); float x = sx - ix; float y = sy - iy; float z = sz - iz; if (x >= y) { if (x >= z) { value += Simplex3DFunction(point, ix + 1, iy, iz); if (y >= z) { value += Simplex3DFunction(point, ix + 1, iy + 1, iz); } else { value += Simplex3DFunction(point, ix + 1, iy, iz + 1); } } else { value += Simplex3DFunction(point, ix, iy, iz + 1); value += Simplex3DFunction(point, ix + 1, iy, iz + 1); } } else { if (y >= z) { value += Simplex3DFunction(point, ix, iy + 1, iz); if (x >= z) { value += Simplex3DFunction(point, ix + 1, iy + 1, iz); } else { value += Simplex3DFunction(point, ix, iy + 1, iz + 1); } } else { value += Simplex3DFunction(point, ix, iy, iz + 1); value += Simplex3DFunction(point, ix, iy + 1, iz + 1); } } return(value * (8f * 2f / HASH_LENGTH) - 1f); }