Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Generator : MonoBehaviour {
- public Material mat;
- List<Vector3> currentList;
- List<Vector3> oldList;
- List<int> indList;
- int it = 0;
- Mesh mesh;
- public int N;
- public int rN;
- int initialVertex;
- public float factorHeight;
- public float factorPower;
- public float firstSegment;
- public float secondSegment;
- public float thirdSegment;
- public bool automatic;
- public float minRadi;
- public float maxRadi;
- public int randomSeed;
- void OnEnable () {
- Random.InitState(randomSeed);
- it = 0;
- mesh = new Mesh();
- oldList = new List<Vector3>();
- currentList = new List<Vector3>();
- float r = 1f/N;
- while(r<=1f){
- float radi = Random.Range(minRadi,maxRadi);
- currentList.Add(new Vector3(
- radi*Mathf.Cos(2f*Mathf.PI*r),
- radi*Mathf.Sin(2f*Mathf.PI*r),
- 0f)
- );
- r += 1f*Random.Range(1,rN)/N;
- }
- initialVertex = currentList.Count;
- indList = new List<int>();
- for(int i=0;i<currentList.Count;i++){
- indList.Add(i);
- }
- indList.Add(0);
- if(automatic){
- while(initialVertex*Mathf.Pow(4,it+1)<64000){
- it++;
- NewGeneration();
- }
- }
- }
- void Update () {
- if(Input.GetKeyDown(KeyCode.Space) && initialVertex*Mathf.Pow(4,it+1)<64000 && !automatic){
- it++;
- NewGeneration();
- }
- mesh.SetVertices(currentList);
- mesh.SetIndices(indList.ToArray(),MeshTopology.LineStrip,0);
- Graphics.DrawMesh(mesh,transform.localToWorldMatrix,mat,0);
- }
- void NewGeneration(){
- oldList.Clear();
- oldList.InsertRange(0,currentList);
- currentList.Clear();
- for(int i=0;i<oldList.Count;i++){
- Vector3 a = oldList[i];
- Vector3 b = oldList[(i+1)%oldList.Count];
- Vector3 dirU = b-a;
- Vector3 dirT = dirU.normalized;
- dirT.z = dirT.x;
- dirT.x = -dirT.y;
- dirT.y = dirT.z;
- dirT.z = 0f;
- currentList.Add(a);
- currentList.Add(a+dirU*firstSegment);
- currentList.Add(a+dirU*secondSegment+dirU.magnitude*factorHeight*dirT/((Mathf.Pow(factorPower,it))));
- currentList.Add(a+dirU*thirdSegment);
- }
- indList.Clear();
- for(int i=0;i<currentList.Count;i++){
- indList.Add(i);
- }
- indList.Add(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment