Skip to content

Instantly share code, notes, and snippets.

@KdotJPG
Last active January 29, 2020 23:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KdotJPG/f8a072ed73aac51aef8f6d40d3ae825c to your computer and use it in GitHub Desktop.
Save KdotJPG/f8a072ed73aac51aef8f6d40d3ae825c to your computer and use it in GitHub Desktop.
Sample code for only generating enough noise octaves to know the sign of the final value (simplified from a small WIP MC mod of mine)
private double computeGoodEnoughNoise(int worldX, int worldY, int worldZ, double startingValue) {
// Final noise value begins with the threshold.
double value = startingValue; // e.g. -heightThreshold (negative)
double freq = BASE_FREQUENCY; // e.g. 1.0/256.0
double amp = BASE_AMPLITUDE; // e.g. 64.0
for (int i = 0; i < nOctaves && actualValue > -uncertaintyBounds[i] && actualValue < uncertaintyBounds[i]; i++) {
value += octaves[i].noise3_XZBeforeY(worldX * hScale * freq, worldY * vScale * freq, worldZ * hScale * freq) * amp;
freq *= 2.0;
amp /= 2.0;
}
return actualValue;
}
/////////////////////////////////
// Pregenerate this in a constructor, or somewhere.
// for nOctaves=4, this would generate
// { BASE_AMPLITUDE * (1.0 + 0.5 + 0.25 + 0.125), BASE_AMPLITUDE * (0.5 + 0.25 + 0.125), BASE_AMPLITUDE * (0.25 + 0.125), BASE_AMPLITUDE * (0.125) };
uncertaintyBounds = new double[nOctaves];
{
double boundBase = 0.0;
for (int i = nOctaves - 1; i >= 0; i--) {
boundBase += 1.0 / (1 << i);
uncertaintyBounds[i] = OUTPUT_NOISE_MAIN_AMPLITUDE * boundBase;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment