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
0
Question by kOnINC · Mar 09, 2019 at 05:02 PM · shaderspriterenderingmaterial

Shader renders sprite imprecisely

Hello. I've found a shader that fits perfectly my needs (gradient radial shader) but I don't really know why it renders why sprite imprecisely. It renders the sprite with the same imprecision as the Standard shader in Unity. However, if I use the Sprites/Default shader in Unity it renders perfectly (so I know it's not an image problem).

My image is a sprite applied to a SpriteRender, with a material with RadialGradientShader applied to it.

How it looks with the RadialGradientShader (same with Unity default shader): alt text

How it looks with the Sprites/Default unity shader (intended rendering): alt text

I tried to adjust a few things myself but I don't know much about shaders and was unsuccessful.

Here is the shader (picked from here):

 Shader "Tavio/Sprites/RadialAlphaGradient"
 {
     Properties
     {
 
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
 
         _ColorCount("Color Count", int) = 3
         _AlphaCount("Alpha Count", int) = 3
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
           LOD 100
             Cull Off
             Lighting Off
             ZWrite Off
 
             Fog { Mode Off }
             ColorMask RGB
             Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 2.0
             #pragma multi_compile _ PIXELSNAP_ON
             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
             #include "UnityCG.cginc"
               struct appdata_t {
                  float4 vertex : POSITION;
                  float2 texcoord : TEXCOORD0;
                  float4 color    : COLOR;
              };
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 half2 texcoord : TEXCOORD0;
                 half4 color : COLOR;
             };
 
             fixed4 _Color;
             v2f vert (appdata_t v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.texcoord = v.texcoord;
                 o.color = _Color;
                 return o;
             }
 
             sampler2D _MainTex;
             sampler2D _AlphaTex;
 
             uniform fixed4 _Colors[32];
             uniform fixed _ColorAnchors[32];
             fixed4 _CurrentColor;
             fixed4 _NextColor;
             int _ColorCount = 0;
 
             uniform fixed _Alphas[32];
             uniform fixed _AlphaAnchors[32];
             fixed _CurrentAlpha;
             fixed _NextAlpha;
             int _AlphaCount = 0;
 
 
             fixed4 frag (v2f i) : SV_Target
             {
                 
                 float pos = 1 - length(i.texcoord - float2(0.5, 0.5)) * 1.41421356237; // 1.141... = sqrt(2)
 
                 fixed colorAnchor;
                 fixed nextColorAnchor;
 
                 for (int j = 0; j < _ColorCount; j++) {
                     colorAnchor = _ColorAnchors[j];
                     nextColorAnchor = _ColorAnchors[j + 1];
                     if (pos >= colorAnchor && pos < nextColorAnchor) {
                         _CurrentColor = _Colors[j];
                         _NextColor = _Colors[j + 1];
                         j = _ColorCount;
                     }
                 }
                 
                 fixed alphaAnchor;
                 fixed nextAlphaAnchor;
 
                 for (int j = 0; j < _AlphaCount; j++) {
                     alphaAnchor = _AlphaAnchors[j];
                     nextAlphaAnchor = _AlphaAnchors[j + 1];
                     if (pos >= alphaAnchor && pos < nextAlphaAnchor) {
                         _CurrentAlpha = _Alphas[j];
                         _NextAlpha = _Alphas[j + 1];
                         j = _AlphaCount;
                     }
                 }
 
                 fixed4 result = lerp(_CurrentColor, _NextColor, smoothstep(colorAnchor, nextColorAnchor, pos));
             
                 result.w = lerp(_CurrentAlpha, _NextAlpha, smoothstep(alphaAnchor, nextAlphaAnchor, pos));
 
                 return result;
 
             }
 
         ENDCG
         }
     }
 }


capture.png (14.9 kB)
captured.png (50.7 kB)
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

· Add your reply
  • Sort: 

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

207 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

Related Questions

Radial blur effect shader for textures to work with sprites? 1 Answer

Using Color.Lerp with Lightweight Render Pipeline 1 Answer

How can i get my quad to only render my texture without stretching it? 1 Answer

Sprite Material help 0 Answers

Standard Terrain Shader Upgrade to URP 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