Beispiel #1
0
    /// <summary>
    /// Computes Worley/Voroni noise for the given seed.
    /// </summary>
    /// <param name="distFunc">
    /// The function to get the distance between two positions.
    /// </param>
    /// <param name="distsToValue">
    /// Takes in the closest and second-closest distances and outputs a noise value from them.
    /// </param>
    /// <example>
    /// //A standard effect is straight-line distance and using the closest distance value.
    /// float noiseVal = WorleyNoise(seed, Vector2.Distance, (f1, f2) => f1);
    /// //Another nice effect is distance squared and using the average of both distance values.
    /// float noiseVal2 = WorleyNoise(seed,
    ///								  (v1, v2) => (v1 - v2).sqrMagnitude,
    ///								  (f1, f2) => (f1 + f2) * 0.5f);
    /// </example>
    public static float WorleyNoise(Vector2 seed, Func <Vector2, Vector2, float> distFunc,
                                    Func <float, float, float> distsToValue)
    {
        //Get the min corner of each of the 9 grid cells near the seed value.
        Vector2 posCenter    = new Vector2(Mathf.Floor(seed.x), Mathf.Floor(seed.y)),
                posMinCorner = posCenter - Vector2.one,
                posMaxCorner = posCenter + Vector2.one,
                pos4         = new Vector2(posCenter.x, posMinCorner.y),
                pos5         = new Vector2(posMaxCorner.x, posMinCorner.y),
                pos6         = new Vector2(posMinCorner.x, posCenter.y),
                pos7         = new Vector2(posMaxCorner.x, posCenter.y),
                pos8         = new Vector2(posMinCorner.x, posMaxCorner.y),
                pos9         = new Vector2(posCenter.x, posMaxCorner.y);

        //Get a random point inside each of these cells
        //    and get the distance from the seed pos to the two closest points.
        float min1 = float.PositiveInfinity,
              min2 = float.PositiveInfinity;

        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(posCenter) + posCenter, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(posMinCorner) + posMinCorner, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(posMaxCorner) + posMaxCorner, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos4) + pos4, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos5) + pos5, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos6) + pos6, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos7) + pos7, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos8) + pos8, seed));
        Utils.GetWorleyMins(ref min1, ref min2, distFunc(Utils.GetWorleyPos(pos9) + pos9, seed));

        //Filter these distance values into some noise value.
        return(distsToValue(min1, min2));
    }
Beispiel #2
0
    /// <summary>
    /// Computes Worley/Voroni noise for the given seed.
    /// </summary>
    /// <param name="distsToValue">
    /// Takes in the closest and second-closest distances and outputs a noise value from them.
    /// </param>
    public static float WorleyNoise(float seed, Func <float, float, float> distsToValue)
    {
        //Get the min corner of each of the 9 grid cells near the seed value.
        float posMid  = Mathf.Floor(seed),
              posLess = posMid - 1.0f,
              posMore = posMid + 1.0f;

        //Get a random point inside each of these cells
        //    and get the distance from the seed pos to the two closest points.
        float min1 = float.PositiveInfinity,
              min2 = float.PositiveInfinity;

        Utils.GetWorleyMins(ref min1, ref min2, Mathf.Abs(seed - (posMid + WhiteNoise(posMid))));
        Utils.GetWorleyMins(ref min1, ref min2, Mathf.Abs(seed - (posLess + WhiteNoise(posLess))));
        Utils.GetWorleyMins(ref min1, ref min2, Mathf.Abs(seed - (posMore + WhiteNoise(posMore))));

        //Filter these distance values into some noise value.
        return(distsToValue(min1, min2));
    }