Home > Net >  How do I apply perlin noise to a line renderer in unity?
How do I apply perlin noise to a line renderer in unity?

Time:06-05

I am trying to apply perlin noise to a line renderer in unity 2d here is my code:

public class Ground : MonoBehaviour
{
    private static bool seedGenerated;
    public static float Seed;
    public LineRenderer LineR;
    public EdgeCollider2D col;
    private Vector2[] points;
    public int lengthOfLineRenderer;
    public float scale = 20f;
    public float OffsetX = 0;
    public float OffsetY = 0f;
    // Start is called before the first frame update
    void Start()
    {
        if(!seedGenerated)
        {
            Seed = Random.Range(0f,9999f);
            seedGenerated = true;
        }
        OffsetX = Seed   transform.position.x;
        points = new Vector2[lengthOfLineRenderer * 10   1];
        LineR.positionCount = lengthOfLineRenderer * 10   1;

        for (float i = 0f; i < lengthOfLineRenderer; i  = 0.1f)
        {
            LineR.SetPosition((int)Mathf.Round(i * 10), new Vector3(i,CalculateHeight(i),0.0f));
            //points[(int)Mathf.Round(i*10)] = new Vector2(i,CalculateHeight(i));
        }
        LineR.SetPosition(100,new Vector3(10f,CalculateHeight(10f),0));
        points[100] = new Vector2(10f,CalculateHeight(101));
        col.points = points;
    }

    // Update is called once per frame
    void Update()
    {

    }
    float CalculateHeight(float x)
    {
        float width = lengthOfLineRenderer * 10;
        float xCoord = x / width * scale   OffsetX;
        return Mathf.PerlinNoise(xCoord,OffsetY);
    }
}

the positions are applied correctly and the variable lengthOfLineRenderer is equal to 10 because I want the scale of the line to be 10 everything works fine but when I spawn another line with a 10 offset different in OffsetX they dont seem to align correctly most of the time, is the problem in my code or in the perlin noise method?

CodePudding user response:

Let's just do some simple math. According to your description we have:

lengthOfLineRenderer = 10
scale = 10
width = lengthOfLineRenderer * 10
      = 100

The last point of the first line is:

xCoord = 10f / width * scale   OffsetX
       = 10f / 100 * 10   OffsetX
       = 1   OffsetX

The first point of the second line (with a 10 offset different in OffsetX) is:

xCoord = 0f / width * scale   OffsetX   10
       = OffsetX   10

Apparently 1 OffsetX != OffsetX 10, so to make them equal you need scale = 100.

CodePudding user response:

here is the new code I fixed some stuff in:

    private static bool seedGenerated;
    public static float Seed;
    public LineRenderer LineR;
    public EdgeCollider2D col;
    private Vector2[] points;
    public int lengthOfLineRenderer;
    public float scale;
    public float OffsetX = 0;
    public float OffsetY = 0f;
    public float frequency;
    // Start is called before the first frame update
    void Start()
    {
        if(!seedGenerated)
        {
            Seed = Random.Range(0,9999);
            seedGenerated = true;
        }
        OffsetX = Seed   transform.position.x;
        points = new Vector2[lengthOfLineRenderer * 10 1 ];
        LineR.positionCount = lengthOfLineRenderer * 10 1 ;

        for (float i = 0f; i < lengthOfLineRenderer 0.1f; i  = 0.1f)
        {
            LineR.SetPosition((int)Mathf.Round(i * 10), new Vector3(i,CalculateHeight(i) * frequency,0.0f));
            points[(int)Mathf.Round(i*10)] = new Vector2(i,CalculateHeight(i) * frequency);
        }
        col.points = points;
    }

    // Update is called once per frame
    void Update()
    {

    }
    float CalculateHeight(float x)
    {
        float width = lengthOfLineRenderer * 10;
        float xCoord = x / width * scale   OffsetX;
        return Mathf.PerlinNoise(xCoord,OffsetY);
    }
  • Related