josemorval

Two colors tube

Sep 19th, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GuideScript : MonoBehaviour {
  6.  
  7.     public AnimationCurve anim;
  8.  
  9.     Mesh mesh;
  10.     public MeshFilter meshFilter;
  11.     public float widthTube;
  12.     public float lengthTube;
  13.     public float angle;
  14.     public int pointsPerLine;
  15.     public int numberSegments;
  16.     public float vel;
  17.  
  18.  
  19.     public List<Vector3> vs;
  20.  
  21.     void Start(){
  22.  
  23.         mesh = new Mesh(); 
  24.         vs = new List<Vector3>();
  25.  
  26.         for(int i=0;i<(pointsPerLine+1)*(numberSegments+1);i++){
  27.             vs.Add(Vector3.zero);
  28.         }
  29.            
  30.         mesh.SetVertices(vs);
  31.  
  32.         int[] inds = new int[6*pointsPerLine*numberSegments];
  33.         List<Vector2> uvs = new List<Vector2>();
  34.  
  35.         int k=0;
  36.         for(int i=0;i<numberSegments;i++){
  37.             for(int j=0;j<pointsPerLine;j++){
  38.  
  39.                 uvs.Add(new Vector2(1f*j/pointsPerLine,1f*i/numberSegments));
  40.  
  41.                 inds[k+1] = j+i*(pointsPerLine+1);
  42.                 inds[k+0] = (j+1)+i*(pointsPerLine+1);
  43.                 inds[k+2] = (j+1)+(i+1)*(pointsPerLine+1);
  44.  
  45.                 inds[k+4] = (j+1)+(i+1)*(pointsPerLine+1);
  46.                 inds[k+3] = j+(i+1)*(pointsPerLine+1);
  47.                 inds[k+5] = j+i*(pointsPerLine+1);
  48.  
  49.                 k+=6;
  50.             }
  51.  
  52.             uvs.Add(new Vector2(1f,1f*i/numberSegments));
  53.  
  54.         }
  55.  
  56.  
  57.         for(int j=0;j<pointsPerLine+1;j++){
  58.  
  59.             uvs.Add(new Vector2(1f*j/pointsPerLine,1f));
  60.         }
  61.  
  62.  
  63.         mesh.SetUVs(0,uvs);
  64.         mesh.SetIndices(inds,MeshTopology.Triangles,0);
  65.         mesh.Optimize();
  66.         meshFilter.mesh = mesh;
  67.  
  68.     }
  69.  
  70.  
  71.     void Update () {
  72.  
  73.         angle += vel*anim.Evaluate(Time.time)*Time.deltaTime;
  74.         vs = new List<Vector3>();
  75.         for(int i=0;i<(numberSegments+1);i++){
  76.             float t = angle-lengthTube*i/(numberSegments+1);
  77.             t%=2f;
  78.             t-=1f;
  79.             Curve(t);
  80.         }
  81.  
  82.         mesh.SetVertices(vs);
  83.         mesh.Optimize();
  84.  
  85.     }
  86.  
  87.     void Curve(float angle){
  88.  
  89.         if(angle>=-1f && angle<0f){
  90.  
  91.             Curve1(angle);
  92.  
  93.         }else if(angle>=0f && angle<1f){
  94.            
  95.             Curve2(angle);
  96.         }
  97.  
  98.     }
  99.  
  100.     void Curve1(float angle){
  101.  
  102.         Vector3 v,vo;
  103.  
  104.         v.x = 1f+Mathf.Cos(2f*Mathf.PI*angle+Mathf.PI);
  105.         v.y = Mathf.Sin(2f*Mathf.PI*angle+Mathf.PI);
  106.         v.z = Mathf.Cos(Mathf.PI*angle);
  107.  
  108.         Vector3 dir;
  109.         dir.x = -Mathf.Sin(2f*Mathf.PI*angle+Mathf.PI);
  110.         dir.y = Mathf.Cos(2f*Mathf.PI*angle+Mathf.PI);
  111.         dir.z = 0;
  112.  
  113.         Vector3 dirOrto;
  114.         dirOrto.x = dir.y;
  115.         dirOrto.y = -dir.x;
  116.         dirOrto.z = 0f;
  117.  
  118.         Vector3 biOrto = Vector3.Cross(dir,dirOrto);
  119.  
  120.  
  121.         for(int i=0;i<pointsPerLine+1;i++){
  122.             vo = dirOrto*Mathf.Cos(Mathf.PI*2f*i/pointsPerLine);
  123.             vo+= biOrto*Mathf.Sin(Mathf.PI*2f*i/pointsPerLine);
  124.             vo*=widthTube;
  125.             vo+=v;
  126.             vs.Add(vo);
  127.         }
  128.            
  129.         transform.position = v;
  130.  
  131.     }
  132.  
  133.     void Curve2(float angle){
  134.  
  135.         Vector3 v,vo;
  136.  
  137.         v.x = -1f+Mathf.Cos(-2f*Mathf.PI*angle);
  138.         v.y = Mathf.Sin(-2f*Mathf.PI*angle);
  139.         v.z = Mathf.Cos(Mathf.PI*angle);
  140.  
  141.         Vector3 dir;
  142.         dir.x = Mathf.Sin(-2f*Mathf.PI*angle);
  143.         dir.y = -Mathf.Cos(-2f*Mathf.PI*angle);
  144.         dir.z = 0f;
  145.  
  146.         Vector3 dirOrto;
  147.         dirOrto.x = dir.y;
  148.         dirOrto.y = -dir.x;
  149.         dirOrto.z = 0f;
  150.  
  151.         Vector3 biOrto = Vector3.Cross(dir,dirOrto);
  152.  
  153.         for(int i=0;i<pointsPerLine+1;i++){
  154.             vo = dirOrto*Mathf.Cos(Mathf.PI*2f*i/pointsPerLine);
  155.             vo+= biOrto*Mathf.Sin(Mathf.PI*2f*i/pointsPerLine);
  156.             vo*=widthTube;
  157.             vo+=v;
  158.             vs.Add(vo);
  159.         }
  160.  
  161.         transform.position = v;
  162.  
  163.  
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment