Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.6 KiB

  1. // Simplified SDF shader:
  2. // - No Shading Option (bevel / bump / env map)
  3. // - No Glow Option
  4. // - Softness is applied on both side of the outline
  5. Shader "TextMeshPro/Mobile/Distance Field - Masking" {
  6. Properties {
  7. _FaceColor ("Face Color", Color) = (1,1,1,1)
  8. _FaceDilate ("Face Dilate", Range(-1,1)) = 0
  9. _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  10. _OutlineWidth ("Outline Thickness", Range(0,1)) = 0
  11. _OutlineSoftness ("Outline Softness", Range(0,1)) = 0
  12. _UnderlayColor ("Border Color", Color) = (0,0,0,.5)
  13. _UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0
  14. _UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0
  15. _UnderlayDilate ("Border Dilate", Range(-1,1)) = 0
  16. _UnderlaySoftness ("Border Softness", Range(0,1)) = 0
  17. _WeightNormal ("Weight Normal", float) = 0
  18. _WeightBold ("Weight Bold", float) = .5
  19. _ShaderFlags ("Flags", float) = 0
  20. _ScaleRatioA ("Scale RatioA", float) = 1
  21. _ScaleRatioB ("Scale RatioB", float) = 1
  22. _ScaleRatioC ("Scale RatioC", float) = 1
  23. _MainTex ("Font Atlas", 2D) = "white" {}
  24. _TextureWidth ("Texture Width", float) = 512
  25. _TextureHeight ("Texture Height", float) = 512
  26. _GradientScale ("Gradient Scale", float) = 5
  27. _ScaleX ("Scale X", float) = 1
  28. _ScaleY ("Scale Y", float) = 1
  29. _PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875
  30. _Sharpness ("Sharpness", Range(-1,1)) = 0
  31. _VertexOffsetX ("Vertex OffsetX", float) = 0
  32. _VertexOffsetY ("Vertex OffsetY", float) = 0
  33. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  34. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  35. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  36. _MaskTex ("Mask Texture", 2D) = "white" {}
  37. _MaskInverse ("Inverse", float) = 0
  38. _MaskEdgeColor ("Edge Color", Color) = (1,1,1,1)
  39. _MaskEdgeSoftness ("Edge Softness", Range(0, 1)) = 0.01
  40. _MaskWipeControl ("Wipe Position", Range(0, 1)) = 0.5
  41. _StencilComp ("Stencil Comparison", Float) = 8
  42. _Stencil ("Stencil ID", Float) = 0
  43. _StencilOp ("Stencil Operation", Float) = 0
  44. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  45. _StencilReadMask ("Stencil Read Mask", Float) = 255
  46. _ColorMask ("Color Mask", Float) = 15
  47. }
  48. SubShader {
  49. Tags
  50. {
  51. "Queue"="Transparent"
  52. "IgnoreProjector"="True"
  53. "RenderType"="Transparent"
  54. }
  55. Stencil
  56. {
  57. Ref [_Stencil]
  58. Comp [_StencilComp]
  59. Pass [_StencilOp]
  60. ReadMask [_StencilReadMask]
  61. WriteMask [_StencilWriteMask]
  62. }
  63. Cull [_CullMode]
  64. ZWrite Off
  65. Lighting Off
  66. Fog { Mode Off }
  67. ZTest [unity_GUIZTestMode]
  68. Blend One OneMinusSrcAlpha
  69. ColorMask [_ColorMask]
  70. Pass {
  71. CGPROGRAM
  72. #pragma vertex VertShader
  73. #pragma fragment PixShader
  74. #pragma shader_feature __ OUTLINE_ON
  75. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  76. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  77. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  78. #include "UnityCG.cginc"
  79. #include "UnityUI.cginc"
  80. #include "TMPro_Properties.cginc"
  81. struct vertex_t {
  82. float4 vertex : POSITION;
  83. float3 normal : NORMAL;
  84. fixed4 color : COLOR;
  85. float2 texcoord0 : TEXCOORD0;
  86. float2 texcoord1 : TEXCOORD1;
  87. };
  88. struct pixel_t {
  89. float4 vertex : SV_POSITION;
  90. fixed4 faceColor : COLOR;
  91. fixed4 outlineColor : COLOR1;
  92. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  93. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  94. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  95. #if (UNDERLAY_ON | UNDERLAY_INNER)
  96. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  97. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  98. #endif
  99. };
  100. float _MaskWipeControl;
  101. float _MaskEdgeSoftness;
  102. fixed4 _MaskEdgeColor;
  103. bool _MaskInverse;
  104. pixel_t VertShader(vertex_t input)
  105. {
  106. float bold = step(input.texcoord1.y, 0);
  107. float4 vert = input.vertex;
  108. vert.x += _VertexOffsetX;
  109. vert.y += _VertexOffsetY;
  110. float4 vPosition = UnityObjectToClipPos(vert);
  111. float2 pixelSize = vPosition.w;
  112. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  113. float scale = rsqrt(dot(pixelSize, pixelSize));
  114. scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + 1);
  115. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  116. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  117. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  118. float layerScale = scale;
  119. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  120. float bias = (0.5 - weight) * scale - 0.5;
  121. float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  122. float opacity = input.color.a;
  123. #if (UNDERLAY_ON | UNDERLAY_INNER)
  124. opacity = 1.0;
  125. #endif
  126. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  127. faceColor.rgb *= faceColor.a;
  128. fixed4 outlineColor = _OutlineColor;
  129. outlineColor.a *= opacity;
  130. outlineColor.rgb *= outlineColor.a;
  131. outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
  132. #if (UNDERLAY_ON | UNDERLAY_INNER)
  133. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  134. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  135. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  136. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  137. float2 layerOffset = float2(x, y);
  138. #endif
  139. // Generate UV for the Masking Texture
  140. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  141. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  142. // Structure for pixel shader
  143. pixel_t output = {
  144. vPosition,
  145. faceColor,
  146. outlineColor,
  147. float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
  148. half4(scale, bias - outline, bias + outline, bias),
  149. half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
  150. #if (UNDERLAY_ON | UNDERLAY_INNER)
  151. float4(input.texcoord0 + layerOffset, input.color.a, 0),
  152. half2(layerScale, layerBias),
  153. #endif
  154. };
  155. return output;
  156. }
  157. // PIXEL SHADER
  158. fixed4 PixShader(pixel_t input) : SV_Target
  159. {
  160. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  161. half4 c = input.faceColor * saturate(d - input.param.w);
  162. #ifdef OUTLINE_ON
  163. c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  164. c *= saturate(d - input.param.y);
  165. #endif
  166. #if UNDERLAY_ON
  167. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  168. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  169. #endif
  170. #if UNDERLAY_INNER
  171. half sd = saturate(d - input.param.z);
  172. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  173. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  174. #endif
  175. // Alternative implementation to UnityGet2DClipping with support for softness.
  176. #if UNITY_UI_CLIP_RECT
  177. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  178. c *= m.x * m.y;
  179. #endif
  180. float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a);
  181. float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl;
  182. a = saturate(t / _MaskEdgeSoftness);
  183. c.rgb = lerp(_MaskEdgeColor.rgb*c.a, c.rgb, a);
  184. c *= a;
  185. #if (UNDERLAY_ON | UNDERLAY_INNER)
  186. c *= input.texcoord1.z;
  187. #endif
  188. #if UNITY_UI_ALPHACLIP
  189. clip(c.a - 0.001);
  190. #endif
  191. return c;
  192. }
  193. ENDCG
  194. }
  195. }
  196. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  197. }