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
1
Question by michael 4 · May 13, 2011 at 05:17 PM · texturerenderuv

Generating UVs for a scripted mesh

I generated a plane through script, now I need to generate UVs for it so it can render a texture on it. I having a difficult time understanding how to do this as im used to the visual tools I use in 3d packages.

All I would like to do is stretch out the texture to be the size of my scripted plane.

I used this example from the documentation, It allowed me to render a texture but not correctly (just a bunch of lines) I messed with some of its parameters and havnt had any luck.

        Vector3[] vertices = new Vector3[4];
    Vector2[] uvs = new Vector2[vertices.Length];
    int i = 0;
    while (i < uvs.Length) {
        uvs[i] = new Vector2(vertices[i].y, vertices[i].x);
        i++;
    }
    m_Mesh.uv = uvs;
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

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by flaviusxvii · May 13, 2011 at 05:42 PM

You'll want to divide vertices[i].y and vertices[i].x by the width and height of your plane. Your UV values should be between 0 and 1 in both dimensions if you want the texture to just stretch across the plane and not repeat.

Comment
Add comment · Show 4 · 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 Owen-Reynolds · May 13, 2011 at 06:22 PM 0
Share

Or, since he only has 4 verts, just handset them to (0,0) (0,1) (1,1) and (1,0): uvs[0]=new Vector2(0,0); .... (0,0) is the lower-left corner and (1,1) is the upper-right. For a subdivided plane, yeah, you have to divide.

avatar image flaviusxvii · May 13, 2011 at 06:26 PM 0
Share

True. I was addressing the more general case but you're absolutely right.

avatar image michael 4 · May 13, 2011 at 08:37 PM 0
Share

Thanks, and Im assu$$anonymous$$g in cases where I want the texture to repeat I just have to make the number in the axis even smaller, correct?

avatar image flaviusxvii · May 13, 2011 at 09:10 PM 0
Share

If you want the texture to repeat 3 times, then multiply your UV values by 3.

avatar image
3
Wiki

Answer by intrepidis · Aug 02, 2014 at 08:37 PM

I have created this class to calculate UV's for any given triangular vertices. It works for meshes that are programmatically generated from triangles (where the triangle vertices are stored clockwise). It assumes the length of the vertex array is a multiple of 3 (it doesn't check the array's length).

The public method's first parameter takes the triangle vertices (in multiples of 3). The second parameter is a float, used to scale the texture coordinates (just use 1.0f to have default scaling). It returns an array of UV values, the array will be the same length as the input vertices array.

 public class UvCalculator
 {
     private enum Facing { Up, Forward, Right };
     
     public static Vector2[] CalculateUVs(Vector3[] v/*vertices*/, float scale)
     {
         var uvs = new Vector2[v.Length];
         
         for (int i = 0 ; i < uvs.Length; i += 3)
         {
             int i0 = i;
             int i1 = i+1;
             int i2 = i+2;
             
             Vector3 v0 = v[i0];
             Vector3 v1 = v[i1];
             Vector3 v2 = v[i2];
             
             Vector3 side1 = v1 - v0;
             Vector3 side2 = v2 - v0;
             var direction = Vector3.Cross(side1, side2);
             var facing = FacingDirection(direction);
             switch (facing)
             {
             case Facing.Forward:
                 uvs[i0] = ScaledUV(v0.x, v0.y, scale);
                 uvs[i1] = ScaledUV(v1.x, v1.y, scale);
                 uvs[i2] = ScaledUV(v2.x, v2.y, scale);
                 break;
             case Facing.Up:
                 uvs[i0] = ScaledUV(v0.x, v0.z, scale);
                 uvs[i1] = ScaledUV(v1.x, v1.z, scale);
                 uvs[i2] = ScaledUV(v2.x, v2.z, scale);
                 break;
             case Facing.Right:
                 uvs[i0] = ScaledUV(v0.y, v0.z, scale);
                 uvs[i1] = ScaledUV(v1.y, v1.z, scale);
                 uvs[i2] = ScaledUV(v2.y, v2.z, scale);
                 break;
             }
         }
         return uvs;
     }
     
     private static bool FacesThisWay(Vector3 v, Vector3 dir, Facing p, ref float maxDot, ref Facing ret)
     {
         float t = Vector3.Dot(v, dir);
         if (t > maxDot)
         {
             ret = p;
             maxDot = t;
             return true;
         }
         return false;
     }
     
     private static Facing FacingDirection(Vector3 v)
     {
         var ret = Facing.Up;
         float maxDot = Mathf.NegativeInfinity;
         
         if (!FacesThisWay(v, Vector3.right, Facing.Right, ref maxDot, ref ret))
             FacesThisWay(v, Vector3.left, Facing.Right, ref maxDot, ref ret);
         
         if (!FacesThisWay(v, Vector3.forward, Facing.Forward, ref maxDot, ref ret))
             FacesThisWay(v, Vector3.back, Facing.Forward, ref maxDot, ref ret);
         
         if (!FacesThisWay(v, Vector3.up, Facing.Up, ref maxDot, ref ret))
             FacesThisWay(v, Vector3.down, Facing.Up, ref maxDot, ref ret);
         
         return ret;
     }
     
     private static Vector2 ScaledUV(float uv1, float uv2, float scale)
     {
         return new Vector2(uv1 / scale, uv2 / scale);
     }
 }
