Java Noise (JNoise?)

Hello. I just wanted to share this work I have been doing for the last couple of weeks with the community. I have always been running around here asking alot of questions, and I thought it was about time I gave something back.

Please note that I did not generate all of this code, rather it is a culmination from various sources across the internet who have given tutorials on how to implement these specific noise functions (albeit I did implement voronoi myself)

So, in the quest to create procedurally generated terrain, I decided I needed to delve into the depths of Procedural Noise functions. This led me across the internet to discover many types of noise which I bring here today for all your procedural noise needs.

The types of noises I have ported/implemented include perlin, fractal browinian motion, ridgid multi fractal, simplex, and multiple voronoi types (just uncomment different distance calculations in voronoi.java)

Here is a picture of some of the results I have achieved with these functions:



Also, here is a culmination of Voronoi + ( rmf - fbm)



So how does this work?

The source includes a functions package which holds the noise functions, while the test package holds a Test_Noise and TestTerrain classes.

By changing the noise calculations in the for() loops, your function will be written to an image file.

By copying this function to Noise.java (overwritting the functions found on line 75 - note this gives TestTerrain its height data) your functions will be realized in 3d using terrain pages.

Here is an example of one of these realized functions:



If anyone has any questions about how this works, or has any suggestions, feel free to ask and I will be happy to assist.

Who knows, maybe this could even be the beginning of a standard Java Noise Library or something :stuck_out_tongue:

Have fun!



EDIT 7/24/11:

Only use Simplex Noise for education purposes as it is patented.

______

Source attached:

1 Like

I was just about to ask how this was different from this perlin noise class, but look who posted it ;D I see you took it a couple steps further, nice!



So are there any plans for a jMonkeyPlatform plugin? Maybe see if there’s a way you could incorporate this into the WIP jME3 Terrain System.

This is pretty cool and looks quite nice! We could combine it with some hydraulic erosion to make some very realistic looking terrain.

erlend_sh :



If the terrain engine reads in a floatbuffer like the current terrain page system to represent data from a heightmap, then it shouldnt be a problem to incorporate into another terrain system. It was really easy to get my heightmap data into the terrain page.



As far as a plugin goes, I am not opposed, however it is so small right now that anyone could easily plop it into their projects if they want. Currently it fits my needs for attempting to implement geomipmapping, but for further use I may need to take it even farther, and I will be happy to share as I go.





Sploreg : you are absolutely correct, however my purposes will not work with that much pre-processing. :slight_smile:

It sure would be fun to model though.

This looks very interesting, I’d like to test this but file attachments are not there anymore? Eggsworth, any chance you could share some of code with me? thanks in advance :slight_smile: I sent you pm btw

very interesting

could you provide sources?

hi guys.



yes i still have the source, i will get it ready to go and reattach it.



I cant upload it right now from this location, but i am just letting you know that i am aware of your request and will share the source soon :slight_smile:



i am glad people are interested



EDIT: Source located at

http://www.mediafire.com/?99ua6ya0y251o4p

oh, I see what you did there, I was expecting another post for source so I did not check the thread but I see you edit it :wink: thanks man, very cool what you did with this stuff :slight_smile:

yeah, a noise libary would be an epic idea :slight_smile:

thanks for the code

(Sorry to semi-necro this)



I thought you really saved my life there with this lib.



Actually, i have some questions…



Most of the time, the Lacunarity value doesn’t matter. I can set it to 0, or -1 or 10000 and i still get the same output.



Also, I don’t have the skills to understand each noise function specifically, but i’m having a serious problem. Seems some functions “start over” when they reach a specific number. IE - when getting x = 0 y = 0 of the RMF function, i get the same thing as x = 200 and y = 200.



Also, did the noise function became part of the Engine since Alpha 3, or should i continue to tweak these on my side?



[edit] Oh yeah, maybe i should add this:

I tweaked your functions so i could use the same seed (to explain why i’m getting the same thing in x=0 and x=200). Other than that, i added in most functions the amplitude and frequency arguments, so i could play with these, but otherwise i didnt change any of the math calculations.

well you do need to remember that these functions are not infinite and will get boring or repeat after some certain values. Some of the functions are best to increase by 1, others by 0.01f, so you need to play with them and see.



I have to say that I have gotten them all to produce unique and interesting results, so if you have any questions about any specific noise functions just ask and I will let you know some arguments ive used.



Also note, they are not a part of the JME engine, although I am not stopping anyone from doing so.

Thanks for the great contribution. I’ve been working on a massive terrain renderer, and my fractal code is pretty crappy. Now I can just use a stack of these functions rather than a fractal approach… Thanks again.



