Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Saiaku · Mar 11, 2019 at 02:41 PM · meshuv mapping

Figuring out UVs for a circle mesh created by code

I'm trying to generate a circular mesh made up of triangles with a common center at the center of the circle.

I want the UVs to point outward, so that a moving shader applied to this will seem like it's flowing from the center to the edges. This already happens naturally on the sphere primitive, but I only need a flat surface without an underside.

mesh

I use this code to generate the mesh. I thought the UV points would be similar to the vertexes, but I'm not getting any good results.

 private void _MakeMesh(int sides, float radius = 0.5f)
 {
 m_LiquidMesh.Clear();
 float angleStep = 360.0f / (float) sides;
 List<Vector3> vertexes      = new List<Vector3>();
 List<int>       triangles   = new List<int>();
 List<Vector2>   uvs         = new List<Vector2>();
 Quaternion      rotation  = Quaternion.Euler(0.0f, angleStep, 0.0f);
 
 // Make first triangle.
 vertexes.Add(new Vector3(0.0f, 0.0f, 0.0f));  
 vertexes.Add(new Vector3(radius, 0.0f, 0.0f));    
 vertexes.Add(rotation * vertexes[1]);        
 
 // First UV ??
 uvs.Add(new Vector2(0, 0));
 uvs.Add(new Vector2(1, 0));
 uvs.Add(rotation * uvs[1]);
 
 // Add triangle indices.
 triangles.Add(0);
 triangles.Add(1);
 triangles.Add(2);
 for (int i = 0; i < sides - 1; i++)
 {
     triangles.Add(0); 
     triangles.Add(vertexes.Count - 1);
     triangles.Add(vertexes.Count);
 
     // UV ??
 
     vertexes.Add(rotation * vertexes[vertexes.Count - 1]);
 }
 m_LiquidMesh.vertices   = vertexes.ToArray();
 m_LiquidMesh.triangles  = triangles.ToArray();
 m_LiquidMesh.uv         = uvs.ToArray();
 
 m_LiquidMesh.RecalculateNormals();
 m_LiquidMesh.RecalculateBounds();
 }

Please, where am I going wrong with this?

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
0

Answer by GuessGen · May 03 at 01:24 PM

This is an old post, but maybe someone else will benefit from my solution.

So basically I gave my center point the center of the uv (0.5, 0.5) and then used the used circle formula to give every other point the uv coordinate. But of course I had to remap the cos and sin results from -1..1 to 0..1 and everything is working great.

     Vector2[] uv = new Vector2[vertices.Length];
     uv[uv.Length - 1] = new Vector2(0.5f, 0.5f);

     for (int i = 0; i < uv.Length - 1; i++)
     {
         float radians = (float) i / (uv.Length - 1) * 2 * Mathf.PI;
         uv[i] = new Vector2(Mathf.Cos(radians).Remap(-1f, 1f, 0f, 1f), Mathf.Sin(radians).Remap(-1f, 1f, 0f, 1f));
     }
         
     mesh.uv = uv;

Where the remap is an extension like this and it basically take a value in a range and remaps it to another range (in this case from -1..1 to 0..1):

 public static float Remap(this float value, float from1, float to1, float from2, float to2) {
     return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
 }
Comment
Add comment · 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

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

178 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Help setting UV coordinates for code-generated plane 0 Answers

Custom Cube Mesh Incompatible with Normal/Height Maps 0 Answers

Weird shadows on my mesh 1 Answer

How to correct texture mapping on arbitrary quadrilaterals ? 2 Answers

instantiate gameobject with the uv 0 Answers


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