0
\$\begingroup\$

I know this might be a bit of a noob question, but I can't figure out what I'm doing wrong, and the math that I have done should make this work correctly. I am trying to make a perlin noise map that is normal until a specific range, at which point it would decrease to where all the values trend towards zero, creating an island in an ocean. I am currently generating perlin "sample" values, and then dividing them by a factor which stays close to one up to a specific bound, and then increases polynomialy. The problem however, is that the noise stays stuck in the top right corner, and is darker than I would expect. Here is the mathematical function I am using graphed on Desmos where n is the boundary and x is the distance from the center of the map:

enter image description here

And here is the main body code I am using to generate the noise map:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenNoise : MonoBehaviour {

public int width = 256;
public int height = 256;

public float scale = 20f;

public float offsetX;
public float offsetY;

public float falloffRange = 100;
public float falloffPower = 10;

public void RenderNoiseMap()
{
    Renderer renderer = GetComponent<Renderer>();
    renderer.sharedMaterial.mainTexture = GenerateTexture();
}

Texture2D GenerateTexture () {
    Texture2D texture = new Texture2D(width, height);

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            Color color = GenerateColor(x, y);
            texture.SetPixel(x, y, color);
        }
    }

    texture.Apply();
    return texture;
}

Color GenerateColor(int x, int y)
{
    float xCoord = ((float)x / width * scale) + offsetX;
    float yCoord = ((float)y / height * scale) + offsetY;
    float falloffFactor = FalloffFactor(x, y, width, height);

    float sample = Mathf.PerlinNoise(xCoord, yCoord);
    sample = sample / falloffFactor;
    return new Color(sample, sample, sample);
}

float FalloffFactor(int x, int y, int width, int height)
{
    float falloffFactor = 1;
    float centerX = (float)width / 2;
    float centerY = (float)height / 2;

    float distanceFromCenter = Mathf.Sqrt(Mathf.Pow(2, ((float)x - centerX)) + Mathf.Pow(2, ((float)y - centerY)));
    falloffFactor = Mathf.Pow(2, (Mathf.Pow(falloffPower, (distanceFromCenter / falloffRange)) + 1));

    return falloffFactor;
}
}

And Here is the code that I am using to generate the image in the editor:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(GenNoise))]
public class NewBehaviourScript : Editor {
public override void OnInspectorGUI()
{
    DrawDefaultInspector();

    GenNoise script = (GenNoise)target;
    if (GUILayout.Button("Generate"))
    {
        script.RenderNoiseMap();
    }
}

}

Here is a picture of the result: enter image description here

\$\endgroup\$
0
\$\begingroup\$

I think your Mathf.pow functions all have the arguments reversed. e.g. Mathf.Pow(2, ((float)y - centerY))) is 2^(y-centerY), where you probably meant to do (y-centerY)^2 considering that you're trying to calculate a distance.

\$\endgroup\$
  • 1
    \$\begingroup\$ Literally that simple! You saved my day, Thanks dude! \$\endgroup\$ – kineticpull May 9 '18 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.