Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LibNoise Ported to Unity

Discussion in 'Made With Unity' started by pakfront, Nov 27, 2010.

  1. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Ported LibNoise noise library from LibNoise.Xna to Unity. Like those two, it is released under the LGPL so it can be used and modified pretty much however you like.

    Generates Perlin, RiggedMultifractal,Voronoi and Billow noises and has Operators for combining them. Noise2D is a convenience class for creating Texture2D from noises.

    Now also available from github, thanks to Arges:
    https://github.com/ricardojmendez/LibNoise.Unity


    Simple Demo scene included in attached Unitypackage.

    View attachment $LibNoiseUnity.unitypackage
     
    Last edited: Dec 19, 2010
  2. reissgrant

    reissgrant

    Joined:
    Aug 20, 2009
    Posts:
    726
    Nice work!
     
  3. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Thanks! Wanted to do this myself but you made it first.
     
  4. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    I've found so many great free resources for unity. This project was an easy karma builder and I learned a bunch in the process. Let me know if any issues crop up.
     
  5. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    Great job, not much need for it right now (can get away with my own poor perlin) but I'm sure I'll use it in the future.
     
  6. Pixero

    Pixero

    Joined:
    Dec 10, 2009
    Posts:
    130
    When trying the demo I get :

    Assets/Demo.cs(85,18): error CS0117: `System.IO.File' does not contain a definition for `WriteAllBytes'
     
  7. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Hmm, What Unity version?

    for the moment you can just disable to write to PNG code in there.
     
  8. Pixero

    Pixero

    Joined:
    Dec 10, 2009
    Posts:
    130
    3.1.0.f4 Windows 7 64 bit
     
  9. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Did you get it to work by disabling the write to PNG?
     
  10. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    Last edited: Dec 12, 2010
  11. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Hmm, not sure. Maybe write a Clamp module?
     
  12. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    Code (csharp):
    1. var island_p =  Spheres(0.20);
    2.     var const_sub_p = Const(-0.4); //Distance to edge.  Smaller number makes it closer to the edge
    3.     var const_mult_p = Const(5.0); //falloff
    4.     var moduleBase = new Add(island_p, const_sub_p);
    5.     var islandmult_p  = new Multiply(moduleBase, const_mult_p);
    6.     var m_noiseMap = new Noise2D(512, 512, islandmult_p);
    If I run into the answer I'll edit this post. But can I recycle the variables? moduleBase = new Multiply(yada,yada); throws an error
    Code (csharp):
    1. BCE0022: Cannot convert 'LibNoise.Unity.Operator.Multiply' to 'LibNoise.Unity.Operator.Add'.
    edit: post below solves it. thanks!
     
    Last edited: Dec 14, 2010
  13. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    If you explicitly type moduleBase as ModuleBase it should work. I don't know uscript, but something like:
    Code (csharp):
    1. var moduleBase : ModuleBase = new Add(island_p, const_sub_p);
    2. //do stuff here
    3. moduleBase = new Multiply(island_p, const_sub_p);
     
    twobob likes this.
  14. Arges

    Arges

    Joined:
    Oct 5, 2008
    Posts:
    359
  15. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Could you tell me what is the main difference between your xna port and this http://libnoisedotnet.codeplex.com/ one?

    The one from the link works as is without any effort. Still id like to know if theres anything different in yours.
     
  16. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    Working with this again, I'm trying to use Scale but I can't find out how to use it.

    var terrainbase : ModuleBase = Select(type_min, type_max, type_falloff, p_mountain_p, p_flatlandmod_p);

    It requires 2 input and 1 controller - how do I add the other input to it?
     
  17. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    Same here... I'm looking through the source code to figure this out...

    It seems to be an oversight I believe... if you look through the Select class, you'll see that the constructor maps the input module to m_modules[0], and the 'controller' to m_modules[1]... now, the Controller property maps the given value to m_modules[2], so I believe we are supposed to pass both input modules to the constructor, and set the control module through it's property... Even though the contructor says the second module is the controller...

    I haven't tried it yet, but let's see how it goes... I'll post back with any news :)

    BTW, great work on this port!! my terrain is looking infinitely better now :)

    Cheers

    EDIT: Got it to work!! the constructor indeed has those parameters mislabeled, and there is one other thing:

    the Select constructor is calling it's base class with only 2 module "slots", so I just tweaked the constructor to pass 3 to the base call... (you gotta do this, or else you get an index out of range exception) :)

    Anyways, it works!! thanks a million for the port!!

    Cheers again!
     
    Last edited: Jan 28, 2011
  18. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Probably very little, except I added some convenience methods for handling Unity data types like Texture2D.

    Can you post the exact code fixes so we can wrap them into the git repo? OR even do it yourself if you are comfortable with it.
     
  19. CorruptScanline

    CorruptScanline

    Joined:
    Mar 26, 2009
    Posts:
    217
    Im having a problem with the Perlin class. I create a new Perlin() and call GetValue(x,y,z) but the value is always 0. Is there something im missing?
     
  20. LeadCarbonate

    LeadCarbonate

    Joined:
    Mar 10, 2011
    Posts:
    18
    Thank you very much for this port. It's been a great help. Cheers.
     
  21. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    Thanks for package.. digging in.
     
  22. Ardan

    Ardan

    Joined:
    Oct 5, 2009
    Posts:
    68
    no matter what seed I pun in a noise, I always get the same results.
     
    Last edited: May 15, 2011
  23. MarkPixel

    MarkPixel

    Joined:
    Apr 15, 2010
    Posts:
    39
    How can it be animated?
     
  24. robert_d

    robert_d

    Joined:
    Dec 6, 2011
    Posts:
    16
    This error appears in web player projects (it doesn't appear in desktop projects) because web player .Net platform doesn't have this function.
     
    Last edited: Dec 6, 2011
    twobob likes this.
  25. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    Needed to make an island - best way is to start with a circle and (generally) use turbulence on it. So I modified the Cylinders generator to make this.
    Circles(radius, boost)
    boost is a multiplier. Higher = sharper edge.
    This forms one circle only. Unaffected by height.
    Was not used to c#, hence poor code in last function.

    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6. using System.Collections;
    7.  
    8. namespace LibNoise.Unity.Generator
    9. {
    10.     /// <summary>
    11.     /// Provides a noise module that outputs concentric Circles. [GENERATOR]
    12.     /// </summary>
    13.     public class Circles : ModuleBase
    14.     {
    15.         #region Fields
    16.  
    17.         private double m_radius = 0.5;
    18.         private double m_boost = 3.0;
    19.         #endregion
    20.  
    21.         #region Constructors
    22.  
    23.         /// <summary>
    24.         /// Initializes a new instance of Circles.
    25.         /// </summary>
    26.         public Circles()
    27.             : base(0)
    28.         {
    29.         }
    30.  
    31.         /// <summary>
    32.         /// Initializes a new instance of Circles.
    33.         /// </summary>
    34.         /// <param name="radius">The radius of the concentric Circles.</param>
    35.         public Circles(double radius, double boost)
    36.             : base(0)
    37.         {
    38.             this.Radius = radius;
    39.             this.Boost = boost;
    40.         }
    41.  
    42.         #endregion
    43.  
    44.         #region Properties
    45.  
    46.         /// <summary>
    47.         /// Gets or sets the radius of the concentric Circles.
    48.         /// </summary>
    49.         public double Radius
    50.         {
    51.             get { return this.m_radius; }
    52.             set { this.m_radius = value; }
    53.         }
    54.         public double Boost
    55.         {
    56.             get { return this.m_boost; }
    57.             set { this.m_boost = value; }
    58.         }
    59.         #endregion
    60.  
    61.         #region ModuleBase Members
    62.  
    63.         /// <summary>
    64.         /// Returns the output value for the given input coordinates.
    65.         /// </summary>
    66.         /// <param name="x">The input coordinate on the x-axis.</param>
    67.         /// <param name="y">The input coordinate on the y-axis.</param>
    68.         /// <param name="z">The input coordinate on the z-axis.</param>
    69.         /// <returns>The resulting output value.</returns>
    70.         public override double GetValue(double x, double y, double z)
    71.         {
    72.         //This is 2D only.  Disregard Y
    73.  
    74.             Vector2 v =  Vector2.zero;
    75.             v.x = (float)(x);
    76.             v.y = (float)(z);
    77.             double dist = Vector2.Distance( v, Vector2.zero);
    78.             return (m_radius - dist) / m_radius * m_boost;
    79.         }
    80.         #endregion
    81.     }
    82. }
    View attachment $Circles.cs
     
  26. TheEnigmist

    TheEnigmist

    Joined:
    Jun 10, 2012
    Posts:
    51
    There is some1 can give me a demo scene?
     
  27. vatara

    vatara

    Joined:
    Feb 26, 2010
    Posts:
    22
    I'm not sure if this is maintained anymore, but I submitted a change on the github and I figured I should post it here as well. These lines in Curve.cs:
    Code (csharp):
    1. double ip0 = this.m_data[i1].Value;
    2. double ip1 = this.m_data[i2].Value;
    Should be:
    Code (csharp):
    1. double ip0 = this.m_data[i1].Key;
    2. double ip1 = this.m_data[i2].Key;
     
  28. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    When I import this package into Unity, I get three errors just trying to parse the script in the editor:

    Code (csharp):
    1.  
    2. Assets/Demo.cs(53,53): error CS1502: The best overloaded method match for `LibNoise.Unity.Operator.Add.Add(LibNoise.Unity.ModuleBase, LibNoise.Unity.ModuleBase)' has some invalid arguments
    3.  
    4. Assets/Demo.cs(53,53): error CS1503: Argument `#1' cannot convert `Perlin' expression to type `LibNoise.Unity.ModuleBase'
    5.  
    6. Assets/Demo.cs(57,17): error CS0029: Cannot implicitly convert type `Perlin' to `LibNoise.Unity.ModuleBase'
    7.  
    I'm using version 3.5.6f4 Pro.
     
  29. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Thought it might be because I was using the UnityPackage from the first post, so I downloaded the github version and swapped it into Assets/Resources/LibNoise.Unity. Now I get:

    Code (csharp):
    1.  
    2. Assets/Demo.cs(52,17): error CS0246: The type or namespace name `RiggedMultifractal' could not be found. Are you missing a using directive or an assembly reference?
    3.  
     
  30. bitbutter

    bitbutter

    Joined:
    Jul 19, 2012
    Posts:
    60
    Thanks for porting this.

    I'm having a little difficulty understanding how to use the Select module.

    This is the constructor in the port which accepts the most arguments:

    In the Libnoise documentation, it seems that Select needs references to three modules to work: Two input modules, and a control module. But the constructor arguments only accept two.

    Could anyone provide a short code example of how to correctly use Select in the unity port of Libnoise?

    Thanks in advance!
     
  31. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    hello, HELP! I am learning to code by myself and I would like to understand how to use different noise functions instead of the unity one:

    Code (csharp):
    1. vertex.y = Mathf.PerlinNoise((vtxArray[i].x/55.76+9999) , (vtxArray[i].z/55.23 +9999));
    vertex.y is terrain height, vtxArray[].x and z are position. How can i rewrite this line using a libnoise function?
    Please how do i do that ^^
    __________________________________ ______________________________ ________________


    Also. I got some small errors from the unity package,it runs fine if you rewrite lines 70-78 to this:
    Code (csharp):
    1.              //Generate the textures
    2.              this.m_textures[0] = this.m_noiseMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);
    3.              this.m_textures[0].Apply();
    4.            
    5.              this.m_textures[1] = this.m_noiseMap.GetTexture(LibNoise.Unity.Gradient.Terrain);
    6.              this.m_textures[1].Apply();
    7.              
    8.              this.m_textures[2] = this.m_noiseMap.GetNormalMap(3.0f);
    9.               this.m_textures[2].Apply();
    I am Noob but i did already write an infinite world script in about 100 lines efficient enough to update 144x* 1024 vtx tiles @30fps... I dont know how to interpret a C# library :( I know how to do lots of crazy functions for it using my audio wave shaping experience. :) i.e. canyons, then i will make a function to mix terrain styles and make random worlds with natural shapes.


    Code (csharp):
    1.     vertex.y =  Mathf.PerlinNoise((vtxArray[i].x/55.76+9999) , (vtxArray[i].z/55.23 +9999));
    2.    
    3.    
    4.     vertex.y = vertex.y*2;
    5.     if (vertex.y < 0.3 ){ vertex.y = vertex.y*vertex.y*vertex.y; }
    6.     if (vertex.y > 1.0 )  { vertex.y = vertex.y*.04+ .96;}
    7.     if (vertex.y > .6 )  { vertex.y = vertex.y*3;}
    8.     vertex.y = vertex.y*40-20;
     

    Attached Files:

    Last edited: Feb 4, 2013
  32. fractalfrenzy

    fractalfrenzy

    Joined:
    Oct 21, 2012
    Posts:
    3
    I'm wondering if anyone has also ported the noiseutils library available here: http://libnoise.sourceforge.net/tutorials/tutorial3.html

    You need these to go through the tutorial. I see the Unity package contains something called Utils.cs but this does not seem to be the same thing. If noone has already, I might try to port it.
     
  33. fractalfrenzy

    fractalfrenzy

    Joined:
    Oct 21, 2012
    Posts:
    3
    I realized that the features of that library are in Noise2D.cs.

    ALSO! I updated the demo file to give a better demonstration of clipping planar noise. Added real time controls for navigating the noise.

    It's attached.
     

    Attached Files:

    • $Demo.cs
      File size:
      3.8 KB
      Views:
      1,533
    Last edited: Feb 19, 2013
    twobob likes this.
  34. EvansGreen

    EvansGreen

    Joined:
    Nov 9, 2012
    Posts:
    129
    Thank you for the port. It's most useful. Cheers!
     
  35. dcheng334

    dcheng334

    Joined:
    Apr 13, 2013
    Posts:
    1
    I would like to first express my thanks for porting this incredibly useful library over to unity for all of us to use, you've really done all of us a great service!

    I would also like to say that many of us (or at least me!) would appreciate a port of noiseutils for unity, if you would be so willing. Thanks again!
     
    Last edited: Apr 28, 2013
  36. Cosmas

    Cosmas

    Joined:
    Jan 9, 2010
    Posts:
    133
    Has anyone been able to get the terrain more accurate to the original libnoise:

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

    I'm getting mostly a white/yellow effect with minor spots of blue and green. The shapes appear correct but the color ramp isn't getting applied properly. Unless of course I need to manually change each pixel through a loop, which I suppose I could do but that's a little painful.
     
  37. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    455
    Thanks a lot for this, it works great.
    If understand the LGPL correctly, I am allowed to use this in a package on the asset store, if I keep the LibNoise part of the package the way it is and seperate from my scripts.
    Is this correct?
    (just want to be sure)
     
  38. MrEsquire

    MrEsquire

    Joined:
    Nov 5, 2013
    Posts:
    2,712
    Hi all,

    Can someone explain to me how this is used and if its suitable for mobile.

    Thank you
     
  39. Otactu

    Otactu

    Joined:
    May 22, 2014
    Posts:
    24
    I got this error :

    Asset/Demo.cs(71,61); error CS0104: 'Gradient' is an ambiguous reference between 'LibNoise.Unity.Gradient' and 'UnityENgine.Gradient'

    So it doesn't work.
     
  40. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Many Thanks for doing this port pakfront , greatly appreciated.

    Instructions for successfully importing this package with the demo :

    Download both the package and the GIT repository in the first post. (unzip the GIT download)
    Create a new project
    Import from the package : Demo.cs , Demo.unity
    In your file browser, copy and paste the GIT folder LibNoise.Unity-master into the project Assets folder
    Edit the Demo script : Change every instance of RiggedMultifractal to RidgedMultifractal
    Edit the Demo script : Change every instance of Gradient to GradientPresets

    These steps worked for me.

    I have made some C# scripts that follow the tutorial on the official LibNoise 'site , they can be found here.

    Or you can download this package :)
     

    Attached Files:

    Last edited: Jun 29, 2014
  41. RDeluxe

    RDeluxe

    Joined:
    Sep 29, 2013
    Posts:
    117
    Many thanks for this port, and the awesome (and updated) Github repo. This is going to help me a lot to get started, as the pratical content about map generation using Perlin noise is not so easy to find (at least things I can understand...)

    Edit : I found some implementations of Simplex Noise in C# like this one or this implementation of libnoise which contains Simplex Noise. Is there a significant performance gain?
     
    Last edited: Aug 7, 2014
  42. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    455
    Since a recent version of Unity (4.3 or 4.5 or so), the demo.cs file has errors (due to Unity including UnityEngine.Gradient, causing there to be ambiguity).
    You can just remove this file, everything else works.
    I made a package with just this demo file (and demo scene) removed, other than that, it's the exact same package as pakfront originally shared here.
     

    Attached Files:

  43. cms1919

    cms1919

    Joined:
    Sep 1, 2014
    Posts:
    1
    None of the tutorials work whatsoever.
     
  44. HumanSquid

    HumanSquid

    Joined:
    Feb 9, 2015
    Posts:
    15
    Was anyone able to get height maps working so the maps actually show height / cast shadows etc . Is this even possible in Unity? I know it's done with the noiseutils in the tutorials
     
  45. TehWardy

    TehWardy

    Joined:
    Mar 20, 2013
    Posts:
    38
    @HumanSquid : I built my own voxel engine to handle terrain rendering using noise modules from here.
    It's still very much a work in progress but maybe it could help you get some pointers.

    The git repo is here:
    https://github.com/TehWardy/ccoder-Voxels


    Also @pakfront would you be interested in helping convert / build some sort of unity editor extension to get perhaps a module designer in unity.
    I built a basic UI to show what I was thinking as HTML but a node based editor on top of this would be mind blowingly awesome don't you think?

    http://ccoder.co.uk/ModuleDesigner

    Thoughts?
     
  46. HumanSquid

    HumanSquid

    Joined:
    Feb 9, 2015
    Posts:
    15
    Thanks for the link! I will take a look. I ended up using SimplexNoise and experimenting with it by using a $2 asset on the unity store called "SimpleNoise" Also coupled with some tutorials at http://studentgamedev.blogspot.ca/p/unity3d-c-voxel-and-procedural-mesh.html I learned quite a bit so far!

     
  47. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    213
    Is it normal that noise output values are not in -1 to 1 range? I get 2.5 and -2.9 values for example.
     
  48. 321Alex

    321Alex

    Joined:
    Jul 22, 2015
    Posts:
    7
  49. Toaster123

    Toaster123

    Joined:
    Dec 17, 2014
    Posts:
    1
    Im trying to Generrate a Map but I'm struggling with getting the PlanarProjection rigth

    Im using this code do Generrate the projection However im always getting a white image
    Code (CSharp):
    1.  
    2.  
    3. var left = x * Tile.TileResolution;
    4.   var right =left + Tile.TileResolution;
    5.   var top = y * Tile.TileResolution;
    6.   var bottom = top + Tile.TileResolution;
    7.   Debug.Log(string.Format("x:{0}; y:{1}; l:{2}; r:{3} t:{4}; b:{5}", x, y, left, right, top, bottom));
     
  50. coding_crow

    coding_crow

    Joined:
    Sep 26, 2014
    Posts:
    4
    Hello everyone, first of all thank you for the great work! I have encountered a problem, possibly a bug where in a spherical texture I'm trying to generate, the first and last rows of pixels are the same, resulting in a vertical seam on my sphere. I followed AlucardJ's tutorial and after having a look he advised me to try and ask the pros around here. Maybe some of you could find the time and have a look at this question - while we have found a workaround, we are still unclear about the cause of the issue. Thanks again!
    (editet for more details)
     
    Last edited: Jun 20, 2016