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
3
Question by Giannis7312777 · Sep 24, 2013 at 09:11 PM · texturecube

Change texture of cube sides

Hello everyone! Is there any way to change the texture of each side for a cube? Like minecaft? Please help me.

Thanks, any help is appriciated

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 robertbu · Sep 24, 2013 at 09:21 PM 1
Share

$$anonymous$$apping different sides of a cube to different textures is handled by changing the UV coordinates in the $$anonymous$$esh. Research UV coordinates and the $$anonymous$$esh class.

avatar image Giannis7312777 · Sep 24, 2013 at 09:45 PM 0
Share

Thanks for the reply. Can I create a texture from photoshop and then put it on the cube? I did it but it's was absolutely wrong :(

avatar image robertbu · Sep 24, 2013 at 09:53 PM 0
Share

Yes, you can create a texture in Photoshop and map it to side(s) of the cube. Typically the single Photoshop texture would have six "sections" one for each side. The UV would map each section to one of the sides of the cube. In theory you could construct a cube with submeshes for each side and a separate material/texture for each side. Using multiple materials is less efficient, and is somewhat complex to construct without a 3D graphics editor.

avatar image Eric5h5 · Sep 24, 2013 at 10:05 PM 0
Share

You need a 3D modeling app anyway to do the UV mapping. The built-in Unity cube just maps the entire texture to each side.

3 Replies

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

Answer by robertbu · Sep 25, 2013 at 05:48 AM

As @Eric5h5 mentions, typically the UV mapping is done in a 3D graphics app. And that is how its been done for resources I've used. But in theory the UV map of a built in-cube can be mapped to an atlas. I wanted to see how hard it would be.

Here is the sample atlas I used:

alt text

And here is the sample script. Attach the script to a cube. Create a material using the above texture and also add it to a cube.

 #pragma strict
 
 function Start () {
     var mf = GetComponent(MeshFilter);
     var mesh : Mesh;
     if (mf != null)
         mesh = mf.mesh;
         
     if (mesh == null || mesh.uv.Length != 24) {
         Debug.Log("Script needs to be attached to built-in cube");
         return;
     }
 
     var uvs = mesh.uv;
     
     // Front
     uvs[0]  = Vector2(0.0, 0.0);
     uvs[1]  = Vector2(0.333, 0.0);
     uvs[2]  = Vector2(0.0, 0.333);
     uvs[3]  = Vector2(0.333, 0.333);
     
     // Top
     uvs[8]  = Vector2(0.334, 0.0);
     uvs[9]  = Vector2(0.666, 0.0);
     uvs[4]  = Vector2(0.334, 0.333);
     uvs[5]  = Vector2(0.666, 0.333);
     
     // Back
     uvs[10] = Vector2(0.667, 0.0);
     uvs[11] = Vector2(1.0, 0.0);
     uvs[6]  = Vector2(0.667, 0.333);
     uvs[7]  = Vector2(1.0, 0.333);
     
     // Bottom
     uvs[12] = Vector2(0.0, 0.334);
     uvs[14] = Vector2(0.333, 0.334);
     uvs[15] = Vector2(0.0, 0.666);
     uvs[13] = Vector2(0.333, 0.666);                
     
     // Left
     uvs[16] = Vector2(0.334, 0.334);
     uvs[18] = Vector2(0.666, 0.334);
     uvs[19] = Vector2(0.334, 0.666);
     uvs[17] = Vector2(0.666, 0.666);    
     
     // Right        
     uvs[20] = Vector2(0.667, 0.334);
     uvs[22] = Vector2(1.00, 0.334);
     uvs[23] = Vector2(0.667, 0.666);
     uvs[21] = Vector2(1.0, 0.666);    
     
     mesh.uv = uvs;
 }



cubeatlas.png (5.0 kB)
Comment
Add comment · Show 3 · 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 Giannis7312777 · Sep 25, 2013 at 09:50 AM 0
Share

Thanks, you solved my problem :)

avatar image siddharth3322 · Oct 27, 2014 at 06:45 AM 0
Share

After seeing above uvs array, I have a question in $$anonymous$$d. How do you decide which index belongs to which phase of cube?

avatar image robertbu · Oct 27, 2014 at 07:42 AM 0
Share

When I did this, I wrote a small script to output the vertices of the mesh and looked at the values...labor intensive for anything complex.

avatar image
12

Answer by qole · Feb 12, 2016 at 01:51 PM

Here's the Unity 5 C# script equivalent of @robertbu's script for mapping to the current cube primitive. I had to shuffle the UVs around until all of the sides looked right.

 void Start () {

     Mesh mesh = GetComponent<MeshFilter>().mesh;
     Vector2[] UVs = new Vector2[mesh.vertices.Length];

     // Front
     UVs[0] = new Vector2(0.0f, 0.0f);
     UVs[1] = new Vector2(0.333f, 0.0f);
     UVs[2] = new Vector2(0.0f, 0.333f);
     UVs[3] = new Vector2(0.333f, 0.333f);

     // Top
     UVs[4] = new Vector2(0.334f, 0.333f);
     UVs[5] = new Vector2(0.666f, 0.333f);
     UVs[8] = new Vector2(0.334f, 0.0f);
     UVs[9] = new Vector2(0.666f, 0.0f);

     // Back
     UVs[6] = new Vector2(1.0f, 0.0f);
     UVs[7] = new Vector2(0.667f, 0.0f);
     UVs[10] = new Vector2(1.0f, 0.333f);
     UVs[11] = new Vector2(0.667f, 0.333f);

     // Bottom
     UVs[12] = new Vector2(0.0f, 0.334f);
     UVs[13] = new Vector2(0.0f, 0.666f);
     UVs[14] = new Vector2(0.333f, 0.666f);
     UVs[15] = new Vector2(0.333f, 0.334f);

     // Left
     UVs[16] = new Vector2(0.334f, 0.334f);
     UVs[17] = new Vector2(0.334f, 0.666f);
     UVs[18] = new Vector2(0.666f, 0.666f);
     UVs[19] = new Vector2(0.666f, 0.334f);

     // Right        
     UVs[20] = new Vector2(0.667f, 0.334f);
     UVs[21] = new Vector2(0.667f, 0.666f);
     UVs[22] = new Vector2(1.0f, 0.666f);
     UVs[23] = new Vector2(1.0f, 0.334f);

     mesh.uv = UVs;
 }

I also made a big, clean version of @robertbu's atlas, using words instead of numbers. Here is my version of the atlas: alt text


big-atlas-words.jpg (62.4 kB)
Comment
Add comment · Show 8 · 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 TransMind · Feb 17, 2016 at 04:12 PM 0
Share

After attaching this script and the material with the atlas as a texture, the cube displays the full atlas on each side. What am I missing here?

avatar image Cheater4free TransMind · Feb 23, 2016 at 06:32 PM 0
Share

You need to add GetComponent ().mesh = mesh;

avatar image impurekind Cheater4free · Aug 04, 2018 at 12:53 PM 0
Share

Where do you need to add this? And are you sure that is ALL that needs to be added? Basically, do I need to write, know or do ANYTHING else to get this to work, which for some reason neither of you mentioned above even though it's apparently a necessary step to get it to work?

avatar image Soulice · Oct 05, 2016 at 02:39 AM 0
Share

This worked like a charm. Any performance concerns?

avatar image AhsanNaeem · Aug 13, 2018 at 06:42 AM 0
Share

I am facing the same issue as reported by @Trans$$anonymous$$ind i.e. the cube displays the full atlas on each side. I created the $$anonymous$$aterial by adding the atlas image at Albedo option of the $$anonymous$$aterial. Can someone help??

avatar image SavedByZero · Oct 26, 2018 at 12:13 AM 0
Share

Worked fine for me. Everyone having trouble should make a material and use Unlit / Texture (the latter mainly for testing purposes). Thanks for the update!

avatar image radric · Dec 05, 2018 at 02:02 PM 1
Share

For those who don't see the mapping correctly while in editormode (it should work in play) you just have to add [ExecuteInEdit$$anonymous$$ode] just before the class: https://docs.unity3d.com/ScriptReference/ExecuteInEdit$$anonymous$$ode.html

avatar image Fenikkel radric · Dec 03, 2020 at 09:59 AM 0
Share

Also you need to change mesh to shared$$anonymous$$esh to avoid an error:

$$anonymous$$esh mesh = GetComponent().mesh; to $$anonymous$$esh mesh = GetComponent().shared$$anonymous$$esh;

avatar image
0

Answer by Eric5h5 · Sep 24, 2013 at 09:52 PM

You need to make a texture atlas, then create a cube in a 3D app and UV map the sides as appropriate for your texture atlas. Alternately, create each side as a submesh, and use different materials with different textures for each submesh. This is slower (more draw calls) but won't have any mipmap bleeding issues.

Comment
Add comment · Show 6 · 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 impurekind · Aug 13, 2018 at 09:11 AM 0
Share

Yeah, I ended up just creating the cube out of 6 quads and giving each one its own texture. For cubes, this is is actually simpler to understand and implement than writing texture atlases and UV mapping and stuff--at least for me. And I'm going to presume it's not that much more graphics intensive since it's still using the same amount of polygons and stuff.

avatar image Eric5h5 impurekind · Aug 13, 2018 at 09:15 AM 0
Share

It's definitely more intensive since there are 6 objects and materials ins$$anonymous$$d of 1, but if it's just a single cube, it's not really worth considering. If there are lots of cubes, it's a different story.

avatar image impurekind Eric5h5 · Aug 13, 2018 at 09:43 AM 0
Share

Strange, considering technically it's still the same amount of polys and stuff, and the six separate textures are ultimately just the same images I'd be using if I were to draw a large image map with the six different directions on it and the use code to chop it up. But I guess there's more going on behind the scene that I will ever quite understand to be honest.

I wish I understood better what you guys are talking about when you talk of texture atluses and UV mapping, as well as having to go off and learn another package just to set it up for this too. :-o

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

28 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

How to constantly animate thousands of cubes 1 Answer

One texture file for different objects 1 Answer

Auto tiling texture 2 Answers

How to change diffrent material/texture for diffrent faces of Cube 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