Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ketan12345 · Jun 01, 2013 at 01:24 PM · meshcolormeshrenderer

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 float segmentRadius = 1f;
         public float tubeRadius = 0.1f;
         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
                     tubeList.Add(new Vector3(x, z, y));
 
                     // Add the vertex to global vertex list
                     verticesList.Add(new Vector3(x, z, y));
                 }
 
                 // Add the filled tubeList to the segmentList
                 segmentList.Add(tubeList);
             }
 
             // 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[i];
                 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
                     indicesList.Add((int)verticesList.IndexOf(v1));
                     indicesList.Add((int)verticesList.IndexOf(v2));
                     indicesList.Add((int)verticesList.IndexOf(v3));
 
                     // Finish the quad
                     indicesList.Add((int)verticesList.IndexOf(v3));
                     indicesList.Add((int)verticesList.IndexOf(v4));
                     indicesList.Add((int)verticesList.IndexOf(v1));
                 }
             }
             
             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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by electricsauce · Jun 01, 2013 at 03:08 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ketan12345 · Jun 01, 2013 at 07:01 PM 0
Share

Does not work. I added a VertexLit shader and the following code after line 120:

     Vector3[] vertices$$anonymous$$ = mesh.vertices;
     Color[] colors = new Color[vertices$$anonymous$$.Length];
     int c = 0;
     while (c < vertices$$anonymous$$.Length) {
         colors[c] = Color.Lerp(Color.red, Color.green, vertices[c].y);
         c++;
     }
     mesh.colors = colors;

Unity also gives me a warning Shader wants texture coordinates, but the mesh doesn't have them

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't the color changing? 1 Answer

why does Mesh.SetColors take a list, but Mesh.colors is an array? 0 Answers

How to Make Imported MagicaVoxel 3D Object Flash White 0 Answers

setting vertex color of mesh not working 2 Answers

My meshes arent loading properly. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges