Search Unity

LibNoise Ported to Unity

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

  1. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    Island generation via gradient. Make sure your gradient is between -1 and 1 if you want the full range.


    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.Generator
    9. {
    10.     /// <summary>
    11.     /// Provides a noise module that outputs concentric Circles. [GENERATOR]
    12.     /// </summary>
    13.     public class Island : ModuleBase
    14.     {
    15.         #region Fields
    16.  
    17.  
    18.         private double m_centerx = 0;
    19.         private double m_centery = 0;
    20.         private double m_radius = 1;
    21.  
    22.         private AnimationCurve m_islandShape = AnimationCurve.Linear(0.0f, 1.0f, 1.0f, 1.0f);
    23.  
    24.         #endregion
    25.  
    26.         #region Constructors
    27.  
    28.         /// <summary>
    29.         /// Initializes a new instance of Circles.
    30.         /// </summary>
    31.         public Island()
    32.             : base(0)
    33.         {
    34.         }
    35.  
    36.         /// <summary>
    37.         /// Initializes a new instance of Circles.
    38.         /// </summary>
    39.         /// <param name="radius">The radius of the concentric Circles.</param>
    40.         public Island(AnimationCurve curve,double radius, double centerx, double centery)
    41.             : base(0)
    42.         {
    43.             this.m_islandShape = curve;
    44.             this.m_radius = radius;
    45.             this.m_centerx = centerx;
    46.             this.m_centery = centery;
    47.         }
    48.  
    49.         #endregion
    50.  
    51.         #region Properties
    52.  
    53.         /// <summary>
    54.         /// Gets or sets the radius of the concentric Circles.
    55.         /// </summary>
    56.  
    57.         public AnimationCurve IslandCurve
    58.         {
    59.             get { return this.m_islandShape; }
    60.             set { this.m_islandShape = value; }
    61.         }
    62.         public double Radius
    63.         {
    64.             get { return this.m_radius; }
    65.             set { this.m_radius = value; }
    66.         }
    67.  
    68.         public double CenterX
    69.         {
    70.             get { return this.m_centerx; }
    71.             set { this.m_centerx = value; }
    72.         }
    73.         public double CenterY
    74.         {
    75.             get { return this.m_centery; }
    76.             set { this.m_centery = value; }
    77.         }
    78.  
    79.         #endregion
    80.  
    81.         #region ModuleBase Members
    82.  
    83.         /// <summary>
    84.         /// Returns the output value for the given input coordinates.
    85.         /// </summary>
    86.         /// <param name="x">The input coordinate on the x-axis.</param>
    87.         /// <param name="y">The input coordinate on the y-axis.</param>
    88.         /// <param name="z">The input coordinate on the z-axis.</param>
    89.         /// <returns>The resulting output value.</returns>
    90.         public override double GetValue(double x, double y, double z)
    91.         {
    92.         //This is 2D only.  Disregard Y
    93.             Vector2 v =  Vector2.zero;
    94.             Vector2 C = new Vector2((float)m_centerx, (float)m_centery);
    95.             v.x = (float)(x);
    96.             v.y = (float)(z);
    97.             double dist = Vector2.Distance( v, C);
    98.             // normalize distance / radius
    99.             double nrmDist = dist / m_radius;
    100.             return (double)m_islandShape.Evaluate ((float)nrmDist);
    101.         }
    102.         #endregion
    103.     }
    104. }
     
    twobob likes this.
  2. YetAnotherKen

    YetAnotherKen

    Joined:
    Jun 17, 2019
    Posts:
    30
    I have a simple question (I think.) When I use:
    heightMapBuilder.SetDestSize (256, 256); will this heightmap be indexed along each axis from 0-255, or -127-+127?