Advertisement
JoshDreamland

Light rod

May 24th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. // This is free and unencumbered software released into the public domain.
  2.  
  3. // Anyone is free to copy, modify, publish, use, compile, sell, or
  4. // distribute this software, either in source code form or as a compiled
  5. // binary, for any purpose, commercial or non-commercial, and by any
  6. // means.
  7.  
  8. // In jurisdictions that recognize copyright laws, the author or authors
  9. // of this software dedicate any and all copyright interest in the
  10. // software to the public domain. We make this dedication for the benefit
  11. // of the public at large and to the detriment of our heirs and
  12. // successors. We intend this dedication to be an overt act of
  13. // relinquishment in perpetuity of all present and future rights to this
  14. // software under copyright law.
  15.  
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. // OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. // For more information, please refer to <http://unlicense.org/>
  25.  
  26. import java.util.Random; // I can't stand arbitrary output
  27.  
  28. // Image parameters: This is the stuff that's easy to change
  29. final int w = 640, h = 640, d = 640;
  30.  
  31. // Other locals
  32. PImage img = createImage(w, h, RGB);
  33.  
  34. // Generic setup code: Nothing of interest happens here, I promise.
  35. // This function just normalizes the x/y values to [0,1] and passes
  36. // them to colAt() one by one to populate the image. In a fragment
  37. // shader, this function would not exist.
  38. void setup() {
  39.   size(w, h);
  40. }
  41.  
  42. float zat = -1;
  43. void draw() {
  44.   if (++zat > d)
  45.     zat = 0;
  46.   img.loadPixels();
  47.  
  48.   int ind = 0;
  49.   for (int y = 0; y < h; ++y) {
  50.     float yf = y / (float)h;
  51.     for (int x = 0; x < w; ++x)
  52.       img.pixels[ind++] = colAt(x / (float)w, yf, zat / d);
  53.   }
  54.  
  55.   img.updatePixels();
  56.   image(img, 0, 0);
  57. }
  58.  
  59.  
  60. // Utility functions, boilerplate boilerplate boilerplate
  61. static float sqr(float x) { return x*x; }
  62. static float hypot(float x, float y) { return sqrt(sqr(x) + sqr(y)); }
  63.  
  64. // This is where the actual logic starts.
  65.  
  66. float distLine(float x1, float y1, float z1, float x2, float y2, float z2, float x, float y, float z) {
  67.   float l2 = sqr(x1 - x2) + sqr(y1 - y2) + sqr(z1 - z2);
  68.   // System.out.println("(" + x1 + ", " + y1 + ") -> (" + x + ", " + y + ") = " + hypot(x - x1, y - y1));
  69.   if (l2 == 0) return sqrt(sqr(x - x1) + sqr(y - y1) + sqr(z - z1));
  70.   float t = ((x - x1) * (x2 - x1) + (y - y1) * (y2 - y1) + (z - z1) * (z2 - z1)) / l2;
  71.   if (t < 0) return sqrt(sqr(x-x1) + sqr(y-y1) + sqr(z-z1));
  72.   if (t > 1) return sqrt(sqr(x-x2) + sqr(y-y2) + sqr(z-z2));
  73.   return sqrt(sqr(x - (x1 + t * (x2 - x1))) + sqr(y - (y1 + t * (y2 - y1))) + sqr(z - (z1 + t * (z2 - z1))));
  74. }
  75.  
  76. float lineLight(float x1, float y1, float z1, float x2, float y2, float z2, float r, float x, float y, float z) {
  77.   float dist = distLine(x1, y1, z1, x2, y2, z2, x, y, z);
  78.   if (dist >= r) return 0;
  79.   return 1 - dist / r;
  80. }
  81.  
  82. // This function samples a pixel and is called millions of times to draw your image
  83. color colAt(float x, float y, float z) {
  84.   return lerpColor(0x000000, 0xAFFFCF, lineLight(w/5, h/5, d*2.25/5, w*4/5, h*4/5, d*2.75/5, hypot(w/8, h/8), x * w, y * h, z * d));
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement