LibNoise, How to use it?

Okay, so I was trying to make a Procedually generated voxel terrain. So yesterday i found this library which i tried to implement but,I couldnt, If anyone could explain me on how to implement it or has a better way to complete my goal. I will thank them very much.

Any pointers, links, a base code to start off, or simple information is useful

Thank you

For future readers who find this question and want to use the LibNoise library for Unity, I have written C# scripts for the tutorial series on the LibNoise official 'site.

First step would be to grab the LibNoise library. I have posted a comment with the package I used when writing these scripts.

Follow the guide on the LibNoise site, then look at the code I have written. This shall hopefully give insight on how to use these classes for yourself.

For each script, I used a cube placed in the scene. When the noise is calculated, it generates a Texture2D which is applied to the cube renderer for an immediate visual output. Parameters can be modified in the inspector while playing the scene, then Left-Clicking in the scene window will update the texture and show on the cube.


Tutorial 3: Generating and rendering a terrain height map

//------------------------------//
//  Tutorial_3.cs               //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  http://libnoise.sourceforge.net/tutorials/tutorial3.html


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_3 : MonoBehaviour 
{
	public int mapSizeX = 256; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 256; // for heightmaps, this would be 2^n +1
	
	public float sampleSizeX = 4.0f; // perlin sample size
	public float sampleSizeY = 4.0f; // perlin sample size
	
	public float sampleOffsetX = 2.0f; // to tile, add size to the offset. eg, next tile across would be 6.0f
	public float sampleOffsetY = 1.0f; // to tile, add size to the offset. eg, next tile up would be 5.0f


	public Renderer cubeRenderer; // renderer texture set for testing

	private Texture2D texture; // texture created for testing
	
	
	//  Persistant Functions
	//	----------------------------------------------------------------------------
	

	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	void Generate() 
	{
		Perlin myPerlin = new Perlin();

		ModuleBase myModule = myPerlin;



		// ------------------------------------------------------------------------------------------
		
		// - Generate -
		
		// this part generates the heightmap to a texture, 
		// and sets the renderer material texture of a cube to the generated texture
		
		
		Noise2D heightMap;

		heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );

		heightMap.GeneratePlanar(
			sampleOffsetX,
			sampleOffsetX + sampleSizeX,
			sampleOffsetY,
			sampleOffsetY + sampleSizeY
			);

		texture = heightMap.GetTexture(GradientPresets.Grayscale);

		cubeRenderer.material.mainTexture = texture;
	}
}

Tutorial 4: Modifying the parameters of the noise module

//------------------------------//
//  Tutorial_4.cs               //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  http://libnoise.sourceforge.net/tutorials/tutorial4.html


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_4 : MonoBehaviour 
{
	public int mapSizeX = 256; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 256; // for heightmaps, this would be 2^n +1
	
	public float sampleSizeX = 4.0f; // perlin sample size
	public float sampleSizeY = 4.0f; // perlin sample size
	
	public float sampleOffsetX = 2.0f; // to tile, add size to the offset. eg, next tile across would be 6.0f
	public float sampleOffsetY = 1.0f; // to tile, add size to the offset. eg, next tile up would be 5.0f
	
	public int octaves = 6;

	public float frequency = 1.0f;
	
	public float persistence = 0.5f;


	public Renderer cubeRenderer; // renderer texture set for testing
	
	private Texture2D texture; // texture created for testing
	
	
	//  Persistant Functions
	//	----------------------------------------------------------------------------
	
	
	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	void Generate() 
	{
		Perlin myPerlin = new Perlin();

		// modify the number of octaves
		myPerlin.OctaveCount = octaves;

		// modify the frequency
		myPerlin.Frequency = frequency;
		
		// modify the persistence
		myPerlin.Persistence = persistence;


		ModuleBase myModule;
		
		myModule = myPerlin;



		// ------------------------------------------------------------------------------------------
		
		// - Generate -
		
		// this part generates the heightmap to a texture, 
		// and sets the renderer material texture of a cube to the generated texture


		Noise2D heightMap;

		heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );

		heightMap.GeneratePlanar(
			sampleOffsetX,
			sampleOffsetX + sampleSizeX,
			sampleOffsetY,
			sampleOffsetY + sampleSizeY
			);

		texture = heightMap.GetTexture( GradientPresets.Grayscale );

		cubeRenderer.material.mainTexture = texture;
	}
}

Tutorial 5: Generating more complex terrain

//------------------------------//
//  Tutorial_5.cs               //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  http://libnoise.sourceforge.net/tutorials/tutorial5.html


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_5 : MonoBehaviour 
{
	public int mapSizeX = 256; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 256; // for heightmaps, this would be 2^n +1
	
	public float sampleSizeX = 4.0f; // perlin sample size
	public float sampleSizeY = 4.0f; // perlin sample size
	
	public float sampleOffsetX = 6.0f; // to tile, add size to the offset. eg, next tile across would be 6.0f
	public float sampleOffsetY = 1.0f; // to tile, add size to the offset. eg, next tile up would be 5.0f


	public Renderer cubeRenderer; // renderer texture set for testing
	
	private Texture2D texture; // texture created for testing


	//  Persistant Functions
	//	----------------------------------------------------------------------------
	
	
	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	public float baseflatFrequency = 2.0f;
	
	public float flatScale = 0.125f;
	public float flatBias = -0.75f;
	
	public float terraintypeFrequency = 0.5f;
	public float terraintypePersistence = 0.25f;

	public float finalTerrainEdgeFalloff = 0.125f;

	
	void Generate() 
	{
		// - Mountain Terrain -

		//module::RidgedMulti mountainTerrain;

		RidgedMultifractal mountainTerrain = new RidgedMultifractal();



		// - Base Flat Terrain -

		//module::Billow baseFlatTerrain;
		//baseFlatTerrain.SetFrequency (2.0);
		
		Billow baseFlatTerrain = new Billow();

		baseFlatTerrain.Frequency = baseflatFrequency;



		// - Flat Terrain -
		
		//module::ScaleBias flatTerrain;
		//flatTerrain.SetSourceModule( 0, baseFlatTerrain );
		//flatTerrain.SetScale (0.125);
		//flatTerrain.SetBias (-0.75);
		
		ScaleBias flatTerrain = new ScaleBias( flatScale, flatBias, baseFlatTerrain ); // scale, bias, input
		
		
		
		// - Terrain Type -

		//module::Perlin terrainType;
		//terrainType.SetFrequency (0.5);
		//terrainType.SetPersistence (0.25);

		Perlin terrainType = new Perlin();

		terrainType.Frequency = terraintypeFrequency;
		terrainType.Persistence = terraintypePersistence;
		
		
		
		// - Final Terrain -

		//module::Select finalTerrain;
		//finalTerrain.SetSourceModule (0, flatTerrain);
		//finalTerrain.SetSourceModule (1, mountainTerrain);

		Select finalTerrain = new Select( flatTerrain, mountainTerrain, terrainType ); // input A, input B, Controller


		// finalTerrain.SetBounds (0.0, 1000.0);
		finalTerrain.SetBounds( 0.0, 1000.0 );

		//finalTerrain.SetEdgeFalloff (0.125);
		finalTerrain.FallOff = finalTerrainEdgeFalloff;



		// ------------------------------------------------------------------------------------------
		
		// - Compiled Terrain -

		ModuleBase myModule;

		// each of these are used in each chapter after the above variables are declared and assigned
		//myModule = mountainTerrain; // testing base flat terrain (Generating the rough mountainous terrain)
		//myModule = baseFlatTerrain; // testing base flat terrain (Generating the base values for the flat terrain)
		//myModule = flatTerrain; // testing flat terrain (Generating the flat terrain)
		//myModule = terrainType; // testing terrain type (Generating the terrain-type map)
		myModule = finalTerrain; // testing final terrain (Generating the final terrain)



		// ------------------------------------------------------------------------------------------

		// - Generate -

		// this part generates the heightmap to a texture, 
		// and sets the renderer material texture of a cube to the generated texture
		// (this part never changes, always here since the start of the tutorial page) 
		// (only myModule changes, so this part remains the same)

		
		Noise2D heightMap;
		
		heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );
		
		heightMap.GeneratePlanar(
			sampleOffsetX,
			sampleOffsetX + sampleSizeX,
			sampleOffsetY,
			sampleOffsetY + sampleSizeY
			);
		
		texture = heightMap.GetTexture(GradientPresets.Grayscale);
		
		cubeRenderer.material.mainTexture = texture;
	}
}

