• 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
6
Question by Lemo Dev · Aug 23, 2012 at 09:14 PM · uvminecraftmapping

UV mapping

Hello guys.

I am making a game similar to minecrafts graphic style. I only work as a programmer, so I am a total noob when it comes to 3D models. So for example: I have a grass block where the sides are going to be the same, but the top and bottom have different textures. So I was wondering if I could UV map the standard cube in Unity from within my script.

Thanks a lot.

Comment
Add comment · Show 12
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 SarperS · Aug 23, 2012 at 09:16 PM 0
Share

It would be ten times easier with a free modelling software like Blender. Just download it, create a cube and look for any uv mapping tutorial.

avatar image Zarenityx · May 18, 2013 at 07:02 PM 0
Share

@Sarper Soher You can't do that, because different blocks need different textures, and you can't have one texture per block because they all need to combine into a single mesh in each chunk.

avatar image SarperS · May 18, 2013 at 07:06 PM 1
Share

@Zarenityx Yes you can, texture atlasing...

avatar image Zarenityx · May 19, 2013 at 10:19 AM 3
Share

@Sarper Soher No, if you are making $$anonymous$$inecraft-style graphics each cube needs to have only certain faces rendered, and each face a different texture. So no, you can't create anything in blender for this because voxels are never actually cubes. Voxels are hypothetical cubes that are rendered completely in code. I am working on a voxel engine too (although it will be smooth, eventually), and if you made each block an actual block, you would destroy your computer's GPU. A voxel contains UV data for the different sides, and applies it to whatever faces it actually renders. So yes, texture atlasing is required, but I am pretty sure that is what this guy is doing, anyway, given that you need only one mesh per chunk, but each block can have a different texture, and you can only have one texture per mesh. No, Blender or anything else is pointless when making a voxel engine, because there are no cubes to begin with.

avatar image Fattie · May 19, 2013 at 10:26 AM 1
Share

@Zarenityx you might as well get used to typing this over and over because there is about one question per hour on making $$anonymous$$ecraft here :)

and every single questioner has the same confusion, they think $$anonymous$$ecraft is made by "stacking up cubes"

it's fascinating observation that $$anonymous$$ecraft does not contain one cube. if I'm not mistaken there has never ever been a cube rendered in $$anonymous$$ecraft!!! (I believe when you play you can't have just "one cube" right? even if you did they'd probably only render the facing faces, heh!)

the whole thing is doubly-confused because there is a free complete $$anonymous$$ecraft starter kit kicking around on the forums, heh !

avatar image AlucardJay · May 19, 2013 at 10:43 AM 0
Share

No, you wouldn't use this for a $$anonymous$$inecraft clone. $$anonymous$$y answer is purely addressing the actual question asked by the OP : So I was wondering if I could UV map the standard cube in Unity from within my script. . That's all, UV mapping the Unity Native Cube. (Not voxels, not poxels, just a simple primitive cube)

avatar image SarperS · May 19, 2013 at 09:03 PM 0
Share

@Zarenityx Lemo Dev says he's trying to make a game with $$anonymous$$ecraft's graphic "style" not the technology behind it. Voxels are not mentioned anywhere. He just asks how to UV map a cube so it let's the developer to assign different textures to separate surfaces. So our argument is pointless as what we say are both true for different cases.

avatar image Zarenityx · May 20, 2013 at 12:26 AM 0
Share

I guess that's true... I just hear '$$anonymous$$inecraft' and automatically connect it with 'voxel'.

avatar image Zarenityx · May 20, 2013 at 12:33 AM 0
Share

@Fattie Yeah, the number of voxel and marching-cube stuff on the forum is huge. The problem is, they are almost always either really incomplete, way too expensive, or the code is completely impossible to decipher, making it impossible to expand upon. I prefer to just build my own. Besides, You can't exactly distribute a game that uses all the $$anonymous$$inecraft sounds and textures, or stay sane if you decide to change all of them.

avatar image Pixie3D · Aug 13, 2015 at 01:50 PM 0
Share

I don't know much about $$anonymous$$inecraft but I looked it up at wiki.

https://en.wikipedia.org/wiki/Voxel

"$$anonymous$$inecraft is a game with a voxel editor. However $$anonymous$$inecraft blocks are rendered as polygons."

So according to this it is rendered in a traditional way. I believe the trick is to only draw cubes which are visible for the viewer. (Also just realized this is an old post but just in case anyone would be confused)

avatar image Zarenityx · Aug 13, 2015 at 07:50 PM 0
Share

@Pixie3D It is true that $$anonymous$$inecraft (As well as almost every other voxel game you will find) does actually render with polygons, not point clouds, and for the very good reason that polygons render really fast. What is different about these "voxels" as opposed to traditional polygons is that meshes are generated based on voxel data, rather than created by hand or other procedures. So yes, most so-called voxel games are still rendering polygons and not points, but they still will almost never render a cube.

The second part of your comment is therefore only partially correct, because it's not drawing cubes at all, but drawing a mesh created out of what would be the visible parts of the voxel grid. In $$anonymous$$inecraft's case, this mesh is made by checking a voxel's adjescent voxels to deter$$anonymous$$e where it should place quads, resulting in a regular, blocky mesh that appears to be made from cubes. It would, however, be fairly simple to switch to another mesh generating algorithm, such as $$anonymous$$arching Cubes, resulting in a smooth mesh.

So once again I find myself saying: Sorry, but voxels aren't gameobjects! In my implementations, voxels are structs, with little more than an ID number that refers to a set of values in a lookup table.

Show more comments

4 Replies

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

Answer by AlucardJay · Aug 24, 2012 at 04:51 AM

I have listed all the ways to custom UV map a unity cube here : http://answers.unity3d.com/questions/294165/apply-uv-coordinates-to-unity-cube-by-script.html

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.

for example this script would map the TOP of the cube to any UV coordinates of a texture you like :

 #pragma strict
 // TOP
 
 function Start() 
 {
     // Get the mesh
     var theMesh : Mesh;
     theMesh = this.transform.GetComponent(MeshFilter).mesh as Mesh;
 
     // Now store a local reference for the UVs
     var theUVs : Vector2[] = new Vector2[theMesh.uv.Length];
     theUVs = theMesh.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
     theMesh.uv = theUVs;
 }



EDIT, In reply to : So if I create a texture atlas and assign sections to the different sides of the cube, is it possible to tile each side differently? Or would everything get tiled by the same amount? Cheers for the help, Muzzstick.

Me : I think an example scene would be a good visual demonstration. First, grab this texture : http://metaverse.mitsi.com/Secondlife/posts/uvmaps/images/uv_checker%20large.png

Now create a new scene, create a cube and attach that texture as a material. Create a new script and attach it to the cube. Copy in the below code, then hit play.

You see for each face a different part of the texture has been assigned, this is basically UV mapping. Change the values in the cube Inspector, then hit the Left mouse button. First try changing the width and height just to see the effects. Then change the x and the y for different start positions on the texture.

Hopefully this shows what UV mapping is, and how it usually applies to one texture atlas (sometimes called a texture map). Again, for a different texture, the plane method is the way to do it.

Anyway, here is the code :

 #pragma strict
 
 // using an image that is an 8x8 grid
 // each image is 0.125 in width and 0.125 in height of the full image
 
 // UVs in this example are given as a Rect
 // uvs : start position X, start position y, width, height
 // start positions are staggered to show different images
 // width and height are set to 0.125 (1/8th square of the full image)
 
 public var uvsFront : Rect = new Rect( 0.0, 1.0, 0.125, 0.125 );
 public var uvsBack : Rect = new Rect( 0.125, 0.875, 0.125, 0.125 );
 public var uvsLeft : Rect = new Rect( 0.25, 0.75, 0.125, 0.125 );
 public var uvsRight : Rect = new Rect( 0.375, 0.625, 0.125, 0.125 );
 public var uvsTop : Rect = new Rect( 0.5, 0.5, 0.125, 0.125 );
 public var uvsBottom : Rect = new Rect( 0.625, 0.375, 0.125, 0.125 );
 
 private var theMesh : Mesh;
 private var theUVs : Vector2[];
 private var xOffset : float = 0.0;
 
 function Start() 
 {
     theMesh = transform.GetComponent(MeshFilter).mesh;
     theUVs = new Vector2[theMesh.uv.Length];
     theUVs = theMesh.uv;
     
     SetUVs();
 }
 
 function Update() 
 {
     // change the UV settings in the Inspector, then click the left mouse button to view
     if ( Input.GetMouseButtonUp(0) )
     {
         SetUVs();
     }
 }
 
 // 2 --- 3
 // |     |
 // |     |
 // 0 --- 1
 
 function SetUVs() 
 {
     // - set UV coordinates -
     
     // FRONT    2    3    0    1
     theUVs[2] = Vector2( uvsFront.x, uvsFront.y );
     theUVs[3] = Vector2( uvsFront.x + uvsFront.width, uvsFront.y );
     theUVs[0] = Vector2( uvsFront.x, uvsFront.y - uvsFront.height );
     theUVs[1] = Vector2( uvsFront.x + uvsFront.width, uvsFront.y - uvsFront.height );
     
     // BACK    6    7   10   11
     theUVs[6] = Vector2( uvsBack.x, uvsBack.y );
     theUVs[7] = Vector2( uvsBack.x + uvsBack.width, uvsBack.y );
     theUVs[10] = Vector2( uvsBack.x, uvsBack.y - uvsBack.height );
     theUVs[11] = Vector2( uvsBack.x + uvsBack.width, uvsBack.y - uvsBack.height );
     
     // LEFT   19   17   16   18
     theUVs[19] = Vector2( uvsLeft.x, uvsLeft.y );
     theUVs[17] = Vector2( uvsLeft.x + uvsLeft.width, uvsLeft.y );
     theUVs[16] = Vector2( uvsLeft.x, uvsLeft.y - uvsLeft.height );
     theUVs[18] = Vector2( uvsLeft.x + uvsLeft.width, uvsLeft.y - uvsLeft.height );
     
     // RIGHT   23   21   20   22
     theUVs[23] = Vector2( uvsRight.x, uvsRight.y );
     theUVs[21] = Vector2( uvsRight.x + uvsRight.width, uvsRight.y );
     theUVs[20] = Vector2( uvsRight.x, uvsRight.y - uvsRight.height );
     theUVs[22] = Vector2( uvsRight.x + uvsRight.width, uvsRight.y - uvsRight.height );
     
     // TOP    4    5    8    9
     theUVs[4] = Vector2( uvsTop.x, uvsTop.y );
     theUVs[5] = Vector2( uvsTop.x + uvsTop.width, uvsTop.y );
     theUVs[8] = Vector2( uvsTop.x, uvsTop.y - uvsTop.height );
     theUVs[9] = Vector2( uvsTop.x + uvsTop.width, uvsTop.y - uvsTop.height );
     
     // BOTTOM   15   13   12   14
     theUVs[15] = Vector2( uvsBottom.x, uvsBottom.y );
     theUVs[13] = Vector2( uvsBottom.x + uvsBottom.width, uvsBottom.y );
     theUVs[12] = Vector2( uvsBottom.x, uvsBottom.y - uvsBottom.height );
     theUVs[14] = Vector2( uvsBottom.x + uvsBottom.width, uvsBottom.y - uvsBottom.height );
      
     // - Assign the mesh its new UVs -
     theMesh.uv = theUVs;
 }


alt text

Comment
Add comment · Show 11 · 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 Lemo Dev · Aug 24, 2012 at 03:22 PM 0
Share

Thank you :)

avatar image Muzzstick · Feb 18, 2013 at 03:17 AM 0
Share

That's a great example! Thanks

How would you then load your texture into those coordinates? (assu$$anonymous$$g you want the top side texture to be different from the rest)

avatar image AlucardJay · Feb 18, 2013 at 03:42 AM 0
Share

This is where the UV coordinates come in. They take a piece of the material(texture) you are using. For the above example (Top of cube), I used :

 theUVs[4] = Vector2( 0.5, 1.0 ); // top left
 theUVs[5] = Vector2( 1.0, 1.0 ); // top right
 theUVs[8] = Vector2( 0.5, 0.5 ); // bottom left
 theUVs[9] = Vector2( 1.0, 0.5 ); // bottom right

looking at top left : x is 0.5 so this is the middle of the texture, y is 1.0 so this is the top of the image.

now looking at bottom right : x is 1.0 so this is the right of the texture, y is 0.5 so this is the middle of the image.

these UV coordinates would show the top right quarter of a material(texture/image). Is that what you were asking?

Edit : Looking at your comment on the other question, I think you are asking how to put a whole separate texture on a single face. UVs are for using a portion of a single texture, not adding other textures, sorry. What you could do is either create a texture atlas so that you can UV map, or create a plane and size/position that to replace one of the cube faces. Then you can put your separate texture on the plane (acting as a single face on the cube).

avatar image Muzzstick · Feb 18, 2013 at 05:42 AM 0
Share

Thanks for the reply! Yes, loading a seperate texture for one side was actually what I was hoping for.

I'd thought about creating a plane to cover the cube on one side but hoped there was a more efficient way. (Although in all honesty it probably won't make much difference).

So if I create a texture atlas and assign sections to the different sides of the cube, is it possible to tile each side differently? Or would everything get tiled by the same amount?

Cheers for the help.

avatar image AlucardJay · Feb 18, 2013 at 06:51 AM 0
Share

The answer has been edited to include this question

Show more comments
avatar image
3

Answer by DaveA · Aug 23, 2012 at 09:16 PM

You can modify the mesh, yes. But really, there are many voxel systems available, I would strongly suggest getting one of them, it will save you a LOT of work. Voxelform, for example, uses tri-planar world-space shaders so it gets grass on top and dirt underneath and seamless textures.

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 electricsauce · Feb 18, 2013 at 04:29 AM

I would UV unwrap and texture in Blender.

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 Zarenityx · May 19, 2013 at 10:28 AM 0
Share

Like I told @Sarper Soher , creating something in blender wouldn't work because there are no actual cubes. Every face is procedurally generated from a 3d array of hypothetical voxels, giving the illusion of blocks. And as each block needs different textures, but there can only be one texture per 'block'. This really needs to be done in code.

avatar image Fattie · May 19, 2013 at 10:33 AM 0
Share

indeed. there would have to be 1000 explanations on here that $$anonymous$$ecraft has no cubes whatsoever, it only has conceptual cubes in the software .. just arrays and so on, like a database - $$anonymous$$ecraft has no cubes, it does not have one cube, it contains zero cube models, $$anonymous$$ecraft has never shown one cube, ever .. $$anonymous$$ecraft simply shows large horizontal or vertical surfaces that happens to have drawings on them that look like they are cubes ..if they were painted differently nobody would ever raise the cube issue" ... heh so that's 1001

avatar image Zarenityx · May 19, 2013 at 06:25 PM 0
Share

$$anonymous$$y first attempt at a voxel engine involved cubes that shut off their behaviors and renderers at certain distances. It didn't quite work... It's probably the most common misconception that $$anonymous$$inecraft uses cubes.

avatar image
-1

Answer by jethrogillgren · May 31, 2018 at 08:16 AM

In 2018, this can be done using Unity's free ProBuilder add-on.

This example shows how to do that with a cube, and other shapes.

Comment
Add comment · Show 1 · 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 Zarenityx · Aug 29, 2018 at 09:05 PM 0
Share

No. See above. The many posts by myself and Fattie explain why the meshes can't be premade in any way. The importance of UV mapping in code is so that they can be created procedurally. Personally I like the way they put it better:

$$anonymous$$ecraft has no cubes whatsoever, it only has conceptual cubes in the software .. just arrays and so on, like a database - $$anonymous$$ecraft has no cubes, it does not have one cube, it contains zero cube models, $$anonymous$$ecraft has never shown one cube, ever .. $$anonymous$$ecraft simply shows large horizontal or vertical surfaces that happens to have drawings on them that look like they are cubes ..if they were painted differently nobody would ever raise the cube issue

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

This script does not work propely in my game 1 Answer

Proper texture mapping with standart primitives 1 Answer

Surface SpecularMap Shader color integration. 0 Answers

using script to fill the uv array 0 Answers

Projection Mapping a texture, into UV of objects 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