Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by WeeGee9000 · Oct 04, 2013 at 05:49 PM · texturemeshtile

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 04, 2013 at 05:50 PM 1
Share

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);
 }

avatar image WeeGee9000 robertbu · Oct 04, 2013 at 10:55 PM 0
Share

Where exactly do i put this code?

avatar image robertbu robertbu · Oct 04, 2013 at 11:04 PM 0
Share

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.

avatar image WeeGee9000 robertbu · Oct 04, 2013 at 11:19 PM 0
Share

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.

avatar image robertbu · Oct 05, 2013 at 12:10 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
9

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. ;)

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Foulcloud · Aug 02, 2015 at 04:53 PM 1
Share

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"?

avatar image DevLurkin · Aug 02, 2015 at 05:43 PM 0
Share

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.

avatar image Sharpless512 · Sep 25, 2015 at 06:19 PM 0
Share

Works Perfect!

avatar image Khyrid · Dec 24, 2016 at 06:00 AM 0
Share

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.

avatar image Chris_Entropy Khyrid · Dec 24, 2016 at 09:35 AM 0
Share

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

avatar image Khyrid Chris_Entropy · Dec 31, 2016 at 01:05 PM 0
Share

Nice. Sold

avatar image BaronWhite · Oct 31, 2017 at 05:18 PM 0
Share

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.

avatar image Chris_Entropy BaronWhite · Nov 01, 2017 at 11:53 AM 0
Share

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.

avatar image
2

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WeeGee9000 · Apr 17, 2014 at 10:10 PM 0
Share

umm.... cool, but.... 30 bucks? holy crap

avatar image lodendsg · Aug 16, 2014 at 06:25 PM 0
Share

Lot of shaders in it have to maintain em all but perhaps I should work up a lighter version with fewer shaders.

avatar image
2

Answer by Gaidzin · Jul 17, 2018 at 01:23 PM

Use this shader: https://yadi.sk/d/wjboOVJa3ZGhKn

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_QzDAumlYdeWF_Q · Nov 03, 2018 at 10:34 PM 0
Share

Works like a charm. Thank you!

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SilentSin · Oct 05, 2013 at 12:13 AM 0
Share

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.

avatar image WeeGee9000 · Oct 05, 2013 at 12:33 AM 0
Share

But what kind of shader and how?

avatar image SilentSin · Oct 05, 2013 at 12:40 AM 0
Share

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.

avatar image WeeGee9000 · Oct 05, 2013 at 12:49 AM 0
Share

okaaaaaaaay :I

avatar image WeeGee9000 · Oct 05, 2013 at 04:09 PM 0
Share

Well i couldn't find anything that sounded like it.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges