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
2
Question by fschaar · Aug 01, 2012 at 12:04 PM · javascripttextureuvprimitives

Apply uv coordinates to unity cube by script

I'm currently using built-in primitives from unity and I don't need more for my game. Now I want to place a texture on one side of a cube primitive. As far as I know, that is only possible, with uv coordinates. Is it possible to apply such coordinates by script or has anyone another idea, to achive this without messing up with a 3d software?

Comment
Add comment · Show 1
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 AlucardJay · Aug 01, 2012 at 12:11 PM 0
Share

This is interesting, I am trying the same thing right now! And that spins me out slightly =]

Below is a script to read the mesh info from the cube. I however am having trouble assigning the UV's for the back face.

I have just discovered if you follow the verts, they are all messed up. I am drawing out the tri's with the vert numbers to find what vert goes with what tri to set the UVs.

Check my answer below ($$anonymous$$ $$anonymous$$ay).

1 Reply

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

Answer by AlucardJay · Aug 01, 2012 at 12:56 PM

First I read the native cube mesh with :

 #pragma strict

 public var theCube : Transform;

 private var theVerts : String = "";
 private var theUVs : String = "";
 private var theNorms : String = "";
 private var theTris : String = "";

 function Start ()
 {
     if (theCube == null) 
         theCube = this.transform;
 
     var theMesh : Mesh = theCube.GetComponent(MeshFilter).mesh as Mesh;
 
     theVerts = theVerts + "(" + theMesh.vertices.length.ToString() + ") :";
     theUVs = theUVs + "(" + theMesh.uv.length.ToString() + ") :";
     theNorms = theNorms + "(" + theMesh.normals.length.ToString() + ") :";
     theTris = theTris + "(" + theMesh.triangles.length.ToString() + ") :";
 
     for (var i:int = 0; i < theMesh.vertices.length; i ++) {
         theVerts = theVerts + "  " + i + "=" + theMesh.vertices[i].ToString();
         theUVs = theUVs + "  " + i + "=" + theMesh.uv[i].ToString();
         theNorms = theNorms + "  " + i + "=" + theMesh.normals[i].ToString();
     }
 
     for (i = 0; i < theMesh.triangles.length; i ++) {
         theTris = theTris + "  " + i + "=" + theMesh.triangles[i].ToString();
     }
 }

 function OnGUI ()
 {
     GUI.Label( Rect( 10, Screen.height * 0.00, Screen.width - 20, Screen.height * 0.25), "Verts " + theVerts );
     GUI.Label( Rect( 10, Screen.height * 0.25, Screen.width - 20, Screen.height * 0.25), "UVs " + theUVs );
     GUI.Label( Rect( 10, Screen.height * 0.50, Screen.width - 20, Screen.height * 0.25), "Normals " + theNorms );
     GUI.Label( Rect( 10, Screen.height * 0.75, Screen.width - 20, Screen.height * 0.25), "Tris " + theTris );
 }





With the information displayed from OnGUI, I wrote down on a piece of paper each vertices location, then each triangle connection between the vertices.

Imagine looking at the front of the cube, the first 4 vertices are arranged like so

 //   2 --- 3
 //   |     |
 //   |     |
 //   0 --- 1

then the UV's are mapped as follows :

 theUVs[2] = Vector2( 0, 1 );
 theUVs[3] = Vector2( 1, 1 );
 theUVs[0] = Vector2( 0, 0 );
 theUVs[1] = Vector2( 1, 0 );

 //    2    3    0    1   Front
 //    6    7   10   11   Back
 //   19   17   16   18   Left
 //   23   21   20   22   Right
 //    4    5    8    9   Top
 //   15   13   12   14   Bottom

So where the UV's for Vertices 2 is ( 0, 1 ), so is 6, 19, 23, 4, 15.

You can test this by replacing all the zero's with 0.5, then check every face of the cube has the same portion of the image, and rotated the correct way (not inverted) . Note if standing at the origin looking up the positive Z-Axis, Right is your right.

Comment
Add comment · Show 9 · 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 fschaar · Aug 01, 2012 at 01:29 PM 0
Share

Ok I have found the uv coordinates an I can manipulate them. But I'm not quite sure what the numbers mean. there are 4 uv coordinates for the whole cube?? Or more? I can add only one texture to the object so I have to put all images, i want to assign to the different sides of the cube together in one image - that means two images - one for the front side and one für the other sides - merged to one image The Vector2 saved in each uv are the image coordinates? ore the percetage of the image?

So if I want a special image on the top i have to manipulate the uvs 9,8,5,4?? right?

avatar image AlucardJay · Aug 01, 2012 at 01:37 PM 0
Share

The UVs are the point of the image to set on the vertices. the UV coordinates are in relation to your image.

normally when you draw a picture, the top-left is 0,0 and the bottom right is 1,1

Unity UV's are up-side-down. So the top of your picture is 0,1 and the bottom is 1,0. This just makes it even more confusing!

Ok so imagine this is the picture/texture/material :

 // 0,0 ------------ 1,0
 //     |          |
 //     |   PIC    |
 //     |          |
 // 0,1 ------------ 1,1


In unity it is actually

 // 0,1 ------------ 1,1
 //     |          |
 //     |   PIC    |
 //     |          |
 // 0,0 ------------ 1,0


so say I want the top left quarter of the image on the cube, the UV coordinates would be

 Top-Left of texture     : 0.0 , 1.0
 Top-Right of texture    : 0.5 , 1.0
 Bottom-Left of texture  : 0.0 , 0.5
 Bottom-Right of texture : 0.5 , 0.5

That's the UV's you tell to the vertices.

avatar image fschaar · Aug 01, 2012 at 01:49 PM 0
Share

Ok, I think I got it - I will try it out and let you know, if it works fine for me ;) Thank you anyway.

avatar image AlucardJay · Aug 01, 2012 at 02:03 PM 0
Share

Ok, since you did all the theory, I'll now give you the script (I'm not sure if you're familiar with working with meshes, and I understand how frustrating it would be if you were so close to finishing, then had to learn alot of new stuff just to finish). There's other (I'll say cheating) ways of doing this, like creating a plane and parenting it to cube, but now you have this you can see what other funky stuff you can do, like changing the UVs in an Update, and therefore playing an animation on the top or any number of faces on the cube =]

Here's the script, just attach it to the cube :

 #pragma strict
 // TOP

 function Start() 
 {
     // Get the mesh
     var the$$anonymous$$esh : $$anonymous$$esh;
     the$$anonymous$$esh = this.transform.GetComponent($$anonymous$$eshFilter).mesh as $$anonymous$$esh;

     // Now store a local reference for the UVs
     var theUVs : Vector2[] = new Vector2[the$$anonymous$$esh.uv.Length];
     theUVs = the$$anonymous$$esh.uv;

     // set UV co-ordinates
     theUVs[4] = Vector2( 0.5, 1.0 );
     theUVs[5] = Vector2( 1.0, 1.0 );
     theUVs[8] = Vector2( 0.5, 0.5 );
     theUVs[9] = Vector2( 1.0, 0.5 );

     // Assign the mesh its new UVs
     the$$anonymous$$esh.uv = theUVs;
 }


I've put some different values in for the picture UVs so you can see them working in relation to your image (this should show from the top to near the bottom , and the left-half). Happy Coding =]

avatar image fschaar · Aug 01, 2012 at 02:29 PM 0
Share

Ok, it worked partly: I made an image with two images arranged horizontal. the image on the right side was attached to the top of the cube (worked perfectly!), the second image should appear on each of the other sides of the cube. But there is only a part of it appearing on the sides. here is my uv assigning code - maybe anyone can find a mistake ...

Ahhh - found it myself.

Anyway thanks for the awesome stuff - helped a lot!!

Show more comments

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Proper texture mapping with standart primitives 1 Answer

Texturing a icosphere 0 Answers

Strange smearing on texture with unity 4.3.4 1 Answer

Identical material with different textures 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