- Home /
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.
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
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.
Oh, and the best of all - Due to being RTT Render scale, this in fact uses trilinear scaling using the mipmaps - best quality :)
Excellent! Also, this works on Unity 4.6.8 and above, not only Unity5.
O$$anonymous$$G !! YOU SAVED $$anonymous$$Y DAY @@ i just searched google for creating thumbnail... not think about texture scaler !! you are the best !!
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
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;
}
Works perfectly!
Although you did forget to use your cached incX and incY values in the for loops. :)
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 :)
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.
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?
Answer by Graham-Dunnett · Aug 01, 2011 at 10:51 AM
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.
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.
many errors UCE0001: ';' expected. Insert a semicolon at the end.
The code looks fine to me. This is C# code. Did you past it into a Javascript?
Your answer
Follow this Question
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