I’m going to make a few modifications. I’m going to put all the tweak parameters in the constructor, so that all classes derive from a single interface with a single 2d and 3d noise function (where the default z is just a tweak parameter for the 2d function). Then I’ll standardize the tweak parameters as much as possible (Transform for input, scale and add for output, seed value). Then I can make a SummedNoise class that contains a list of Noise instances, and simply executes the noise function for each. The cool thing is, it can implement the same interface as the rest of the Noise functions, allowing the Noise interface to be a parameter to my terrain loader, and it just calls the noise function for every pixel. This can be as simple as a single Simplex noise call, or 3 octaves of Perlin noise modifying a Voronoi peak generator, with a little fbm on top for flavor.



When I get a chance to create an editor for all this, I can imagine an interface that allows you to set up the tweak parameters with a set of sliders, and a list of these for a SummedNoise box. Maybe I could even put in the concept that you can have different ranges of influence for different function sets, so you can have a rough and rocky part of your world in one area and smooth plains in another area, all specified by less than 1k of parameters. Each range of influence should probably be specified by a central point, a falloff start distance and a falloff end distance. Hmm, that seems a little restrictive to limit everything to circles. Maybe a transformed circle will be good enough (an arbitrarily oriented ellipse). Hmmm, if I include a way to link the same parameter set to multiple influence ranges, then you can just splat circles around until you cover the area you want. Kind of a material/brush idea.



This is going to be a fun project, thanks again.

Awesome that sounds like a really cool idea!



If you plan on having it real-time rendering though, be careful because if you stack to many noise functions, it will increase the time to generate the y value of each vertex.



Good luck !!!

Oh i thought every procedural functions were infinite… Isn’t it supposed to be case? Is there a way to change them to be infinite?



I know some functions can be increased by .1 for different result, although it does simply “zoom” in on the function.



Oh by the way, i came up with pretty interesting stuff playing with these, that’s not the problem :wink: I wanted to generate infinite landscape, but now i’ll have to revise a few things, i guess.



Oh yeah, one last question: Is the Voronoi tilable? I can’t seem to get past the original output (if you see the attached image:). Sorry for the image size! :S



The voronoi implementation seems to have some issues, one of which is the one you mention. I’m writing a new implementation of it for my library, complete with a selector to choose which of the voronoi functions to return. With any luck, it will be finished this afternoon.

yeah i didnt make the voroni very well. It was more of a way to see if I could actually do it, and it worked perfectly for what I needed - a finite grid of voronoi noise. It wont work for infinite space and thats what you are running in to.



here is some discussion about the infinity of noise functions - specifically perlin. Might be a good read for you guys: http://www.gamedev.net/community/forums/topic.asp?topic_id=487457

Here’s a video of a practically infinite terrain (8192 km^2) generated by my modifications to this package. The terrain engine obviously still has a lot of work to go, but it functions as a good picture of what the noise functions output.



Terrain Noise on Youtube

http://www.youtube.com/watch?v=0iohG1WK9ZY



The stuttering every second or two is from fraps, not the engine. Laptop hard drives are not that great at streaming writes.



Here’s the source for the terrain engine and all. Don’t laugh at the terrain code, it’s still got a ways to go, but the Noise stuff is pretty solid.



Source Code



It’s a jMP project, so you should be able to play with it in jMP directly.



The structure of the Noise classes is a little different than my last post. I realized that there are really 2 concepts, Basis Functions and Noise. Noise is some basis function sampled at multiple octaves, possibly in a multifractal manner (where the output of the previous octave affects the next). So, I created a Noise interface that just has “float noise(float x, float y, float z)” and “float noise(float x, float y)”, where the latter is expected to use optimizations specific to 2D queries. Then BasicNoise implements this interface, and has all the options that are common to noise functions (octaves, lacunarity, gain, input transformation, output transformation, basis, etc.).



Then I created the Basis abstract class that has a “float get(float x, float y, float z)” and a “float get(float x, float y)”. These are to be overriden by child classes, and should provide noise in the range -1 to 1. The input is pre-transformed before this function is called, and the output is transformed afterwards, so the basis classes can focus on generating a basis.



Two interesting Basis classes are UnaryOperator and BinaryOperator. These are abstract classes that extend Basis, implement the get methods, and provide abstract “float operate(float value)” and “float operate(float valA, float valB)”. These are designed to operate on one or two bases before returning them to the noise class. For example, the Ridge function is a UnaryOperator which takes the value returned by a basis, takes 1 - it’s absolute value, and squares that result to turn a nice smooth basis into strong peaks.



Finally, there is another class which implements the Noise interface (not Basis). This one, called SummedNoise, takes the output of several noise functions and adds them together.



As an example: here is the code which generated the terrain in the movie above:

[java]

Basis basis = new Ridge(new Simplex(System.nanoTime()));

Transform rmft = new Transform(); rmft.setScale(1.f/4.f);

BasicNoise ridgedMF = new BasicNoise(rmft, 32768f, 0.0f, System.nanoTime(), 3, 2.0f, 0.5f, true, basis);



Transform simt = new Transform(); simt.setScale(1.f/16.f);

