MixedRealityTextMeshPro.shader 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. // Simplified SDF shader:
  4. // - No Shading Option (bevel / bump / env map)
  5. // - No Glow Option
  6. // - Softness is applied on both side of the outline
  7. // MRTK Additions
  8. // - Single Pass Instanced Stereo Rendering Support
  9. // - Support for Clipping Primitives (Plane, Sphere, Box)
  10. // - ZWrite Property
  11. Shader "Mixed Reality Toolkit/TextMeshPro" {
  12. Properties {
  13. _FaceColor ("Face Color", Color) = (1,1,1,1)
  14. _FaceDilate ("Face Dilate", Range(-1,1)) = 0
  15. _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  16. _OutlineWidth ("Outline Thickness", Range(0,1)) = 0
  17. _OutlineSoftness ("Outline Softness", Range(0,1)) = 0
  18. _UnderlayColor ("Border Color", Color) = (0,0,0,.5)
  19. _UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0
  20. _UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0
  21. _UnderlayDilate ("Border Dilate", Range(-1,1)) = 0
  22. _UnderlaySoftness ("Border Softness", Range(0,1)) = 0
  23. _WeightNormal ("Weight Normal", float) = 0
  24. _WeightBold ("Weight Bold", float) = .5
  25. _ShaderFlags ("Flags", float) = 0
  26. _ScaleRatioA ("Scale RatioA", float) = 1
  27. _ScaleRatioB ("Scale RatioB", float) = 1
  28. _ScaleRatioC ("Scale RatioC", float) = 1
  29. _MainTex ("Font Atlas", 2D) = "white" {}
  30. _TextureWidth ("Texture Width", float) = 512
  31. _TextureHeight ("Texture Height", float) = 512
  32. _GradientScale ("Gradient Scale", float) = 5
  33. _ScaleX ("Scale X", float) = 1
  34. _ScaleY ("Scale Y", float) = 1
  35. _PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875
  36. _Sharpness ("Sharpness", Range(-1,1)) = 0
  37. _VertexOffsetX ("Vertex OffsetX", float) = 0
  38. _VertexOffsetY ("Vertex OffsetY", float) = 0
  39. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  40. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  41. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  42. _StencilComp ("Stencil Comparison", Float) = 8
  43. _Stencil ("Stencil ID", Float) = 0
  44. _StencilOp ("Stencil Operation", Float) = 0
  45. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  46. _StencilReadMask ("Stencil Read Mask", Float) = 255
  47. _ColorMask ("Color Mask", Float) = 15
  48. _ZWrite ("Depth Write", Float) = 0
  49. }
  50. SubShader {
  51. Tags
  52. {
  53. "Queue"="Transparent"
  54. "IgnoreProjector"="True"
  55. "RenderType"="Transparent"
  56. }
  57. Stencil
  58. {
  59. Ref [_Stencil]
  60. Comp [_StencilComp]
  61. Pass [_StencilOp]
  62. ReadMask [_StencilReadMask]
  63. WriteMask [_StencilWriteMask]
  64. }
  65. Cull [_CullMode]
  66. ZWrite[_ZWrite]
  67. Lighting Off
  68. Fog { Mode Off }
  69. ZTest [unity_GUIZTestMode]
  70. Blend One OneMinusSrcAlpha
  71. ColorMask [_ColorMask]
  72. Pass {
  73. CGPROGRAM
  74. #pragma vertex VertShader
  75. #pragma fragment PixShader
  76. #pragma shader_feature __ OUTLINE_ON
  77. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  78. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  79. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  80. #pragma multi_compile __ _CLIPPING_PLANE
  81. #pragma multi_compile __ _CLIPPING_SPHERE
  82. #pragma multi_compile __ _CLIPPING_BOX
  83. #include "UnityCG.cginc"
  84. #include "UnityUI.cginc"
  85. #if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX)
  86. #define _CLIPPING_PRIMITIVE
  87. #else
  88. #undef _CLIPPING_PRIMITIVE
  89. #endif
  90. // Direct include for portability.
  91. //#include "TMPro_Properties.cginc"
  92. // UI Editable properties
  93. uniform sampler2D _FaceTex; // Alpha : Signed Distance
  94. uniform float _FaceUVSpeedX;
  95. uniform float _FaceUVSpeedY;
  96. uniform fixed4 _FaceColor; // RGBA : Color + Opacity
  97. uniform float _FaceDilate; // v[ 0, 1]
  98. uniform float _OutlineSoftness; // v[ 0, 1]
  99. uniform sampler2D _OutlineTex; // RGBA : Color + Opacity
  100. uniform float _OutlineUVSpeedX;
  101. uniform float _OutlineUVSpeedY;
  102. uniform fixed4 _OutlineColor; // RGBA : Color + Opacity
  103. uniform float _OutlineWidth; // v[ 0, 1]
  104. uniform float _Bevel; // v[ 0, 1]
  105. uniform float _BevelOffset; // v[-1, 1]
  106. uniform float _BevelWidth; // v[-1, 1]
  107. uniform float _BevelClamp; // v[ 0, 1]
  108. uniform float _BevelRoundness; // v[ 0, 1]
  109. uniform sampler2D _BumpMap; // Normal map
  110. uniform float _BumpOutline; // v[ 0, 1]
  111. uniform float _BumpFace; // v[ 0, 1]
  112. uniform samplerCUBE _Cube; // Cube / sphere map
  113. uniform fixed4 _ReflectFaceColor; // RGB intensity
  114. uniform fixed4 _ReflectOutlineColor;
  115. //uniform float _EnvTiltX; // v[-1, 1]
  116. //uniform float _EnvTiltY; // v[-1, 1]
  117. uniform float3 _EnvMatrixRotation;
  118. uniform float4x4 _EnvMatrix;
  119. uniform fixed4 _SpecularColor; // RGB intensity
  120. uniform float _LightAngle; // v[ 0,Tau]
  121. uniform float _SpecularPower; // v[ 0, 1]
  122. uniform float _Reflectivity; // v[ 5, 15]
  123. uniform float _Diffuse; // v[ 0, 1]
  124. uniform float _Ambient; // v[ 0, 1]
  125. uniform fixed4 _UnderlayColor; // RGBA : Color + Opacity
  126. uniform float _UnderlayOffsetX; // v[-1, 1]
  127. uniform float _UnderlayOffsetY; // v[-1, 1]
  128. uniform float _UnderlayDilate; // v[-1, 1]
  129. uniform float _UnderlaySoftness; // v[ 0, 1]
  130. uniform fixed4 _GlowColor; // RGBA : Color + Intensity
  131. uniform float _GlowOffset; // v[-1, 1]
  132. uniform float _GlowOuter; // v[ 0, 1]
  133. uniform float _GlowInner; // v[ 0, 1]
  134. uniform float _GlowPower; // v[ 1, 1/(1+4*4)]
  135. // API Editable properties
  136. uniform float _ShaderFlags;
  137. uniform float _WeightNormal;
  138. uniform float _WeightBold;
  139. uniform float _ScaleRatioA;
  140. uniform float _ScaleRatioB;
  141. uniform float _ScaleRatioC;
  142. uniform float _VertexOffsetX;
  143. uniform float _VertexOffsetY;
  144. //uniform float _UseClipRect;
  145. uniform float _MaskID;
  146. uniform sampler2D _MaskTex;
  147. uniform float4 _MaskCoord;
  148. uniform float4 _ClipRect; // bottom left(x,y) : top right(z,w)
  149. //uniform float _MaskWipeControl;
  150. //uniform float _MaskEdgeSoftness;
  151. //uniform fixed4 _MaskEdgeColor;
  152. //uniform bool _MaskInverse;
  153. uniform float _MaskSoftnessX;
  154. uniform float _MaskSoftnessY;
  155. // Font Atlas properties
  156. uniform sampler2D _MainTex;
  157. uniform float _TextureWidth;
  158. uniform float _TextureHeight;
  159. uniform float _GradientScale;
  160. uniform float _ScaleX;
  161. uniform float _ScaleY;
  162. uniform float _PerspectiveFilter;
  163. uniform float _Sharpness;
  164. #if defined(_CLIPPING_PLANE)
  165. fixed _ClipPlaneSide;
  166. float4 _ClipPlane;
  167. inline float PointVsPlane(float3 worldPosition, float4 plane)
  168. {
  169. float3 planePosition = plane.xyz * plane.w;
  170. return dot(worldPosition - planePosition, plane.xyz);
  171. }
  172. #endif
  173. #if defined(_CLIPPING_SPHERE)
  174. fixed _ClipSphereSide;
  175. float4 _ClipSphere;
  176. inline float PointVsSphere(float3 worldPosition, float4 sphere)
  177. {
  178. return distance(worldPosition, sphere.xyz) - sphere.w;
  179. }
  180. #endif
  181. #if defined(_CLIPPING_BOX)
  182. fixed _ClipBoxSide;
  183. float4 _ClipBoxSize;
  184. float4x4 _ClipBoxInverseTransform;
  185. inline float PointVsBox(float3 worldPosition, float3 boxSize, float4x4 boxInverseTransform)
  186. {
  187. float3 distance = abs(mul(boxInverseTransform, float4(worldPosition, 1.0))) - boxSize;
  188. return length(max(distance, 0.0)) + min(max(distance.x, max(distance.y, distance.z)), 0.0);
  189. }
  190. #endif
  191. struct vertex_t {
  192. UNITY_VERTEX_INPUT_INSTANCE_ID
  193. float4 vertex : POSITION;
  194. float3 normal : NORMAL;
  195. fixed4 color : COLOR;
  196. float2 texcoord0 : TEXCOORD0;
  197. float2 texcoord1 : TEXCOORD1;
  198. };
  199. struct pixel_t {
  200. UNITY_VERTEX_INPUT_INSTANCE_ID
  201. UNITY_VERTEX_OUTPUT_STEREO
  202. float4 vertex : SV_POSITION;
  203. fixed4 faceColor : COLOR;
  204. fixed4 outlineColor : COLOR1;
  205. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  206. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  207. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  208. #if (UNDERLAY_ON | UNDERLAY_INNER)
  209. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  210. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  211. #endif
  212. #if defined(_CLIPPING_PRIMITIVE)
  213. float3 worldPosition : TEXCOORD5;
  214. #endif
  215. };
  216. pixel_t VertShader(vertex_t input)
  217. {
  218. pixel_t output;
  219. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  220. UNITY_SETUP_INSTANCE_ID(input);
  221. UNITY_TRANSFER_INSTANCE_ID(input, output);
  222. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  223. float bold = step(input.texcoord1.y, 0);
  224. float4 vert = input.vertex;
  225. vert.x += _VertexOffsetX;
  226. vert.y += _VertexOffsetY;
  227. float4 vPosition = UnityObjectToClipPos(vert);
  228. float2 pixelSize = vPosition.w;
  229. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  230. float scale = rsqrt(dot(pixelSize, pixelSize));
  231. scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + 1);
  232. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  233. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  234. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  235. float layerScale = scale;
  236. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  237. float bias = (0.5 - weight) * scale - 0.5;
  238. float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  239. float opacity = input.color.a;
  240. #if (UNDERLAY_ON | UNDERLAY_INNER)
  241. opacity = 1.0;
  242. #endif
  243. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  244. faceColor.rgb *= faceColor.a;
  245. fixed4 outlineColor = _OutlineColor;
  246. outlineColor.a *= opacity;
  247. outlineColor.rgb *= outlineColor.a;
  248. outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
  249. #if (UNDERLAY_ON | UNDERLAY_INNER)
  250. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  251. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  252. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  253. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  254. float2 layerOffset = float2(x, y);
  255. #endif
  256. // Generate UV for the Masking Texture
  257. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  258. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  259. // Populate structure for pixel shader
  260. output.vertex = vPosition;
  261. output.faceColor = faceColor;
  262. output.outlineColor = outlineColor;
  263. output.texcoord0 = float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y);
  264. output.param = half4(scale, bias - outline, bias + outline, bias);
  265. output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
  266. #if (UNDERLAY_ON || UNDERLAY_INNER)
  267. output.texcoord1 = float4(input.texcoord0 + layerOffset, input.color.a, 0);
  268. output.underlayParam = half2(layerScale, layerBias);
  269. #endif
  270. #if defined(_CLIPPING_PRIMITIVE)
  271. output.worldPosition = mul(unity_ObjectToWorld, vert).xyz;
  272. #endif
  273. return output;
  274. }
  275. // PIXEL SHADER
  276. fixed4 PixShader(pixel_t input) : SV_Target
  277. {
  278. UNITY_SETUP_INSTANCE_ID(input);
  279. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  280. half4 c = input.faceColor * saturate(d - input.param.w);
  281. #ifdef OUTLINE_ON
  282. c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  283. c *= saturate(d - input.param.y);
  284. #endif
  285. #if UNDERLAY_ON
  286. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  287. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  288. #endif
  289. #if UNDERLAY_INNER
  290. half sd = saturate(d - input.param.z);
  291. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  292. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  293. #endif
  294. // Alternative implementation to UnityGet2DClipping with support for softness.
  295. #if UNITY_UI_CLIP_RECT
  296. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  297. c *= m.x * m.y;
  298. #endif
  299. #if (UNDERLAY_ON | UNDERLAY_INNER)
  300. c *= input.texcoord1.z;
  301. #endif
  302. // Primitive clipping.
  303. #if defined(_CLIPPING_PRIMITIVE)
  304. float primitiveDistance = 1.0;
  305. #if defined(_CLIPPING_PLANE)
  306. primitiveDistance = min(primitiveDistance, PointVsPlane(input.worldPosition, _ClipPlane) * _ClipPlaneSide);
  307. #endif
  308. #if defined(_CLIPPING_SPHERE)
  309. primitiveDistance = min(primitiveDistance, PointVsSphere(input.worldPosition, _ClipSphere) * _ClipSphereSide);
  310. #endif
  311. #if defined(_CLIPPING_BOX)
  312. primitiveDistance = min(primitiveDistance, PointVsBox(input.worldPosition, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
  313. #endif
  314. c *= step(0.0, primitiveDistance);
  315. #endif
  316. #if UNITY_UI_ALPHACLIP
  317. clip(c.a - 0.001);
  318. #endif
  319. return c;
  320. }
  321. ENDCG
  322. }
  323. }
  324. CustomEditor "Microsoft.MixedReality.Toolkit.Editor.MixedRealityTextMeshProShaderGUI"
  325. }