josemorval

Cantor begins

Nov 9th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Generator : MonoBehaviour {
  6.  
  7.     public Material mat;
  8.  
  9.     List<Vector3> currentList;
  10.     List<Vector3> oldList;
  11.     List<int> indList;
  12.  
  13.     int it = 0;
  14.  
  15.     Mesh mesh;
  16.  
  17.     public int N;
  18.     public int rN;
  19.     int initialVertex;
  20.  
  21.     public float factorHeight;
  22.     public float factorPower;
  23.  
  24.     public float firstSegment;
  25.     public float secondSegment;
  26.     public float thirdSegment;
  27.  
  28.     public bool automatic;
  29.  
  30.     public float minRadi;
  31.     public float maxRadi;
  32.  
  33.     public int randomSeed;
  34.  
  35.     void OnEnable () {
  36.  
  37.         Random.InitState(randomSeed);
  38.  
  39.         it = 0;
  40.         mesh = new Mesh();
  41.         oldList = new List<Vector3>();
  42.  
  43.         currentList = new List<Vector3>();
  44.  
  45.         float r = 1f/N;
  46.  
  47.         while(r<=1f){
  48.  
  49.             float radi = Random.Range(minRadi,maxRadi);
  50.  
  51.             currentList.Add(new Vector3(
  52.                 radi*Mathf.Cos(2f*Mathf.PI*r),
  53.                 radi*Mathf.Sin(2f*Mathf.PI*r),
  54.                 0f)
  55.             );
  56.  
  57.             r += 1f*Random.Range(1,rN)/N;
  58.  
  59.         }
  60.  
  61.         initialVertex = currentList.Count;
  62.  
  63.         indList = new List<int>();
  64.  
  65.         for(int i=0;i<currentList.Count;i++){
  66.             indList.Add(i);
  67.         }
  68.  
  69.         indList.Add(0);
  70.  
  71.         if(automatic){
  72.             while(initialVertex*Mathf.Pow(4,it+1)<64000){
  73.                 it++;
  74.                 NewGeneration();
  75.             }
  76.         }
  77.  
  78.  
  79.     }
  80.    
  81.     void Update () {
  82.    
  83.         if(Input.GetKeyDown(KeyCode.Space) && initialVertex*Mathf.Pow(4,it+1)<64000 && !automatic){
  84.             it++;
  85.             NewGeneration();
  86.         }
  87.  
  88.         mesh.SetVertices(currentList);
  89.         mesh.SetIndices(indList.ToArray(),MeshTopology.LineStrip,0);
  90.         Graphics.DrawMesh(mesh,transform.localToWorldMatrix,mat,0);
  91.  
  92.     }
  93.  
  94.     void NewGeneration(){
  95.  
  96.         oldList.Clear();
  97.         oldList.InsertRange(0,currentList);
  98.         currentList.Clear();
  99.  
  100.         for(int i=0;i<oldList.Count;i++){
  101.  
  102.             Vector3 a = oldList[i];
  103.             Vector3 b = oldList[(i+1)%oldList.Count];
  104.             Vector3 dirU = b-a;
  105.             Vector3 dirT = dirU.normalized;
  106.             dirT.z = dirT.x;
  107.             dirT.x = -dirT.y;
  108.             dirT.y = dirT.z;
  109.             dirT.z = 0f;
  110.  
  111.             currentList.Add(a);
  112.  
  113.             currentList.Add(a+dirU*firstSegment);
  114.             currentList.Add(a+dirU*secondSegment+dirU.magnitude*factorHeight*dirT/((Mathf.Pow(factorPower,it))));
  115.             currentList.Add(a+dirU*thirdSegment);
  116.  
  117.         }
  118.  
  119.  
  120.         indList.Clear();
  121.  
  122.         for(int i=0;i<currentList.Count;i++){
  123.             indList.Add(i);
  124.         }
  125.  
  126.         indList.Add(0);
  127.  
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment