rsidwell

DCCracklePWF-test

Dec 17th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.06 KB | None | 0 0
  1. /*
  2.   JWildfire - an image and animation processor written in Java
  3.   Copyright (C) 1995-2011 Andreas Maschke
  4.  
  5.   This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  6.   General Public License as published by the Free Software Foundation; either version 2.1 of the
  7.   License, or (at your option) any later version.
  8.  
  9.   This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  10.   even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11.   Lesser General Public License for more details.
  12.  
  13.   You should have received a copy of the GNU Lesser General Public License along with this software;
  14.   if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  15.   02110-1301 USA, or see the FSF site: http://www.fsf.org.
  16. */
  17.  
  18. // Port of Hexes and Crackle plugin by slobo777, see http://slobo777.deviantart.com/art/Apo-Plugins-Hexes-And-Crackle-99243824
  19. // All credits for this wonderful plugin to him!
  20.  
  21. // This version has a "preset" variable that sets the other variables to make common patterns. By Rick Sidwell
  22. // Values are taken from the Built-in script Crackle_Styles_Chooser_Rev01_by_MH, originally from
  23. // Ian Anderson and Anu Wilde.
  24. // "Hexes" variation breaks plane into hexagonal cells and applies same
  25. //    power, scaling, rotation.
  26. // Direct color used from dc_crackle_wf implemented by Brad Stefanov
  27.  
  28. package org.jwildfire.create.tina.variation;
  29.  
  30. import static org.jwildfire.base.mathlib.MathLib.M_PI;
  31. import static org.jwildfire.base.mathlib.MathLib.cos;
  32. import static org.jwildfire.base.mathlib.MathLib.fabs;
  33. import static org.jwildfire.base.mathlib.MathLib.floor;
  34. import static org.jwildfire.base.mathlib.MathLib.pow;
  35. import static org.jwildfire.base.mathlib.MathLib.sin;
  36. import static org.jwildfire.create.tina.variation.NoiseTools.simplexNoise3D;
  37. import static org.jwildfire.create.tina.variation.VoronoiTools.VORONOI_MAXPOINTS;
  38. import static org.jwildfire.create.tina.variation.VoronoiTools._x_;
  39. import static org.jwildfire.create.tina.variation.VoronoiTools._y_;
  40. import static org.jwildfire.create.tina.variation.VoronoiTools._z_;
  41. import static org.jwildfire.create.tina.variation.VoronoiTools.closest;
  42. import static org.jwildfire.create.tina.variation.VoronoiTools.voronoi;
  43.  
  44. import org.jwildfire.base.Tools;
  45. import org.jwildfire.create.tina.base.Layer;
  46. import org.jwildfire.create.tina.base.XForm;
  47. import org.jwildfire.create.tina.base.XYZPoint;
  48.  
  49. public class DCCracklePWFFunc extends VariationFunc {
  50.   private static final long serialVersionUID = 1L;
  51.  
  52.   public static final String VAR_NAME = "dc_cracklep_wf";
  53.  
  54.   private static final String PARAM_PRESET = "preset";
  55.   public static final String PARAM_CELLSIZE = "cellsize";
  56.   public static final String PARAM_POWER = "power";
  57.   public static final String PARAM_DISTORT = "distort";
  58.   public static final String PARAM_SCALE = "scale";
  59.   private static final String PARAM_Z = "z";
  60.   private static final String PARAM_COLOR_SCALE = "colorScale";
  61.   private static final String PARAM_COLOR_OFFSET = "colorOffset";  
  62.  
  63.   protected static final String[] paramNames = { PARAM_PRESET, PARAM_CELLSIZE, PARAM_POWER, PARAM_DISTORT, PARAM_SCALE, PARAM_Z,PARAM_COLOR_SCALE,PARAM_COLOR_OFFSET };
  64.  
  65.   private int preset = 0;
  66.   private double cellsize = 1.0;
  67.   private double power = 0.2;
  68.   private double distort = 0.0;
  69.   private double scale = 1.0;
  70.   private double z = 0.0;
  71.   private double colorScale = 0.5;
  72.   private double colorOffset = 0.0;  
  73.  
  74.   @Override
  75.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  76.     double DXo, DYo, L, R, s, trgL;
  77.     double U[] = new double[2];
  78.     int XCv, YCv;
  79.  
  80.     // An infinite number of invisible cells? No thanks!
  81.     if (0.0 == cellsize) {
  82.       return;
  83.     }
  84.  
  85.     // Scaling factor
  86.     s = cellsize / 2.0;
  87.  
  88.     if (pAmount != 0) {
  89.         // For a blur effect, base everything starting on a circle radius 1.0
  90.         // (as opposed to reading the values from FTx and FTy)
  91.         double blurr = (pContext.random() + pContext.random()) / 2.0 + (pContext.random() - 0.5) / 4.0;
  92.  
  93.         double theta = 2 * M_PI * pContext.random();
  94.  
  95.         U[_x_] = blurr * sin(theta);
  96.         U[_y_] = blurr * cos(theta);
  97.     }
  98.     else {
  99.         // pAmount=0, so color only; don't blur
  100.         U[_x_] = pAffineTP.x;
  101.         U[_y_] = pAffineTP.y;
  102.     }
  103.  
  104.     // Use integer values as Voronoi grid co-ordinates
  105.     XCv = (int) floor(U[_x_] / s);
  106.     YCv = (int) floor(U[_y_] / s);
  107.  
  108.     // Get a set of 9 square centre points, based around the one above
  109.     int di, dj;
  110.     int i = 0;
  111.     for (di = -1; di < 2; di++) {
  112.       for (dj = -1; dj < 2; dj++) {
  113.         cached_position(C, XCv + di, YCv + dj, z, s, distort, P[i]);
  114.         i++;
  115.       }
  116.     }
  117.  
  118.     int q = closest(P, 9, U);
  119.  
  120.     int offset[][] = new int[][] { { -1, -1 }, { -1, 0 }, { -1, 1 },
  121.         { 0, -1 }, { 0, 0 }, { 0, 1 },
  122.         { 1, -1 }, { 1, 0 }, { 1, 1 } };
  123.  
  124.     // Remake list starting from chosen square, ensure it is completely surrounded (total 9 points)
  125.  
  126.     // First adjust centres according to which one was found to be closest
  127.     XCv += offset[q][_x_];
  128.     YCv += offset[q][_y_];
  129.  
  130.     // Get a new set of 9 square centre points, based around the definite closest point
  131.     i = 0;
  132.     for (di = -1; di < 2; di++) {
  133.       for (dj = -1; dj < 2; dj++) {
  134.         cached_position(C, XCv + di, YCv + dj, z, s, distort, P[i]);
  135.         i++;
  136.       }
  137.     }
  138.  
  139.     L = voronoi(P, 9, 4, U); // index 4 is centre cell
  140.  
  141.     // Delta vector from centre
  142.     DXo = U[_x_] - P[4][_x_];
  143.     DYo = U[_y_] - P[4][_y_];
  144.  
  145.     /////////////////////////////////////////////////////////////////
  146.     // Apply "interesting bit" to cell's DXo and DYo co-ordinates
  147.  
  148.     // trgL is the new value of L
  149.     trgL = pow(L + 1e-100, power) * scale; // ( 0.9 )
  150.  
  151.     R = trgL / (L + 1e-100);
  152.  
  153.     DXo *= R;
  154.     DYo *= R;
  155.  
  156.     // Add cell centre co-ordinates back in
  157.     DXo += P[4][_x_];
  158.     DYo += P[4][_y_];
  159.     // Finally add values in
  160.     applyCellCalculation(pVarTP, pAmount, DXo, DYo, L);
  161.   }
  162.  
  163.   protected void applyCellCalculation(XYZPoint pVarTP, double pAmount, double DXo, double DYo, double L) {
  164.     pVarTP.color = L * colorScale + colorOffset; ;
  165.     if (pVarTP.color < 0)
  166.       pVarTP.color = 0.0;
  167.     else if (pVarTP.color > 1.0)
  168.       pVarTP.color = 1.0;
  169.     pVarTP.x += pAmount * DXo;
  170.     pVarTP.y += pAmount * DYo;
  171.   }
  172.  
  173.   @Override
  174.   public String[] getParameterNames() {
  175.     return paramNames;
  176.   }
  177.  
  178.   @Override
  179.   public Object[] getParameterValues() {
  180.     return new Object[] { preset, cellsize, power, distort, scale, z,colorScale,colorOffset};
  181.   }
  182.  
  183.   @Override
  184.   public void setParameter(String pName, double pValue) {
  185.     if (PARAM_PRESET.equalsIgnoreCase(pName)) {
  186.       preset = Tools.FTOI(pValue);
  187.       switch (preset) {
  188.         case 0:
  189.           cellsize = 1.0; power = 0.2; distort = 0.0; scale = 1.00; z=0.0;
  190.           break;
  191.         case 1:
  192.           cellsize = 0.5; power = 0.5; distort = 1.0; scale = 0.95; z=10.0;
  193.           break;
  194.         case 2:
  195.             cellsize = 0.5; power = 0.01; distort = 0.5; scale = 1.0; z=0.0;
  196.             break;
  197.           case 3:
  198.             cellsize = 0.05; power = 0.9; distort = 0.9; scale = 0.5; z=0.0;
  199.             break;
  200.           case 4:
  201.             cellsize = 0.5; power = 1.0; distort = 1.0; scale = 0.93; z=10.0;
  202.             break;
  203.           case 5:
  204.             cellsize = 1.0; power = 1.0; distort = 0.0; scale = 0.9; z=0.0;
  205.             break;
  206.           case 6:
  207.             cellsize = 0.8; power = 0.5; distort = 0.5; scale = 0.8; z=0.5;
  208.             break;
  209.           case 7:
  210.             cellsize = 0.2; power = 0.01; distort = 0.0; scale = 0.4; z=2.0;
  211.             break;
  212.           case 8:
  213.             cellsize = 1.0; power = 0.5; distort = 0.25; scale = 0.5; z=0.0;
  214.             break;
  215.           case 9:
  216.             cellsize = 0.6; power = 0.75; distort = 1.0; scale = 0.25; z=0.75;
  217.             break;
  218.           case 10:
  219.             cellsize = 0.5; power = 25.0; distort = 0.0; scale = 9.0; z=6.0;
  220.             break;
  221.           case 11:
  222.             cellsize = 0.2; power = 1.0; distort = 0.0; scale = 0.4; z=0.0;
  223.             break;
  224.            case 12:
  225.             cellsize = 1.5; power = 0.01; distort = 0.0; scale = 0.4; z=0.0;
  226.             break;
  227.           case 13:
  228.             cellsize = 8.0; power = 0.01; distort = 0.0; scale = 0.4; z=0.0;
  229.             break;
  230.           case 14:
  231.             cellsize = 0.2; power = 0.05; distort = 1.0; scale = 5.0; z=0.0;
  232.             break;
  233.           case 15:
  234.             cellsize = 0.07; power = 0.05; distort = 0.5; scale = 9.0; z=6.0;
  235.             break;
  236.           case 16:
  237.             cellsize = 0.2; power = 0.1; distort = 0.0; scale = 1.5; z=2.0;
  238.             break;
  239.           case 17:
  240.             cellsize = 0.297494; power = 0.662265; distort = 0.0708866; scale = 0.228156; z=0.0;
  241.             break;
  242.           case 18:
  243.             cellsize = 0.205939; power = 1.0; distort = 0.0; scale = 0.6298; z=0.35;
  244.             break;
  245.           case 19:
  246.             cellsize = 0.5; power = 0.001; distort = 1.0; scale = 2.0; z=0.0;
  247.             break;
  248.           case 20:
  249.             cellsize = 0.5; power = 0.0005; distort = 0.748; scale = 1.465; z=6.0;
  250.             break;
  251.       }
  252.     }
  253.     else if (PARAM_CELLSIZE.equalsIgnoreCase(pName))
  254.       cellsize = pValue;
  255.     else if (PARAM_POWER.equalsIgnoreCase(pName))
  256.       power = pValue;
  257.     else if (PARAM_DISTORT.equalsIgnoreCase(pName))
  258.       distort = pValue;
  259.     else if (PARAM_SCALE.equalsIgnoreCase(pName))
  260.       scale = pValue;
  261.     else if (PARAM_Z.equalsIgnoreCase(pName))
  262.       z = pValue;
  263.     else if (PARAM_COLOR_SCALE.equalsIgnoreCase(pName))
  264.       colorScale = pValue;
  265.     else if (PARAM_COLOR_OFFSET.equalsIgnoreCase(pName))
  266.       colorOffset = pValue;  
  267.     else
  268.       throw new IllegalArgumentException(pName);
  269.   }
  270.  
  271.   @Override
  272.   public String getName() {
  273.     return VAR_NAME;
  274.   }
  275.  
  276.   //These set cache size for cell centres, they take a lot of processing, so it's handy to
  277.   //keep values between calls
  278.   private final static int CACHE_NUM = 10;
  279.   private final static int CACHE_WIDTH = 21;
  280.  
  281.   // P is a working list of points
  282.   private double P[][] = new double[VORONOI_MAXPOINTS][2];
  283.   // C is a cache of pre-calculated centres
  284.   private double C[][][] = new double[CACHE_WIDTH][CACHE_WIDTH][2];
  285.  
  286.   //Waffle's cells are based on a simple square grid which is distorted using a noise function
  287.  
  288.   //position() calculates cell centre for cell (x, y), given plane slice z, scale factor s, distortion d
  289.   // and stores in supplied array
  290.   private void position(int x, int y, double z, double s, double d, double V[]) {
  291.     double E[] = new double[3];
  292.     double F[] = new double[3];
  293.  
  294.     // Values here are arbitrary, chosen simply to be far enough apart so they do not correlate
  295.     E[_x_] = x * 2.5;
  296.     E[_y_] = y * 2.5;
  297.     E[_z_] = z * 2.5;
  298.     // Cross-over between x and y is intentional
  299.     F[_x_] = y * 2.5 + 30.2;
  300.     F[_y_] = x * 2.5 - 12.1;
  301.     F[_z_] = z * 2.5 + 19.8;
  302.  
  303.     V[_x_] = (x + d * simplexNoise3D(E)) * s;
  304.     V[_y_] = (y + d * simplexNoise3D(F)) * s;
  305.   }
  306.  
  307.   //cached_position gives centre co-ordinates either from cache, or calculated from scratch if needed
  308.   private void cached_position(double Cache[][][], int x, int y, double z, double s, double d, double V[]) {
  309.     if (fabs(x) <= CACHE_NUM && fabs(y) <= CACHE_NUM) {
  310.       V[_x_] = Cache[x + CACHE_NUM][y + CACHE_NUM][_x_];
  311.       V[_y_] = Cache[x + CACHE_NUM][y + CACHE_NUM][_y_];
  312.     }
  313.     else {
  314.       position(x, y, z, s, d, V);
  315.     }
  316.   }
  317.  
  318.   @Override
  319.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  320.     // Pre-calculate cache of grid centres, to save time later . . .
  321.     for (int x = -CACHE_NUM; x <= CACHE_NUM; x++) {
  322.       for (int y = -CACHE_NUM; y <= CACHE_NUM; y++) {
  323.         position(x, y, z, cellsize / 2.0, distort, C[x + CACHE_NUM][y + CACHE_NUM]);
  324.       }
  325.     }
  326.   }
  327.  
  328. }
Add Comment
Please, Sign In to add comment