Calc Noise - CSharp System

CSharp examples for System:Math Number

Description

Calc Noise

Demo Code


using System;// w ww .j  ava  2s. c o  m
using System.Collections;
using UnityEngine;

public class Main{
    public static int[,] CalcNoise(int pixWidth, int pixHeight,float[] heightMap, string seed = null, float scale = 10f)
    {
        if (seed == null)
        {
            seed = Time.time.ToString();
        }
        System.Random randNum = new System.Random(seed.GetHashCode());


        float xOrg = randNum.Next(pixHeight);
        float yOrg = xOrg;

        int[,] map = new int[pixWidth, pixHeight];
        float[,] floatMap = new float[pixWidth, pixHeight];
        int y = 0;
        while (y < pixHeight)
        {
            int x = 0;
            while (x < pixWidth)
            {
                float xCoord = xOrg + (float)x / (float)pixWidth * scale;
                float yCoord = yOrg + (float)y / (float)pixHeight * scale;
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                floatMap[x, y] = sample;
                map[x, y] = ScaleFloatToInt(sample,heightMap);
                x++;
            }
            y++;
        }

        return map;

    }
    public static float[,] CalcNoise(int pixWidth, int pixHeight, string seed = null, float scale = 10f)
    {
        if (seed == null)
        {
            seed = Time.time.ToString();
        }
        System.Random randNum = new System.Random(seed.GetHashCode());

        float xOrg = randNum.Next(pixHeight);
        float yOrg = xOrg;

        float[,] map = new float[pixWidth, pixHeight];
        int y = 0;
        while (y < pixHeight)
        {
            int x = 0;
            while (x < pixWidth)
            {
                float xCoord = xOrg + (float)x / (float)pixWidth * scale;
                float yCoord = yOrg + (float)y / (float)pixHeight * scale;
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                map[x, y] = sample;
                x++;
            }
            y++;
        }

        return map;
    }
        //public int pixWidth;
    //public int pixHeight;
    //public float xOrg;
    //public float yOrg;
    //public float scale = 1.0F;


    public static float[,] CalcNoise(int pixWidth, int pixHeight, float xOrg, float yOrg, float scale = 1f)
    {
        float[,] map = new float[pixWidth, pixHeight];
        int y = 0;
        while (y < pixHeight)
        {
            int x = 0;
            while (x < pixWidth)
            {
                float xCoord = xOrg + x / pixWidth * scale;
                float yCoord = yOrg + y / pixHeight * scale;
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                map[x, y] = sample;
                x++;
            }
            y++;
        }

        return map;
    }
}

Related Tutorials