Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. float randomRadians() {
  2. return random(2 * PI);
  3. }
  4.  
  5. // x = y axis rotation
  6. void rotateXY(float omega) {
  7. final float c = cos(omega);
  8. final float s = sin(omega);
  9. final float r2 = sqrt(2);
  10. applyMatrix(
  11. (1+c)/2, (1-c)/2, r2*s/2, 0,
  12. (1-c)/2, (1+c)/2, -r2*s/2, 0,
  13. -r2*s/2, r2*s/2, c, 0,
  14. 0, 0, 0, 1
  15. );
  16. }
  17.  
  18. // red and block core
  19. void centralBoxes() {
  20. stroke(#ff0000, 200);
  21. strokeWeight(3);
  22. fill(#000000, 150);
  23. pushMatrix();
  24. rotateXY(-atan(sqrt(2)));
  25. final int boxSize = 20;
  26. for(int i = 0; i < 8; ++i) {
  27. pushMatrix();
  28. translate(
  29. (i & 1) != 0 ? boxSize : -boxSize,
  30. (i & 2) != 0 ? boxSize : -boxSize,
  31. (i & 4) != 0 ? boxSize : -boxSize
  32. );
  33. box(boxSize * 1.5);
  34. popMatrix();
  35. }
  36. popMatrix();
  37. strokeWeight(1);
  38. }
  39.  
  40. // around the central core
  41. void orbitalBoards(float size, float radius, int n) {
  42. stroke(#ffffff, 255);
  43. fill(#444444);
  44. for(int i = 0; i < n; ++i) {
  45. final float theta = i * 2*PI / n;
  46. pushMatrix();
  47. rotateX(PI/2);
  48. translate(radius * cos(theta), 0, radius * sin(theta));
  49. rotateY(-theta + PI/2);
  50. rect(-size/2, -size/2, size, size);
  51. line(-size/6, -size/2, -size/6, 0);
  52. line(-size/6, 0, size/6, 0);
  53. line( size/6, size/2, size/6, 0);
  54. popMatrix();
  55. }
  56. }
  57.  
  58. // wire-framed triangles
  59. void asteroidBelt() {
  60. stroke(#ffffff, 127);
  61. noFill();
  62. final int n = 500;
  63. final float r = 210.f; // radius
  64. final float phi = 2 * PI / n;
  65. for(int i = 0; i < n; ++i) {
  66. final float theta = i * phi + random(phi);
  67. final float randomR = r + randomGaussian() * 16.f;
  68. final float x = randomR * cos(theta);
  69. final float y = randomR * sin(theta);
  70. pushMatrix();
  71. translate(x, y, 0);
  72. rotateX(randomRadians());
  73. rotateY(randomRadians());
  74. rotateZ(randomRadians());
  75. scale(1.5);
  76. // (0, 0)-(0, 3)-(4, 0) => G(4/3, 1)
  77. triangle(-4.f/3,-1, -4.f/3,2, 8.f/3,-1);
  78. popMatrix();
  79. }
  80. }
  81.  
  82. // boxels converge to the origin
  83. void logGrid() {
  84. final int xn = 100;
  85. final int yn = 100;
  86. final int size = 5;
  87. for(int xi = -xn; xi < xn; ++xi) {
  88. final float x = xi * size; // x : [-xn * size, xn * size)
  89. for(int yi = -yn; yi < yn; ++yi) {
  90. final float y = yi * size; // y : [-yn * size, yn * size)
  91. final float d = sqrt(x*x + y*y); // distance from the origin
  92. final float z = 30 * log(d + 1) + 20;
  93. final float a = constrain(4000 / (d + 1), 0, 255); // alpha
  94. if(d >= 500) {
  95. continue; // off-range
  96. }
  97. // random spot highlight
  98. if((int)random(200) == 0 && d < 300) {
  99. stroke(#ffffff, a/2 + 30);
  100. fill(#cccccc, a);
  101. }
  102. else {
  103. stroke(#aaaaaa, a/4);
  104. fill(#444444, a);
  105. }
  106. pushMatrix();
  107. translate(x - size/2.f, y - size/2.f, z);
  108. box(size);
  109. popMatrix();
  110. }
  111. }
  112. }
  113.  
  114. // red lightning from central core
  115. void thunderBolt() {
  116. noStroke();
  117. fill(#ff0000, 255);
  118. float x = 0.f;
  119. float y = 0.f;
  120. for(int i = 0; i < 100; ++i) {
  121. final float weight = i * 0.02f;
  122. final float widthBase = i * 0.3f;
  123. final float xr = random(i * 0.3f);
  124. final float yr = random(widthBase) * (i % 2 == 0 ? 1 : -1);
  125. triangle(x, y, x + xr, yr + weight, x + xr, yr - weight);
  126. x += xr;
  127. y = yr;
  128. }
  129. }
  130.  
  131. void setup() {
  132. size(800, 600, P3D);
  133. randomSeed(131);
  134. background(0);
  135.  
  136. pushMatrix();
  137. translate(width*3/5, height*4/9, height*2/5);
  138. rotateX(PI * 4 / 11);
  139. rotateY(PI / 11);
  140. rotateZ(PI / 11);
  141.  
  142. // center boxes
  143. centralBoxes();
  144.  
  145. // orbital boards
  146. pushMatrix();
  147. rotateZ(PI / 11 * 16);
  148. rotateX(PI / 13);
  149. orbitalBoards(20, 120, 12);
  150. popMatrix();
  151. pushMatrix();
  152. rotateZ(PI / 11 * 9);
  153. rotateX(PI / 13 * 2);
  154. orbitalBoards(10, 90, 16);
  155. popMatrix();
  156.  
  157. // asteroids
  158. asteroidBelt();
  159.  
  160. // grid
  161. logGrid();
  162. pushMatrix();
  163. rotateX(PI);
  164. logGrid();
  165. popMatrix();
  166.  
  167. // thunderbolt
  168. pushMatrix();
  169. rotateZ(PI/10 * 6);
  170. rotateY(PI/11);
  171. translate(50, 0, 0);
  172. thunderBolt();
  173. popMatrix();
  174. pushMatrix();
  175. rotateX(PI/2);
  176. rotateZ(PI/12 * 11);
  177. translate(70, 0, 0);
  178. thunderBolt();
  179. popMatrix();
  180. pushMatrix();
  181. rotateZ(-PI/7);
  182. rotateY(-PI/9);
  183. translate(70, 0, 0);
  184. thunderBolt();
  185. popMatrix();
  186. popMatrix();
  187.  
  188. //save("output.png");
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement