|
Author |
Topic: More controlled Perlin noise? (Read 1649 times) |
|
rgovostes
|
More controlled Perlin noise?
« on: Oct 6th, 2003, 11:37pm » |
|
I have a sketch which generates a nifty multi-colored blob. It uses the noise() function to make the blob randomized. However, I would like to animate the blob, and so I need to be able to generate a new Perlin noise map. I submitted a request for a noise map regenerator, but I realized that this would not fit my needs exactly. If I just had it change the noise map each iteration, it would just draw another random blob. Therefore, I need to generate a base map, then each iteration it must be adjusted slightly. I am not sure how to go about doing this. I think it would be easiest to start with a new, custom Perlin noise object. It would need a method to generate the map, a function to get a specific value from it, then another method the adjust the entire map. Again, I'm not sure where to start, so any suggestions would be welcome.
|
|
|
|
toxi
|
Re: More controlled Perlin noise?
« Reply #2 on: Oct 7th, 2003, 5:14pm » |
|
any chance we can have a look at the blob so far are you already using all 3 dimensions of the noise space if so, then you'll need 4D perlin noise, which we haven't implemented. if not, you can easily animate the blob by constantly changing the z coordinate parameter for noise(). a regeneration of the map is hardly necessary. random base/offset coordinates and scale factors for each iteration should work perfectly fine to create infinite variations. also have a look at the source of the cloud examples here to see what i mean.
|
http://toxi.co.uk/
|
|
|
rgovostes
|
Re: More controlled Perlin noise?
« Reply #3 on: Oct 7th, 2003, 9:02pm » |
|
Code:// Alien Blob v1.1 (Oct 07 '03) // based on formula by Jim Bumgardner // implementation by Ryan Govostes float z = 0; void setup() { size(256, 256); colorMode(HSB, 2); noiseDetail(1); fill(0, 0, 0); rect(0, 0, width, height); } void loop() { for (float x = 0; x < width; x ++) { for (float y = 0; y < height; y ++) { pixels[int(y) * width + int(x)] = getColor(x, y); } } z += 0.05; } color getColor(float x, float y) { float d, h, s, b; d = dist(x, y, width/2, height/2); h = sin(d * .05 + noise(x/16.0, y/16.0, z) * 3); s = sin(d * .05 + noise(x/16.0, y/16.0, z) * 2) * .5; b = sin(d * .05 + noise(x/16.0, y/16.0, z)); return color(h + 1, s + 1, b + 1.5); } |
| Thanks for the suggestion with the z parameter, I'll try that out and update the code above if it works. UPDATE: Yeah, worked like a charm. Unfortunately it takes too long to draw on my computer, so the animation is fairly skippy. I'll try to see what it looks like on a faster machine. I have uploaded it: http://rgovostes.macstorm.org/p5/alienblob/index.html
|
« Last Edit: Oct 7th, 2003, 9:10pm by rgovostes » |
|
|
|
|
toxi
|
Re: More controlled Perlin noise?
« Reply #4 on: Oct 8th, 2003, 2:15pm » |
|
i took the liberty and optimized your code a bit more, so on my machine it runs now quite fluid: Code:float xoff=0, yoff=0, zoff = 0; float[] sineTable; void setup() { size(256, 256); colorMode(HSB, 2); noiseDetail(1); background(0); // precalculate 1 period of the sine wave (360 degrees) sineTable=new float[360]; for(int i=0; i<360; i++) sineTable[i]=sin(radians(i)); } void loop() { float d, h, s, b, n; float xx; float yy=yoff; int w2=width/2; int h2=height/2; int offset=0; for (int y = 0; y < height; y ++) { xx=xoff; for (int x = 0; x < width; x ++) { d = dist(x, y, w2, h2)*0.05; // noise only needs to be computed once per pixel n = noise(xx, yy, zoff); // use pre-calced sine results h = sineTable[(int)degrees(d + n * 3) % 360]; s = sineTable[(int)degrees(d + n * 2) % 360] * .5; b = sineTable[(int)degrees(d + n) % 360]; // update pixel and increment offset pixels[offset++] = color(h + 1, s + 1, b + 1.5); xx+=0.0625; // = x/16.0 } // this value only needs to be changed once per pixel line yy+=0.0625; } // move through noise space -> animation xoff += 0.3; yoff += 0.07; zoff += 0.1; } |
| your problem was that you do a lot of redundant computing per pixel: 3x sin() and calling 3 times the noise() function with identical parameters. putting this code in a function means another overhead. also try to avoid using divisions wherever possible, use multiplication (e.g. 23/10.0 = 23*0.1 ) or even better use addition instead (see code above). by eliminating those bottle necks you can achieve almost fluid animation. the main slowdown left right now is your use of the HSB colour space instead of straight RGB as this involves additional conversions, or in other words, precious CPU cycles.
|
« Last Edit: Oct 8th, 2003, 4:32pm by toxi » |
|
http://toxi.co.uk/
|
|
|
rgovostes
|
Re: More controlled Perlin noise?
« Reply #5 on: Oct 8th, 2003, 9:41pm » |
|
Excellent work, toxi! I knew that the code could be optimized, but I didn't think that it would be that much faster. I have applied your changes and appended your name to the header, and I will upload the new version in a few minutes. Thanks!
|
« Last Edit: Oct 8th, 2003, 9:44pm by rgovostes » |
|
|
|
|
|