Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import com.thomasdiewald.pixelflow.java.DwPixelFlow;
  2. import com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwFilter;
  3.  
  4. import toxi.geom.*;
  5. import toxi.physics2d.*;
  6. import toxi.physics2d.behaviors.*;
  7. import toxi.util.*;
  8.  
  9. VerletPhysics2D physics;
  10. Vec2D mouse;
  11. AttractionBehavior2D mouseAttractor;
  12.  
  13. PGraphics2D pg_render;
  14. PGraphics2D pg_luminance;
  15. PGraphics2D pg_bloom;
  16.  
  17. DwPixelFlow context;
  18. DwFilter filter;
  19.  
  20. void setup() {
  21.   size(1000, 400, P2D);
  22.   frameRate(1000);
  23.   colorMode(HSB, 100);
  24.   //fullScreen();
  25.   physics = new VerletPhysics2D();
  26.  
  27.   int divPoints = 50;
  28.  
  29.   for (int i = 0; i < 30; i++) {
  30.     VerletParticle2D a = new VerletParticle2D(15, random(50, height - 50));
  31.     VerletParticle2D b = new VerletParticle2D(width - 15, random(50, height - 50));
  32.     a.lock();
  33.     b.lock();
  34.     ArrayList<VerletParticle2D> particles = new ArrayList<VerletParticle2D>();
  35.     particles.add(a);
  36.     for (int j = 1; j < divPoints; j++) {
  37.       Vec2D diff = b.sub(a);
  38.       float distance = diff.magnitude() / divPoints * j;
  39.       diff.normalizeTo(distance);
  40.       diff.addSelf(a);
  41.       VerletParticle2D vp = new VerletParticle2D(diff);
  42.       particles.add(vp);
  43.     }
  44.     particles.add(b);
  45.  
  46.     ParticleString2D string = new ParticleString2D(physics, particles, 1);
  47.   }
  48.  
  49.   for (VerletParticle2D vp1 : physics.particles) {
  50.     for (VerletParticle2D vp2 : physics.particles) {
  51.       float distance = vp1.distanceTo(vp2);
  52.       if (distance > 0 && distance < 10) {
  53.         VerletSpring2D spring = new VerletSpring2D(vp1, vp2, 2, 0.1);
  54.         physics.addSpring(spring);
  55.       }
  56.     }
  57.   }
  58.  
  59.   pg_render = (PGraphics2D) createGraphics(width, height, P2D);
  60.   pg_render.smooth(8);
  61.  
  62.  
  63.   pg_luminance = (PGraphics2D) createGraphics(width, height, P2D);
  64.   pg_luminance.smooth(8);
  65.  
  66.   pg_bloom = (PGraphics2D) createGraphics(width, height, P2D);
  67.   pg_bloom.smooth(0);
  68.  
  69.   context = new DwPixelFlow(this);
  70.   filter = new DwFilter(context);
  71. }
  72.  
  73. void draw() {
  74.   background(50);
  75.   pg_render.beginDraw();
  76.  
  77.   pg_render.colorMode(HSB, 100);
  78.   pg_render.background(0);
  79.  
  80.   pg_render.stroke(100);
  81.   for (VerletSpring2D spring : physics.springs) {
  82.     pg_render.line(spring.a.x, spring.a.y, spring.b.x, spring.b.y);
  83.   }
  84.  
  85.   pg_render.noStroke();
  86.   //pg_render.fill(255);
  87.   for (VerletParticle2D p : physics.particles) {
  88.     pg_render.fill(map(p.x, 0, width, 0, 100), 100, 100);
  89.     pg_render.ellipse(p.x, p.y, 5, 5);
  90.   }
  91.   pg_render.endDraw();
  92.  
  93.   physics.update();
  94.  
  95.   filter.luminance_threshold.param.threshold = 0f; // when 0, all colors are used
  96.   filter.luminance_threshold.param.exponent  = 100;
  97.   filter.luminance_threshold.apply(pg_render, pg_luminance);
  98.   filter.bloom.param.mult   = 2.7;
  99.   filter.bloom.param.radius = 0.04;
  100.   filter.bloom.apply(pg_luminance, pg_bloom, pg_render);
  101.   filter.copy.apply(pg_bloom, pg_render);
  102.   image(pg_render, 0, 0);
  103.  
  104.   surface.setTitle(DateUtils.timeStampGMT() + "     " + frameRate + " fps");
  105. }
  106.  
  107. void mousePressed() {
  108.   mouse = new Vec2D(mouseX, mouseY);
  109.   mouseAttractor = new AttractionBehavior2D(mouse, 250, 0.9);
  110.   physics.addBehavior(mouseAttractor);
  111. }
  112.  
  113. void mouseReleased() {
  114.  physics.removeBehavior(mouseAttractor);
  115. }
  116.  
  117. void mouseDragged() {
  118.  mouse.set(mouseX, mouseY);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement