简体   繁体   English

如何将 perlin 噪声统一应用于线渲染器?

[英]How do I apply perlin noise to a line renderer in unity?

I am trying to apply perlin noise to a line renderer in unity 2d here is my code:我正在尝试将 perlin 噪声应用于统一 2d 中的线渲染器,这是我的代码:

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?位置应用正确,变量lengthOfLineRenderer等于 10,因为我希望线的比例为 10,一切正常,但是当我在OffsetX中生成另一条偏移量为 10 的线时,它们似乎大部分时间都没有正确对齐,是我的代码中的问题还是柏林噪声方法中的问题?

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:第二行的第一个点(在 OffsetX 中有 10 个不同的偏移量)是:

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

Apparently 1 + OffsetX != OffsetX + 10 , so to make them equal you need scale = 100 .显然1 + OffsetX != OffsetX + 10 ,所以要使它们相等,您需要scale = 100

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM