# Create torus with different colored segments

As I understand, there is no easy way to create a torus in Unity compared to openGL. I found the following code from http://forum.unity3d.com/threads/8487-Torus-in-Unity that creates a torus:

``````using System;
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class Primitives : MonoBehaviour {

private static float Pi = 3.14159f;

public int segments = 32;
public int tubes = 12;

void Start() {
Torus();
}

void Update()
{
//transform.Rotate(Time.deltaTime,1f,10);

}

public void Torus() {
// Total vertices
int totalVertices = segments * tubes;

// Total primitives
int totalPrimitives = totalVertices * 2;

// Total indices
int totalIndices = totalPrimitives * 3;

// Init vertexList and indexList
ArrayList verticesList = new ArrayList();
ArrayList indicesList = new ArrayList();

// Save these locally as floats
float numSegments = segments;
float numTubes = tubes;

// Calculate size of segment and tube
float segmentSize = 2 * Pi / numSegments;
float tubeSize = 2 * Pi / numTubes;

// Create floats for our xyz coordinates
float x = 0;
float y = 0;
float z = 0;

// Init temp lists with tubes and segments
ArrayList segmentList = new ArrayList();
ArrayList tubeList = new ArrayList();

// Loop through number of tubes
for (int i = 0; i < numSegments; i++)
{
tubeList = new ArrayList();

for (int j = 0; j < numTubes; j++)
{
// Calculate X, Y, Z coordinates.
x = (segmentRadius + tubeRadius * Mathf.Cos(j * tubeSize)) * Mathf.Cos(i * segmentSize);
y = (segmentRadius + tubeRadius * Mathf.Cos(j * tubeSize)) * Mathf.Sin(i * segmentSize);
z = tubeRadius * Mathf.Sin(j * tubeSize);

// Add the vertex to the tubeList

// Add the vertex to global vertex list
}

// Add the filled tubeList to the segmentList
}

// Loop through the segments
for (int i = 0; i < segmentList.Count; i++)
{
// Find next (or first) segment offset
int n = (i + 1) % segmentList.Count;

// Find current and next segments
ArrayList currentTube = (ArrayList)segmentList*;*
``````

ArrayList nextTube = (ArrayList)segmentList[n];

// Loop through the vertices in the tube
for (int j = 0; j < currentTube.Count; j++)
{
// Find next (or first) vertex offset
int m = (j + 1) % currentTube.Count;

// Find the 4 vertices that make up a quad
Vector3 v1 = (Vector3)currentTube[j];
Vector3 v2 = (Vector3)currentTube[m];
Vector3 v3 = (Vector3)nextTube[m];
Vector3 v4 = (Vector3)nextTube[j];

// Draw the first triangle

}
}

Mesh mesh = new Mesh();

Vector3[] vertices = new Vector3[totalVertices];
verticesList.CopyTo(vertices);
int[] triangles = new int[totalIndices];
indicesList.CopyTo(triangles);
mesh.vertices = vertices;
mesh.triangles = triangles;

mesh.RecalculateBounds();
mesh.Optimize();
MeshFilter mFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
mFilter.mesh = mesh;
}
}
Right now, the whole torus is in one color. Is there a way to assign different colors to the segments as in http://www.paulsprojects.net/tutorials/simplebump/simplebump2.jpg? In this photo only two colors have been used. I want a unique color for each segment.
Thanks.

I would apply the color to the vertices:

http://docs.unity3d.com/Documentation/ScriptReference/Mesh-colors.html

and use a shader that uses the vertex colors. For example:

http://wiki.unity3d.com/index.php?title=VertexColor

You probably wonâ€™t end up with the clearly defined colors on a low poly mesh though.