Advertisement
JoshDreamland

Worley Lava

Nov 4th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 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 = 1600, h = 900;
  30. final long seed = 0xDEADBEEF;
  31. final int noiseCount = 256;
  32. final float baser = 32, basey = 96;
  33. final float dist1 = .005, dist2 = .0005;
  34. final int nth = 0;
  35.  
  36. // Other locals
  37. Worley worley = new Worley(seed, noiseCount);
  38. Worley orange = new Worley(seed + 1, noiseCount * 4);
  39. PImage img = createImage(w, h, RGB);
  40.  
  41. // Generic setup code: Nothing of interest happens here, I promise.
  42. // This function just normalizes the x/y values to [0,1] and passes
  43. // them to colAt() one by one to populate the image. In a fragment
  44. // shader, this function would not exist.
  45. void setup() {
  46.   size(w, h);
  47.   img.loadPixels();
  48.  
  49.   int ind = 0;
  50.   for (int y = 0; y < h; ++y) {
  51.     float yf = y / (float)h;
  52.     for (int x = 0; x < w; ++x)
  53.       img.pixels[ind++] = colAt(x / (float)w, yf);
  54.   }
  55.   img.updatePixels();
  56.   image(img, 0, 0);
  57.   save("worley-lava-tex.png");
  58. }
  59.  
  60.  
  61. // Utility functions, boilerplate boilerplate boilerplate
  62. static double sqr(double x) { return x*x; }
  63. static void arrins(int[] arr, int x, int ind) {
  64.   for (int i = arr.length - 1; i > ind; --i)
  65.     arr[i] = arr[i-1];
  66.   arr[ind] = x;
  67. } // This function exists because Java generics don't like primitives.
  68. // This will work in Java 7. OHWAITNOITWONT
  69. static void arrins(double[] arr, double x, int ind) {
  70.   for (int i = arr.length - 1; i > ind; --i)
  71.     arr[i] = arr[i-1];
  72.   arr[ind] = x;
  73. }
  74.  
  75.  
  76. // This is where the actual logic starts.
  77.  
  78. // This is a class to sample one octave of worley noise.
  79. static class Worley {
  80.   // These are the variables needed by the actual image logic
  81.   double[][] points;
  82.  
  83.   // This creates our points, scattering them randomly according to the seed.
  84.   public Worley(long seed, int npts) {
  85.     Random worlrand = new Random(seed);
  86.     points = new double[npts][2];
  87.    
  88.     for (int i = 0; i < points.length; ++i) {
  89.       points[i][0] = worlrand.nextDouble();
  90.       points[i][1] = worlrand.nextDouble();
  91.     }
  92.   }
  93.  
  94.   // This samples a point by computing the squared distance to the nearest k points,
  95.   // then returning the kth closest
  96.   public float get(float x, float y, final int k, double max, float clamp) {
  97.     double[] d2 = new double[k + 1];
  98.     int[] ptnum = new int[k + 1];
  99.     for (int i = 0; i <= k; ++i) {
  100.       d2[i] = Double.POSITIVE_INFINITY;
  101.       ptnum[i] = 0;
  102.     }
  103.     for (int i = 0; i < points.length; ++i) {
  104.       double d2i = sqr(x - points[i][0]) + sqr(y - points[i][1]);
  105.       for (int j = 0; j <= k; ++j)
  106.         if (d2i < d2[j]) {
  107.           arrins(d2, d2i, j);
  108.           arrins(ptnum, i, j);
  109.           break;
  110.         }
  111.     }
  112.     return min(clamp, (float)(d2[k] / max));
  113.   }
  114. }
  115.  
  116. // This function samples a pixel and is called millions of times to draw your image
  117. color colAt(float x, float y) {
  118.   color accum = color(x * 255, 0, y * 255);
  119.   float wo = worley.get(x, y, nth, dist1, 2) + orange.get(x, y, nth, dist2, 1.5) / 12;
  120.   accum = color(min(1, wo) * (255 - baser) + baser, basey * (wo - .25), 0);
  121.   return accum;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement