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 Cole1220 · May 19, 2016 at 02:48 AM · verticesprocedural meshuv mappingprocedural generation

[Solved] Issues mapping procedurally generated cube without repeating vertices

Having an issue UV mapping a procedurally generated cube without duplicate vertices (Generated through rings and then top and bottom covers (not 6 quads lined up with each other)). Get the triangles where I'm reusing vertices all stretched. I'm assuming it's because the map is wrapping all the way back from where it currently is to the vertex it wants. Does anyone know a solution where I can keep my array without duplicating vertices and not get that image distortion?

 //Creates vertices
 protected void CreateVertices()
     {
         int cornerVertices = 8;
         int edgeVertices = (gridSize + gridSize + gridSize - 3) * 4;
         int faceVertices = (
             (gridSize - 1) * (gridSize - 1) +
             (gridSize - 1) * (gridSize - 1) +
             (gridSize - 1) * (gridSize - 1)) * 2;
         vertices = new Vector3[cornerVertices + edgeVertices + faceVertices];
         normals = new Vector3[vertices.Length];
         uvs = new Vector2[vertices.Length];
 
         int v = 0;
         for (int y = 0; y <= gridSize; y++)
         {
             for (int x = 0; x <= gridSize; x++)
             {
                 SetVertex(v++, x, y, 0, FRONT);
             }
             for (int z = 1; z <= gridSize; z++)
             {
                 SetVertex(v++, gridSize, y, z, RIGHT);
             }
             for (int x = gridSize - 1; x >= 0; x--)
             {
                 SetVertex(v++, x, y, gridSize, BACK);
             }
             for (int z = gridSize - 1; z > 0; z--)
             {
                 SetVertex(v++, 0, y, z, LEFT);
             }
         }
         //Top face
         for (int z = 1; z < gridSize; z++)
         {
             for (int x = 1; x < gridSize; x++)
             {
                 SetVertex(v++, x, gridSize, z, TOP);
             }
         }
         //Bottom face
         for (int z = 1; z < gridSize; z++)
         {
             for (int x = 1; x < gridSize; x++)
             {
                 SetVertex(v++, x, 0, z, BOTTOM);
             }
         }
 
         mesh.vertices = vertices;
         mesh.normals = normals;
         mesh.uv = uvs;
     }
 
 
 //Function call to set Vertex and UV coords
 protected void SetVertex(int i, int x, int y, int z, int face = 10)
     {
         Vector3 v = new Vector3(x, y, z) * 2f / gridSize - Vector3.one;
         float x2 = v.x * v.x;
         float y2 = v.y * v.y;
         float z2 = v.z * v.z;
         Vector3 s;
         s.x = v.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f);
         s.y = v.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f);
         s.z = v.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
 
         normals[i] = s;
         vertices[i] = v; //Map a cube
 
         switch (face)
         {
             case 0:
                 uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
                 break;
             case 5:
                 uvs[i] = new Vector2(vertices[i].x , -vertices[i].z);
                 break;
             case 1:
                 uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
                 break;
             case 3:
                 uvs[i] = new Vector2(-vertices[i].x, vertices[i].y);
                 break;
             case 2:
                 uvs[i] = new Vector2(vertices[i].z, vertices[i].y);
                 break;
             case 4:
                 uvs[i] = new Vector2(-vertices[i].z, vertices[i].y);
                 break;
             default:
                 break;
         }
 
         vertices[i] = normals[i] * radius; //Make it a sphere now that it's mapped
     }

alt text

edgeverticeissue.png (303.6 kB)
Comment
Add comment · Show 5
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 Eno-Khaon · May 19, 2016 at 03:51 AM 0
Share

If you set your texture to use point filtering (none) rather than bilinear or trilinear, do you still experience the same problem?

avatar image Cole1220 · May 19, 2016 at 05:03 AM 0
Share

Yes I do. Just lower or higher quality. Clamping texture breaks it. I'm pretty sure it has to do with how I'm referencing the vertices, but no idea how to fix

avatar image Eno-Khaon Cole1220 · May 19, 2016 at 09:34 AM 0
Share

Hmm... as a second possibility, then, it may be that your UVs are connected to each other around those cube face divisions. I can't guarantee anything where the texture seam appears between two faces with the same alignment (top and right of the visible three in the image), but if the uvs are connected, then it may be that the edges of the uvs are stretched across the entire uv map.

If that is the case, then this script should help make what you're seeing more obvious. With a view of the UVs, you'll be able to see whether everything looks organized and aligned or if some of the vertices:

 // C#
 // ViewUVs.cs
 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class ViewUVs : $$anonymous$$onoBehaviour
 {
     [SerializeField]
     $$anonymous$$esh m;
     [SerializeField]
     int[] t;
     [SerializeField]
     Vector2[] uv;
     public Vector3 position = new Vector3(0, 0, 0);
     public float scale = 5.0f;
     public float rotationAngle = 0;
     public Vector3 rotationAxis = Vector3.up;
 
     void Start()
     {
         if(gameObject.GetComponent<$$anonymous$$eshFilter>() == null)
         {
             Debug.LogError("$$anonymous$$eshFilter not found! Script not added to object.");
             DestroyImmediate(this);
         }
         else
         {
             m = gameObject.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;
             t = m.triangles;
             uv = m.uv;
 
             position = transform.position + (Vector3.up * 5);
         }
     }
 
     void OnDrawGizmos()
     {
 
         Quaternion rotation = Quaternion.AngleAxis(rotationAngle, rotationAxis.normalized);
         for(int i = 0; i < t.Length; i += 3)
         {
             Gizmos.DrawLine(position + (rotation * new Vector3(uv[t[i]].x, uv[t[i]].y, 0)) * scale, position + (rotation * new Vector3(uv[t[i + 1]].x, uv[t[i + 1]].y, 0)) * scale);
             Gizmos.DrawLine(position + (rotation * new Vector3(uv[t[i + 1]].x, uv[t[i + 1]].y, 0)) * scale, position + (rotation * new Vector3(uv[t[i + 2]].x, uv[t[i + 2]].y, 0)) * scale);
             Gizmos.DrawLine(position + (rotation * new Vector3(uv[t[i + 2]].x, uv[t[i + 2]].y, 0)) * scale, position + (rotation * new Vector3(uv[t[i]].x, uv[t[i]].y, 0)) * scale);
         }
     }
 }
avatar image Bunny83 · May 19, 2016 at 10:14 AM 1
Share

Just want to add that i've created such a script several times now ^^. Here's one of them:

http://answers.unity3d.com/answers/1078038/view.html

You can't create a sphere / cube without any duplicated vertices if you want to individually want to UV map each side. A UV seam requires it's own unique vertices.

The script i've made creates an arbitrary large sphere mesh which is split in at least 6 seperate meshes (or even more if needed) to overcome Unity's vertex limit per mesh.

If you want to fix your code you might want to reduce your GridSize drastically for debugging (to lets say 2 or 3). That way it should be more clear which triangle references which vertices.

Your for loops look suspicious as some go to gridsize and some to (gridsize-1). And the z loop only down to 1 and not 0.

It's hard to tell where it went wrong especially you haven't included the triangle generation.

avatar image Cole1220 Bunny83 · May 19, 2016 at 07:39 PM 0
Share

Yea that solution is way better than finding a solution to this problem. I could explain why I went about it the way I did but since the UV can't be created for it, no point. This also sets things so that I can separate mesh into chunks. Thank you for the insight and putting an end to my search.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Modifying a procedural mesh using Lists is slow, what else can I use? 1 Answer

For loop not starting from zero 0 Answers

Having 6x times more vertices than in 3d max. 1 Answer

need guide in uv texture 0 Answers

Performance increase by unused Vertices? 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