Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by benfattino · Jul 31, 2011 at 02:59 PM · texturescale

Texture scale

How do I scale a texture applied to an object (such as tiling command), keeping the original proportions? Tiling command adapt texture to the object face. I have little texture (256x256) must I tile many time on various object, manteining proportion.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
17

Answer by petersvp · Jun 16, 2015 at 05:39 AM

If you need to scale the texture image data itself, and need the image data, you can use my Texture Scaler script. Helical version process the data on the CPU, while my script scales the texture on the GPU. But my requires Unity 5. Here is the code: http://pastebin.com/qkkhWs2J

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 Helical · Jun 17, 2015 at 10:04 AM 0
Share

I like the approach. I take it that this way is actually more efficient. Also its nice to have an example that uses GL and Graphics classes. I think those are way underated.

All in all, nice work, thanks for sharing.

avatar image petersvp · Jun 17, 2015 at 11:00 AM 0
Share

Oh, and the best of all - Due to being RTT Render scale, this in fact uses trilinear scaling using the mipmaps - best quality :)

avatar image tinmar · Nov 18, 2015 at 03:57 PM 0
Share

Excellent! Also, this works on Unity 4.6.8 and above, not only Unity5.

avatar image Andrew-Le · Feb 23, 2017 at 06:23 PM 0
Share

O$$anonymous$$G !! YOU SAVED $$anonymous$$Y DAY @@ i just searched google for creating thumbnail... not think about texture scaler !! you are the best !!

avatar image pertz · May 23, 2018 at 02:56 AM 0
Share

Thanks for the great solution @petersvp ! I was using this fine on several Android devices, but today while testing on an older device (Nexus 7 - 2012) this code produced an invalid texture. Any ideas why? $$anonymous$$aybe a too old OpenGL version? $$anonymous$$aybe some feature being used not supported by it? Any ideas on how to detect? Thanks

avatar image
4

Answer by Helical · Feb 01, 2015 at 04:54 PM

I implemented a ScaleTexture function today Its not the most accurate scaler. but it works well enough for my purpose. Also note that for a 800x640 picture which is 500k pixels it takes a second or two to do this.

 private Texture2D ScaleTexture(Texture2D source,int targetWidth,int targetHeight) {
     Texture2D result=new Texture2D(targetWidth,targetHeight,source.format,false);

     float incX=(1.0f / (float)targetWidth);
     float incY=(1.0f / (float)targetHeight);

     for (int i = 0; i < result.height; ++i) {
         for (int j = 0; j < result.width; ++j) {
             Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
             result.SetPixel(j, i, newColor);
         }
     }

     result.Apply();
     return result;
 }
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 Antony-Blackett · Apr 24, 2015 at 11:20 PM 0
Share

Works perfectly!

Although you did forget to use your cached incX and incY values in the for loops. :)

avatar image nahoy · May 13, 2015 at 03:47 PM 0
Share

Also ins$$anonymous$$d of calling SetPixel multiple times on the result object, you could store that in an array and just call once SetPixels :)

avatar image Helical · May 13, 2015 at 05:09 PM 0
Share

Thats a nice optimization idea. I'm not completly certain that it isn't already cached that way through the result.Apply(). However if you be so kind as to provide the code for it, i will replace $$anonymous$$e with yours. cause me lazy.

avatar image jayatubi · Sep 04, 2017 at 06:03 AM 0
Share

I tried this method and it works. However, the result texture seems to be point filtered ins$$anonymous$$d of bilinear which it should be. Any idea?

avatar image LuisToroDigital · Jan 14, 2019 at 07:42 PM 0
Share

Works Perfectly!! Thank You...

avatar image
3

Answer by Graham-Dunnett · Aug 01, 2011 at 10:51 AM

http://unity3d.com/support/documentation/ScriptReference/Material-mainTextureScale.html

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 benfattino · Aug 01, 2011 at 11:52 AM 0
Share

Thanks. I see. But what I want is different. Let me explain. I have cube 10x10x100. If I use tiling value of 10, Unity deform the texture on the long side 100. What I want is unity fill the face (100 long side) without distorting the texture.

avatar image
0

Answer by yigitcan · Jan 05, 2012 at 10:55 PM

you can use something like this:

 Texture myTexture = Resources.Load("some_texture") as Texture;    
 transform.gameObject.renderer.material.mainTexture = myTexture;        
 renderer.material.mainTextureScale = new Vector2(transform.lossyScale.x*0.5f,transform.lossyScale.x*0.5f);

This will repeat the texture on your object.

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 przem997 · Jul 17, 2013 at 10:51 AM 0
Share

many errors UCE0001: ';' expected. Insert a semicolon at the end.

avatar image robertbu · Jul 17, 2013 at 10:51 AM 0
Share

The code looks fine to me. This is C# code. Did you past it into a Javascript?

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

13 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

Related Questions

Changing scale of WebCamTexture 1 Answer

Resizing GUI textures 0 Answers

Android mesh's scaling animation make the texture bug. 0 Answers

Why does Unity scale my 512 by 256 texture to a 512 512 on import? 2 Answers

Interactively Scaling a 2D Texture 1 Answer


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