Noise-based 2D Planet optimization
in
Programming Questions
•
10 months ago
This runs very smoothly on its own, but it slaughters the framerates of other elements, like a simple player-controlled spaceship. Is there anything glaringly obvious to be done to optimize it? I'm hesitant to write it to a PImage since the "clouds" are not static, unlike the planet's surface, and even only drawing the clouds severely slows down anything else on the screen. Thanks in advance.
- float x,y;
- int seed;
- float mass,sealevel,skylevel,inita,rotrate,offset,skyoffset,wind;
- color sky,sand,sea;
- void setup(){
- size(512,512);
- smooth();
- noStroke();
- x = width/2;
- y = height/2;
- seed = floor(random(2147483647));
- println("seed: " + seed);
- mass = random(176) + 16;
- println("mass: " + mass);
- sealevel = random(1); //based on temp, etc;
- println("sea: " + sealevel);
- skylevel = random(1 - sealevel) + sealevel;
- println("sky: " + skylevel);
- inita = random(TWO_PI);
- //colors shouldn't really be random
- sky = color(random(255),random(255),random(255));//sky
- sand = color(random(255),random(255),random(255));//sand
- sea = color(random(255),random(255),random(255));//sea
- rotrate = random(1) / mass;
- offset = 0;
- skyoffset = random(mass);
- wind = random(1);
- }
- void draw(){
- background(0);
- noiseSeed(seed);
- float a = inita;
- float s = mass / 64;
- pushMatrix();
- translate(x,y);
- rotate(offset);
- fill(sky);
- rect(-(s/2),-(s/2),s,s);
- for (float r = mass/2; r >= 0;){
- float px = cos(a) * r;
- float py = sin(a) * r;
- float v = noise(px * 10/mass + a, py * 10/mass + a);
- if (v < sealevel) fill(sea);
- else fill(sand);
- rect(px-(s),py-(s),s*2,s*2);
- v = noise(px * 10/mass + a + skyoffset, py * 10/mass + a);
- fill(sky,(1-v)*255);
- rect(px-(s),py-(s),s*2,s*2);
- a += QUARTER_PI / r;
- if (a > inita + TWO_PI){
- a = 0;
- r -= s;
- }
- }
- offset += rotrate;
- skyoffset += wind;
- popMatrix();
- }
1