// Get the noise at a location. public float GetAt(Vector2 position) { // This is heavily based on the video by 'The Art of Code' // https://www.youtube.com/watch?v=zXsWftRdsvU Vector2 intiger = new Vector2() { X = Mathf.Truncate(position.X), Y = Mathf.Truncate(position.Y) }; Vector2 fraction = position - intiger; // Smoothly interperlate the fraction of each 'cell' fraction = fraction * fraction * new Vector2(3 - 2 * fraction.X, 3 - 2 * fraction.Y); // I am not blending diagonally as I want the noise to have a bias to stick to the voxel grid. float btmL = sample(intiger); float btmR = sample(intiger + Vector2.UnitX); float topL = sample(intiger + Vector2.UnitY); float topR = sample(intiger + Vector2.One); float btm = lerp(btmL, btmR, fraction.X); float top = lerp(topL, topR, fraction.X); return(lerp(btm, top, fraction.Y)); }
float sample(Vector2 position) { // Make a crazy big number and do some stuff to it. Then get only // the decimal part of this big number. unchecked { float t = position.X * xSample + position.Y * ySample; t = (1 + Mathf.Sin(t) * 0.5f) * scaler; return(t - Mathf.Truncate(t)); } }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <returns></returns> public static Half Truncate(Half x) => (Half)M.Truncate(x);