Comment
Add comment · Show 4 · 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 mnml_ · Jul 22, 2015 at 08:41 AM 2
Share

initialize maxDot with 0 ins$$anonymous$$d of NegativeInfinity and there's a bug gone ;)

avatar image Em3rgency mnml_ · Jan 19, 2019 at 10:33 AM 0
Share

THAN$$anonymous$$ YOU! This was driving me crazy I couldn't figure out what the issue was.

avatar image vincewins · Jan 28, 2019 at 11:39 AM 0
Share

I have 4 vertices and this does not work for me: IndexOutOfRangeException: Index was outside the bounds of the array. UvCalculator.CalculateUVs (UnityEngine.Vector3[] v, System.Single scale)

avatar image Em3rgency vincewins · Jan 28, 2019 at 12:20 PM 1
Share

Well he did say his code assumes your vertices are a multiple of 3. 4 is not a multiple of 3. I have run into the same issue, and its an easy fix to loop around the final odd vertices back to the beginning. Add these small if checks into the code:

                <...>
                 int i0 = i;
                 int i1 = i+1;
                 int i2 = i+2;
 
                 //Special handling if vertex count isn't a multiple of 3
                 if(i == uvs.Length - 1) {
                     i1 = 0;
                     i2 = 1;
                 }
                 if(i == uvs.Length - 2) {
                     i2 = 0;
                 }
                 
                 Vector3 v0 = v[i0];
                 Vector3 v1 = v[i1];
                 Vector3 v2 = v[i2];
 
                 <...>
avatar image
1

Answer by Bovine · May 06, 2012 at 06:47 PM

I am using Unwrapping.GenerateSecondaryUVSet() to reasonable effect for light mapping, although I find that in certain circumstances I get bleeding between unrelated geometry. There is another static UV generation method on the Unwrapping class that may also help.

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

Answer by tapticc · Nov 06, 2016 at 08:08 PM

Hi thanks @intrepidis for sharing, I had to change the Facing.Right slightly to prevent the UV from rotating 90 degrees on portions of a circular mesh:

             case Facing.Right:
                 //This was causing circular meshes to switch uv sideways
                 //uvs.Add(ScaledUV(v0.y, v0.z, scale));
                 //uvs.Add(ScaledUV(v1.y, v1.z, scale));
                 //uvs.Add(ScaledUV(v2.y, v2.z, scale));

                 uvs.Add(ScaledUV(v0.z, v0.y, scale));
                 uvs.Add(ScaledUV(v1.z, v1.y, scale));
                 uvs.Add(ScaledUV(v2.z, v2.y, scale));
                 break;
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Difference between TRANSFORM_TEX and MultiplyUV 0 Answers

texture maps render on one side only 1 Answer

can't put texture materials on imported mesh. 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