nagedev
2/3/2018 - 11:15 PM

Wire grid with Perlin noise

Wire grid with Perlin noise

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

public class WireGrid : MonoBehaviour
{

    public int size = 64;
    public float width = .1f;
    public float height = 10f;
    public float perlinSpeedX = 1f;
    public float perlinSpeedY = 1f;
    public Material lineMat;
    public Color lowColor;
    public Color highColor;

    [HideInInspector] public List<List<Vector3>> points = new List<List<Vector3>>();
    [HideInInspector] public List<LineRenderer> lines = new List<LineRenderer>();

    private void Awake()
    {
        InitPoints();
        InitLines();
    }

    private void Update()
    {
        NoisePoints();
        ConnectPoints();
    }

    private void InitPoints()
    {
        points = new List<List<Vector3>>(size);
        for (int x = 0; x < size; x++)
        {
            points.Add(new List<Vector3>(size));
            for (int z = 0; z < size; z++)
            {
                points[x].Add(new Vector3(x, 0f, z));
            }
        }
    }

    private void InitLines()
    {
        for (int i = 0; i < size * size * 3 - size - size - size - size + 1; i++)
        {
            GameObject lineObj = new GameObject("Line " + i);
            lineObj.transform.SetParent(transform);
            LineRenderer line = lineObj.AddComponent<LineRenderer>();
            line.startWidth = width;
            line.endWidth = width;
            lines.Add(line);
        }
    }


    private float perlinOffsetX;
    private float perlinOffsetY;
    private void NoisePoints()
    {
        perlinOffsetX += Time.deltaTime * perlinSpeedX;
        perlinOffsetY += Time.deltaTime * perlinSpeedY;
        for (int x = 0; x < size; x++)
        {
            for (int z = 0; z < size; z++)
            {
                float perlinX = (float) x / size + perlinOffsetX;
                float perlinY = (float) z / size + perlinOffsetY;
                points[x][z] = new Vector3(points[x][z].x, Mathf.PerlinNoise(perlinX, perlinY) * height, points[x][z].z);
            }
        }
    }

    private void ConnectPoints()
    {
        int lineNum = 0;
        for (int x = 0; x < size; x++)
        {
            for (int z = 0; z < size; z++)
            {
                if (x - 1 >= 0)
                {
                    lines[lineNum].SetPositions(new Vector3[] { points[x - 1][z], points[x][z] });
                    lines[lineNum].startColor = Color.Lerp(lowColor, highColor, points[x - 1][z].y / height);
                    lines[lineNum].endColor = Color.Lerp(lowColor, highColor, points[x][z].y / height);
                    lines[lineNum].material = lineMat;
                    lineNum += 1;
                }
                if (z - 1 >= 0)
                {
                    lines[lineNum].SetPositions(new Vector3[] { points[x][z - 1], points[x][z] });
                    lines[lineNum].startColor = Color.Lerp(lowColor, highColor, points[x][z - 1].y / height);
                    lines[lineNum].endColor = Color.Lerp(lowColor, highColor, points[x][z].y / height);
                    lines[lineNum].material = lineMat;
                    lineNum += 1;
                }
                if (x - 1 >= 0 && z - 1 >= 0)
                {
                    lines[lineNum].SetPositions(new Vector3[] { points[x - 1][z - 1], points[x][z] });
                    lines[lineNum].startColor = Color.Lerp(lowColor, highColor, points[x - 1][z - 1].y / height);
                    lines[lineNum].endColor = Color.Lerp(lowColor, highColor, points[x][z].y / height);
                    lines[lineNum].material = lineMat;
                    lineNum += 1;
                }
            }
        }
    }
    
}