- Home /
Perlin Noise subtraction formula
I am currently trying to generate "Floating Island" style terrain using Perlin noise. I can get the terrain to render just fine, but am having trouble getting the terrain layout to be what I want. Here is a diagram of what I am trying to achieve:
In theory , this is what I want to do:
Obtain two perlin noise values, one "baseline" value, and one "intensified" value.
If the baseline value is greater than the intensified value, then we return the baseline value, otherwise we return zero.
This is the code I am currently using:
private float PerlinSubtractor(int x, int z) {
float returnVal = -1;
float baseHeight = (float)height * Mathf.PerlinNoise((float)x / 16f * 1.5f + 0.001f, (float)z / 16f * 1.5f + 0.001f);
float intensifiedHeight = (float)height * 1.2f * Mathf.PerlinNoise((float)x / 16f * 1.5f + 100.001f, (float)z / 16f * 1.5f + 100.001f);
if (baseHeight > intensifiedHeight) returnVal = baseHeight;
return returnVal;
}
However that returns the following result:
I dont understand why it goes straight down instead of tapering off. Am I passing incorrect values, or is my formula too intense? Any help or suggestions would be appreciated.
Well, what exactly are you doing when you return a value of -1 ? Are you sure you don't actually use that value as a height value? A mesh never generates vertices on its own. You have to generate those vertices down there. So the issues has to be in your meshing code.
I think you may be right, I am returning it as a height value.
Right now, I am using this method to populate the terrain map:
void PopulateTerrain$$anonymous$$ap() {
for (int x = 0; x < width + 1; x++) {
for (int y = 0; y < height + 1; y++) {
for (int z = 0; z < width + 1; z++) {
terrain$$anonymous$$ap[x, y, z] = (float) y - PerlinSubtractor(x, z); ;
}
}
}
}
And this method parses the terrain map:
void $$anonymous$$archCube(Vector3Int position) {
//position here is the position on the terrain map
float[] cube = new float[8];
for (int i = 0; i < 8; i++) {
cube[i] = SampleTerrain(position + CornerTable[i]);
}
//This method gets which things to draw
int configIndex = GetCubeConfiguration(cube);
//I skip these two configurations because they represent air and underground, so no need to draw them.
if (configIndex == 0 || configIndex == 255) {
return;
}
int edgeIndex = 0;
for (int i = 0; i < 5; i++) {
for (int point = 0; point < 3; point++) {
int index = TriangleTable[configIndex, edgeIndex];
if (index == -1) return;
//this gets the data from the marching cubes tables I'm using
Vector3 vertex1 = position + CornerTable[EdgeIndexes[index, 0]];
Vector3 vertex2 = position + CornerTable[EdgeIndexes[index, 1]];
Vector3 vertexPosition = (vertex1 + vertex2) / 2;
vertices.Add(vertexPosition);
triangles.Add(vertices.Count - 1);
edgeIndex++;
}
}
}
From here, I generate the terrain using the generated vertices and triangles.
I think this would generate a mesh as I'm trying, but clearly I've messed up somewhere. If youd like, you can view the whole script here