UnityMol  0.9.6-875
UnityMol viewer / In developement
TubeRenderer.cs
Go to the documentation of this file.
1 
66 // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
67 // Do test the code! You usually need to change a few small bits.
68 
69 using UnityEngine;
70 using System.Collections;
71 
72 
73 
74 public class TubeRenderer : MonoBehaviour
75 {
76 
77 
79 public Material material;
80 
81 public int crossSegments = 3;
82 private Vector3[] crossPoints;
83 private int lastCrossSegments;
84 public float flatAtDistance=-1;
85 
86 private Vector3 lastCameraPosition1;
87 private Vector3 lastCameraPosition2;
88 public int movePixelsForRebuild= 6;
89 public float maxRebuildTime= 0.1f;
90 private float lastRebuildTime= 0.00f;
91 private Camera mainCamera = null;
92 
93 void Reset ()
94 {
95 
96  vertices = new TubeVertex[2];
97  vertices[0]=new TubeVertex(Vector3.zero, 1.0f, Color.white);
98  vertices[1]=new TubeVertex(new Vector3(1,0,0), 1.0f, Color.white);
99 
100 }
101 void Start ()
102 {
103  gameObject.AddComponent<MeshFilter>();
104  MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
105  mr.material = material;
106  mainCamera = Camera.main;
107 }
108 
109 void LateUpdate ()
110 {
111  if(mainCamera == null)
112  mainCamera = Camera.main;
113  if (vertices==null || vertices.Length <= 1)
114  {
115  GetComponent<Renderer>().enabled=false;
116  return;
117  }
118  GetComponent<Renderer>().enabled=true;
119 
120  //rebuild the mesh?
121  bool re=false;
122 // float distFromMainCam;
123  if(vertices.Length > 1)
124  {
125  Vector3 cur1 = mainCamera.WorldToScreenPoint(vertices[0].point);
126 // distFromMainCam = lastCameraPosition1.z;
127  lastCameraPosition1.z = 0;
128  Vector3 cur2 = mainCamera.WorldToScreenPoint(vertices[vertices.Length - 1].point);
129  lastCameraPosition2.z = 0;
130 
131  float distance = (lastCameraPosition1 - cur1).magnitude;
132  distance += (lastCameraPosition2 - cur2).magnitude;
133 
134  if(distance > movePixelsForRebuild || Time.time - lastRebuildTime > maxRebuildTime)
135  {
136  re = true;
137  lastCameraPosition1 = cur1;
138  lastCameraPosition2 = cur2;
139  }
140  }
141 
142  if (re) {
143  //draw tube
144 
145  if (crossSegments != lastCrossSegments) {
146  crossPoints = new Vector3[crossSegments];
147  float theta = 2.0f*Mathf.PI/crossSegments;
148  int c;
149  for (c=0;c<crossSegments;c++) {
150  crossPoints[c] = new Vector3(Mathf.Cos(theta*c), Mathf.Sin(theta*c), 0);
151  }
152  lastCrossSegments = crossSegments;
153  }
154 
155  Vector3[] meshVertices = new Vector3[vertices.Length*crossSegments];
156  Vector2[] uvs = new Vector2[vertices.Length*crossSegments];
157  Color[] colors = new Color[vertices.Length*crossSegments];
158  int[] tris = new int[vertices.Length*crossSegments*6];
159  int[] lastVertices = new int[crossSegments];
160  int[] theseVertices = new int[crossSegments];
161  Quaternion rotation;
162  int p;
163  for (p=0;p<vertices.Length;p++) {
164  if (p<vertices.Length-1)
165  {
166  rotation = Quaternion.FromToRotation(Vector3.forward, vertices[p+1].point-vertices[p].point);
167  }
168  else
169  {
170  rotation = Quaternion.FromToRotation(Vector3.forward, vertices[p].point-vertices[p-1].point);
171  }
172  int c;
173  for (c=0;c<crossSegments;c++) {
174  int vertexIndex = p*crossSegments+c;
175  meshVertices[vertexIndex] = vertices[p].point + rotation * crossPoints[c] * vertices[p].radius;
176  uvs[vertexIndex] = new Vector2((0.0f+c)/crossSegments,(0.0f+p)/vertices.Length);
177  colors[vertexIndex] = vertices[p].color;
178 
179 // print(c+" - vertex index "+(p*crossSegments+c) + " is " + meshVertices[p*crossSegments+c]);
180  lastVertices[c]=theseVertices[c];
181  theseVertices[c] = p*crossSegments+c;
182  }
183  //make triangles
184  if (p>0) {
185  for (c=0;c<crossSegments;c++) {
186  int start= (p*crossSegments+c)*6;
187  tris[start] = lastVertices[c];
188  tris[start+1] = lastVertices[(c+1)%crossSegments];
189  tris[start+2] = theseVertices[c];
190  tris[start+3] = tris[start+2];
191  tris[start+4] = tris[start+1];
192  tris[start+5] = theseVertices[(c+1)%crossSegments];
193 // print("Triangle: indexes("+tris[start]+", "+tris[start+1]+", "+tris[start+2]+"), ("+tris[start+3]+", "+tris[start+4]+", "+tris[start+5]+")");
194  }
195  }
196  }
197 
198  Mesh mesh = new Mesh();
199  mesh.vertices = meshVertices;
200  mesh.triangles = tris;
201  mesh.RecalculateNormals();
202  mesh.uv = uvs;
203  GetComponent<MeshFilter>().mesh = mesh;
204  }
205 }
206 
207 //sets all the points to points of a Vector3 array, as well as capping the ends.
208 public void SetPoints ( Vector3[] points , float radius , Color col ){
209  if (points.Length < 2) return;
210  vertices = new TubeVertex[points.Length+2];
211 
212  Vector3 v0offset = (points[0]-points[1])*0.01f;
213  vertices[0] = new TubeVertex(v0offset+points[0], 0.0f, col);
214  Vector3 v1offset = (points[points.Length-1] - points[points.Length-2])*0.01f;
215  vertices[vertices.Length-1] = new TubeVertex(v1offset+points[points.Length-1], 0.0f, col);
216  int p;
217  for (p=0;p<points.Length;p++) {
218  vertices[p+1] = new TubeVertex(points[p], radius, col);
219  }
220 }
221 }
int lastCrossSegments
Definition: TubeRenderer.cs:83
Vector3 lastCameraPosition1
Definition: TubeRenderer.cs:86
TubeVertex[] vertices
Definition: TubeRenderer.cs:78
Vector3 lastCameraPosition2
Definition: TubeRenderer.cs:87
void LateUpdate()
Color color
Definition: TubeVertex.cs:73
float flatAtDistance
Definition: TubeRenderer.cs:84
void SetPoints(Vector3[] points, float radius, Color col)
void Reset()
Definition: TubeRenderer.cs:93
Camera mainCamera
Definition: TubeRenderer.cs:91
int movePixelsForRebuild
Definition: TubeRenderer.cs:88
float lastRebuildTime
Definition: TubeRenderer.cs:90
Vector3[] crossPoints
Definition: TubeRenderer.cs:82
Material material
Definition: TubeRenderer.cs:79
float radius
Definition: TubeVertex.cs:72
float maxRebuildTime
Definition: TubeRenderer.cs:89
Vector3 point
Definition: TubeVertex.cs:71