- Home /
Repeat texture common question
Sooo, really wondering and check tons of pages but couldn't find what i wanted; is there a way to have
a CUBE, with a texture that repeats when scaling instead of streching. That only uses one draw call hence i've seen many people saying i tweak this
renderer.material.mainTextureScale
but isn't good since it create a new material and extra draw call for every cube; I've seen you can use a tri-planar shader like here: http://www.blog.radiator.debacle.us/2013/02/a-smoother-triplanar-shader-for-unity.html so you can have different material for each side of a cube and only uses one draw call but seem a bit buggy with the position of the texture being off.
So is there a way to fix this problem? basically like what probuilder has by playing around with UVs or shaders, which unfortunately i know nothing of :/
Kind of a big question but i'm clearly not the only one with this issues, plz help us :)
Just a note to make sure you understand. $$anonymous$$odifying the material only caused additional draw calls if you have the same material on multiple objects. Also, if all your objects with the same material where the texture scale is set for that material, then all the objects will draw in a single draw call.
Answer by robertbu · May 14, 2014 at 03:32 AM
I just ran a quick test, and supplying uv coordinates in the mesh that are greater than 1.0 will cause the texture to tile. So the following bit of code, when applied to the built-in cube, will cause the texture to be tiled 5x5 on each side:
#pragma strict
function Start() {
var mesh = GetComponent(MeshFilter).mesh;
var uvs = mesh.uv;
for (var i = 0; i < uvs.Length; i++ ){
if (uvs[i].x > 0.99)
uvs[i].x = 5.0;
if (uvs[i].y > 0.99)
uvs[i].y = 5.0;
}
mesh.uv = uvs;
}
Note that changing the uv coordinates does not break batching.
thanks, that a nice start; though it only really works for one side of the cube, u can only tile the image in the x and y so it's kinda difficult when talking about a 3D object.
I didn't realize that in shader i posted in the link also repeats the texture when scaling the object, but problem is, it seems like the offset of the texture is off; anyone here know a simple fix for that?
I'm either missing something about your answer, or you are missing something about how uv coordinates work. It does tile all sides of a cube. You can adjust the uv coordinates for any offset and/or tiling. But clearly you are looking for a shader solution. I'd rework and then repost your question looking towards a shader-specific solution.
well yes i know nothing of how UV coding works, but I put in a variable so that i can play around in place of the hard coded tile to 5, and moved everything in the update; but no matter how u go about it the tiling doesnt work properly.
There might be a way to do it without shaders but im not sure how; kinda simple what i want, a texture that tile on a cube no matter which way is scaled. There's no need to repost this question, whether the cube uses a script or a shader does not matter to me, and technically the question would be the same so whats the point?
The tiling doesn't work properly because this is very simple example...and because I don't have a clear vision of what you mean by properly. I think the modification of the uv will give what you want, but it is up to you to education yourself on how uv coordinates work and therefore how you need to modify the uv array to get the effect you are looking for. You can map any side of the cube to any portion of a texture. You can basically do what renderer.material.mainTextureScale and renderer.material.mainTextureOffset only do it independently on each side of the cube. And you can do it without increasing draw calls.
http://www.rozengain.com/blog/2007/08/26/uv-coordinate-basics/
I suggested a new post if you are primarily looking for a shader solution. There are not a lot of people who answer questions on UA. When you have a somewhat specialized question, you want to draw the person to your question who can give you an answer. Right now someone interested in answering shader questions might not even spot your question.
I'll have a look at the link but looks kinda complicated and i got a feeling there's an easier way; but i don't understand what isn't clear. Scale a cube, texture tiles properly on on side but stretches elsewhere, hence it's not good. I'm just trying to get texture tiling i dont have the time to waste on long problems; i have 500 other issues i have to get to and i've been looking for a solution for the last week so yea im not getting anywhere much :/
Answer by DevLurkin · Dec 16, 2014 at 05:10 AM
I had a similar need and could't find a complete answer anywhere. I ended up writing my own script that changes the UV map according to the desired scale. You simply attach the script to your Cube's GamObject. Then, anytime you change the scale of your cube you call the reCalcCubeTexture()
method. It does the rest.
You should know that because you are customizing the UVs, it creates a mesh instance and the object will no longer use the sharedMesh (it still uses the sharedMaterial). However, I did write in a hack that clears the instance and sets your MeshFilter back to sharedMesh if your cube is ever scaled back to ( 1 , 1 , 1).
It can be found here, and includes a button for your inspector that allows you to change(update) the texture scale in the editor itself. That way you do not have to wait for runtime to see the correct texture.
havent looked at this issue for a while but will check it out later, thx :)