We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › generating random blobs
Page Index Toggle Pages: 1
generating random blobs (Read 1250 times)
generating random blobs
Apr 26th, 2009, 10:52pm
 
I've been trying to find code for generating random blobs. You specify a (vague) center, a radius for the blob, some density/noisiness-type parameters, and it generates a blob-y shape, kind of like splattering paint against a surface.

Any pointers will be appreciated. I hacked something using Perlin noise and a kludge-y scaling kernel, but have mixed feelings about the results. Happy to share if there's interest.

Thanks,
Bill

Re: generating random blobs
Reply #1 - Apr 27th, 2009, 2:09pm
 
Blobs? mmmmm i think toxi has a great implementation of metaballs which give a blobby kind of feel and also in the examples in processing there is an example of metaballs but none of them are as easy as you want them to be i even have a piece of code myself when i was experimenting with metaballs. Tell me if you want it
Re: generating random blobs
Reply #2 - Apr 27th, 2009, 3:00pm
 
I've looked at toxi's metaballs, but they're not quite the look I want (I'd like something a bit more splatter-like). I'd like to see your code as well, thanks.

I was going to post a link to my code, but the forum software won't let me do that until I've posted more messages, argh.

Bill

Re: generating random blobs
Reply #3 - Apr 28th, 2009, 6:48am
 
The splatter effect is mainly down to the number & size of balls used as well as which threshold you're using to define the blob boundary, but the algorithm is the same (although there're endless possibilities how to interpret/map the computed field values to colours). If using lots of particle like blobs you should also think about modelling their group dynamics, e.g. connect them with springs in a physical simulation. That way it'd become more fluid-like... just some thoughts! Smiley
Re: generating random blobs
Reply #4 - Apr 28th, 2009, 11:29pm
 
Thanks for the suggestions! I'm actually adapting Glen Murphy's particle systems code as well, to push around the splatter patterns. The splatter/blobs establish initial outlines, which I fill with particles.

I'll post a link soon to illustrate what I mean, once this forum thing decides I've posted enough and am allowed to do so...

Re: generating random blobs
Reply #5 - May 5th, 2009, 9:46am
 
Hey man,

If you could PM me a link to what you've done so far (if you still can't post a link!) that'd be awesome. I'm looking into trying to do some watercolour-esque paint splats, so you working on something along the same lines could be very handy for me indeed Smiley
Re: generating random blobs
Reply #6 - May 8th, 2009, 11:19pm
 
[Ok, let's see if I can fake out the forum software...]

The code is at the end of this post.

Press any key to generate some blobs. Press again to generate new blobs.

makeBlob() is the blob generator function. The input parameters are the approximate center of the blob, the diameter, a parameter to control how blob-like or noisy the result is, and a parameter to very roughly control the density. I've tried to make this fairly efficient; I haven't looked into using lots of metaballs instead, but I think this might be faster.

I'm using the blobs in a project where I do a lot of pixel hacking, so I write straight into the pixel array.

Two sets of blobs are displayed. The bottom shows the blobs without trimming the circular border; if that's what you need, great, but I think it makes the splatters look unnatural. I use a hack-y scaling function to trim the borders; that's the top.

Because it's just Perlin noise with thresholds, sometimes you get more than one blob. And the density control is very approximate. I suppose I could look into segmenting or parameterizing the blobs better, but I think this is workable for what I need.

Hope this is interesting to someone. Comments appreciated!

Bill

// Random Blob Generation
// by Bill Hsu

// Uses Perlin noise with threshold to generate blobs
// at specified center and radius
// Kludge-y scaling kernel to trim borders


int bgCol = 0;
int fgCol = color(255, 255, 255);

PGraphics pg;

void setup() {
 size(200, 400);
 pg = createGraphics(200, 400, P3D);
 pg.beginDraw();
 pg.background(bgCol);
 pg.noStroke();
 pg.endDraw();
 image(pg, 0, 0);
 pg.loadPixels();

}

void draw() {
}

void keyPressed() {
 
 pg.beginDraw();
 pg.background(bgCol);

 makeBlob(width/2, width/2, width/2, .05, .25);

 pg.endDraw();
 image(pg, 0, 0);

}

void makeBlob(int midX, int midY, // (x,y) coordinates of center
             int side, // length of a side, or diameter of blob
             float noiseMult, // position multiplier .02-.25
                              // very blobby to very noisy
             float percentDev) // percent deviation .005-.5
                               // very sparse to very dense
{

 float xoff = random(10);
 float yoff = random(10);
 
 float baseLevel = noise(midX*noiseMult + xoff, midY*noiseMult + yoff);

 float pixelScaleBoundary = 40.; // start scaling 40 pixels from circle edge

 for (int i=midX-side/2; i<midX+side/2; i++) {
   for (int j=midY-side/2; j<midY+side/2; j++) {
     float radius = sqrt((i - midX) * (i - midX) + (j - midY) * (j - midY));
     float diffRad = radius - side/2.;

     if (diffRad >= 0.)
       continue;
     float newNoise = noise(i*noiseMult + xoff,j*noiseMult + yoff);
     
     // "fade out" if too close to circular boundary
     float fudgeFactor = -diffRad / pixelScaleBoundary;
     if (fudgeFactor > 1.)
       fudgeFactor = 1.;

     float newRatio = newNoise / baseLevel;

     // bound base noise level if original is too low
     if (baseLevel < .25) {
       newRatio = newNoise * 4.;
     }

     // draw blob
     if (newRatio > 1 && newRatio < (1. + percentDev * fudgeFactor)) {
       pg.pixels[j*width + i] = fgCol;
     }
     else if (newRatio < 1. && newRatio > (1. - percentDev * fudgeFactor)) {
       pg.pixels[j*width + i] = fgCol;
     }
     
     // draw blob without boundary scaling for comparison
     if (newRatio > (1.-percentDev) && newRatio < (1. + percentDev)) {
       pg.pixels[(j+height/2)*width + i] = fgCol;
     }
   }
 }
}



Page Index Toggle Pages: 1