Advertisement
AStrkl

TextMeshPro Wobbly shader

Nov 8th, 2019
1,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.03 KB | None | 0 0
  1. Shader "TextMeshPro/Distance Field Wiggle Vertex Alpha" {
  2.  
  3. Properties {
  4.     _FaceTex            ("Face Texture", 2D) = "white" {}
  5.     _FaceUVSpeedX       ("Face UV Speed X", Range(-5, 5)) = 0.0
  6.     _FaceUVSpeedY       ("Face UV Speed Y", Range(-5, 5)) = 0.0
  7.     _FaceColor          ("Face Color", Color) = (1,1,1,1)
  8.     _FaceDilate         ("Face Dilate", Range(-1,1)) = 0
  9.  
  10.     _OutlineColor       ("Outline Color", Color) = (0,0,0,1)
  11.     _OutlineTex         ("Outline Texture", 2D) = "white" {}
  12.     _OutlineUVSpeedX    ("Outline UV Speed X", Range(-5, 5)) = 0.0
  13.     _OutlineUVSpeedY    ("Outline UV Speed Y", Range(-5, 5)) = 0.0
  14.     _OutlineWidth       ("Outline Thickness", Range(0, 1)) = 0
  15.     _OutlineSoftness    ("Outline Softness", Range(-1,1)) = 0
  16.  
  17.     _Bevel              ("Bevel", Range(0,1)) = 0.5
  18.     _BevelOffset        ("Bevel Offset", Range(-0.5,0.5)) = 0
  19.     _BevelWidth         ("Bevel Width", Range(-.5,0.5)) = 0
  20.     _BevelClamp         ("Bevel Clamp", Range(0,1)) = 0
  21.     _BevelRoundness     ("Bevel Roundness", Range(0,1)) = 0
  22.  
  23.     _LightAngle         ("Light Angle", Range(0.0, 6.2831853)) = 3.1416
  24.     _SpecularColor      ("Specular", Color) = (1,1,1,1)
  25.     _SpecularPower      ("Specular", Range(0,4)) = 2.0
  26.     _Reflectivity       ("Reflectivity", Range(5.0,15.0)) = 10
  27.     _Diffuse            ("Diffuse", Range(0,1)) = 0.5
  28.     _Ambient            ("Ambient", Range(1,0)) = 0.5
  29.  
  30.     _BumpMap            ("Normal map", 2D) = "bump" {}
  31.     _BumpOutline        ("Bump Outline", Range(0,1)) = 0
  32.     _BumpFace           ("Bump Face", Range(0,1)) = 0
  33.  
  34.     _ReflectFaceColor   ("Reflection Color", Color) = (0,0,0,1)
  35.     _ReflectOutlineColor("Reflection Color", Color) = (0,0,0,1)
  36.     _Cube               ("Reflection Cubemap", Cube) = "black" { /* TexGen CubeReflect */ }
  37.     _EnvMatrixRotation  ("Texture Rotation", vector) = (0, 0, 0, 0)
  38.        
  39.  
  40.     _UnderlayColor      ("Border Color", Color) = (0,0,0, 0.5)
  41.     _UnderlayOffsetX    ("Border OffsetX", Range(-1,1)) = 0
  42.     _UnderlayOffsetY    ("Border OffsetY", Range(-1,1)) = 0
  43.     _UnderlayDilate     ("Border Dilate", Range(-1,1)) = 0
  44.     _UnderlaySoftness   ("Border Softness", Range(0,1)) = 0
  45.  
  46.     _GlowColor          ("Color", Color) = (0, 1, 0, 0.5)
  47.     _GlowOffset         ("Offset", Range(-1,1)) = 0
  48.     _GlowInner          ("Inner", Range(0,1)) = 0.05
  49.     _GlowOuter          ("Outer", Range(0,1)) = 0.05
  50.     _GlowPower          ("Falloff", Range(1, 0)) = 0.75
  51.  
  52.     _WeightNormal       ("Weight Normal", float) = 0
  53.     _WeightBold         ("Weight Bold", float) = 0.5
  54.  
  55.     _ShaderFlags        ("Flags", float) = 0
  56.     _ScaleRatioA        ("Scale RatioA", float) = 1
  57.     _ScaleRatioB        ("Scale RatioB", float) = 1
  58.     _ScaleRatioC        ("Scale RatioC", float) = 1
  59.  
  60.     _MainTex            ("Font Atlas", 2D) = "white" {}
  61.     _TextureWidth       ("Texture Width", float) = 512
  62.     _TextureHeight      ("Texture Height", float) = 512
  63.     _GradientScale      ("Gradient Scale", float) = 5.0
  64.     _ScaleX             ("Scale X", float) = 1.0
  65.     _ScaleY             ("Scale Y", float) = 1.0
  66.     _PerspectiveFilter  ("Perspective Correction", Range(0, 1)) = 0.875
  67.     _Sharpness          ("Sharpness", Range(-1,1)) = 0
  68.  
  69.     _VertexOffsetX      ("Vertex OffsetX", float) = 0
  70.     _VertexOffsetY      ("Vertex OffsetY", float) = 0
  71.    
  72.     _MaskCoord          ("Mask Coordinates", vector) = (0, 0, 32767, 32767)
  73.     _ClipRect           ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  74.     _MaskSoftnessX      ("Mask SoftnessX", float) = 0
  75.     _MaskSoftnessY      ("Mask SoftnessY", float) = 0
  76.  
  77.     _StencilComp        ("Stencil Comparison", Float) = 8
  78.     _Stencil            ("Stencil ID", Float) = 0
  79.     _StencilOp          ("Stencil Operation", Float) = 0
  80.     _StencilWriteMask   ("Stencil Write Mask", Float) = 255
  81.     _StencilReadMask    ("Stencil Read Mask", Float) = 255
  82.  
  83.     _ColorMask          ("Color Mask", Float) = 15
  84.  
  85. }
  86.  
  87. SubShader {
  88.  
  89.     Tags
  90.     {
  91.         "Queue"="Transparent"
  92.         "IgnoreProjector"="True"
  93.         "RenderType"="Transparent"
  94.     }
  95.  
  96.     Stencil
  97.     {
  98.         Ref [_Stencil]
  99.         Comp [_StencilComp]
  100.         Pass [_StencilOp]
  101.         ReadMask [_StencilReadMask]
  102.         WriteMask [_StencilWriteMask]
  103.     }
  104.  
  105.     Cull [_CullMode]
  106.     ZWrite Off
  107.     Lighting Off
  108.     Fog { Mode Off }
  109.     ZTest [unity_GUIZTestMode]
  110.     Blend One OneMinusSrcAlpha
  111.     ColorMask [_ColorMask]
  112.  
  113.     Pass {
  114.         CGPROGRAM
  115.         #pragma target 3.0
  116.         #pragma vertex VertShader
  117.         #pragma fragment PixShader
  118.         #pragma shader_feature __ BEVEL_ON
  119.         #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  120.         #pragma shader_feature __ GLOW_ON
  121.  
  122.         #pragma multi_compile __ UNITY_UI_CLIP_RECT
  123.         #pragma multi_compile __ UNITY_UI_ALPHACLIP
  124.  
  125.         #include "UnityCG.cginc"
  126.         #include "UnityUI.cginc"
  127.         #include "TMPro_Properties.cginc"
  128.         #include "TMPro.cginc"
  129.  
  130.         struct vertex_t {
  131.             UNITY_VERTEX_INPUT_INSTANCE_ID
  132.             float4  position        : POSITION;
  133.             float3  normal          : NORMAL;
  134.             fixed4  color           : COLOR;
  135.             float2  texcoord0       : TEXCOORD0;
  136.             float2  texcoord1       : TEXCOORD1;
  137.         };
  138.  
  139.  
  140.         struct pixel_t {
  141.             UNITY_VERTEX_INPUT_INSTANCE_ID
  142.             UNITY_VERTEX_OUTPUT_STEREO
  143.             float4  position        : SV_POSITION;
  144.             fixed4  color           : COLOR;
  145.             float2  atlas           : TEXCOORD0;        // Atlas
  146.             float4  param           : TEXCOORD1;        // alphaClip, scale, bias, weight
  147.             float4  mask            : TEXCOORD2;        // Position in object space(xy), pixel Size(zw)
  148.             float3  viewDir         : TEXCOORD3;
  149.            
  150.         #if (UNDERLAY_ON || UNDERLAY_INNER)
  151.             float4  texcoord2       : TEXCOORD4;        // u,v, scale, bias
  152.             fixed4  underlayColor   : COLOR1;
  153.         #endif
  154.             float4 textures         : TEXCOORD5;
  155.         };
  156.  
  157.         // Used by Unity internally to handle Texture Tiling and Offset.
  158.         float4 _FaceTex_ST;
  159.         float4 _OutlineTex_ST;
  160.  
  161.  
  162.  
  163.         pixel_t VertShader(vertex_t input)
  164.         {
  165.             pixel_t output;
  166.  
  167.             UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  168.             UNITY_SETUP_INSTANCE_ID(input);
  169.             UNITY_TRANSFER_INSTANCE_ID(input,output);
  170.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  171.  
  172.             float bold = step(input.texcoord1.y, 0);
  173.  
  174.             float4 vert = input.position;
  175.             vert.x += _VertexOffsetX;
  176.             vert.y += _VertexOffsetY;
  177.  
  178.             float4 vPosition = UnityObjectToClipPos(vert);
  179.  
  180.             float2 pixelSize = vPosition.w;
  181.             pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  182.             float scale = rsqrt(dot(pixelSize, pixelSize));
  183.             scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + 1);
  184.             if (UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  185.  
  186.             float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  187.             weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  188.  
  189.             float bias =(.5 - weight) + (.5 / scale);
  190.  
  191.             float alphaClip = (1.0 - _OutlineWidth * _ScaleRatioA - _OutlineSoftness * _ScaleRatioA);
  192.        
  193.         #if GLOW_ON
  194.             alphaClip = min(alphaClip, 1.0 - _GlowOffset * _ScaleRatioB - _GlowOuter * _ScaleRatioB);
  195.         #endif
  196.  
  197.             alphaClip = alphaClip / 2.0 - ( .5 / scale) - weight;
  198.  
  199.         #if (UNDERLAY_ON || UNDERLAY_INNER)
  200.             float4 underlayColor = _UnderlayColor;
  201.             underlayColor.rgb *= underlayColor.a;
  202.  
  203.             float bScale = scale;
  204.             bScale /= 1 + ((_UnderlaySoftness*_ScaleRatioC) * bScale);
  205.             float bBias = (0.5 - weight) * bScale - 0.5 - ((_UnderlayDilate * _ScaleRatioC) * 0.5 * bScale);
  206.  
  207.             float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  208.             float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  209.             float2 bOffset = float2(x, y);
  210.         #endif
  211.  
  212.             // Generate UV for the Masking Texture
  213.             float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  214.             float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  215.  
  216.             // Support for texture tiling and offset
  217.             float2 textureUV = UnpackUV(input.texcoord1.x);
  218.             float2 faceUV = TRANSFORM_TEX(textureUV, _FaceTex);
  219.             float2 outlineUV = TRANSFORM_TEX(textureUV, _OutlineTex);
  220.  
  221.            
  222.             //WIGGLE EFFECT - Vertex alpha 0 = full effect
  223.  
  224.             float _WobbleStrength = 0.20 ;
  225.             float _WobbleSpeed = 450.0;
  226.             float _WobbleTiling = 1.25;
  227.  
  228.             float noise = sin((vert.x *_WobbleTiling*0.1) + (_Time * _WobbleSpeed)) *_WobbleStrength*0.1;
  229.             vPosition += float4(0,noise * (1.0-input.color.a),0,0);
  230.  
  231.            
  232.  
  233.             output.position = vPosition;
  234.             output.color = input.color;
  235.             output.atlas =  input.texcoord0;
  236.             output.param =  float4(alphaClip, scale, bias, weight);
  237.             output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
  238.             output.viewDir =    mul((float3x3)_EnvMatrix, _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, vert).xyz);
  239.             #if (UNDERLAY_ON || UNDERLAY_INNER)
  240.             output.texcoord2 = float4(input.texcoord0 + bOffset, bScale, bBias);
  241.             output.underlayColor =  underlayColor;
  242.             #endif
  243.             output.textures = float4(faceUV, outlineUV);
  244.    
  245.             return output;
  246.         }
  247.  
  248.  
  249.         fixed4 PixShader(pixel_t input) : SV_Target
  250.         {
  251.             UNITY_SETUP_INSTANCE_ID(input);
  252.  
  253.             float c = tex2D(_MainTex, input.atlas).a;
  254.        
  255.         #ifndef UNDERLAY_ON
  256.             clip(c - input.param.x);
  257.         #endif
  258.  
  259.             float   scale   = input.param.y;
  260.             float   bias    = input.param.z;
  261.             float   weight  = input.param.w;
  262.             float   sd = (bias - c) * scale;
  263.  
  264.             float outline = (_OutlineWidth * _ScaleRatioA) * scale;
  265.             float softness = (_OutlineSoftness * _ScaleRatioA) * scale;
  266.  
  267.             half4 faceColor = _FaceColor;
  268.             half4 outlineColor = _OutlineColor;
  269.  
  270.             faceColor.rgb *= input.color.rgb;
  271.            
  272.             faceColor *= tex2D(_FaceTex, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y);
  273.             outlineColor *= tex2D(_OutlineTex, input.textures.zw + float2(_OutlineUVSpeedX, _OutlineUVSpeedY) * _Time.y);
  274.  
  275.             faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
  276.  
  277.         #if BEVEL_ON
  278.             float3 dxy = float3(0.5 / _TextureWidth, 0.5 / _TextureHeight, 0);
  279.             float3 n = GetSurfaceNormal(input.atlas, weight, dxy);
  280.  
  281.             float3 bump = UnpackNormal(tex2D(_BumpMap, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y)).xyz;
  282.             bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
  283.             n = normalize(n- bump);
  284.  
  285.             float3 light = normalize(float3(sin(_LightAngle), cos(_LightAngle), -1.0));
  286.  
  287.             float3 col = GetSpecular(n, light);
  288.             faceColor.rgb += col*faceColor.a;
  289.             faceColor.rgb *= 1-(dot(n, light)*_Diffuse);
  290.             faceColor.rgb *= lerp(_Ambient, 1, n.z*n.z);
  291.  
  292.             fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDir, -n));
  293.             faceColor.rgb += reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  294.         #endif
  295.  
  296.         #if UNDERLAY_ON
  297.             float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
  298.             faceColor += input.underlayColor * saturate(d - input.texcoord2.w) * (1 - faceColor.a);
  299.         #endif
  300.  
  301.         #if UNDERLAY_INNER
  302.             float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
  303.             faceColor += input.underlayColor * (1 - saturate(d - input.texcoord2.w)) * saturate(1 - sd) * (1 - faceColor.a);
  304.         #endif
  305.  
  306.         #if GLOW_ON
  307.             float4 glowColor = GetGlowColor(sd, scale);
  308.             faceColor.rgb += glowColor.rgb * glowColor.a;
  309.         #endif
  310.  
  311.         // Alternative implementation to UnityGet2DClipping with support for softness.
  312.         #if UNITY_UI_CLIP_RECT
  313.             half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  314.             faceColor *= m.x * m.y;
  315.         #endif
  316.  
  317.         #if UNITY_UI_ALPHACLIP
  318.             clip(faceColor.a - 0.001);
  319.         #endif
  320.  
  321.         //return faceColor * input.color.a;  using alpha for wiggle mask instead of transparency
  322.         return faceColor;
  323.         }
  324.  
  325.         ENDCG
  326.     }
  327. }
  328.  
  329. Fallback "TextMeshPro/Mobile/Distance Field"
  330. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement