Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
2
Question by Coguelin · Aug 09, 2016 at 02:11 PM · uishaderfragmentflow

Additive Flow Texture in a UI Shader

Hey folks, I've been asked to put together a shader which might allow us to have a subtle flowing watermark in our UI Elements (i.e. Buttons).

I've made a few flow shaders before but I haven't done any kind of work with UI shaders... or Sprite shaders, for that matter... so I'm quietly going insane.

My code looks like this :

 Shader "Custom/UI/AdditiveFlow"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         
         _StencilComp ("Stencil Comparison", Float) = 8
         _Stencil ("Stencil ID", Float) = 0
         _StencilOp ("Stencil Operation", Float) = 0
         _StencilWriteMask ("Stencil Write Mask", Float) = 255
         _StencilReadMask ("Stencil Read Mask", Float) = 255
 
         _ColorMask ("Color Mask", Float) = 15
 
         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
 
         _FlowTex("Flow Texture", 2D) = "grey" {}
         _XMul ("UV.x mul", float) = 0.25
         _YMul ("UV.y mul", float) = 0.25
         _Speed ("Speed", float) = 0.05
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         
         Stencil
         {
             Ref [_Stencil]
             Comp [_StencilComp]
             Pass [_StencilOp] 
             ReadMask [_StencilReadMask]
             WriteMask [_StencilWriteMask]
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         ZTest [unity_GUIZTestMode]
         Blend SrcAlpha OneMinusSrcAlpha
         ColorMask [_ColorMask]
 
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
             #include "UnityUI.cginc"
 
             #pragma multi_compile __ UNITY_UI_ALPHACLIP
             
             struct appdata_t
             {
                 float4 vertex   : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
                 float2 flowcoord : TEXCOORD1;
             };
 
             struct v2f
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
                 half2 texcoord  : TEXCOORD0;
                 float2 flowcoord : TEXCOORD1;
                 float4 worldPosition : TEXCOORD2;
             };
             
             fixed4 _Color;
             fixed4 _TextureSampleAdd;
             float4 _ClipRect;
 
             v2f vert(appdata_t IN)
             {
                 v2f OUT;
                 OUT.worldPosition = IN.vertex;
                 OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
 
                 OUT.texcoord = IN.texcoord;
                 
                 #ifdef UNITY_HALF_TEXEL_OFFSET
                 OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
                 #endif
 
                 OUT.flowcoord = IN.flowcoord;
 
                 OUT.color = IN.color * _Color;
                 return OUT;
             }
 
             uniform sampler2D _MainTex;
             uniform sampler2D _FlowTex;
             uniform fixed _XMul;
                uniform fixed _YMul;
             uniform fixed _Speed;
 
             fixed4 frag(v2f IN) : SV_Target
             {
                 fixed phase = _Time[1];
                 fixed2 flowDir = fixed2(_XMul, _YMul) * _Speed;
                 fixed2 flowUV = IN.flowcoord + flowDir * phase;
                 fixed4 tex1 = tex2D(_FlowTex, flowUV);
                 fixed4 tex0 = tex2D(_MainTex, IN.texcoord);
 
                 fixed4 add = tex1 < .5 ? 2.0 * tex1 * tex0 : 1.0 - 2.0 * (1.0 - tex1) * (1.0 - tex0); //additive calc
 
                 fixed4 c = (add + _TextureSampleAdd) * IN.color;
 
                 c.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                 
                 #ifdef UNITY_UI_ALPHACLIP
                 clip (c.a - 0.001);
                 #endif
 
                 return c;
             }
         ENDCG
         }
     }
 }

I can think of several issues I'm probably running up against... starting with the fact that there isn't really a UV for me to handle all that texture wizardry, and ending with the fact that there are several things going on in the Default UI shader which (if I'm honest) I can only guess at the purpose of.

Help me Obiwanswer Kenobi, you are my only hope.

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

88 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

Related Questions

Camera layering : UI shader is cleared by second camera with Depth Only flag 1 Answer

Projected Texture Shader Issues 1 Answer

Simple shader causing random portions of object to disappear at random camera positions 0 Answers

How do I invert the color of ui text to make it more readable, on a messy background? 4 Answers

UI Triangle Color picker 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