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);
103 gameObject.AddComponent<MeshFilter>();
104 MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
106 mainCamera = Camera.main;
111 if(mainCamera == null)
112 mainCamera = Camera.main;
113 if (vertices==null || vertices.Length <= 1)
115 GetComponent<Renderer>().enabled=
false;
118 GetComponent<Renderer>().enabled=
true;
123 if(vertices.Length > 1)
125 Vector3 cur1 = mainCamera.WorldToScreenPoint(vertices[0].point);
127 lastCameraPosition1.z = 0;
128 Vector3 cur2 = mainCamera.WorldToScreenPoint(vertices[vertices.Length - 1].
point);
129 lastCameraPosition2.z = 0;
131 float distance = (lastCameraPosition1 - cur1).magnitude;
132 distance += (lastCameraPosition2 - cur2).magnitude;
134 if(distance > movePixelsForRebuild || Time.time - lastRebuildTime > maxRebuildTime)
137 lastCameraPosition1 = cur1;
138 lastCameraPosition2 = cur2;
145 if (crossSegments != lastCrossSegments) {
150 crossPoints[c] =
new Vector3(Mathf.Cos(theta*c), Mathf.Sin(theta*c), 0);
155 Vector3[] meshVertices =
new Vector3[vertices.Length*
crossSegments];
158 int[] tris =
new int[vertices.Length*crossSegments*6];
163 for (p=0;p<vertices.Length;p++) {
164 if (p<vertices.Length-1)
166 rotation = Quaternion.FromToRotation(Vector3.forward, vertices[p+1].
point-vertices[p].
point);
170 rotation = Quaternion.FromToRotation(Vector3.forward, vertices[p].
point-vertices[p-1].
point);
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;
180 lastVertices[c]=theseVertices[c];
181 theseVertices[c] = p*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];
198 Mesh mesh =
new Mesh();
199 mesh.vertices = meshVertices;
200 mesh.triangles = tris;
201 mesh.RecalculateNormals();
203 GetComponent<MeshFilter>().mesh = mesh;
208 public void SetPoints ( Vector3[] points ,
float radius , Color col ){
209 if (points.Length < 2)
return;
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);
217 for (p=0;p<points.Length;p++) {
218 vertices[p+1] =
new TubeVertex(points[p], radius, col);
Vector3 lastCameraPosition1
Vector3 lastCameraPosition2
void SetPoints(Vector3[] points, float radius, Color col)