Tiling a material using game units


I'm trying to figure out if there's some way to make a material tile a texture using game units instead of tiling it based on the size of the object.

For example, in the image below, there are 2 quads using the same material, but the material doesn't look the same on both of them because the quad on the right is larger than the one on the left:

alt text

So, the tiling settings in the material mean if it's set to 1x1, it'll put the texture across the whole surface once; if it's set to 2x2, it'll put the texture across the whole surface 4 times. If the surface is huge, the texture will be a lot more visible; if the surface is tiny, the texture will be shrunken.

Is there any way to make it tile the texture based on game units? For example, if it was set to 1x1 tiling, then it could tile it 4 times per game unit, so if I had a quad with 4 units of size, it'd tile 4 times, not just once across the whole quad.

Sorry if I've typed up a storm and made this more difficult to understand than it has to be; if I haven't explained enough, please let me know. Thanks for reading this far, and thank you for your time!

more ▼

asked Oct 07, 2013 at 06:01 AM

avatar image

124 60 51 55

try normalizing the tiling value to object scaling.I didnt try this but try something like this (UVTiling/Localscale)*FixedScaleVal

Oct 07, 2013 at 06:13 AM sethuraj
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can set the texture scale at runtime. I think that is what @sethuraj is suggesting:

 renderer.material.SetTextureScale ("_MainTex", transform.localScale);

Note this line of code assumes that your shader is using _MainTex as the texture (most do). It is also assuming that you are basing your texture on the X and Y of the local scale of the object (true of Quad). Wouldn't work for a Plane for example, you would need instead:

 renderer.material.SetTextureScale ("_MainTex", Vector2(transform.localScale.x, transform.localScale.z));

Note setting the local scale breaks batching, so if you are going to do this for many game object, you might want to look for an alternate solution.

more ▼

answered Oct 07, 2013 at 04:35 PM

avatar image

113k 25 52 229

Thanks for the answer. That works great for planes and quads, but what about with a customized shape, like a level? For example, if you had a wall that twists and turns at various locations and you wanted to put a texture across it, how would you apply your code to that situation?

Oct 09, 2013 at 07:01 AM SirMacJefferson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 07, 2013 at 06:01 AM

Seen: 1008 times

Last Updated: Oct 09, 2013 at 07:01 AM