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 xmanreturn · Apr 02, 2018 at 09:17 PM · proceduraluvuv mappingprocedural texturing

procedural texture uv error

I am creating a game with procedural generated plane and uv, it works mostly fine, but there is one place where the uv seems distorted in a very confusing way.

Intro: the uv is mapped in a "Global" uv coordinates, instead of a local one, because the texture has to be aligned with adjacent planes. Each texture has a tiling of 2*2, I introduce a uv scale of 2 which means each plane "v" coordinates will be mapped in range [0, 0.5] of the texture vertically, to make sure horizontally "u" coordinates do not go out of range [0, 1]. (horizontally the noise is unpredictable to some extend). code:

         Vector2[] CalculateU(float[] leftXMap, float[] rightXMap) {
             int length = leftXMap.Length;
             float totalHeight = (length - 1) * 1;       // distance between two vertices vertically is 1
             float uvheight = totalHeight * uvScale;     // uvScale is 2, purpose is to map a plane's v(vertically), into a range of [0, 0.5]..
             Vector2[] UMap = new Vector2[length * 2];
             for(int i = 0; i < length; i++) {
                 float left = leftXMap[i];
                 float right = rightXMap[i];
                 // make left and right positive.
                 while (left < 0) {
                     left += uvheight;
                 }
                 while(right < 0) {
                     right += uvheight;
                 }
                 float leftu = (left % uvheight) / uvheight;
                 float leftv = (i / (float)(length-1)) / uvScale;
                 float rightu = (right % uvheight) / uvheight;
                 float rightv = leftv; //(i / (float)length) / 2f;
                 UMap[i * 2] = new Vector2(leftu, leftv);
                 UMap[i * 2 + 1] = new Vector2(rightu, rightv);
                 
             }
         }

the distorted uv : alt text

the detailed : alt text

distorted.png (220.0 kB)
distorted-detail.png (115.1 kB)
Comment
Add comment · Show 4
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 xmanreturn · Apr 02, 2018 at 09:20 PM 0
Share

clarification:

the parameters for the function, are the noise maps for the generated plane. the noise is the x coordinates. while the y coordinates are simple value of 0, 1, 2, .... (length-1).

avatar image Eno-Khaon · Apr 02, 2018 at 10:20 PM 0
Share

For a little further clarification, where exactly is this "one place" where the uvs wind up distorted? For example, is it near 0, where you'd flip between positive and negative value behavior?

Seeing how the UVs should fundamentally match the mesh, *does* it match? Here's an example script to help verify that element:

 // DrawDebugUVs.cs
 using UnityEngine;
 
 public class DrawDebugUVs : $$anonymous$$onoBehaviour
 {
     public $$anonymous$$eyCode keyCode = $$anonymous$$eyCode.P;
 
     Vector2[] uvs;
     int[] tris;
 
     public Vector3 position = Vector3.zero;
     public Vector3 rotation = Vector3.zero;
 
     public int uvCount;
 
     private void GetUVs()
     {
         $$anonymous$$eshFilter mf = gameObject.GetComponent<$$anonymous$$eshFilter>();
         $$anonymous$$esh m = mf.mesh;
 
         uvs = m.uv;
         tris = m.triangles;
         uvCount = uvs.Length;
 
         Debug.Log("Getting uvs for " + gameObject.name + ": UV count = " + uvs.Length + ", triangle count = " + tris.Length);
     }
 
     private void Update()
     {
         if(Input.Get$$anonymous$$eyDown(keyCode))
         {
             GetUVs();
         }
 
         if(uvs != null && uvs.Length > 0)
         {
             Quaternion rot = Quaternion.Euler(rotation);
             // Draw 0-1 outline box
             Debug.DrawLine(position, position + (rot * Vector3.up), Color.black);
             Debug.DrawLine(position + (rot * Vector3.up), position + (rot * new Vector3(1, 1, 0)), Color.green);
             Debug.DrawLine(position + (rot * new Vector3(1, 1, 0)), position + (rot * Vector3.right), Color.red);
             Debug.DrawLine(position + (rot * Vector3.right), position, Color.black);
 
             // Draw each uv triangle
             for(int i = 0; i < tris.Length; i+=3)
             {
                 Debug.DrawLine(position + (rot * uvs[tris[i + 0]]), position + (rot * uvs[tris[i + 1]]));
                 Debug.DrawLine(position + (rot * uvs[tris[i + 1]]), position + (rot * uvs[tris[i + 2]]));
                 Debug.DrawLine(position + (rot * uvs[tris[i + 2]]), position + (rot * uvs[tris[i + 0]]));
             }
         }
     }
 }

Note: This script uses a hotkey (currently a default of 'P') to activate

avatar image xmanreturn Eno-Khaon · Apr 03, 2018 at 06:11 AM 0
Share

wow, I checked again and you are right, it's the place where x coordinates turn negative.

Great thanks!!! I will use that code for a test.

avatar image xmanreturn Eno-Khaon · Apr 03, 2018 at 06:36 AM 0
Share

O$$anonymous$$G the problem is solved

I used to map the negative X coordinate values to positive values, then calculate UV coordinates based on the positive values.

But then there is problem, in my case the left noise map is negative, right noise map is positive, after mapping the left turns positive, but way larger than right coordinate value, then the UV is messed

I turned off mapping, it turns out correct.

Which means (very horrible to me), UV coordinate values can be negative, I mean it is $$anonymous$$d blowing

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

82 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

Related Questions

Polygon buffering/offsetting? 1 Answer

Unity procedural mesh generation UV texture flickering horizontal lines 0 Answers

How do we generate a UV mapping data for a complicated mesh? 0 Answers

Is it possible to combine materials into a pattern at runtime? 0 Answers

Procedural Mesh UVs Edge Case? 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