Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Particle;
- import processing.core.PApplet;
- import processing.core.PVector;
- import processing.opengl.PShader;
- import java.util.ArrayList;
- public class MainApp extends PApplet {
- public static void main(String[] args) {
- PApplet.main("MainApp");
- }
- public void settings() {
- fullScreen(P3D);
- smooth(4);
- }
- private ArrayList<Particle> particles = new ArrayList<Particle>();
- private float t = 0;
- private float rot = 0;
- private float rotSpd = 0;
- private int backgroundColorA;
- private int backgroundColorB;
- private PVector center;
- float screen;
- boolean onPC = true;
- PShader phong;
- public void setup() {
- if(onPC){
- phong = loadShader("PhongFrag.glsl", "PhongVert.glsl");
- }
- colorMode(RGB, 255, 255, 255, 100);
- orientation(PORTRAIT);
- ellipseMode(CENTER);
- rectMode(CENTER);
- fullScreen(P3D);
- backgroundColorA = color(0, 0, 20);
- backgroundColorB = color(20, 0, 0);
- screen = min(width, height);
- particles.addAll(generateGalaxy(300, screen * .3f, screen * .45f));
- center = new PVector(width * .5f, height * .45f);
- }
- private void loadPhong() {
- }
- public void draw() {
- hint(DISABLE_DEPTH_TEST);
- backgroundGradient();
- hint(ENABLE_DEPTH_TEST);
- t = radians(frameCount);
- translate(center.x, center.y, screen / 5.4f);
- rotateX(HALF_PI * .8f); //flip top of plane towards middle
- float f = (rot - t / 128f); // main change per frame
- rotateY(-PI / 32f + f / 100f); //skew plane to the left a little
- rotateZ(f); //rotate around plane center
- ambientLight(5, 5, 5);
- lightSpecular(200, 200, 255);
- pointLight(255, 255, 255, 0, 0, 0);
- shininess(5);
- strokeWeight(1);
- for (Particle particle : particles) {
- particle.draw();
- }
- noLights();
- if(onPC){
- resetShader();
- }
- fill(255);
- box(20);
- // coords();
- rot += rotSpd;
- rotSpd *= .98;
- }
- private void backgroundGradient() {
- noStroke();
- beginShape();
- fill(backgroundColorA);
- vertex(0, 0);
- vertex(width, 0);
- fill(backgroundColorB);
- vertex(width, height);
- vertex(0, height);
- endShape(CLOSE);
- }
- public void mouseDragged() {
- rotSpd += radians(pmouseX - mouseX) / 60f;
- }
- private ArrayList<Particle> generateGalaxy(float count, float startR, float endR) {
- float minSize = screen * .03f;
- float maxSize = screen * .08f;
- ArrayList<Particle> galaxy = new ArrayList<Particle>();
- for (int i = 0; i < count; i++) {
- float r = map(i, 0, count, startR, endR);
- float a = radians(i * 1.61803398f * 80);
- float x = r * cos(a);
- float y = r * sin(a);
- float dN = map(abs(count / 2 - i), 0, count / 2, 0, 1);
- float size = minSize + random(dN * (maxSize - minSize));
- float zMax = (1 - dN) * r * .25f;
- float z = random(-zMax, zMax);
- galaxy.add(new Particle(x, y, z, size));
- }
- println("generated " + galaxy.size() + " stars");
- return galaxy;
- }
- class Particle {
- PVector pos;
- PVector rotPos;
- PVector rotSpd;
- float r;
- float boxW, boxH, boxD;
- int sphereDetail;
- boolean fill;
- boolean box;
- int foregroundColor;
- float rotationMaxSpeed = radians(.1f);
- Particle(float x, float y, float z, float size) {
- pos = new PVector(x, y, z);
- rotPos = new PVector(random(PI), random(PI), random(PI));
- rotSpd = new PVector(random(rotationMaxSpeed), random(rotationMaxSpeed), random(rotationMaxSpeed));
- this.r = size;
- fill = true;
- box = true; //random(1) > .5f;
- boxW = random(r);
- boxH = random(r);
- boxD = random(r);
- foregroundColor = color(random(255), 0, random(255));
- sphereDetail = floor(random(2, 16));
- }
- void draw() {
- if (r <= 0) {
- return;
- }
- pushMatrix();
- translate(pos.x, pos.y, pos.z);
- rotPos.add(rotSpd);
- rotateY(rotPos.y);
- rotateX(rotPos.x);
- rotateZ(rotPos.z);
- if (fill) {
- fill(0);
- ambient(foregroundColor);
- specular(foregroundColor);
- noStroke();
- } else {
- noFill();
- stroke(255);
- }
- if(onPC){
- shader(phong);
- }
- if (box) {
- box(boxW, boxH, boxD);
- } else {
- sphereDetail(sphereDetail);
- sphere(boxW * .8f);
- }
- popMatrix();
- }
- }
- private float mx() {
- float val = map(mouseX, 0, screen, 0, 1);
- println("mx: " + val);
- return val;
- }
- private float my() {
- float val = map(mouseY, 0, height, 0, 1);
- println("my: " + val);
- return val;
- }
- private void coords() {
- strokeWeight(3);
- float mag = 1000;
- stroke(255, 0, 0);
- line(-mag, 0, 0, mag, 0, 0);
- stroke(0, 255, 0);
- line(0, -mag, 0, 0, mag, 0);
- stroke(0, 0, 255);
- line(0, 0, -mag, 0, 0, mag);
- }
- }
Add Comment
Please, Sign In to add comment