Click to randomize flower. Press “P” to pause. Moving the mouse left and right will scrub the animation. Press “B” to toggle background between black and white. Press “A” to toggle auto-play. Auto-play will randomize the flower occasionally.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
Flower flower; float bgColor = 0; boolean isPaused = false; int time = 0; boolean autoplay = false; void setup() { size(940, 540, P3D); colorMode(HSB); init(); } void draw() { if (autoplay) { time++; if (time > 1000) { init(); time = 0; } } background(bgColor); pushMatrix(); translate(width/2, height/2); flower.draw(); popMatrix(); } void init() { time = 0; flower = new Flower(); } void mousePressed() { init(); } void keyPressed() { if (key == 'b') { if (bgColor == 0) bgColor = 255; else if (bgColor == 255) bgColor = 0; } if (key == 'p') { isPaused = !isPaused; } if (key == 'a') autoplay = !autoplay; } class Flower { PetalCluster[] clusters; int numClusters = 5; Flower() { numClusters = round(random(2, 5)); createClusters(); } void draw() { for (int i=0; i<numClusters; i++) { clusters[i].draw(); } } void createClusters() { clusters = new PetalCluster[numClusters]; int numPetals = round(random(2, 30)); for (int i=0; i<numClusters; i++) { if (numPetals % 2 == 0 && numPetals > 2 && i > 0) { if (random(1) > 0.5) { numPetals *= 0.5; } } else if (numPetals % 3 == 0 && numPetals > 3 && i > 0) { if (random(1) > 0.5) { numPetals /= 3; } } PetalCluster cluster = new PetalCluster(numPetals); clusters[i] = cluster; } } } class Petal { PVector[] allVectors; int numNodes = 3; float hue, sat, bright; float nearRot = -360; float farRot = 360; float rotation = 0; float rotSpeed = 1; float size = 0.0; float targetSize = 1.0; float time = 0; float mouseRot = 0; float shadowRatio = 0.5; Petal() { hue = random(0, 255); sat = random(0, 255); bright = random(150, 255); numNodes = round(random(1, 10)); nearRot = random(-360, 360); farRot = random(-360, 360); rotSpeed = random(0.1, 1.0); shadowRatio = random(0.3, 0.7); createVectors(); mouseRot = map(mouseX, 0, width, nearRot*3, farRot*3); } void update() { time++; size += (targetSize - size) * 0.01; if (!isPaused) { rotation += rotSpeed; } mouseRot += (map(mouseX, 0, width, nearRot*3, farRot*3) - mouseRot) * 0.1; } void draw() { pushMatrix(); scale(size); float rotX = map(size, 0, 1, 90, 0); rotateX(radians(rotX)); rotateY(radians(rotation + mouseRot)); fill(hue, sat, bright); stroke(hue, sat, bright * 0.7); strokeWeight(1); beginShape(TRIANGLE); curveVertex(0, 0, 0); curveVertex(0, 0, 0); fill(hue, sat, bright * shadowRatio); for (int i=0; i<numNodes; i++) { curveVertex(allVectors[i].x, allVectors[i].y, allVectors[i].z); } fill(hue, sat, bright); curveVertex(0, 0, 0); curveVertex(0, 0, 0); endShape(); popMatrix(); } void createVectors() { allVectors = new PVector[numNodes]; for (int i=0; i<numNodes; i++) { PVector vector = new PVector(random(-200, 200), random(300), random(-200, 200)); allVectors[i] = vector; } } } class PetalCluster { Petal petal; int numPetals = 10; float rotationOffset = 0.01; float time = 0.0; PetalCluster(int _numPetals) { numPetals = _numPetals; petal = new Petal(); rotationOffset = random(-0.1, 0.1); time = random(1000); } void draw() { if (!isPaused) { time++; time += map(mouseX, 0, width, -rotationOffset*10, rotationOffset*10); } petal.update(); for (int i=0; i<numPetals; i++) { float step = 360.0f/numPetals; float rot = step * i; drawPetal(rot); } } void drawPetal(float rot) { pushMatrix(); rotateZ(radians(rot + time*rotationOffset)); petal.draw(); popMatrix(); } } |














