Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kobus · Apr 24, 2018 at 09:38 AM · meshuv coordinates

Procedural UV problem?

Hi I am trying out procedural mesh generation. I am managing the vertices and triangles, but am struggling with the UVs. I started by just creating a procedural cube. I thought this is a good shape to start with because the different sides all have differing values for x, y, z. In my code I am looping through all the vertices to create the UV list, but at the moment I am only using the x- and y-coordinates of the vertices to generate the UVs. However the UVs only work correctly for some of the sides (the sides in the x,y plane). I am trying to come up with an algorithm that can generate the UV coordinate correctly for whichever vertex. Can someone point me in the right direction to help me convert the Vector3 vertex coordinate to the Vector2 UV coordinate?

My code currently is as follows:

 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshCollider))]
 public class CubeMesh_Script : MonoBehaviour {
 
     [SerializeField]
     private Material myMaterial = null;
     private MeshRenderer meshRenderer = null;
     private MeshFilter meshFilter = null;
     private Mesh mesh = null;
     private MeshCollider meshCollider = null;
 
         
     // Use this for initialization
     void Start () {
         InitMesh();
         CreateMesh();
     }
         
 
     private void InitMesh()
     {
         meshRenderer = gameObject.GetComponent<MeshRenderer>();
         meshFilter = gameObject.GetComponent<MeshFilter>();
         mesh = meshFilter.mesh;
         mesh.Clear();
         meshCollider = gameObject.GetComponent<MeshCollider>();
     }
     
 
     private void CreateMesh()
     {
         List<Vector3> vertsList = DoVertices();
         mesh.SetVertices(vertsList);
         mesh.SetTriangles(DoTriangles(vertsList), 0);
         mesh.SetUVs(0, DoUVs(vertsList));
 
         meshRenderer.material = myMaterial;
     }
     
 
     private List<Vector3> DoVertices()
     {
         List<Vector3> vertsList = new List<Vector3>();
 
         // side 1
         vertsList.Add(new Vector3(-1f, 0f, -1f));
         vertsList.Add(new Vector3(1f, 0f, -1f));
         vertsList.Add(new Vector3(-1f, 2f, -1f));
         vertsList.Add(new Vector3(1f, 2f, -1f));
 
         // side 2
         vertsList.Add(new Vector3(1f, 0f, -1f));
         vertsList.Add(new Vector3(1f, 0f, 1f));
         vertsList.Add(new Vector3(1f, 2f, -1f));
         vertsList.Add(new Vector3(1f, 2f, 1f));
 
         // side 3
         vertsList.Add(new Vector3(1f, 0f, 1f));
         vertsList.Add(new Vector3(-1f, 0f, 1f));
         vertsList.Add(new Vector3(1f, 2f, 1f));
         vertsList.Add(new Vector3(-1f, 2f, 1f));
 
         // side 4
         vertsList.Add(new Vector3(-1f, 0f, 1f));
         vertsList.Add(new Vector3(-1f, 0f, -1f));
         vertsList.Add(new Vector3(-1f, 2f, 1f));
         vertsList.Add(new Vector3(-1f, 2f, -1f));
 
         // top
         vertsList.Add(new Vector3(-1f, 2f, -1f));
         vertsList.Add(new Vector3(1f, 2f, -1f));
         vertsList.Add(new Vector3(-1f, 2f, 1f));
         vertsList.Add(new Vector3(1f, 2f, 1f));
 
         // bottom
         vertsList.Add(new Vector3(-1f, 0f, 1f));
         vertsList.Add(new Vector3(1f, 0f, 1f));
         vertsList.Add(new Vector3(-1f, 0f, -1f));
         vertsList.Add(new Vector3(1f, 0f, -1f));
 
         return vertsList;
     }
     
 
     private List<int> DoTriangles(List<Vector3> vertsListP)
     {
         List<int> trisList = new List<int>();
 
         for (int index = 0; index < vertsListP.Count; index += 4)
         {
             trisList.Add(index);
             trisList.Add(index + 2);
             trisList.Add(index + 3);
             trisList.Add(index);
             trisList.Add(index + 3);
             trisList.Add(index + 1);
         }
 
         return trisList;
     }
    
 
     private List<Vector2> DoUVs(List<Vector3> vertsListP)
     {
         List<Vector2> uvsList = new List<Vector2>();
 
         for (int index = 0; index < vertsListP.Count; index++)
         {
             float uvX = vertsListP[index].x;
             float uvY = vertsListP[index].y;
             uvsList.Add(new Vector2(uvX, uvY));
         }
 
         return uvsList;
     }
 
 }
 
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 Harinezumi · Apr 24, 2018 at 11:04 AM

UV coordinates should fall in the range [0, 1] (inclusive). As some of your vertices have negative and greater than 1 coordinates, DoUVs() will also assign negative UVs.

Fortunately, the way you set up the vertices of the cube makes it really easy to set up your UVs (that is, this isn't general, but will work for your current setup):

 private List<Vector2> DoUVs(List<Vector3> vertsListP) {
     List<Vector2> uvsList = new List<Vector2>();
 
     for (int index = 0; index < vertsListP.Count; index++) {
         int uvIndex = index % 4;
         switch (uvIndex) {
             case 0: { uvsList.Add(new Vector2(0, 0)); break; }
             case 1: { uvsList.Add(new Vector2(1, 0)); break; }
             case 2: { uvsList.Add(new Vector2(0, 1)); break; }
             case 3: { uvsList.Add(new Vector2(1, 1)); break; }
         }
     }
 
     return uvsList;
 }
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

89 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

Related Questions

How to get uvCordinat of raycast hit without mesh collider 1 Answer

UV V coordinate Not showing on Texture. 1 Answer

Texture on custom mesh splits at edge. 1 Answer

need guide in uv texture 0 Answers

Vertex Colors not smooth enough? 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