Advertisement
rsidwell

rings3

Jan 8th, 2025
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. /*
  2.   JWildfire - an image and animation processor written in Java
  3.   Copyright (C) 1995-2021 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 org.jwildfire.create.tina.base.Layer;
  20. import org.jwildfire.create.tina.base.XForm;
  21. import org.jwildfire.create.tina.base.XYZPoint;
  22.  
  23. import static org.jwildfire.base.mathlib.MathLib.EPSILON;
  24.  
  25. public class Rings3Func extends VariationFunc implements SupportsGPU {
  26.   private static final long serialVersionUID = 1L;
  27.  
  28.   private static final String PARAM_VAL = "val";
  29.   private static final String PARAM_N = "n";
  30.  
  31.   private static final String[] paramNames = {PARAM_VAL, PARAM_N};
  32.  
  33.   private double val = 0.01;
  34.   private double n = 0.0;
  35.  
  36.   @Override
  37.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  38.     double l = pAffineTP.getPrecalcSqrt();
  39.     if (_dx == 0 || l == 0) {
  40.       return;
  41.     }
  42.     double k = (int) (l / _dx + 1) / 2;
  43.     double r = pAmount * (2.0 - _dx * (k * 2 / l + 1) - n * (k * c - 1) / l);
  44.  
  45.     pVarTP.x += r * pAffineTP.x;
  46.     pVarTP.y += r * pAffineTP.y;
  47.  
  48.     if (pContext.isPreserveZCoordinate()) {
  49.       pVarTP.z += pAmount * pAffineTP.z;
  50.     }
  51.   }
  52.  
  53.   @Override
  54.   public String[] getParameterNames() {
  55.     return paramNames;
  56.   }
  57.  
  58.   @Override
  59.   public Object[] getParameterValues() {
  60.     return new Object[]{val, n};
  61.   }
  62.  
  63.   @Override
  64.   public void setParameter(String pName, double pValue) {
  65.     if (PARAM_VAL.equalsIgnoreCase(pName))
  66.       val = pValue;
  67.     else if (PARAM_N.equalsIgnoreCase(pName))
  68.       n = pValue;
  69.     else
  70.       throw new IllegalArgumentException(pName);
  71.   }
  72.  
  73.   @Override
  74.   public String getName() {
  75.     return "rings3";
  76.   }
  77.  
  78.   private double _dx;
  79.   private double c;
  80.  
  81.   @Override
  82.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  83.     _dx = val * val + EPSILON;
  84.     c = 2.0 * (_dx - _dx * _dx);
  85.   }
  86.  
  87.   @Override
  88.   public VariationFuncType[] getVariationTypes() {
  89.     return new VariationFuncType[]{VariationFuncType.VARTYPE_2D, VariationFuncType.VARTYPE_SUPPORTS_GPU, VariationFuncType.VARTYPE_SUPPORTED_BY_SWAN};
  90.   }
  91.  
  92.  
  93.   @Override
  94.   public String getGPUCode(FlameTransformationContext context) {
  95.     return "float l = sqrtf(__x * __x + __y * __y);\n"
  96.         + "float _dx = __rings2_val * __rings2_val ADD_EPSILON;\n"
  97.         + "float c = 2.f * (_dx - _dx * _dx);\n"
  98.         + " if (_dx == 0.f || l == 0.f) {\n"
  99.         + "    return;\n"
  100.         + " }\n"
  101.         + "float k = (int) ((l / _dx + 1.f) / 2.f)"
  102.         + " float r = __rings2 * (2.f - _dx * (k * 2.f / l + 1.f) - __rings2_n * (k * c - 1.f) / l);\n"
  103.         + "\n"
  104.         + "    __px += r * __x;\n"
  105.         + "    __py += r * __y;\n"
  106.         + "\n"
  107.         + (context.isPreserveZCoordinate() ? "__pz += __rings2 * __z;\n" : "");
  108.   }
  109.  
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement