Advertisement
PeterSvP

Unity3d Texture Image Scaler

Jun 15th, 2015
16,841
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. /// A unility class with functions to scale Texture2D Data.
  4. ///
  5. /// Scale is performed on the GPU using RTT, so it's blazing fast.
  6. /// Setting up and Getting back the texture data is the bottleneck.
  7. /// But Scaling itself costs only 1 draw call and 1 RTT State setup!
  8. /// WARNING: This script override the RTT Setup! (It sets a RTT!)    
  9. ///
  10. /// Note: This scaler does NOT support aspect ratio based scaling. You will have to do it yourself!
  11. /// It supports Alpha, but you will have to divide by alpha in your shaders,
  12. /// because of premultiplied alpha effect. Or you should use blend modes.
  13. public class TextureScaler
  14. {
  15.  
  16.     /// <summary>
  17.     /// Returns a scaled copy of given texture.
  18.     /// </summary>
  19.     /// <param name="tex">Source texure to scale</param>
  20.     /// <param name="width">Destination texture width</param>
  21.     /// <param name="height">Destination texture height</param>
  22.     /// <param name="mode">Filtering mode</param>
  23.     public static Texture2D scaled(Texture2D src, int width, int height, FilterMode mode = FilterMode.Trilinear)
  24.     {
  25.         Rect texR = new Rect(0,0,width,height);
  26.         _gpu_scale(src,width,height,mode);
  27.        
  28.         //Get rendered data back to a new texture
  29.         Texture2D result = new Texture2D(width, height, TextureFormat.ARGB32, true);
  30.         result.Resize(width, height);
  31.         result.ReadPixels(texR,0,0,true);
  32.         return result;         
  33.     }
  34.    
  35.     /// <summary>
  36.     /// Scales the texture data of the given texture.
  37.     /// </summary>
  38.     /// <param name="tex">Texure to scale</param>
  39.     /// <param name="width">New width</param>
  40.     /// <param name="height">New height</param>
  41.     /// <param name="mode">Filtering mode</param>
  42.     public static void scale(Texture2D tex, int width, int height, FilterMode mode = FilterMode.Trilinear)
  43.     {
  44.         Rect texR = new Rect(0,0,width,height);
  45.         _gpu_scale(tex,width,height,mode);
  46.        
  47.         // Update new texture
  48.         tex.Resize(width, height);
  49.         tex.ReadPixels(texR,0,0,true);
  50.         tex.Apply(true);    //Remove this if you hate us applying textures for you :)
  51.     }
  52.        
  53.     // Internal unility that renders the source texture into the RTT - the scaling method itself.
  54.     static void _gpu_scale(Texture2D src, int width, int height, FilterMode fmode)
  55.     {
  56.         //We need the source texture in VRAM because we render with it
  57.         src.filterMode = fmode;
  58.         src.Apply(true);   
  59.                
  60.         //Using RTT for best quality and performance. Thanks, Unity 5
  61.         RenderTexture rtt = new RenderTexture(width, height, 32);
  62.        
  63.         //Set the RTT in order to render to it
  64.         Graphics.SetRenderTarget(rtt);
  65.        
  66.         //Setup 2D matrix in range 0..1, so nobody needs to care about sized
  67.         GL.LoadPixelMatrix(0,1,1,0);
  68.        
  69.         //Then clear & draw the texture to fill the entire RTT.
  70.         GL.Clear(true,true,new Color(0,0,0,0));
  71.         Graphics.DrawTexture(new Rect(0,0,1,1),src);
  72.     }
  73. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement