Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class GuideScript : MonoBehaviour {
- public AnimationCurve anim;
- Mesh mesh;
- public MeshFilter meshFilter;
- public float widthTube;
- public float lengthTube;
- public float angle;
- public int pointsPerLine;
- public int numberSegments;
- public float vel;
- public List<Vector3> vs;
- void Start(){
- mesh = new Mesh();
- vs = new List<Vector3>();
- for(int i=0;i<(pointsPerLine+1)*(numberSegments+1);i++){
- vs.Add(Vector3.zero);
- }
- mesh.SetVertices(vs);
- int[] inds = new int[6*pointsPerLine*numberSegments];
- List<Vector2> uvs = new List<Vector2>();
- int k=0;
- for(int i=0;i<numberSegments;i++){
- for(int j=0;j<pointsPerLine;j++){
- uvs.Add(new Vector2(1f*j/pointsPerLine,1f*i/numberSegments));
- inds[k+1] = j+i*(pointsPerLine+1);
- inds[k+0] = (j+1)+i*(pointsPerLine+1);
- inds[k+2] = (j+1)+(i+1)*(pointsPerLine+1);
- inds[k+4] = (j+1)+(i+1)*(pointsPerLine+1);
- inds[k+3] = j+(i+1)*(pointsPerLine+1);
- inds[k+5] = j+i*(pointsPerLine+1);
- k+=6;
- }
- uvs.Add(new Vector2(1f,1f*i/numberSegments));
- }
- for(int j=0;j<pointsPerLine+1;j++){
- uvs.Add(new Vector2(1f*j/pointsPerLine,1f));
- }
- mesh.SetUVs(0,uvs);
- mesh.SetIndices(inds,MeshTopology.Triangles,0);
- mesh.Optimize();
- meshFilter.mesh = mesh;
- }
- void Update () {
- angle += vel*anim.Evaluate(Time.time)*Time.deltaTime;
- vs = new List<Vector3>();
- for(int i=0;i<(numberSegments+1);i++){
- float t = angle-lengthTube*i/(numberSegments+1);
- t%=2f;
- t-=1f;
- Curve(t);
- }
- mesh.SetVertices(vs);
- mesh.Optimize();
- }
- void Curve(float angle){
- if(angle>=-1f && angle<0f){
- Curve1(angle);
- }else if(angle>=0f && angle<1f){
- Curve2(angle);
- }
- }
- void Curve1(float angle){
- Vector3 v,vo;
- v.x = 1f+Mathf.Cos(2f*Mathf.PI*angle+Mathf.PI);
- v.y = Mathf.Sin(2f*Mathf.PI*angle+Mathf.PI);
- v.z = Mathf.Cos(Mathf.PI*angle);
- Vector3 dir;
- dir.x = -Mathf.Sin(2f*Mathf.PI*angle+Mathf.PI);
- dir.y = Mathf.Cos(2f*Mathf.PI*angle+Mathf.PI);
- dir.z = 0;
- Vector3 dirOrto;
- dirOrto.x = dir.y;
- dirOrto.y = -dir.x;
- dirOrto.z = 0f;
- Vector3 biOrto = Vector3.Cross(dir,dirOrto);
- for(int i=0;i<pointsPerLine+1;i++){
- vo = dirOrto*Mathf.Cos(Mathf.PI*2f*i/pointsPerLine);
- vo+= biOrto*Mathf.Sin(Mathf.PI*2f*i/pointsPerLine);
- vo*=widthTube;
- vo+=v;
- vs.Add(vo);
- }
- transform.position = v;
- }
- void Curve2(float angle){
- Vector3 v,vo;
- v.x = -1f+Mathf.Cos(-2f*Mathf.PI*angle);
- v.y = Mathf.Sin(-2f*Mathf.PI*angle);
- v.z = Mathf.Cos(Mathf.PI*angle);
- Vector3 dir;
- dir.x = Mathf.Sin(-2f*Mathf.PI*angle);
- dir.y = -Mathf.Cos(-2f*Mathf.PI*angle);
- dir.z = 0f;
- Vector3 dirOrto;
- dirOrto.x = dir.y;
- dirOrto.y = -dir.x;
- dirOrto.z = 0f;
- Vector3 biOrto = Vector3.Cross(dir,dirOrto);
- for(int i=0;i<pointsPerLine+1;i++){
- vo = dirOrto*Mathf.Cos(Mathf.PI*2f*i/pointsPerLine);
- vo+= biOrto*Mathf.Sin(Mathf.PI*2f*i/pointsPerLine);
- vo*=widthTube;
- vo+=v;
- vs.Add(vo);
- }
- transform.position = v;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment