/* JWildfire - an image and animation processor written in Java Copyright (C) 1995-2011 Andreas Maschke This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jwildfire.create.tina.variation; import static org.jwildfire.base.mathlib.MathLib.log; import static org.jwildfire.base.mathlib.MathLib.floor; import static org.jwildfire.base.mathlib.MathLib.pow; import org.jwildfire.base.Tools; import org.jwildfire.create.tina.base.XForm; import org.jwildfire.create.tina.base.XYZPoint; public class RandCubesFunc extends VariationFunc { private static final long serialVersionUID = 1L; private static final String PARAM_BLOCKSIZE = "blockSize"; private static final String PARAM_BLOCKHEIGHT = "blockHeight"; private static final String PARAM_SPREAD = "spread"; private static final String PARAM_SEED = "seed"; private static final String PARAM_DENSITY = "density"; private static final String[] paramNames = { PARAM_BLOCKSIZE, PARAM_BLOCKHEIGHT, PARAM_SPREAD, PARAM_SEED, PARAM_DENSITY }; private double blockSize = 1.0; private double blockHeight = 1.0; private double spread = 3.0; private int seed = 1; private double density = 1.0; private double hash(int a) { //http://burtleburtle.net/bob/hash/integer.html a = (a ^ 61) ^ (a >> 16); a = a + (a << 3); a = a ^ (a >> 4); a = a * 0x27d4eb2d; a = a ^ (a >> 15); return (double) a / (double) Integer.MAX_VALUE; } @Override public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) { // Translation of variation by bezo97, https://bezo97.tk/plugins.html double outx = (floor(log(pContext.random()) * (pContext.random() < 0.5 ? spread : -spread))); double outy = (floor(log(pContext.random()) * (pContext.random() < 0.5 ? spread : -spread))); int blockx = (int) outx, blocky = (int) outy; double z = hash(blockx * seed) + hash(blockx + blocky * seed); //random height for each block outx = ((double) blockx * density + pContext.random()) * blockSize; outy = ((double) blocky * density + pContext.random()) * blockSize; double outz = blockHeight * z * pow(pContext.random(), 0.125); //fade out down pVarTP.x = outx * pAmount; //not += pVarTP.y = outy * pAmount; pVarTP.z = outz * pAmount; pVarTP.color = z / 2.0; //block height -> palette location } @Override public String[] getParameterNames() { return paramNames; } @Override public Object[] getParameterValues() { return new Object[] { blockSize, blockHeight, spread, seed, density }; } @Override public void setParameter(String pName, double pValue) { if (PARAM_BLOCKSIZE.equalsIgnoreCase(pName)) blockSize = pValue; else if (PARAM_BLOCKHEIGHT.equalsIgnoreCase(pName)) blockHeight = pValue; else if (PARAM_SPREAD.equalsIgnoreCase(pName)) spread = pValue; else if (PARAM_SEED.equalsIgnoreCase(pName)) seed = Tools.FTOI(pValue); else if (PARAM_DENSITY.equalsIgnoreCase(pName)) density = pValue; else throw new IllegalArgumentException(pName); } @Override public String getName() { return "randCubes"; } }