- Home /
Repeat Texture instead of Stretching it all over?
Hey peoples. Always when i assign a texture to the cube and then scale the cube, the texture stretches along with the cube and it looks really awkward.
So my question is the following: How do you make the texture repeat itself when the cube is scaled, Like in Valve's Hammer Editor?
NOTE: I already searched for this, and there were no better awnsers than "copy and paste the materials and use diferent tile settings". Thats not what i am looking for.
You need to change the $$anonymous$$aterials mainTextureScale to match the scaling of your object. In this question, I change the scale in one direction to emulate a ruler:
http://answers.unity3d.com/questions/451647/how-to-stretch-my-scale-in-unity-3d.html
For 3D you have to decide what properties of the cube translate into the 2D texture scale. $$anonymous$$aybe something like:
function Update () {
renderer.material.mainTextureScale = Vector2(transform.localScale.x, transform.localScale.y);
}
This code goes on in a script and then is attached to the object. But having reread your question and done a quick search on Valve's Hammer Edition," you may be looking for an edit-time solution, not a runtime solution. At edit time, all you have to do is select your material, and then in the inspector adjust the 'Tiling'. For example if you set Tiling x=2, y=2, you will have four copies on each side of your cube.
Yes but its not the same thing as Hammer. Hammer tiles the objects in real time, where in Unity you have to tile the thing all the time and you can't use the same texture for other objects.
I've obviously missed the point of your question, so I've converted my answer to a comment. As @SilentSin says, it can be done with a shader.
Answer by DevLurkin · Dec 16, 2014 at 05:22 AM
I had the same problem and ended up writing this script to do just that. Attach this script to your Cube. Then call reCalcCubeTexture()
after you change your cube's scale if you are adjusting it at runtime. However, if you change the scale in the unity editor itself, just click the "Update Texture" button in the Inspector and think happy thoughts of me. ;)
This works great. Could you please explain how it is able to save the mesh. I can "update the texture", then remove the script and the mesh seems to be "built in" to my prefab now. No sign of a mesh asset anywhere else. This is perfect, but how does it store the "Cube Instance"?
The first thing the script does is look at the scale of your cube and figures out if any of the sides need a texture to repeat. If they do, it changes the scale of the cube back to 1,1,1 and then changes a couple of components that are ALREADY attached to your cube to make it look right.
So, after you run it, even if you delete the script, the changes have already been saved and will remain. How?
For a repeating texture, it first edits the cube's mesh-filter component. This is probably titled "Cube ($$anonymous$$esh Filter)" in your cube's inspector. $$anonymous$$y script changes the values of the mesh filter's UV's so the video card will know how many times to repeat your texture in order to make it look proper.
Now... the UV data is there and can tell the video card HOW $$anonymous$$UCH to repeat your texture, but you still need to your video card that it SHOULD repeat. To do this, the script finds your cube's $$anonymous$$esh Renderer and changes a value in it to tell the video card to use the new UV data from step 1 and draw the texture in a repeating fashion.
With those two components updated, the cube will render properly without needing the script attached to the game object anymore.
I hope that made sense?
TLDR; it changes the values of parts of the cube that are already there, so after you run it, you don't need it anymore. Even if the script is removed, the values that were changed in the cube's $$anonymous$$esh Filter and $$anonymous$$esh Renderer stay changed and attached to your cube gameobject, so the change remains.
I noticed that this script no longer works in version 5.5.0f3. The texture gets stretchered out. Cubes that I had in the scene from a much older version still work though.
I have a tool in the asset store, that does about what the script above does, but has a ton of more features. You can for example set the scaling, offset and rotation of each side of the object individually and edit more complex objects. Some people use it to texture complete objects. It is only $10.95. And it works with version 5.5: https://www.assetstore.unity3d.com/en/#!/content/41613
Hello, I know this is a very old script, but in case you-re still around, I-d like first to say it-s quite brilliant, and secondly to ask for help, if possible. I have a set of prefab rooms which are an empty with cubes for walls. The textures tile properly, however when savingthe prefab all objects affected by the script go mesh invisible. They still have the green wire mesh showing the outline, but they are completely see through, in editor and in game.
Thanks in advance, and a heads up for anyone using prefabs who finds this.
Hi BaronL, you would need to change a few things with the script. First you would need to edit the shared$$anonymous$$esh ins$$anonymous$$d of mesh. Then you would need to save the mesh as a prefab in the Asset folder. I know, it is shameless self promotion, but my Auto Texture Tiling Tool already has that feature ;-) And a lot more.
Answer by lodendsg · Feb 08, 2014 at 01:42 PM
Your looking for a triplanar shader they come in a few styles (http://www.blog.radiator.debacle.us/2013/02/a-smoother-triplanar-shader-for-unity.html) This is a nice blog on triplanar shader in world space i.e. the object may move but the texture wont.
Just an update I ended up doing a unity asset for triplanar shaders; I include several variations including basic (x,y,z) tops (top, bottom, sides) and simple (single texture) as well as a world space variant of tops. these come in all the usual Unity forms i.e. bumped, spec, parallax, cutout, etc.
You can find it here (https://www.assetstore.unity3d.com/#/content/15520)
Lot of shaders in it have to maintain em all but perhaps I should work up a lighter version with fewer shaders.
Answer by SilentSin · Oct 05, 2013 at 12:03 AM
What you need is a shader that looks up UVs using world space coordinates instead of just using the UV coordinates.
If you want to manually increase the tilling by making a new material for every object you use it on and waste a lot of memory at runtime, then your assertion is correct.
Writing a shader lets you use one material for many objects, just as its supposed to. Short of not knowing how to write shaders, nothing about increasing the tilling will work better than writing a shader.
I can't explain the kind of shader any better than my original answer. A normal shader that does texture lookups differently.
How? ... Either by learning shader coding or finding a shader someone has already made. What you want isn't that unusual so your chances of finding something might not be that bad. Try looking on the wiki.
Your answer
Follow this Question
Related Questions
Texture layers on single mesh 1 Answer
How to texture walls in procedurally generated mesh? 1 Answer
Is having sub-meshes less efficient than one big mesh in an imported model? 1 Answer
Repeating Images From an Atlas 0 Answers
How do I use many different tile textures for one mesh? 2D Tile based game. 2 Answers