Guest User

Untitled

a guest
Nov 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle;
  2. import processing.core.PApplet;
  3. import processing.core.PVector;
  4. import processing.opengl.PShader;
  5.  
  6. import java.util.ArrayList;
  7.  
  8. public class MainApp extends PApplet {
  9.  
  10. public static void main(String[] args) {
  11. PApplet.main("MainApp");
  12. }
  13.  
  14.  
  15. public void settings() {
  16. fullScreen(P3D);
  17. smooth(4);
  18. }
  19.  
  20. private ArrayList<Particle> particles = new ArrayList<Particle>();
  21. private float t = 0;
  22. private float rot = 0;
  23. private float rotSpd = 0;
  24. private int backgroundColorA;
  25. private int backgroundColorB;
  26. private PVector center;
  27. float screen;
  28.  
  29. boolean onPC = true;
  30. PShader phong;
  31.  
  32. public void setup() {
  33. if(onPC){
  34. phong = loadShader("PhongFrag.glsl", "PhongVert.glsl");
  35. }
  36. colorMode(RGB, 255, 255, 255, 100);
  37. orientation(PORTRAIT);
  38. ellipseMode(CENTER);
  39. rectMode(CENTER);
  40. fullScreen(P3D);
  41. backgroundColorA = color(0, 0, 20);
  42. backgroundColorB = color(20, 0, 0);
  43. screen = min(width, height);
  44. particles.addAll(generateGalaxy(300, screen * .3f, screen * .45f));
  45. center = new PVector(width * .5f, height * .45f);
  46. }
  47.  
  48. private void loadPhong() {
  49.  
  50. }
  51.  
  52. public void draw() {
  53. hint(DISABLE_DEPTH_TEST);
  54. backgroundGradient();
  55. hint(ENABLE_DEPTH_TEST);
  56.  
  57. t = radians(frameCount);
  58. translate(center.x, center.y, screen / 5.4f);
  59. rotateX(HALF_PI * .8f); //flip top of plane towards middle
  60. float f = (rot - t / 128f); // main change per frame
  61. rotateY(-PI / 32f + f / 100f); //skew plane to the left a little
  62. rotateZ(f); //rotate around plane center
  63.  
  64. ambientLight(5, 5, 5);
  65. lightSpecular(200, 200, 255);
  66. pointLight(255, 255, 255, 0, 0, 0);
  67. shininess(5);
  68.  
  69. strokeWeight(1);
  70. for (Particle particle : particles) {
  71. particle.draw();
  72. }
  73.  
  74. noLights();
  75. if(onPC){
  76. resetShader();
  77. }
  78. fill(255);
  79. box(20);
  80.  
  81. // coords();
  82. rot += rotSpd;
  83. rotSpd *= .98;
  84. }
  85.  
  86. private void backgroundGradient() {
  87. noStroke();
  88. beginShape();
  89. fill(backgroundColorA);
  90. vertex(0, 0);
  91. vertex(width, 0);
  92. fill(backgroundColorB);
  93. vertex(width, height);
  94. vertex(0, height);
  95. endShape(CLOSE);
  96. }
  97.  
  98. public void mouseDragged() {
  99. rotSpd += radians(pmouseX - mouseX) / 60f;
  100. }
  101.  
  102. private ArrayList<Particle> generateGalaxy(float count, float startR, float endR) {
  103. float minSize = screen * .03f;
  104. float maxSize = screen * .08f;
  105. ArrayList<Particle> galaxy = new ArrayList<Particle>();
  106. for (int i = 0; i < count; i++) {
  107. float r = map(i, 0, count, startR, endR);
  108. float a = radians(i * 1.61803398f * 80);
  109. float x = r * cos(a);
  110. float y = r * sin(a);
  111. float dN = map(abs(count / 2 - i), 0, count / 2, 0, 1);
  112. float size = minSize + random(dN * (maxSize - minSize));
  113. float zMax = (1 - dN) * r * .25f;
  114. float z = random(-zMax, zMax);
  115. galaxy.add(new Particle(x, y, z, size));
  116. }
  117. println("generated " + galaxy.size() + " stars");
  118. return galaxy;
  119. }
  120.  
  121. class Particle {
  122. PVector pos;
  123. PVector rotPos;
  124. PVector rotSpd;
  125. float r;
  126. float boxW, boxH, boxD;
  127. int sphereDetail;
  128. boolean fill;
  129. boolean box;
  130. int foregroundColor;
  131. float rotationMaxSpeed = radians(.1f);
  132.  
  133. Particle(float x, float y, float z, float size) {
  134. pos = new PVector(x, y, z);
  135. rotPos = new PVector(random(PI), random(PI), random(PI));
  136. rotSpd = new PVector(random(rotationMaxSpeed), random(rotationMaxSpeed), random(rotationMaxSpeed));
  137. this.r = size;
  138. fill = true;
  139. box = true; //random(1) > .5f;
  140. boxW = random(r);
  141. boxH = random(r);
  142. boxD = random(r);
  143. foregroundColor = color(random(255), 0, random(255));
  144. sphereDetail = floor(random(2, 16));
  145. }
  146.  
  147. void draw() {
  148. if (r <= 0) {
  149. return;
  150. }
  151. pushMatrix();
  152. translate(pos.x, pos.y, pos.z);
  153. rotPos.add(rotSpd);
  154. rotateY(rotPos.y);
  155. rotateX(rotPos.x);
  156. rotateZ(rotPos.z);
  157. if (fill) {
  158. fill(0);
  159. ambient(foregroundColor);
  160. specular(foregroundColor);
  161. noStroke();
  162. } else {
  163. noFill();
  164. stroke(255);
  165. }
  166.  
  167. if(onPC){
  168. shader(phong);
  169. }
  170.  
  171. if (box) {
  172. box(boxW, boxH, boxD);
  173. } else {
  174. sphereDetail(sphereDetail);
  175. sphere(boxW * .8f);
  176. }
  177. popMatrix();
  178. }
  179. }
  180.  
  181.  
  182. private float mx() {
  183. float val = map(mouseX, 0, screen, 0, 1);
  184. println("mx: " + val);
  185. return val;
  186. }
  187.  
  188. private float my() {
  189. float val = map(mouseY, 0, height, 0, 1);
  190. println("my: " + val);
  191. return val;
  192. }
  193.  
  194. private void coords() {
  195. strokeWeight(3);
  196. float mag = 1000;
  197. stroke(255, 0, 0);
  198. line(-mag, 0, 0, mag, 0, 0);
  199. stroke(0, 255, 0);
  200. line(0, -mag, 0, 0, mag, 0);
  201. stroke(0, 0, 255);
  202. line(0, 0, -mag, 0, 0, mag);
  203. }
  204. }
Add Comment
Please, Sign In to add comment