lollypop
YaBB Newbies
Offline
Posts: 15
Re: weather, topographic, gradient map
Reply #5 - Jan 22nd , 2009, 7:02pm
here is some code I wrote a while back to explore the gradient field of the Perlin noise field. in essence does the same thing that I outlined in my posts, but here i use the central difference technique rather than forward differences (no reason) and I evaluate the noise function, rather than read values from a grid. this is my first attempt at posting code so I hope this comes out. // Sketch to explore Perlin noise. // Mouse X coordinate sets z coordinate in noise lookups. // Mode 1: show Perlin noise value // Mode 2: show gradient of noise // Mode can be set via 1 & 2 keys. int mode = 1; void setup() { size(160, 120); noiseDetail(4); } void draw() { float noiseScale = 0.015f; for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { float x = i * noiseScale; float y = j * noiseScale; float z = mouseX * noiseScale; if (mode == 1) { stroke(noise(x, y, z)*255); } else { // use central difference to compute first derivative float dx = noise(x + noiseScale/2, y, z) - noise(x - noiseScale/2, y, z); float dy = noise(x, y + noiseScale/2, z) - noise(x, y - noiseScale/2, z); // in theory the range of dx & dy is [-1,1], but because noise // is smoothly varying the range is much smaller so we need to scale values // up. float r = constrain((dx*100 + 1) * 0.5, 0, 1); float g = constrain((dy*100 + 1) * 0.5, 0, 1); stroke(r * 255, g * 255, 0); } point(i, j); } } if (keyPressed) { if (key == '1') mode = 1; else if (key == '2') mode = 2; } }