Advertisement
WolfeyGamedev

RGB Spectrum Text Shader

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