Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
  • Help Room /
This question was closed Dec 27, 2020 at 09:58 PM by twoski for the following reason:

Other

avatar image
0
Question by twoski · Dec 28, 2020 at 06:11 AM · shadershadersshader programmingpixelatedpixelation

Shader isn't pixel perfect

I have a shader which basically just uses perlin noise to layer several textures on top of eachother.

The texture is pixel art and i want it to be pixel perfect.

alt text

On the left is a screencap taken from the editor view while the scene is paused. You can see that the texture is exactly pixel perfect.

On the right is a screencap taken from a render target which the game camera renders to.

You can see that the pixel perfectness is gone, in several places the grass is 2px thick.

Every other sprite the camera renders to the render target doesn't have this problem. It's just this shader that has issues. My assumption is that the pixel art texture isn't aligned to the "grid" that everything else is on, so when the camera tries to render what it sees, there are pixels of grass that overlap more than 1 pixel technically. And since it uses point filtering, it renders 2 pixels of grass.

I think this issue needs to be solved at the shader level and the UV it uses needs to be worldspace or something but i dunno how i can rewrite it to work properly.

Here's the current shader:

 Shader "Custom/TexBlend3"
 {
     Properties
     {
         _GridSize ("GridSize", float) = 1.0
         _X ("Seed X", float) = 1.0
         _Y ("Seed Y", float) = 1.0
         _ScaleX ("Scale X", float) = 1.0
         _ScaleY ("Scale Y", float) = 1.0
         _Threshold1 ("Tex 1-2 Threshold", float) = 0.3
         _Threshold2 ("Tex 2-3 Threshold", float) = 0.6
         _Tex1 ("Tex1", 2D) = "white" {}
         _Tex2 ("Tex2", 2D) = "white" {}
         _Tex3 ("Tex3", 2D) = "white" {}
     }
  
     SubShader
     {
         Tags { "RenderType"="Opaque" }
  
         Pass
         {
             CGPROGRAM
             #pragma vertex vert_img
             #pragma fragment frag
             #include "UnityCG.cginc"
  
             float4 permute(float4 x)
             {
                 return fmod(34.0 * pow(x, 2) + x, 289.0);
             }
  
             float2 fade(float2 t) {
                 return 6.0 * pow(t, 5.0) - 15.0 * pow(t, 4.0) + 10.0 * pow(t, 3.0);
             }
  
             float4 taylorInvSqrt(float4 r) {
                 return 1.79284291400159 - 0.85373472095314 * r;
             }
  
             #define DIV_289 0.00346020761245674740484429065744f
  
             float mod289(float x) 
             {
                 return x - floor(x * DIV_289) * 289.0;
             }
  
             float PerlinNoise2D(float2 P)
             {
                   float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
                   float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
  
                   float4 ix = Pi.xzxz;
                   float4 iy = Pi.yyww;
                   float4 fx = Pf.xzxz;
                   float4 fy = Pf.yyww;
  
                   float4 i = permute(permute(ix) + iy);
  
                   float4 gx = frac(i / 41.0) * 2.0 - 1.0 ;
                   float4 gy = abs(gx) - 0.5 ;
                   float4 tx = floor(gx + 0.5);
                   gx = gx - tx;
  
                   float2 g00 = float2(gx.x,gy.x);
                   float2 g10 = float2(gx.y,gy.y);
                   float2 g01 = float2(gx.z,gy.z);
                   float2 g11 = float2(gx.w,gy.w);
  
                   float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
                   g00 *= norm.x;
                   g01 *= norm.y;
                   g10 *= norm.z;
                   g11 *= norm.w;
  
                   float n00 = dot(g00, float2(fx.x, fy.x));
                   float n10 = dot(g10, float2(fx.y, fy.y));
                   float n01 = dot(g01, float2(fx.z, fy.z));
                   float n11 = dot(g11, float2(fx.w, fy.w));
  
                   float2 fade_xy = fade(Pf.xy);
                   float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
                   float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
                   return 2.3 * n_xy;
             }
  
             float _GridSize;
             float _X;
             float _Y;
             float _ScaleX;
             float _ScaleY;
             float _Threshold1;
             float _Threshold2;
 
             sampler2D _Tex1;
             sampler2D _Tex2;
             sampler2D _Tex3;
  
             fixed4 frag(v2f_img i): SV_Target
             {
                 float2 uv = i.uv;
                 uv *= _GridSize;
                 uv += float2(_X, _Y);
                 uv /= float2(_ScaleX, _ScaleY);
                 float ns = PerlinNoise2D(uv) / 2 + 0.5f;
 
                 if (ns < _Threshold1)
                 {
                     return tex2D(_Tex1, i.uv);
                 }
                 if (ns < _Threshold2)
                 {
                     return tex2D(_Tex2, i.uv);
                 }
                 return tex2D(_Tex3, i.uv);
             }
 
             ENDCG
         }
     }
 }

If anyone can point out how i could render the texture using a worldspace UV i'd appreciate it.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

225 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to set the 'ForceNoShadowCasting' SubShader Tag via shader properties or a custom material editor? 1 Answer

Shader error when updating shader. 0 Answers

Shader error in 'libS/Lit': redefinition of 'SurfaceData' at 0 Answers

Apply inner stroke with fragment shader 0 Answers

Help with Base Color + Outline shader! 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