Heightmap test code optimisation
in
Programming Questions
•
3 years ago
Here is some awesome code from the openprocessing.org website by
Simon Bethke .
The sketch creates greyscale images that gives some really nice depth effects. Imagine yourself under the surf of a recently breaking wave and the water and surf is disturbed... the effects look that cool. Processing rocks!!!
However, it is very cpu intensive. Can anyone suggest how this code can be optimized so it can run near-real-time ?
Here is a screenshot
Here is the code
The sketch creates greyscale images that gives some really nice depth effects. Imagine yourself under the surf of a recently breaking wave and the water and surf is disturbed... the effects look that cool. Processing rocks!!!
However, it is very cpu intensive. Can anyone suggest how this code can be optimized so it can run near-real-time ?
Here is a screenshot
Here is the code
floatstartScale=0.005;floatoctaveScaleFactor=0.2;intoctaves = 5;float[] octaveScaleFactors =newfloat[octaves];floatscaleFactorPerOctave = 1.6;floaterosionStrength = 0.1;voidsetup(){size(400, 400);render();}voiddraw(){}voidkeyTyped() {if(int(key) == 49) startScale -= 0.001;if(int(key) == 50) startScale += 0.001;if(int(key) == 51) octaveScaleFactor -= 0.05;if(int(key) == 52) octaveScaleFactor += 0.05;if(int(key) == 53) octaves--;if(int(key) == 54) octaves++;if(int(key) == 55) scaleFactorPerOctave -= 0.02;if(int(key) == 56) scaleFactorPerOctave += 0.02;if(int(key) == 57) erosionStrength -= 0.02;if(int(key) == 48) erosionStrength += 0.02;if(int(key) >= 48 &&int(key) <= 57){print("Render... ");octaveScaleFactors =newfloat[octaves];render();print(" done\n");}println(int(key));}voidrenderHUD(){PFontmyFont =createFont("Arial", 11);textFont(myFont);fill(255, 255, 0);text("(1/2) Scale of Features: "+ startScale, 10, 20);text("(3/4) Verical Scale: "+ octaveScaleFactor, 10, 30);text("(5/6) Octaves: "+ octaves, 10, 40);text("(7/8) Octave Scales: "+ scaleFactorPerOctave, 10, 50);text("(9/0) Erosion: "+ erosionStrength, 10, 60);}voidrender(){background(0);floatxf, yf, altitude;for(floatx=0; x <width; x++) {for(floaty=0; y <height; y++) {xf = x;yf = y;altitude = myNoise(xf, yf, 0);for(intoc = 1; oc < octaves; oc++){float[] off = getSlope(xf, yf, oc);xf += off[0] / scaleFactor(oc);yf += off[1] / scaleFactor(oc);floatval = myNoise(xf, yf, oc);altitude += (val - 0.5) * octaveScaleFactor;}stroke(altitude*255);point(x, y);}}renderHUD();}float[] getSlope(floatx,floaty,intoc){floatf = erosionStrength / scaleFactor(oc);float[] result =newfloat[2];result[0] = myNoise(x, y, oc) - myNoise(x + f, y, oc);result[1] = myNoise(x, y, oc) - myNoise(x, y + f, oc);returnresult;}floatscaleFactor(intoctave){if(octaveScaleFactors[octave] == 0){octaveScaleFactors[octave] = startScale *pow(scaleFactorPerOctave, octave);}returnoctaveScaleFactors[octave];}floatmyNoise(floatx,floaty,intoctave){returnnoise(x * scaleFactor(octave) + octave *width, y * scaleFactor(octave));}
1
