Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by domjon · Apr 14, 2015 at 11:29 AM · pixel arttextureimporter

How to get (usable) bilinear filtering on small textures?

I have a project which involves pixel-art style 2d sprites inhabiting a 3d world. Up until now, I've solved the point filter blinky/jaggedness issue by resizing the sprites 4x in photoshop, and then setting the Import Texture Filter Mode to 'Bilinear' in Unity. Works like a charm!! But has caused another issue entirely: Memory/Storage.

A 1024x1024 sprite sheet sized up 4x becomes a whopping 21mb FULLY compressed. Considering the scale of the game, that's going to become HUGE in file size, not to mention texture memory usage at runtime.

Here are some (long shot) options that came to mind. If anyone has new ideas on how to approach this, or ways to actually accomplish the methods below please let me know! Google has not been kind to me on this topic :)

  1. Resize sprites during runtime. (Tried this, and it does work... but doesn't reduce texture memory - actually worse since you can't resize compressed textures. Not to mention figuring out the equivalent set pixelsPerUnit during runtime... bleh)

  2. Write a custom bilinear shader. Seems to be the most promising option. I'm no expert in CG though, and I have a feeling it would really bog down GPU having hundreds of sprites on screen. Opinions welcomed.

  3. Scaling sprites 2x instead of 4x.. No good, too blurry :(

  4. Create a custom FilterType class (IDEAL). In theory, I only need bilinear to scale itself up 4x. However, I have zero idea if Unity even allows access to the current bilinear texture import setting function. Something like having a "Bilinear Scale" variable would be AMAZINGGGG for this.

There MUST be a way to kill these jaggies without using massive amounts of GB and TexMemory.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by domjon · Apr 19, 2015 at 12:32 PM

Went with the shader option. Wrote a very basic sprite shader that mimics smoothness of Bilinear by blurring. Handles all light situations (boy that was a pain to figure out how to not get white outlines in the dark w/ a point light). It also does shadows! :)

Fairly new to shaders, so I'm unsure of how efficient this is, but it completely solves my issue.

Feedback and input more than welcome:

 Shader "Sprites/Low Res - Diffuse With Shadows"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.0001
         _BlurAmount ("Blur Amount", Range (0, 1)) = 0.055
     }
 
     SubShader
     {
   
         Tags
         { 
             "Queue"="Transparent"
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         
         
         LOD 300
         Cull Back
         Blend SrcAlpha OneMinusSrcAlpha
         
         
         CGPROGRAM
         #pragma surface surf ToonRamp vertex:vert alpha alphatest:_Cutoff addshadow
         #pragma lighting ToonRamp
         
         
         sampler2D _MainTex;
         float _BlurAmount;
         float4 _MainTex_TexelSize;
         
         
         // for hard double-sided proximity lighting
         inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
         {
             half4 c;
             c.rgb = s.Albedo * _LightColor0.rgb * sqrt(atten);
             c.a = s.Alpha;
             return c;
         }
 
 
         struct Input 
         {
             float2 uv_MainTex : TEXCOORD0;
             fixed4 color;
         };
         
         void vert (inout appdata_full v, out Input o)
         {
             UNITY_INITIALIZE_OUTPUT(Input, o);
             o.color = v.color;
         }
         
         void surf (Input IN, inout SurfaceOutput o) 
         {
             half4 original = tex2D(_MainTex, IN.uv_MainTex);
             half4 finalOutput = tex2D(_MainTex, IN.uv_MainTex);
             
             float amount = _BlurAmount;
             float2 up = float2(0.0, _MainTex_TexelSize.y) * amount;
             float2 right = float2(_MainTex_TexelSize.x, 0.0) * amount;
         
             for(int i=0;i<3;i++)
             {
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex + up);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex - up);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex + right);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex - right);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex + right + up);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex - right + up);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex - right - up);
                 finalOutput += tex2D(_MainTex, IN.uv_MainTex + right - up);
                 
                 amount += amount;
                 up = float2(0.0, _MainTex_TexelSize.y) * amount;
                 right = float2(_MainTex_TexelSize.x, 0.0) * amount;
             }
             
             finalOutput = (finalOutput) / 25;
             
             fixed3 finalOutput2 = lerp((0,0,0,0), finalOutput.rgb, finalOutput.a);
             finalOutput2 = lerp(finalOutput2, finalOutput.rgb, original.a);
             finalOutput2 *= IN.color;
             
             o.Alpha = (finalOutput.a * IN.color.a);
             
             o.Albedo = finalOutput2;
             
         }
         
         ENDCG
     }
 }
 
Comment
Add comment · Show 4 · 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 Nevelson · Sep 25, 2020 at 03:41 AM 0
Share

So could I in theory use this shader one a 128x128 tilemap if I enable point filtering? Sorry to resurrect something from 2015 but I have a problem with jitteriness on my tile maps that I was hoping this might solve

avatar image domjon Nevelson · Sep 27, 2020 at 02:35 PM 0
Share

In theory, yes. Surface shaders like these are pretty much a thing of the past with URP and HDRP now sadly, but in you could definitely implement a similar "blur" using the same approach I did here. It essentially just places a bunch of offset duplicated layers and averages them out.

avatar image domjon Nevelson · Sep 27, 2020 at 02:36 PM 0
Share

If you don't have many tile maps, your best route might be to just resize your 128x128 to 512x512 and set the filter to 'bilinear' in the import settings to prevent jitter. I needed this shader fanciness because I had so many separate assets.

avatar image rileyhaspowers · Mar 07, 2021 at 03:29 AM 0
Share

I found a link to this thread on reddit. Hopefully it solves some of my jitter issues, thanks for uploading the script!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Which DXGI_FORMAT is supported by Unity's DDS Importer 0 Answers

Can i Use Pixel Animations for My Top Down Shooter? 1 Answer

Pixel Perfect and Forced Aspect ratio 0 Answers

When to use pixel perfect camera in 2D? 0 Answers

2D Game, Pixels in Borders are messy 0 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