• 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 /
avatar image
0
Question by Gandarufu · Sep 08, 2020 at 12:48 PM · shader programming

Shader Problem: Image Tear

Hi all,

I ported clay's Acid Swirls Shader from Shadertoy (https://www.shadertoy.com/view/3sGGDw) to Unity via the ShaderMan Plugin.

I got it working nicely, however I'm noticing that in Unity I see very visible screen tears that aren't there on Shadertoy. Does anybody know what this could be caused by?

Tear Version: https://imgur.com/7tf6Tdk

No Tear Version: https://imgur.com/F4SoMvw

Here's the full shader code that I ended up with:

 Shader "ShaderMan/Acid"
 {
   Properties
   {
     _MainTex ("MainTex", 2D) = "white" {}
     _AcidIntensity ("Intensity", Float) = 0.008
     _Amplitude ("Amplitude", Float) = .2
     _Frequency ("Frequency", Float) = 1.
     _Rotation ("Rotation", Float) = .4
     _Speed ("Speed", Float) = .4
   }
 
   SubShader
   {
     Tags { "RenderType"="Opaque" }
     LOD 100
 
     Pass
     {
       ZWrite Off
       Blend SrcAlpha OneMinusSrcAlpha
 
       CGPROGRAM
 
       #pragma vertex vert
       #pragma fragment frag
       #include "UnityCG.cginc"
 
       struct VertexInput {
         fixed4 vertex : POSITION;
         fixed2 uv:TEXCOORD0;
         fixed4 tangent : TANGENT;
         fixed3 normal : NORMAL;
       };
 
 
       struct VertexOutput
       {
         fixed4 pos : SV_POSITION;
         fixed2 uv : TEXCOORD0;
       };
 
       //Variables
       sampler2D _MainTex;
       CBUFFER_START(UnityPerMaterial)
         float _AcidIntensity;
         float _Amplitude;
         float _Frequency;
         float _Rotation;
         float _Speed;
       CBUFFER_END
 
       #define PI 3.14159265359
       #define CHECKERED
 
 
       fixed2x2 rot(fixed angle)
       {
           return fixed2x2(cos(angle), -sin(angle),
                       sin(angle), cos(angle));
       }
 
       fixed checkersign(fixed2 uv)
       {
         #ifdef CHECKERED
             uv = floor(uv);
             return sign(fmod(uv.x + uv.y, 2.) - .5);
         #else
             return 1.;
         #endif
       }
 
       fixed3 fmod289(fixed3 x)
       {
         return x - floor(x * (1.0 / 289.0)) * 289.0;
       }
 
       fixed4 fmod289(fixed4 x)
       {
         return x - floor(x * (1.0 / 289.0)) * 289.0;
       }
 
       fixed4 permute(fixed4 x)
       {
           return fmod289(((x*34.0)+1.0)*x);
       }
 
       fixed4 taylorInvSqrt(fixed4 r)
       {
         return 1.79284291400159 - 0.85373472095314 * r;
       }
 
       // https://github.com/ashima/webgl-noise/blob/master/src/noise3Dgrad.glsl
       // fmodified to allow for rotation 
       fixed snoise(fixed3 v, out fixed3 gradient, fixed time)
       {
         const fixed2  C = fixed2(1.0/6.0, 1.0/3.0) ;
         const fixed4  D = fixed4(0.0, 0.5, 1.0, 2.0);
 
         // First corner
         fixed3 i  = floor(v + dot(v, C.yyy) );
         fixed3 x0 =   v - i + dot(i, C.xxx) ;
 
         // Other corners
         fixed3 g = step(x0.yzx, x0.xyz);
         fixed3 l = 1.0 - g;
         fixed3 i1 = min( g.xyz, l.zxy );
         fixed3 i2 = max( g.xyz, l.zxy );
 
         //   x0 = x0 - 0.0 + 0.0 * C.xxx;
         //   x1 = x0 - i1  + 1.0 * C.xxx;
         //   x2 = x0 - i2  + 2.0 * C.xxx;
         //   x3 = x0 - 1.0 + 3.0 * C.xxx;
         fixed3 x1 = x0 - i1 + C.xxx;
         fixed3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
         fixed3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
 
         // Permutations
         i = fmod289(i); 
         fixed4 p = permute( permute( permute( 
                   i.z + fixed4(0.0, i1.z, i2.z, 1.0 ))
                 + i.y + fixed4(0.0, i1.y, i2.y, 1.0 )) 
                 + i.x + fixed4(0.0, i1.x, i2.x, 1.0 ));
           
         // Gradients: 7x7 points over a square, mapped onto an octahedron.
         // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
         fixed n_ = 0.142857142857; // 1.0/7.0
         fixed3  ns = n_ * D.wyz - D.xzx;
           
         fixed4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  fmod(p,7*7)
 
         fixed4 x_ = floor(j * ns.z);
         fixed4 y_ = floor(j - 7.0 * x_ );    // fmod(j,N)
 
         fixed4 x = x_ *ns.x + ns.yyyy;
         fixed4 y = y_ *ns.x + ns.yyyy;
         fixed4 h = 1.0 - abs(x) - abs(y);
 
         fixed4 b0 = fixed4( x.xy, y.xy );
         fixed4 b1 = fixed4( x.zw, y.zw );
 
         //fixed4 s0 = fixed4(lessThan(b0,0.0))*2.0 - 1.0;
         //fixed4 s1 = fixed4(lessThan(b1,0.0))*2.0 - 1.0;
         fixed4 s0 = floor(b0)*2.0 + 1.0;
         fixed4 s1 = floor(b1)*2.0 + 1.0;
         fixed4 sh = -step(h, fixed4(0.0,0.0,0.0,0.0));
 
         fixed4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
         fixed4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
 
         fixed3 p0 = fixed3(a0.xy,h.x);
         fixed3 p1 = fixed3(a0.zw,h.y);
         fixed3 p2 = fixed3(a1.xy,h.z);
         fixed3 p3 = fixed3(a1.zw,h.w);
 
         //Normalise gradients
         fixed4 norm = taylorInvSqrt(fixed4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
         p0 *= norm.x;
         p1 *= norm.y;
         p2 *= norm.z;
         p3 *= norm.w;
 
         // add rotation
         x0.xy = mul(x0.xy, rot(time*checkersign(a0.xy)));
         x1.xy = mul(x1.xy, (time*checkersign(a0.zw)));
         x2.xy = mul(x2.xy, rot(time*checkersign(a1.xy)));
         x3.xy = mul(x3.xy, rot(time*checkersign(a1.zw)));
           
         // Mix final noise value
         fixed4 m = max(0.6 - fixed4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
         fixed4 m2 = m * m;
         fixed4 m4 = m2 * m2;
         fixed4 pdotx = fixed4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3));
 
         // Determine noise gradient
         fixed4 temp = m2 * m * pdotx;
         gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
         gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
         gradient *= 42.0;
 
         return 42.0 * dot(m4, pdotx);
       }
 
       // Flow Noise: http://evasion.imag.fr/Publications/2001/PN01/
       fixed fbm(fixed3 p, inout fixed3 gradient)
       {
         // Initial values
         fixed value = 0.;
         fixed3 grad;
 
         // Loop of octaves
         for (int i = 0; i < 6; i++)
         {
           value += _Amplitude * snoise(_Frequency*p - gradient*.4, grad, _Time.y*_Rotation);
           gradient += _Amplitude * grad;
           _Frequency *= 1.8;
           _Amplitude *= .7;
           _Rotation *= .7;
         }
         return value;
       }
 
       VertexOutput vert (VertexInput v)
       {
         VertexOutput o;
         o.pos = UnityObjectToClipPos (v.vertex);
         o.uv = v.uv;
         return o;
       }
 
       fixed4 frag(VertexOutput i) : SV_Target
       {
         fixed2 uv = i.uv/1;
         fixed3 p = fixed3(uv, 0.);
         fixed3 gradient = fixed3(0.,0.,0.);
         fixed noise = fbm(p + fixed3(0., 0., _Time.y * _Speed), gradient);
         noise = noise*.5+.5;
         fixed3 background = tex2D(_MainTex, uv + gradient.xy * _AcidIntensity).rgb;
         fixed3 col = background;
         return fixed4(col,1.0);
       }
 
       ENDCG
     }
   }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

212 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

Related Questions

Simple "Render on Top of Everything" Shader? 1 Answer

Floating point error message in shader 0 Answers

Dinamic Gradient Shader 0 Answers

How do I get the fragment position in world coordinates? 1 Answer

Transparent shader alpha is black and the colors are shadowness? 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