Advertisement
itblanco

hemesh_Spiel

Feb 25th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. import processing.core.*;
  2. import wblut.hemesh.*;
  3. import wblut.geom.*;
  4. import wblut.processing.*;
  5. import peasy.*;
  6. import controlP5.*;
  7. import java.util.*;
  8.  
  9. PeasyCam cam;
  10.  
  11. WB_Render render;
  12. HE_Mesh mesh;
  13. HE_Selection selection;
  14.  
  15. ControlP5 cp5;
  16.  
  17. boolean facesOn = true;
  18. boolean edgesOn = true;
  19. boolean normalsOn = true;
  20.  
  21. List<List<WB_Coord>> paths1;
  22.  
  23. void setup() {
  24.   size(1000, 500, P3D);
  25.   cam = new PeasyCam(this, 5000);
  26.   surface.setLocation(displayWidth + 10, 10);
  27.   perspective(PI/4, width/height, 1, 100000);
  28.   render = new WB_Render(this);
  29.   colorMode(HSB, 100);
  30.  
  31.   //HEC_Creator creator = new HEC_Box().setSize(200, 5000, 5000);
  32.   HEC_Creator creator = new HEC_Grid(3, 1, 500, 5000);
  33.  
  34.   mesh = new HE_Mesh(creator);
  35.   selection = mesh.selectAllVertices();
  36.   //mesh.modify(new HEM_MidEdgeSplit());
  37.  
  38.  
  39.   cp5 = new ControlP5(this);
  40.   cp5.addToggle("facesOn").setPosition(10, 10).setSize(37, 20).setMode(ControlP5.SWITCH);
  41.   cp5.addToggle("edgesOn").setPosition(57, 10).setSize(37, 20).setMode(ControlP5.SWITCH);
  42.   cp5.addToggle("normalsOn").setPosition(104, 10).setSize(37, 20).setMode(ControlP5.SWITCH);
  43.   cp5.addButton("moveGaussian").setPosition(10, 60).setSize(80, 20);
  44.   cp5.addButton("catmullSubdivide").setPosition(10, 90).setSize(80, 20);
  45.   cp5.addButton("mirror").setPosition(10, 120).setSize(80, 20);
  46.  
  47.   cp5.setAutoDraw(false);
  48.   setMeshColor();
  49. }
  50.  
  51. void draw() {
  52.   background(0);
  53.   lights();
  54.   fill(100);
  55.   noStroke();
  56.   if (facesOn) render.drawFaces(mesh);
  57.   if (facesOn) stroke(0);
  58.   else stroke(100);
  59.   if (edgesOn) render.drawEdges(mesh);
  60.   stroke(#FFEA00);
  61.   if (normalsOn) render.drawVertexNormals(selection, 5);
  62.  
  63.   cam.beginHUD();
  64.   cp5.draw();
  65.   drawScale();
  66.   cam.endHUD();
  67.  
  68. /*
  69.   WB_Point origin = new WB_Point(0, 0, 0);
  70.   WB_Point camera = new WB_Point(cam.getPosition()[0], cam.getPosition()[1], cam.getPosition()[2]);
  71.   WB_Plane sectionPln = new WB_Plane(origin, camera);
  72.   paths1 = HET_Contours.contoursAsPaths(mesh, sectionPln);
  73.   for (List<WB_Coord> pts : paths1) {
  74.     WB_PolyLine pl = new WB_PolyLine(pts);
  75.     push();
  76.     stroke(100, 100, 100);
  77.     render.drawPolyLine(pl);
  78.     pop();
  79.   }*/
  80. }
  81.  
  82. void drawScale() {
  83.   push();
  84.   translate(width-130, 10);
  85.   for (int i = 0; i < 120; i++) {
  86.     stroke(map(i, 0, 120, 10, 100), 100, 100);
  87.     line(i, 0, i, 20);
  88.   }
  89.   pop();
  90. }
  91.  
  92. void keyPressed() {
  93.   if (key == 'y') {
  94.     selection = HET_MeshOp.splitFacesMidEdge(mesh);
  95.   }
  96.   if (key == 'x') {
  97.     selection = HET_MeshOp.splitFacesMidEdge(mesh);
  98.   }
  99.   if (key == 'w') {
  100.     HE_VertexIterator vi = selection.vItr();
  101.     while (vi.hasNext()) {
  102.       HE_Vertex v = vi.next();
  103.       WB_Vector n = v.getVertexNormal();
  104.       WB_Point p = v.getPosition();      
  105.       n.mulSelf(10);
  106.       p.addSelf(n);
  107.     }
  108.   }
  109.   if (key == 's') {
  110.     HE_VertexIterator vi = selection.vItr();
  111.     while (vi.hasNext()) {
  112.       HE_Vertex v = vi.next();
  113.       WB_Vector n = v.getVertexNormal();
  114.       WB_Point p = v.getPosition();      
  115.       n.mulSelf(-10);
  116.       p.addSelf(n);
  117.     }
  118.   }
  119.   if (key == 'q') {
  120.     HE_VertexIterator vi = selection.vItr();
  121.     while (vi.hasNext()) {
  122.       HE_Vertex v = vi.next();
  123.       WB_Vector n = v.getVertexNormal();
  124.       WB_Point p = v.getPosition();      
  125.       n.mulSelf(100);
  126.       p.addSelf(n);
  127.     }
  128.   }
  129.   if (key == 'a') {
  130.     HE_VertexIterator vi = selection.vItr();
  131.     while (vi.hasNext()) {
  132.       HE_Vertex v = vi.next();
  133.       WB_Vector n = v.getVertexNormal();
  134.       WB_Point p = v.getPosition();      
  135.       n.mulSelf(-100);
  136.       p.addSelf(n);
  137.     }
  138.   }
  139.   if (key == 'e') {
  140.     HE_VertexIterator vi = selection.vItr();
  141.     while (vi.hasNext()) {
  142.       HE_Vertex v = vi.next();
  143.       WB_Vector n = v.getVertexNormal();
  144.       WB_Point p = v.getPosition();      
  145.       n.mulSelf(1);
  146.       p.addSelf(n);
  147.     }
  148.   }
  149.   if (key == 'd') {
  150.     HE_VertexIterator vi = selection.vItr();
  151.     while (vi.hasNext()) {
  152.       HE_Vertex v = vi.next();
  153.       WB_Vector n = v.getVertexNormal();
  154.       WB_Point p = v.getPosition();      
  155.       n.mulSelf(-1);
  156.       p.addSelf(n);
  157.     }
  158.   }
  159.   if (key == 'i') {
  160.     selection = selection.invertVertices();
  161.   }
  162.   if(key == 'c') {
  163.    mesh.deleteFaces(mesh.selectRandomFaces(0.1));
  164.   }
  165.  
  166.   setMeshColor();
  167. }
  168.  
  169. void setMeshColor() {
  170.   HE_VertexIterator vi = mesh.vItr();
  171.   while (vi.hasNext()) {
  172.     HE_Vertex v = vi.next();
  173.     color col = color(map(sin((float)v.getScalarMeanCurvature()), -1, 1, 10, 100), 100, 100);
  174.     //println(v.getScalarGaussianCurvature());
  175.     v.setColor(col);
  176.   }
  177. }
  178.  
  179. float sigmoid(float x) {
  180.   return 1/(1+exp(-x));
  181. }
  182.  
  183. void moveGaussian() {
  184.   //selection = HET_MeshOp.splitFacesMidEdge(mesh);
  185.   HE_VertexIterator vi = mesh.vItr();
  186.   while (vi.hasNext()) {
  187.     HE_Vertex v = vi.next();
  188.     WB_Vector n = v.getVertexNormal();
  189.     WB_Point p = v.getPosition();
  190.     float scalar = sigmoid((float)v.getScalarMeanCurvature());
  191.     scalar -= 0.5;
  192.     scalar *= 50;
  193.     n.mulSelf(scalar);
  194.     p.addSelf(n);
  195.   }
  196.   setMeshColor();
  197. }
  198.  
  199. void catmullSubdivide() {
  200.   mesh.subdivide(new HES_CatmullClark());
  201. }
  202.  
  203. void mirror() {
  204.   HEM_Mirror m = new HEM_Mirror().setPlane(0, 0, 0, random(-1, 1), random(-1, 1), random(-1, 1)).setOffset(10).setReverse(false);
  205.   mesh.modify(m);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement