Skip to content

Instantly share code, notes, and snippets.

@drichelson
Created July 20, 2019 18:25
Show Gist options
  • Save drichelson/e1ebe92466b9cc8eb99d82803e1fc79a to your computer and use it in GitHub Desktop.
Save drichelson/e1ebe92466b9cc8eb99d82803e1fc79a to your computer and use it in GitHub Desktop.
package us.ledicio.pattern;// In this file you can define your own custom patterns
import heronarts.lx.LX;
import heronarts.lx.LXCategory;
import heronarts.lx.color.LXColor;
import heronarts.lx.model.LXPoint;
import heronarts.lx.parameter.CompoundParameter;
import heronarts.lx.parameter.EnumParameter;
import us.ledicio.Gradient;
import java.awt.*;
/**
* Utilizes https://raw.githubusercontent.com/Auburns/FastNoise_Java/master/FastNoise.java
*/
@LXCategory("Form")
public class CellularFastNoisePattern extends BaseNoisePattern {
private final FastNoise fn;
public final CompoundParameter fractalOctaves = new CompoundParameter("fractalOctaves", 5, 0, 100)
.setDescription("fractalOctaves");
public final CompoundParameter fractalLacunarity = new CompoundParameter("fractalLacunarity", 2.0f, 0.0, 100.0)
.setDescription("fractalLacunarity");
public final CompoundParameter fractalGain = new CompoundParameter("fractalGain", 0.5f, 0.0, 100.0)
.setDescription("fractalGain");
public final CompoundParameter cellularJitter = new CompoundParameter("cellularJitter", 0.45, 0.0, 2.0)
.setDescription("cellularJitter");
public final EnumParameter<FastNoise.CellularReturnType> cellularReturnType = new EnumParameter<>("returnType",
FastNoise.CellularReturnType.Distance2Div)
.setDescription("Cellular Return Type");
private FastNoise.CellularReturnType currentCellularReturnType;
private final Gradient gradient;
public CellularFastNoisePattern(LX lx) {
super(lx);
System.out.println("CellularFastNoisePattern ctor");
addParameter("fractalOctaves", this.fractalOctaves);
addParameter("fractalLacunarity", this.fractalLacunarity);
addParameter("fractalGain", this.fractalGain);
addParameter("cellularJitter", this.cellularJitter);
addParameter("cellularReturnType", this.cellularReturnType);
fn = new FastNoise(1337);
fn.SetNoiseType(FastNoise.NoiseType.Cellular);
fn.SetFrequency(.02f);
fn.SetInterp(FastNoise.Interp.Quintic);
fn.SetFractalType(FastNoise.FractalType.FBM);
fn.SetFractalOctaves(5);
fn.SetFractalLacunarity(2.0f);
fn.SetFractalGain(0.5f);
fn.SetCellularJitter(0.45f);
fn.SetCellularDistanceFunction(FastNoise.CellularDistanceFunction.Euclidean);
FastNoise fnl = new FastNoise(137);
fn.SetCellularNoiseLookup(fnl);
// Change this for other cool options!
currentCellularReturnType = cellularReturnType.getEnum();
fn.SetCellularReturnType(currentCellularReturnType);
gradient = new Gradient(
new Color(colorA.getColor()),
new Color(colorB.getColor()),
new Color(colorC.getColor()),
new Color(colorD.getColor()),
colorBStop.getValuef(),
colorCStop.getValuef()
);
}
public void run(double deltaMs) {
super.run(deltaMs);
fn.SetFrequency(noiseFrequency.getValuef());
fn.SetFractalOctaves((int) fractalOctaves.getValue());
fn.SetFractalLacunarity(fractalLacunarity.getValuef());
fn.SetFractalGain(fractalGain.getValuef());
fn.SetCellularJitter(cellularJitter.getValuef());
// float brightnessVal = brightness.getValuef();
FastNoise.CellularReturnType newCellularReturnType = cellularReturnType.getEnum();
if (!newCellularReturnType.equals(currentCellularReturnType)) {
System.out.println(
"Changing cellular return type from: " + currentCellularReturnType + " to " + newCellularReturnType);
currentCellularReturnType = newCellularReturnType;
fn.SetCellularReturnType(currentCellularReturnType);
}
gradient.setColorA(new Color(colorA.getColor()));
gradient.setColorB(new Color(colorB.getColor()));
gradient.setColorC(new Color(colorC.getColor()));
gradient.setColorD(new Color(colorD.getColor()));
gradient.setB(colorBStop.getValuef());
gradient.setC(colorCStop.getValuef());
for (LXPoint p : model.points) {
float value = fn.GetCellular(
(float) ((p.x * scale.getValue()) + (elapsedMs * speed.getValue())),
(float) (p.y * scale.getValue() + (elapsedMs * speed.getValue())),
(float) (p.z * scale.getValue() + (elapsedMs * speed.getValue()))
);
// Add this value (converted to long) to our bucket of numbers
histo.update((long) (value * HISTO_FACTOR));
// get a normalized value since the value generated from FastNoise does not have any guaranteed upper/lower bound
float normalizedValue = normalizeWithHisto(value);
// colors[p.index] = LXColor.hsb(hue, sat, brightness * brightnessVal * normalizedValue);
Color c = gradient.getColor(normalizedValue);
colors[p.index] = LXColor.rgb(c.getRed(), c.getGreen(), c.getBlue());
}
if (frameCount % 1000 == 0) {
System.out
.println("Min: " + snapshot.getMin() / HISTO_FACTOR + ". Max: " + snapshot.getMax() / HISTO_FACTOR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment