Jump to content
  • Advertisement
Sign in to follow this  
CaseyHardman

Unity Getting textures to tile on a mesh the same way regardless of the size or shape of the mesh

This topic is 2069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

 

I'm having some trouble...

 

I'm using Unity and about to get into testing out the level design for the 3D game I'm making.  I've got some test textures that I made (poorly) and I'm confused on how to get them to show up the same way regardless of the mesh they're placed on.

 

I already sought help on the matter by asking on the Unity Answers website some time ago.  I explained the dilemma there and I put an image up to help demonstrate it.  Here's a link there.  Just so you know, the answer below my question there isn't really relevant to this thread.

 

I kind of fixed the issue with the help of the community and some of my own hacky coding.

 

What I did was code up a way to make a new material (which, in Unity, is a texture, kind of) and set its tiling based on the distance the material has to be 'stretched across'.  If the mesh is 25 units wide and 8 units tall, then it sets the X tiling to 25 and the Y tiling to 8.  Since the goal was to try and make a way to tile a texture by game units instead of "how many times it's placed on the mesh", I added a variable called 'gameUnitsPerTile' and set the X and Y tiling to X distance / gameUnitsPerTile and Y distance / gameUnitsPerTile, respectively.

 

The thought was, the material tiles across the whole mesh X/Y tiling times.  So if the tiling is 4x4, then it draws the image 4 times along the X axis and 4 times along the Y axis.  If the tiling values were set to the actual 'length' of the mesh, then it'd be tiling it once across the whole mesh.  That way, as long as your gameUnitsPerTile value was the same for each mesh, you could have the texture showing the same way on each mesh.

 

It actually worked pretty well, but I still feel like I'm doing it wrong, and I'm not totally confident my method is very optimized (having a different material for each wall) or will work in every situation I'll need it to.  I'm probably just severely mixed up about how UVs and texture tiling works.  I feel like this is a lot harder than it should be.

 

Anyway, thanks for reading this far, and I hope it makes sense.  If I've failed to explain something properly, please tell me what it is and I'll try to fix it.  Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement

Well, when i started my first game in Unity, i didn't watch any tutorials, I just read docs when I needed and start working. I ended up with many things that were not supposed to be done as I did, but then it worked and who cares.

 

If it works and has no performance impact (i.e. you are doing it when the level is loading or something, not every frame), then just get over it.

 

Sometimes there is no proper way to do a thing, and the goal is to make a game, not write perfect code.

Share this post


Link to post
Share on other sites

Are you changing the size of the mesh often?

 

Is the mesh a plane?

 

I haven't used Unity before, so I'm not sure if this will work:

 

If you are using a plane, simply make a 2x2 vertex plane and set the UV coordinates according to the size of the plane:

If you want each whole number unit to be a "tile", then you simply apply the U and V with respect to X and Z (or Y).

Share this post


Link to post
Share on other sites

I'm curious, why are you doing this at run time? Do you expect your meshes to change?

Share this post


Link to post
Share on other sites

Sorry for the late reply!

 

 

Are you changing the size of the mesh often?

 

Is the mesh a plane?

 

I haven't used Unity before, so I'm not sure if this will work:

 

If you are using a plane, simply make a 2x2 vertex plane and set the UV coordinates according to the size of the plane:

If you want each whole number unit to be a "tile", then you simply apply the U and V with respect to X and Z (or Y).

 

I'm not really using a plane; I can get it to work on planes, because they're simple enough shapes, but actual level geometry, like a whole wall that winds around into itself, is another story.

 

I'm curious, why are you doing this at run time? Do you expect your meshes to change?

 

I'm not anymore.  I was at one point, just to test if the method worked, but I've since made it happen in the editor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!