Advertisement
rsidwell

RandCubesFunc.java

Jul 4th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 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. package org.jwildfire.create.tina.variation;
  18.  
  19. import static org.jwildfire.base.mathlib.MathLib.log;
  20. import static org.jwildfire.base.mathlib.MathLib.floor;
  21. import static org.jwildfire.base.mathlib.MathLib.pow;
  22.  
  23. import org.jwildfire.base.Tools;
  24. import org.jwildfire.create.tina.base.XForm;
  25. import org.jwildfire.create.tina.base.XYZPoint;
  26.  
  27. public class RandCubesFunc extends VariationFunc {
  28.   private static final long serialVersionUID = 1L;
  29.  
  30.   private static final String PARAM_BLOCKSIZE = "blockSize";
  31.   private static final String PARAM_BLOCKHEIGHT = "blockHeight";
  32.   private static final String PARAM_SPREAD = "spread";
  33.   private static final String PARAM_SEED = "seed";
  34.   private static final String PARAM_DENSITY = "density";
  35.   private static final String[] paramNames = { PARAM_BLOCKSIZE, PARAM_BLOCKHEIGHT, PARAM_SPREAD, PARAM_SEED, PARAM_DENSITY };
  36.  
  37.   private double blockSize = 1.0;
  38.   private double blockHeight = 1.0;
  39.   private double spread = 3.0;
  40.   private int seed = 1;
  41.   private double density = 1.0;
  42.  
  43.   private double hash(int a) {  //http://burtleburtle.net/bob/hash/integer.html
  44.       a = (a ^ 61) ^ (a >> 16);
  45.       a = a + (a << 3);
  46.       a = a ^ (a >> 4);
  47.       a = a * 0x27d4eb2d;
  48.       a = a ^ (a >> 15);
  49.       return (double) a / (double) Integer.MAX_VALUE;
  50.   }
  51.  
  52.   @Override
  53.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  54.     // Translation of variation by bezo97, https://bezo97.tk/plugins.html
  55.       double outx = (floor(log(pContext.random()) * (pContext.random() < 0.5 ? spread : -spread)));
  56.       double outy = (floor(log(pContext.random()) * (pContext.random() < 0.5 ? spread : -spread)));
  57.       int blockx = (int) outx, blocky = (int) outy;  
  58.       double z = hash(blockx * seed) + hash(blockx + blocky * seed); //random height for each block
  59.      
  60.       outx = ((double) blockx * density + pContext.random()) * blockSize;
  61.       outy = ((double) blocky * density + pContext.random()) * blockSize;
  62.       double outz = blockHeight * z * pow(pContext.random(), 0.125); //fade out down
  63.  
  64.      
  65.       pVarTP.x = outx * pAmount; //not +=
  66.       pVarTP.y = outy * pAmount;
  67.       pVarTP.z = outz * pAmount;
  68.      
  69.       pVarTP.color = z / 2.0; //block height -> palette location
  70.  
  71.   }
  72.  
  73.   @Override
  74.   public String[] getParameterNames() {
  75.     return paramNames;
  76.   }
  77.  
  78.   @Override
  79.   public Object[] getParameterValues() {
  80.     return new Object[] { blockSize, blockHeight, spread, seed, density };
  81.   }
  82.  
  83.   @Override
  84.   public void setParameter(String pName, double pValue) {
  85.     if (PARAM_BLOCKSIZE.equalsIgnoreCase(pName))
  86.       blockSize = pValue;
  87.     else if (PARAM_BLOCKHEIGHT.equalsIgnoreCase(pName))
  88.       blockHeight = pValue;
  89.     else if (PARAM_SPREAD.equalsIgnoreCase(pName))
  90.         spread = pValue;
  91.     else if (PARAM_SEED.equalsIgnoreCase(pName))
  92.         seed = Tools.FTOI(pValue);
  93.     else if (PARAM_DENSITY.equalsIgnoreCase(pName))
  94.         density = pValue;
  95.     else
  96.       throw new IllegalArgumentException(pName);
  97.   }
  98.  
  99.   @Override
  100.   public String getName() {
  101.     return "randCubes";
  102.   }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement