- Home /
Unity Perlin Noise Bug?
I was testing Perlin noise and I think I found a bug / issue with it. For some reason this code :
float a = Mathf.PerlinNoise(16+i,16+j);
Works fine, but doing this :
float a = Mathf.PerlinNoise(16+i/j,16+j/i);
Does not work, as in it does not calculate it, and trying to debug.log the value doesn't even call the debug function. I have no idea why this is not working. I am using Unity 4 ( latest ) and it is really starting to bug me
The whole function is :
for( int i = 0;i<16;i++){
for( int j = 0;j<16;j++){
float Perlin1 = Mathf.PerlinNoise(((float)i+chunkPosX)/x_,((float)j+chunkPosZ)/z_);
float Perlin2 = Mathf.PerlinNoise(((float)j+chunkPosZ)/z_,((float)i+chunkPosX)/x_);
int y = (int)Mathf.Round(Perlin1*20*Perlin2);
voxels[i,(int)y,j]=new Vox(voxelDatabase[1],1);
}
}
}
That seemed to work, the code you suggested but diving i by j or the other way round doesnt work at all. I rewrote my function, could you see what i would need to cast ?
Answer by hiddenspring81 · May 29, 2013 at 04:17 PM
According to the documentation on Unity's website, both i
and j
need to be floating point values from 0 to 1. Have you tried,
float a = Mathf.PerlinNoise((float)i / 16.0f, (float)j / 16.0f);
The thing is, the values, in Javascript work , the same values, but in c# they dont. I dont get why they wont work in c# but work in Js just fine. And yes, they are more than 0-1 but the JS ones were too but still returned values.
Out of curiosity, are you running this on the main-thread, or are you spawning a background thread?
Also, can you define what you mean by "they won't work in C#"?
Seems to not work on a background thread, I am using multithreading to generate the values in the background, I also tried it inside the main thread and it didnt work either. I mean by not working in c# as that I converted the code from JS to C# using the same values, it works in JS but not in C#.
You cannot use any Unity library on any other thread other than the main-thread. Weird shit happens if you try and use any Unity library function on a background thread.
Answer by Llockham-Industries · Dec 03, 2015 at 11:59 AM
Okay, You are declaring 2 ints in C#. Dividing ints will always result in ints.. ie. whole integers.. if you were running this on the primary thread it would likely be giving you errors telling you it requires floats
cast one of your int's to a float and it should resolve the issue..
The reason the same method works in unityscript is because unityscript uses ambiguous variables, that become whatever they need to be.. in this case, mathf.perlin noise requests a float, so the variables become floats..
as for whether or not it's thread safe.. i see no reason it wouldn't be.. but test it, unity will generally log an error something along the lines of..
!Thread::EqualsCurrentThreadID(m_MainThreadID)
if your using something that's strictly not safe.. it's just a fixed equation that returns a fixed result, it uses no variables outside it's own scope.. so you should be okay..
Answer by gameplaydev · Jun 20, 2014 at 04:38 PM
float a = Mathf.PerlinNoise(16+i/j,16+j/i);
Divide by zero?
Your answer
Follow this Question
Related Questions
Always same result from Mathf.PerlinNoise() 2 Answers
How to use Perlin Noise for waves 0 Answers
Perlin noise problem 0 Answers
Perlin Noise Issue 0 Answers