Tutorial 6: Adding realism with turbulence

//------------------------------//
//  Tutorial_6.cs               //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  http://libnoise.sourceforge.net/tutorials/tutorial6.html


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_6 : MonoBehaviour 
{
	public int mapSizeX = 256; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 256; // for heightmaps, this would be 2^n +1
	
	public float sampleSizeX = 4.0f; // perlin sample size
	public float sampleSizeY = 4.0f; // perlin sample size
	
	public float sampleOffsetX = 6.0f; // to tile, add size to the offset. eg, next tile across would be 6.0f
	public float sampleOffsetY = 1.0f; // to tile, add size to the offset. eg, next tile up would be 5.0f


	public Renderer cubeRenderer; // renderer texture set for testing
	
	private Texture2D texture; // texture created for testing


	//  Persistant Functions
	//	----------------------------------------------------------------------------
	
	
	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	public float baseflatFrequency = 2.0f;
	
	public float flatScale = 0.125f;
	public float flatBias = -0.75f;
	
	public float terraintypeFrequency = 0.5f;
	public float terraintypePersistence = 0.25f;

	public float terrainSelectorEdgeFalloff = 0.125f;
	
	public float finalterrainFrequency = 4.0f;
	public float finalterrainPower = 0.125f;

	
	void Generate() 
	{
		// - Mountain Terrain -

		//module::RidgedMulti mountainTerrain;

		RidgedMultifractal mountainTerrain = new RidgedMultifractal();



		// - Base Flat Terrain -

		//module::Billow baseFlatTerrain;
		//baseFlatTerrain.SetFrequency (2.0);
		
		Billow baseFlatTerrain = new Billow();

		baseFlatTerrain.Frequency = baseflatFrequency;



		// - Flat Terrain -
		
		//module::ScaleBias flatTerrain;
		//flatTerrain.SetSourceModule( 0, baseFlatTerrain );
		//flatTerrain.SetScale (0.125);
		//flatTerrain.SetBias (-0.75);
		
		ScaleBias flatTerrain = new ScaleBias( flatScale, flatBias, baseFlatTerrain ); // scale, bias, input
		
		
		
		// - Terrain Type -

		//module::Perlin terrainType;
		//terrainType.SetFrequency (0.5);
		//terrainType.SetPersistence (0.25);

		Perlin terrainType = new Perlin();

		terrainType.Frequency = terraintypeFrequency;
		terrainType.Persistence = terraintypePersistence;
		
		
		
		// - Terrain Selector -

		//module::Select terrainSelector;
		//terrainSelector.SetSourceModule (0, flatTerrain);
		//terrainSelector.SetSourceModule (1, mountainTerrain);

		Select terrainSelector = new Select( flatTerrain, mountainTerrain, terrainType ); // input A, input B, Controller


		// terrainSelector.SetBounds (0.0, 1000.0);
		terrainSelector.SetBounds( 0.0, 1000.0 );

		//terrainSelector.SetEdgeFalloff (0.125);
		terrainSelector.FallOff = terrainSelectorEdgeFalloff;
		
		
		
		// - Final Terrain -

		//module::Turbulence finalTerrain;
		//finalTerrain.SetSourceModule (0, terrainSelector);

		Turbulence finalTerrain = new Turbulence( terrainSelector );
		
		//finalTerrain.SetFrequency (4.0);
		finalTerrain.Frequency = finalterrainFrequency;
		
		//finalTerrain.SetPower (0.125);
		finalTerrain.Power = finalterrainPower;




		// ------------------------------------------------------------------------------------------
		
		// - Compiled Terrain -

		ModuleBase myModule;

		myModule = finalTerrain;



		// ------------------------------------------------------------------------------------------

		// - Generate -

		// this part generates the heightmap to a texture, 
		// and sets the renderer material texture of a cube to the generated texture

		
		Noise2D heightMap;
		
		heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );
		
		heightMap.GeneratePlanar(
			sampleOffsetX,
			sampleOffsetX + sampleSizeX,
			sampleOffsetY,
			sampleOffsetY + sampleSizeY
			);
		
		texture = heightMap.GetTexture(GradientPresets.Grayscale);
		
		cubeRenderer.material.mainTexture = texture;
	}
}

