- Home /
Perlin Noise.
I downloaded The ported librery LibNoise, to use Perlin Noise, I am quite new to this, so I started learning my self and i got stuck here. To generate using perlin Noise I need a method called GetNoiseValue(x, z) Which i saw in a C++ tutorial using this library.
Well, I couldnt find it in unity the closest I found was GetValue(x, y ,z) but as you can see i have to give a height value also.
Any ideas, suggestions? Thanks
I tried it and i couldnt get it work, also i think its not as developed like this library
The library may give you features that $$anonymous$$athf.PerlinNoise() does not have, but $$anonymous$$athf.PerlinNoise() does work. Try this script (essentially the script in the reference page with a fix and default values):
And this Answer:
http://answers.unity3d.com/questions/417793/perlin-noise-plane-manipulation.html
Unless you need something specific from LibNoise, I recommend using $$anonymous$$athf.PerlinNoise(). A lot more people on this list have knowledge and be willing to help with $$anonymous$$athf.PerlinNoise() than a third-part library.
Any success? Also what are you trying to do with the noise function because you may just not be getting the desired result.
EDIT: By the way, this is what the pseudocode for for that would be:
using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;
public Perlin noise = new Perlin();
int seed = 0; //the seed
float amplitude = 10; //the amplitude or 'noise multiplier' (if this is zero then all of the resultant positions will be Vector3.zero)
void GetValue () {
noise.Seed = seed;
//octaves, frequency, etc. can also be set here
Vector3 newPos = new Vector3(1, 1, 1); //this position could be anything(for example: a vertex)
float noiseValue = (float)noise.GetValue(newPos.x, newPos.x, newPos.x);
Vector3 perlinPos = newPos * (noiseValue * amplitude); //the resultant position(the vertex)
//in some cases normalizing the input position might work better
}
Thanks Chimera, I tried ths but I still cant get the result I want . I want create the noise depending from 2 variables x and z so it can generate procedually, Any ideas? I will keep trying to use your code anyway
Thanks
Answer by Chimera3D · Mar 14, 2014 at 02:17 AM
This function should generate 'flat' (not spherical) procedural terrain. Also make sure you have 'amplitude' and 'seed' public variables (integers for seed and float or int for amplitude) somewhere in your code.
public void ApplyNoise () {
Perlin noise;
noise = new Perlin();
noise.Seed = seed;
MeshFilter meshFilter = GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
Vector3[] verts = mesh.vertices;
for(int i = 0; i < verts.Length; i++){
Vector3 pos = verts[i].normalized;
float noiseValue = (float)noise.GetValue(pos.x, pos.y, pos.z);
verts[i].y = (noiseValue * amplitude);
}
mesh.vertices = verts;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
//the mesh collider can also be updated here if you're using one
}
Well, Thanks but this isnt what I need, I need to generate a voxel terrain, I made a system where I just get the block coodrinates and I render it, Here is the code, I couldnt get it working
void ChunkSetup(Chunk $$anonymous$$){
for(int x = 0; x < CHUN$$anonymous$$_SIZE; x++)
{
for(int z = 0; z < CHUN$$anonymous$$_SIZE; z++)
{
float R = Random.Range(-1.5f, 1.5f);
float height = (float)per.GetValue(x, R, z);
Debug.Log(R);
Debug.Log(height);
for (int y = 0; y < height; y++)
{
{
Vector3 xyz = new Vector3(x, y, z);
////I get block and I set it to active so its visible
$$anonymous$$.getBlockClass(xyz).getBlock().gameObject.SetActive(true);
}
}
}
}
}
Here it is maybe you can have a look a it :D
You have to multiply the height value by something, a number greater than 1 at least, by the way this is not really a voxel terrain like $$anonymous$$inecraft, it won't generate caves or any 'unusual terrain formations' like that by default. If the output was displayed on a grayscale texture, the lighter portions would resemble a cloud. Voxel terrain generation involves an entirely different algorithm. Anyhow, I don't see what you're trying to do with setting the "R" (y) input to a random value (between -1.5 and 1.5). Also the return value from Perlin.GetValue is not an integer and is never more than 1, the for loop with "y" as a result. Then again, if you want to achieve a blocky, voxel terrain then I don't think that this is the best solution. I'm pretty sure it's feasible, but if you want voxels then check that link above. Otherwise read on.
The "R" variable (y), can just be zero. You have to remember that the default Libnoise noise functions are three dimensional, if you only want two dimension, x and z, then you can just use normal $$anonymous$$athf.Perlin as the x and z coordinates of the blocks aren't going to be changed. Either way, since you already have Libnoise going and everything I guess you can just set y to zero. The "height" or return value has to be multiplied by something (my amplitude variable in the code above is the "noise multiplier"). Once again, the only coordinate that's being deter$$anonymous$$ed by the return value from the noise is the y value. So then once you multiply the height by a number greater than one, probably 5 or something, then you just convert the height to an integer which will deter$$anonymous$$e the highest block for that "stack" of blocks, thus the y position of that block (unless your block lengths aren't integers). From there you would just take that y position and generate blocks under it all the way to the "floor" of your terrain. The number of blocks would just be the y position $$anonymous$$us 1. Fairly simple. But this will only generate something along the lines of hills or mountains if the multiplier is set to a large enough value, but not any caves, overhanging cliffs, or anything like that, that you might find in $$anonymous$$inecraft. If you need some code I can write it, but I think I explained it pretty well.
EDIT: If you really want to stick to Libnoise though you could achieve empty underground areas, it would involve something along the line of generating a three dimensional field of points, that correlate with block positions that just wouldn't be generated or could be deleted all depending on how the system works. The three-demsional boundaries of this field could be somewhat random, while the noise would generate field inside of those boundaries. I'm not entirely sure that this would make a good cave, so maybe $$anonymous$$arching Cubes would just work a lot better after all.
This is great, thank you for such explanation, But I am still stuck, so if marching cubes would be better than perlin noise how would i implement it in unity, there is a library? or a method in unity?
I think $$anonymous$$inecraft uses a custom perlin noise algorithm for its generation. I dont like very much that tutorial although i already tried it. I dont want a $$anonymous$$ecraft like generation, I want more like a cube world generation. Just $$anonymous$$ountains. Any way do you have any tutorial about the $$anonymous$$arching Cubes algorithm, or something that feeds my needs?