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

Perlin noise error.

Discussion in 'Scripting' started by Memeparable, Oct 23, 2014.

  1. Memeparable

    Memeparable

    Joined:
    Oct 23, 2014
    Posts:
    3
    I am trying to create a cube chunk generation system and this keeps giving me errors


    using System;
    using System.Collections.Generic;
    using System.Text;

    public class PerlinNoise
    {
    static Random random = new Random();

    // Tools
    public static T[][] GetEmptyArray(int width, int height)
    {
    T[][] image = new T[width][];

    for (int i = 0; i < width; i++)
    {
    image = new T[height];
    }
    return image;
    }

    public static float Interpolate(float x0, float x1, float alpha)
    {
    return x0 * (1 - alpha) + alpha * x1;
    }

    // Perlin Noise Generation
    public static float[][] GenerateWhiteNoise(int width, int height)
    {
    float[][] noise = GetEmptyArray(width, height);

    for (int i = 0; i < width; i++)
    {
    for (int j = 0; j < height; j++)
    {
    noise[j] = (float)random.NextDouble() % 1;
    }
    }
    return noise;
    }

    public static float[][] GenerateSmoothNoise(float[][] baseNoise, int octave)
    {
    int width = baseNoise.Length;
    int height = baseNoise[0].Length;

    float[][] smoothNoise = GetEmptyArray(width, height);
    int samplePeriod = 1 << octave; // calculates 2 ^ k
    float sampleFrequency = 1.0f / samplePeriod;

    for (int i = 0; i < width; i++)
    {
    //calculate the horizontal sampling indices
    int sample_i0 = (i / samplePeriod) * samplePeriod;
    int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
    float horizontal_blend = (i - sample_i0) * sampleFrequency;

    for (int j = 0; j < height; j++)
    {
    //calculate the vertical sampling indices
    int sample_j0 = (j / samplePeriod) * samplePeriod;
    int sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around
    float vertical_blend = (j - sample_j0) * sampleFrequency;

    //blend the top two corners
    float top = Interpolate(baseNoise[sample_i0][sample_j0],
    baseNoise[sample_i1][sample_j0], horizontal_blend);

    //blend the bottom two corners
    float bottom = Interpolate(baseNoise[sample_i0][sample_j1],
    baseNoise[sample_i1][sample_j1], horizontal_blend);

    //final blend
    smoothNoise[j] = Interpolate(top, bottom, vertical_blend);
    }
    }

    return smoothNoise;
    }

    public static float[][] GeneratePerlinNoise(float[][] baseNoise, int octaveCount)
    {
    int width = baseNoise.Length;
    int height = baseNoise[0].Length;

    float[][][] smoothNoise = new float[octaveCount][][]; //an array of 2D arrays containing

    float persistance = 0.45f;

    //generate smooth noise
    for (int i = 0; i < octaveCount; i++) { smoothNoise = GenerateSmoothNoise(baseNoise, i); } float[][] perlinNoise = GetEmptyArray(width, height); //an array of floats initialised to 0 float amplitude = 1.00f; float totalAmplitude = 0.0f; //blend noise together for (int octave = octaveCount - 1; octave >= 0; octave--)
    {
    amplitude *= persistance;
    totalAmplitude += amplitude;

    for (int i = 0; i < width; i++)
    {
    for (int j = 0; j < height; j++)
    {
    perlinNoise[j] += smoothNoise[octave][j] * amplitude;;
    }
    }
    }

    return perlinNoise;
    }

    public static float[][] GeneratePerlinNoise(int width, int height, int octaveCount)
    {
    float[][] baseNoise = GenerateWhiteNoise(width, height);

    return GeneratePerlinNoise(baseNoise, octaveCount);
    }
    }
     
  2. Memeparable

    Memeparable

    Joined:
    Oct 23, 2014
    Posts:
    3
    The T at the top is the most
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  4. Memeparable

    Memeparable

    Joined:
    Oct 23, 2014
    Posts:
    3
  5. crazytaxzi

    crazytaxzi

    Joined:
    Dec 28, 2014
    Posts:
    1
    How did you fix it?