Tutorial 7 : Using the data and applying it to terrain data heights

This is not part of the series, but something I wrote to demonstrate how to get the height data and apply it to a terrain.

//------------------------------//
//  Tutorial_7.cs               //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  using information from data to apply to terrain heightmap


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_7 : MonoBehaviour 
{
	public int mapSizeX = 513; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 513; // for heightmaps, this would be 2^n +1
	
	public float sampleSizeX = 4.0f; // perlin sample size
	public float sampleSizeY = 4.0f; // perlin sample size
	
	public float sampleOffsetX = 6.0f; // to tile, add size to the offset. eg, next tile across would be 6.0f
	public float sampleOffsetY = 1.0f; // to tile, add size to the offset. eg, next tile up would be 5.0f


	public Terrain terrain; // terrain to apply values to heightmap


	//  Persistant Functions
	//	----------------------------------------------------------------------------
	
	
	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	public float baseflatFrequency = 2.0f;
	
	public float flatScale = 0.125f;
	public float flatBias = -0.75f;
	
	public float terraintypeFrequency = 0.5f;
	public float terraintypePersistence = 0.25f;

	public float terrainSelectorEdgeFalloff = 0.125f;
	
	public float finalterrainFrequency = 4.0f;
	public float finalterrainPower = 0.125f;

	
	void Generate() 
	{
		// - Mountain Terrain -

		//module::RidgedMulti mountainTerrain;

		RidgedMultifractal mountainTerrain = new RidgedMultifractal();



		// - Base Flat Terrain -

		//module::Billow baseFlatTerrain;
		//baseFlatTerrain.SetFrequency (2.0);
		
		Billow baseFlatTerrain = new Billow();

		baseFlatTerrain.Frequency = baseflatFrequency;



		// - Flat Terrain -
		
		//module::ScaleBias flatTerrain;
		//flatTerrain.SetSourceModule( 0, baseFlatTerrain );
		//flatTerrain.SetScale (0.125);
		//flatTerrain.SetBias (-0.75);
		
		ScaleBias flatTerrain = new ScaleBias( flatScale, flatBias, baseFlatTerrain ); // scale, bias, input
		
		
		
		// - Terrain Type -

		//module::Perlin terrainType;
		//terrainType.SetFrequency (0.5);
		//terrainType.SetPersistence (0.25);

		Perlin terrainType = new Perlin();

		terrainType.Frequency = terraintypeFrequency;
		terrainType.Persistence = terraintypePersistence;
		
		
		
		// - Terrain Selector -

		//module::Select terrainSelector;
		//terrainSelector.SetSourceModule (0, flatTerrain);
		//terrainSelector.SetSourceModule (1, mountainTerrain);

		Select terrainSelector = new Select( flatTerrain, mountainTerrain, terrainType ); // input A, input B, Controller


		// terrainSelector.SetBounds (0.0, 1000.0);
		terrainSelector.SetBounds( 0.0, 1000.0 );

		//terrainSelector.SetEdgeFalloff (0.125);
		terrainSelector.FallOff = terrainSelectorEdgeFalloff;
		
		
		
		// - Final Terrain -

		//module::Turbulence finalTerrain;
		//finalTerrain.SetSourceModule (0, terrainSelector);

		Turbulence finalTerrain = new Turbulence( terrainSelector );
		
		//finalTerrain.SetFrequency (4.0);
		finalTerrain.Frequency = finalterrainFrequency;
		
		//finalTerrain.SetPower (0.125);
		finalTerrain.Power = finalterrainPower;




		// ------------------------------------------------------------------------------------------
		
		// - Compiled Terrain -

		ModuleBase myModule;

		myModule = finalTerrain;



		// ------------------------------------------------------------------------------------------

		// - Generate -


		Noise2D heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );
		
		heightMap.GeneratePlanar(
			sampleOffsetX,
			sampleOffsetX + sampleSizeX,
			sampleOffsetY,
			sampleOffsetY + sampleSizeY
			);

		// both of these will return normalized values
		//float[,] heights = heightMap.GetData( true, 0, 0, true );
		float[,] heights = heightMap.GetNormalizedData( true, 0, 0 );
		
		// NOTE :
		// terrainData HeightMapData is actually [ y, x ]
		// so tiling is done in the Z-axis for X, and X-axis for Y
		// you could loop through x and y of heights
		// and return correctly sorted values using
		// heights[ y, x ] = heightMap[ x, y, true ];


		TerrainData terrainData = terrain.terrainData;

		terrainData.SetHeights( 0, 0, heights );
	}
}

