Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static float[,] GenerateBlobMap(int sizeX, int sizeY, float fillPercent, int seed, int blurSize, int blurIterations) {
- float[,] map = new float[sizeX,sizeY];
- float halfWidth = (sizeX - 1) / 2f;
- float halfHeight = (sizeY - 1) / 2f;
- System.Random prng = new System.Random (seed);
- float totalArea = sizeX * sizeY;
- float coveredArea = 0;
- int blurExtents = blurSize * blurIterations + 1;
- const int minRadius = 5;
- int maxBlobRadius = (int)Mathf.Clamp (Mathf.Min (halfWidth, halfHeight) - blurExtents, minRadius, float.MaxValue);
- fillPercent = Mathf.Clamp01 (fillPercent);
- const int maxIterations = 200;
- int iterationCount = 0;
- while (coveredArea / totalArea < fillPercent*.95f && iterationCount < maxIterations) {
- iterationCount++;
- int posX = prng.Next (blurExtents + minRadius, sizeX - (blurExtents + minRadius));
- int posY = prng.Next (blurExtents + minRadius, sizeY - (blurExtents + minRadius));
- int dstToClosestEdge = (int)Mathf.Min (halfWidth - Mathf.Abs (halfWidth - posX), halfHeight - Mathf.Abs (halfHeight - posY));
- int maxRadius = dstToClosestEdge - blurExtents;
- float targetFillArea = totalArea - coveredArea;
- float targetRadius = Mathf.Sqrt(targetFillArea / 3.14f);
- int blobRadius = Mathf.Min(Mathf.RoundToInt(targetRadius),maxRadius);
- for (int i = 0; i < blobRadius*2; i++) {
- for (int j = 0; j < blobRadius*2; j++) {
- float x =blobRadius - i;
- float y =blobRadius - j;
- int ix = posX - blobRadius + i;
- int iy = posY - blobRadius + j;
- if (Mathf.Pow (x * x + y * y, .5f) < blobRadius) {
- map [ix, iy] = 1;
- }
- }
- }
- coveredArea += blobRadius * blobRadius * 3.14f;
- }
- for (int i = 0; i < blurIterations; i++) {
- Blur (ref map, blurSize);
- }
- for (int i = 0; i < sizeX; i++) {
- for (int j = 0; j < sizeY; j++) {
- map [i, j] = 1 - map [i,j];
- }
- }
- return map;
- }
- static void Blur (ref float[,] image, int blurSize) {
- int width = image.GetLength (0);
- int height = image.GetLength (1);
- int kernelSize = blurSize * 2 + 1;
- int kernelExtents = blurSize;
- float[,] horizontalBlurPass = new float[width,height];
- float[,] verticalBlurPass = new float[width,height];
- for (int y = 0; y < height; y++) {
- for (int x = -kernelExtents; x <= kernelExtents; x++) {
- int sampleX = Mathf.Clamp (x, 0, kernelExtents);
- horizontalBlurPass [0, y] += image [sampleX, y];
- }
- for (int x = 1; x < width; x++) {
- int removeIndex = Mathf.Clamp(x - kernelExtents - 1, 0, width);
- int addIndex = Mathf.Clamp(x + kernelExtents, 0, width-1);
- horizontalBlurPass [x, y] = horizontalBlurPass [x - 1, y] - image [removeIndex, y] + image [addIndex, y];
- }
- }
- for (int x = 0; x < width; x++) {
- for (int y = -kernelExtents; y <= kernelExtents; y++) {
- int sampleY = Mathf.Clamp (y, 0, kernelExtents);
- verticalBlurPass [x, 0] += horizontalBlurPass [x, sampleY];
- }
- float blurredValue = (verticalBlurPass [x, 0] / (kernelSize * kernelSize));
- image [x, 0] = blurredValue;
- for (int y = 1; y < height; y++) {
- int removeIndex = Mathf.Clamp(y - kernelExtents - 1, 0, height);
- int addIndex = Mathf.Clamp(y + kernelExtents, 0, height-1);
- verticalBlurPass [x, y] = verticalBlurPass [x, y-1] - horizontalBlurPass [x,removeIndex] + horizontalBlurPass [x, addIndex];
- blurredValue = ((float)verticalBlurPass [x, y] / (kernelSize * kernelSize));
- image [x, y] = blurredValue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement