Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2.  
  3. Shader "UI/Substance"
  4. {
  5.     Properties
  6.     {
  7.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  8.         _Color ("Tint", Color) = (1,1,1,1)
  9.        
  10.         _StencilComp ("Stencil Comparison", Float) = 8
  11.         _Stencil ("Stencil ID", Float) = 0
  12.         _StencilOp ("Stencil Operation", Float) = 0
  13.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
  14.         _StencilReadMask ("Stencil Read Mask", Float) = 255
  15.  
  16.         _ColorMask ("Color Mask", Float) = 15
  17.  
  18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  19.  
  20.         // substance
  21.         [NoScaleOffset]_RandomnessTex("Randomness Texture", 2D) = "white" {}
  22.         _Magnitude("Magnitude", Range(0,1)) = 1
  23.  
  24.         [NoScaleOffset]_SurfaceMask("Surface mask ", 2D) = "white" {}
  25.         [NoScaleOffset]_SurfaceMotionUp("Surface motion above", 2D) = "white" {}
  26.         [NoScaleOffset]_SurfaceMotionDown("Surface motion below", 2D) = "white" {}
  27.         _SurfaceCol("Surface color", Color) = (1,1,1,1)
  28.  
  29.         _Percentage("[0,1] Current substance percentage", Range(0,1)) = 0.5
  30.  
  31.         [NoScaleOffset]_ShapeTex("Shape (alpha), Frame (rgb)", 2D) = "white" {}
  32.  
  33.         [Toggle] _Effect1 ("Effect 1", Float) = 1
  34.         _MotionSpeed1 ("Motion speed", Vector) = (1,1,0,0)
  35.         [NoScaleOffset]_EffectTex1("Effect texture", 2D) = "white" {}
  36.         [NoScaleOffset]_MotionTex1("Texture describing movement", 2D) = "white" {}
  37.         _EffectCol1("Effect color", Color) = (1,1,1,1)
  38.  
  39.         [Toggle] _Effect2("Effect 2", Float) = 1
  40.         _MotionSpeed2("Motion speed", Vector) = (1,1,0,0)
  41.         [NoScaleOffset]_EffectTex2("Effect texture", 2D) = "white" {}
  42.         [NoScaleOffset]_MotionTex2("Texture describing movement", 2D) = "white" {}
  43.         _EffectCol2("Effect color", Color) = (1,1,1,1)
  44.     }
  45.  
  46.     SubShader
  47.     {
  48.         Tags
  49.         {
  50.             "Queue"="Transparent"
  51.             "IgnoreProjector"="True"
  52.             "RenderType"="Transparent"
  53.             "PreviewType"="Plane"
  54.             "CanUseSpriteAtlas"="True"
  55.         }
  56.        
  57.         Stencil
  58.         {
  59.             Ref [_Stencil]
  60.             Comp [_StencilComp]
  61.             Pass [_StencilOp]
  62.             ReadMask [_StencilReadMask]
  63.             WriteMask [_StencilWriteMask]
  64.         }
  65.  
  66.         Cull Off
  67.         Lighting Off
  68.         ZWrite Off
  69.         ZTest [unity_GUIZTestMode]
  70.         Blend SrcAlpha OneMinusSrcAlpha
  71.         ColorMask [_ColorMask]
  72.  
  73.         Pass
  74.         {
  75.             Name "Default"
  76.         CGPROGRAM
  77.             #pragma vertex vert
  78.             #pragma fragment frag
  79.             #pragma target 2.0
  80.  
  81.             #include "UnityCG.cginc"
  82.             #include "UnityUI.cginc"
  83.  
  84.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
  85.            
  86.             struct appdata_t
  87.             {
  88.                 float4 vertex   : POSITION;
  89.                 float4 color    : COLOR;
  90.                 float2 texcoord : TEXCOORD0;
  91.                 UNITY_VERTEX_INPUT_INSTANCE_ID
  92.             };
  93.  
  94.             struct v2f
  95.             {
  96.                 float4 vertex   : SV_POSITION;
  97.                 fixed4 color    : COLOR;
  98.                 float2 texcoord  : TEXCOORD0;
  99.                 float4 worldPosition : TEXCOORD1;
  100.                 UNITY_VERTEX_OUTPUT_STEREO
  101.             };
  102.            
  103.             fixed4 _Color;
  104.             fixed4 _TextureSampleAdd;
  105.             float4 _ClipRect;
  106.  
  107.             v2f vert(appdata_t IN)
  108.             {
  109.                 v2f OUT;
  110.                 UNITY_SETUP_INSTANCE_ID(IN);
  111.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  112.                 OUT.worldPosition = IN.vertex;
  113.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  114.  
  115.                 OUT.texcoord = IN.texcoord;
  116.                
  117.                 OUT.color = IN.color * _Color;
  118.                 return OUT;
  119.             }
  120.  
  121.  
  122.  
  123.             sampler2D _MainTex;
  124.  
  125.             sampler2D _SurfaceMask;
  126.             sampler2D _SurfaceMotionUp;
  127.             sampler2D _SurfaceMotionDown;
  128.             half4 _SurfaceCol;
  129.  
  130.             sampler2D _RandomnessTex;
  131.             float _Magnitude;
  132.  
  133.             uniform float _Percentage; // [0,1]
  134.             sampler2D _ShapeTex;
  135.  
  136.             float _Effect1;
  137.             sampler2D _MotionTex1;
  138.             sampler2D _EffectTex1;
  139.             half4 _EffectCol1;
  140.             float4 _MotionSpeed1;
  141.            
  142.             float _Effect2;
  143.             sampler2D _MotionTex2;
  144.             sampler2D _EffectTex2;
  145.             half4 _EffectCol2;
  146.             float4 _MotionSpeed2;
  147.  
  148.             fixed4 frag(v2f i) : SV_Target
  149.             {
  150.  
  151.  
  152.                 /*half4 color = (tex2D(_MainTex, i.texcoord) + _TextureSampleAdd) * i.color;
  153.                
  154.                 color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
  155.                
  156.                 #ifdef UNITY_UI_ALPHACLIP
  157.                 clip (color.a - 0.001);
  158.                 #endif
  159.  
  160.                 return color;*/
  161.  
  162.                 fixed4 col = fixed4(0,0,0,1); // fixed4(0,0,0,1)
  163.                 float alpha = col.a;
  164.                
  165.  
  166.                 fixed4 transparent = fixed4(0, 0, 0, 0);
  167.  
  168.  
  169.                 // FRAME
  170.                 float4 frameColor = tex2D(_ShapeTex, i.texcoord);
  171.                 float _FrameMask = frameColor.r; // greyscale texture
  172.                 // FRAME
  173.  
  174.                 if( frameColor.a < 0.1)
  175.                 {
  176.                     return transparent;
  177.                 }
  178.  
  179.                 // EFFECT 1
  180.                 if (_Effect1 > 0)
  181.                 {
  182.                     fixed4 motion1 = tex2D(_MotionTex1, i.texcoord);
  183.  
  184.                     motion1.y -= _Time.x * _MotionSpeed1.y;
  185.                     motion1.x -= _Time.x * _MotionSpeed1.x;
  186.                     fixed4 effect1 = tex2D(_EffectTex1, motion1.xy) * motion1.a;
  187.                     effect1 *= _EffectCol1;
  188.  
  189.                     col += effect1 * effect1.a;
  190.                 }
  191.                 // EFFECT 1
  192.  
  193.  
  194.                 // EFFECT 2
  195.                 if (_Effect2 > 0)
  196.                 {
  197.                     fixed4 motion2 = tex2D(_MotionTex2, i.texcoord);
  198.  
  199.                     motion2.y -= _Time.x * _MotionSpeed2.y;
  200.                     motion2.x -= _Time.x * _MotionSpeed2.x;
  201.                     fixed4 effect2 = tex2D(_EffectTex2, motion2.xy) * motion2.a;
  202.                     effect2 *= _EffectCol2;
  203.  
  204.                     col += effect2 * effect2.a;
  205.                 }
  206.                 // EFFECT 2
  207.  
  208.  
  209.                 col.a = alpha;
  210.  
  211.                 // FULL SUBSTANCE
  212.                 if (_Percentage == 1)
  213.                 {
  214.                     return fixed4(col.rgb * _FrameMask, col.a);;
  215.                 }
  216.                 // FULL SUBSTANCE
  217.  
  218.                
  219.  
  220.                 // SURFACE DISTORTION
  221.                 float2 distuv = float2(i.texcoord.x + _Time.x * 2 , i.texcoord.y + _Time.x * 2); // distortion y and x
  222.                 float2 disp = tex2D(_RandomnessTex, distuv).xy;
  223.                 disp = ((disp * 2) - 1) * _Magnitude;
  224.                 // SURFACE DISTORTION
  225.  
  226.  
  227.                
  228.  
  229.                 // SUBSTANCE PERCENTAGE
  230.                 int up = 0;
  231.                 float2 newUV;
  232.  
  233.                 if (i.texcoord.y >= _Percentage) // above we want transparent background
  234.                 {
  235.                     fixed4 motion = tex2D(_SurfaceMotionUp, i.texcoord + disp);
  236.                     motion.x -= _Time.x * 8;
  237.                     newUV = float2(motion.x, 1 - (i.texcoord.y - _Percentage));
  238.                     up = 1;
  239.                    
  240.                 }
  241.                 else // below we use blended color
  242.                 {  
  243.                     fixed4 motion = tex2D(_SurfaceMotionDown, i.texcoord + disp);
  244.                     motion.x -= _Time.x * 8;
  245.                     newUV = float2(motion.x, 1 - (_Percentage - i.texcoord.y));
  246.                 }
  247.                    
  248.                  
  249.                 fixed4 surfaceMask = tex2D(_SurfaceMask, newUV);
  250.                 if (up == 1)
  251.                 {
  252.                     col.a = surfaceMask.a;
  253.                 }
  254.  
  255.                 // SUBSTANCE PERCENTAGE
  256.  
  257.                 // BLEND
  258.                 surfaceMask *= _SurfaceCol;
  259.  
  260.                 float srcFactor = surfaceMask.a;
  261.                 float dstFactor = 1 - srcFactor;
  262.  
  263.                 fixed4 newCol = fixed4(col.rgb * dstFactor + surfaceMask.rgb * srcFactor, col.a);
  264.  
  265.                 newCol = fixed4(newCol.rgb * _FrameMask, newCol.a);
  266.                
  267.                 //return newCol;
  268.                 // BLEND
  269.  
  270.                 // SURFACE
  271.  
  272.  
  273.  
  274.  
  275.                 //half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  276.                 half4 color = newCol;
  277.                 color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
  278.                
  279.                 #ifdef UNITY_UI_ALPHACLIP
  280.                 clip (color.a - 0.001);
  281.                 #endif
  282.  
  283.                 return color;
  284.             }
  285.         ENDCG
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement