NoiseTools utility class

Modified on Tue, 28 Jan at 6:29 PM

The NoiseTools class provides utilities for managing noise textures and heightmaps. These tools are ideal for terrain generation and procedural content creation, offering predictable and flexible noise manipulation.


Methods Overview

1. LoadNoiseTexture

  • Description: Loads noise values from a texture.
  • Parameters:
    • Texture tex: The texture to load noise from.
    • int textureSize (output): The size of the texture.
  • Usage:
float[] noiseValues = NoiseTools.LoadNoiseTexture(texture, out int size);

2. LoadHeightmapFromTerrainData

  • Description: Loads heightmap values from Unity TerrainData.
  • Parameters:
    • TerrainData td: The terrain data to extract heights from.
    • int size (output): The resolution of the heightmap.
  • Usage:
float[] heightmapValues = NoiseTools.LoadHeightmapFromTerrainData(terrainData, out int size);

3. GetNoiseValue

  • Description: Samples a 2D or 3D noise texture at a specific position.

Variants:

  1. 2D Noise Sampling:

    float value = NoiseTools.GetNoiseValue(noiseArray, textureSize, x, z);
    
  2. 2D Noise with Ridge Effect:

    float value = NoiseTools.GetNoiseValue(noiseArray, textureSize, x, z, ridgeNoise: true);
    
  3. 3D Noise Sampling:

    float value = NoiseTools.GetNoiseValue(noiseArray, textureSize, x, y, z);
    

4. GetNoiseValueBilinear

  • Description: Samples a 2D noise texture at a specific position using bilinear filtering.
  • Parameters:
    • float[] noiseArray: The noise data array.
    • int textureSize: The size of the texture.
    • double x, z: World position coordinates.
    • bool ridgeNoise: Whether to apply ridge noise (optional).
  • Usage:
float value = NoiseTools.GetNoiseValueBilinear(noiseArray, textureSize, x, z);

Example Usage

  1. Load Noise from a Texture:

    Texture2D texture = ...; float[] noiseValues = NoiseTools.LoadNoiseTexture(texture, out int textureSize);
    
  2. Load Heightmap Data:

    TerrainData terrainData = ...; float[] heightmap = NoiseTools.LoadHeightmapFromTerrainData(terrainData, out int size);
    
  3. Sample 2D Noise:

    float noiseValue = NoiseTools.GetNoiseValue(noiseValues, textureSize, 10.5, 20.3);
    
  4. Sample Noise with Bilinear Filtering:

    float filteredValue = NoiseTools.GetNoiseValueBilinear(noiseValues, textureSize, 15.7, 30.2);
    
  5. Sample 3D Noise:

    float noiseValue3D = NoiseTools.GetNoiseValue(noiseValues, textureSize, 10.5, 15.2, 20.3);
    

Notes

  • The NoiseTools system enables flexible and efficient noise-based calculations, essential for procedural systems.
  • Ridge noise provides an inverted effect, ideal for creating ridges or peaks in terrains.
  • All methods are optimized for performance, ensuring minimal overhead during runtime.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article