- Home /
How to Best Implement a Tileset
Lately I've been re-thinking the way that I create terrain/scenery in a little game that I've been working on, and since I've never been good when it comes to the artsy side of development, I decided that using a full tile set would be a lot easier for me to work with. On the plus side, the assets in my game would feel more connected to each other, as they were all created by the same person, and not random textures found on Google from various sources.
In order to best optimize my game, I'd like to know some theory on what might be a good way to implement something like this.
I currently have an 8192x8192 PNG image that contains smaller images with dimensions of 256x256 laid out in a "grid" within. Thus, there are 16 rows and 16 columns in my large texture image. That's what I'm working with.
The game world that I'm building is going to consist of 1x1x1 cubes, which should make things simple. I'd like to be able to create cubes at run-time as well, so I need to keep that in mind when determining the way to do this.
Since the smaller images are evenly placed out within the larger one, I am thinking that the way to do this would be to use just a single material with the Tiling/Offset properties set to only cover the region for the tile that I'm selecting.
For example, if my rock texture is at position [x,y] within my large image, then the tiling would grab [.0625 x, .0625 y], since .0625 is (1/16).
Is it possible to a single material that is in use on multiple objects with different Tiling/Offsets? It seems that within the Unity Editor I am unable to figure out a way to do this without using a separate material for each tile in the grid.
If this is possible, does that mean that just a single draw call would be able to display all of the objects in my game world even if they are displaying different tiles within the texture?
I know I asked a lot of things. I'm just looking to learn all I can from anyone willing to share their knowledge. Anything you can contribute to this topic would be appreciated, but if I had to limit it, I would mainly be interested in learning about the asset structure for this type of thing (i.e. one material, many materials, etc) as well as the number of draw calls for this type of things despite all of the tiles being within the same texture.
Answer by Eric5h5 · Sep 03, 2012 at 06:14 AM
There are several packages in the asset store which do this. You should probably just get one of them and save yourself the time and bother of doing it manually, unless you're particularly interested in the nuts and bolts of coding it yourself. The general concept is known as a texture atlas. You wouldn't use 1x1x1 cubes (as you note, you'd end up with zillions of materials); instead you'd use the Mesh class to construct everything, where each section is UV mapped appropriately so it uses the correct part of the texture. Speaking of which, just so you know, not all hardware supports such large images; in fact Unity won't import anything over 4096x4096. Or rather, it will, but it will be scaled down. If you want to be really safe, try not to go over 2048x2048 for any given texture.
+1
Thanks for the response. Actually, I was trying to implement something along the lines of a texture atlas. It looked as if it was working properly after a slight workaround (i.e. I was able to render textures using different coordinates from the atlas on separate cubes), but the problem I was having was that I wasn't able to find a way in Unity to have the same material object with variable scale and offset. Thus, if I made two prefab cubes and assigned both of them my texture atlas material, switching the scale and offset on one of them would change the other.
Since the only workaround that I could think of was to make a separate material for each UV mapping, it [seemed to have] defeated the purpose of having the atlas in the first place, as each of these materials would need their own draw call.
Can anyone shine some light on the topic? I'd like to learn how this is meant to be done semantically.
Also, I apologize for the time lapse, I had a busy work week so now that it's Saturday I can pickup where I left off.
You would use the $$anonymous$$esh class to change the UV coordinates.
So rather than changing the scale and offset of the material to align the texture portion that I need, I would be doing this by changing the mesh UVs?
If that sounds right, then I will need to read up on how UVs work as I haven't done any real work with them yet through scripting. Thanks for your help, Eric.
Edit: On second thought, I think I might just go with your original suggestion and invest in something from the Asset store which will likely be more fleshed out anyways than what I was going to do.
Your answer
Follow this Question
Related Questions
Random Tiling Textures 0 Answers
Image Projector 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to texture huge tile grids? 1 Answer