BasicNoise simplex = new BasicNoise(simt, 8192f, 0f, System.nanoTime(), 10, 5f, 0.35f, false, new Simplex(System.nanoTime()));



SummedNoise noise = new SummedNoise(ridgedMF, simplex);



TerrainLoader loader = new TerrainLoader(patchSize, worldSize, yScale, noise);

[/java]



Explanation:



[java]

Basis basis = new Ridge(new Simplex(System.nanoTime()));

Transform rmft = new Transform(); rmft.setScale(1.f/4.f);

BasicNoise ridgedMF = new BasicNoise(rmft, 32768f, 0.0f, System.nanoTime(), 3, 2.0f, 0.5f, true, basis);

[/java]



This is a Ridged Multifractal noise function. The Simplex function (passed into the Ridge constructor) is a standard bumpy, continuous noise function similar to Perlin noise. The Ridge function takes the Simplex function, absolute values it, flips it upside down, and squares it. Now any time you call basis.get(x,y), you’ll generate values in the -1 to 1 range that look like a Ridged Simplex noise function. The transform on the next line transforms the x/y values given to the basis function by the terrain. By default, the terrain gives values such that the northwest corner of the entire 8192 km landscape is represented as 0, 0, and the southeast corner is 256,256. Noise functions are expected to only repeat after 256 units, so this should make the whole world unique. However, I scale the coordinates given to the function by 1/4, so the range it sees is 0-64. There is still plenty of room in the function for more unique landscape. Finally, I create a BasicNoise based on this Ridged Simplex function we just created, set the input transform, scale the output values by 32768, offset them by 0.0, give it a seed based on the clock, ask for 3 octaves of this noise, set lacunarity to 2, gain to 0.5, ask for a multifractal summing of the octaves, and give it the Ridged Simplex basis, resulting in a standard Ridged Multifractal noise function.



For those wondering, lacunarity is a big word for how much we scale the coordinates by each octave. It determines how much you fill in the gaps between the higher octaves of the function. If your noise is too noisy, play with the lacunarity (along with the initial scale and output scale) to adjust. Gain tells you how much to scale the output by for each octave. It should be less than 1 or values will begin to blow up.





[java]

Transform simt = new Transform(); simt.setScale(1.f/16.f);

BasicNoise simplex = new BasicNoise(simt, 8192f, 0f, System.nanoTime(), 10, 5f, 0.35f, false, new Simplex(System.nanoTime()));

[/java]



This function is simpler, it just creates a transform to scale the world even smaller, so you get wider rolling hills in the first few octaves, and the high frequency noise doesn’t show up until you get to higher octaves where the scale is low. Then it creates a BasicNoise which uses this input transform, scales the output by 8192 (basically, we’re picking up on the next octave where the Ridge MF function left off), no offset to the output, a seed value, asks for 10 octaves, 5 lacunarity, 0.35 gain, no multifractal, and a basic Simplex basis.



[java]

SummedNoise noise = new SummedNoise(ridgedMF, simplex);



TerrainLoader loader = new TerrainLoader(patchSize, worldSize, yScale, noise);

[/java]



Finally, I created a SummedNoise which just adds the two noise functions together.



The TerrainLoader has a few details as far as what size the patches is should produce are, how big the world is, how much the height values it returns will be scaled (this is an artifact of when the height values were all shorts, and we needed a way to represent height values at non-integer heights, it will probably be removed), and finally the noise function. The TerrainLoader just converts the world coordinates of the terrain into the range 0-256, and asks for the height value, returning a heightmap and normal map to the terrain. It will probably use more noise functions later on to determine the splat patterns to use and such, but it creates the terrain fast enough, even with the camera moving at 100m/s so far. And that’s with a total of 13 octaves of simplex noise, not bad.



David

1 Like

Eggsworth, this is really awesome stuff. I’m just starting out learning about procedural techniques and have been looking for a good (fast, reasonably well distributed) function for producing a random sequence given a set of coordinates.



Your Perlin.findnoise2 method is almost exactly what I’m after - it produces well distributed results very quickly. I’ve seen this algorithm before (Hugo Elias’s excellent page on Perlin Noise) and was wondering if you, or anyone, know what this algorithm is called? I’d like to understand more clearly the constant values being used.



Thanks in advance,


  • Andy.

Simplex Noise is patented (US Patent #6867776), dont use it! This has really bit me, since I have many dependencies that now have to be rewritten.

lex, good catch



After looking at the cover sheet for that patent, if you do not want to use anything patented, then I would shy away from the Perlin.java and Improved.java classes, because they are based upon Ken Perlin’s formulas (note he is the patent holder).



For the functions in this library I did not write, I amassed them off the internet, some of which even came from Ken Perlin’s supposed personal blog page

http://mrl.nyu.edu/~perlin/noise/



which begs the question if he didnt want you using it, would he post the source code on a NYU website.



(Sorry for the delay of response I just decided to dive back into this stuff again for the weekend)