Tutorial 8: Creating spherical planetary terrain

For this one, use a sphere instead of a cube :slight_smile:

//------------------------------//
//  Tutorial_8.cs              //
//  Written by Alucard Jay      //
//  2014/6/29                   //
//------------------------------//

//  http://libnoise.sourceforge.net/tutorials/tutorial8.html


using UnityEngine;
using System.Collections;

using LibNoise.Unity;
using LibNoise.Unity.Generator;
using LibNoise.Unity.Operator;


public class Tutorial_8 : MonoBehaviour 
{
	public int mapSizeX = 512; // for heightmaps, this would be 2^n +1
	public int mapSizeY = 256; // for heightmaps, this would be 2^n +1
	
	public float south = -90.0f;
	public float north = 90.0f;
	
	public float west = -180.0f;
	public float east = 180.0f;
	
	
	public Renderer sphereRenderer; // renderer texture set for testing
	
	private Texture2D texture; // texture created for testing

	
	
	//  Persistant Functions
	//	----------------------------------------------------------------------------
	
	
	void Start() 
	{
		Generate();
	}
	
	
	void Update() 
	{
		if ( Input.GetMouseButtonDown(0) )
			Generate();
	}
	
	
	//  Other Functions
	//	----------------------------------------------------------------------------
	
	
	void Generate() 
	{
		Perlin mySphere = new Perlin();
		

		// ------------------------------------------------------------------------------------------
		
		// - Compiled Terrain -
		
		ModuleBase myModule;
		
		myModule = mySphere;

		
		// ------------------------------------------------------------------------------------------
		
		// - Generate -
		
		// this part generates the heightmap to a texture, 
		// and sets the renderer material texture of a sphere to the generated texture
		
		
		Noise2D heightMap;
		
		heightMap = new Noise2D( mapSizeX, mapSizeY, myModule );
		
		heightMap.GenerateSpherical( south, north, west, east );

		
		texture = heightMap.GetTexture(GradientPresets.Grayscale);
		
		sphereRenderer.material.mainTexture = texture;
	}
}

I hope these scripts have been of some help =]


You can also get tutorials for this at github:

Hey guys, when I try out the script, it says GradientPresets does not exist in current context. Has it been changed?