inz-00/Assets/Scripts/CircleGenerator.cs
2019-07-27 22:40:42 +02:00

61 lines
1.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class CircleGenerator : MonoBehaviour
{
[Range(1, 200)]
public int NodeCount = 3;
public int Details = 150;
public float Radius = 20.0f;
[Range(0, 1)]
public float AngleVariation = 0.5f;
public float RadiusVariation = 2f;
private Random random = new Random();
private float RandomCoefficient(float multiplier) => 2 * (Random.value - 0.5f) * multiplier;
public void Generate()
{
Vector3 center = new Vector3(0, 0, 0);
Mesh mesh = new Mesh();
var maxAngleVariation = AngleVariation * Mathf.PI * 2 / Details;
var vertices = new List<Vector3>();
for (int i = 0; i < Details; i++)
{
var angle = i * Mathf.PI * 2 / Details + RandomCoefficient(maxAngleVariation);
var radius = Radius + RandomCoefficient(RadiusVariation);
Vector3 direction = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle));
vertices.Add(direction * radius);
}
var triangles = new List<int>();
for (int i = 0; i < Details - 2; i++)
{
triangles.Add(0);
triangles.Add(i);
triangles.Add(i + 1);
}
var normals = new List<Vector3>();
for (int i = 0; i < Details; i++)
{
normals.Add(-Vector3.forward);
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.normals = normals.ToArray();
var mf = GetComponent<MeshFilter>();
mf.mesh = mesh;
}
}