Spine-SkeletonGraphic.shader 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // This is a premultiply-alpha adaptation of the built-in Unity shader "UI/Default" in Unity 5.6.2 to allow Unity UI stencil masking.
  2. Shader "Spine/SkeletonGraphic"
  3. {
  4. Properties
  5. {
  6. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  7. [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0
  8. [Toggle(_CANVAS_GROUP_COMPATIBLE)] _CanvasGroupCompatible("CanvasGroup Compatible", Int) = 0
  9. _Color ("Tint", Color) = (1,1,1,1)
  10. [HideInInspector][Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp ("Stencil Comparison", Float) = 8
  11. [HideInInspector] _Stencil ("Stencil ID", Float) = 0
  12. [HideInInspector][Enum(UnityEngine.Rendering.StencilOp)] _StencilOp ("Stencil Operation", Float) = 0
  13. [HideInInspector] _StencilWriteMask ("Stencil Write Mask", Float) = 255
  14. [HideInInspector] _StencilReadMask ("Stencil Read Mask", Float) = 255
  15. [HideInInspector] _ColorMask ("Color Mask", Float) = 15
  16. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  17. // Outline properties are drawn via custom editor.
  18. [HideInInspector] _OutlineWidth("Outline Width", Range(0,8)) = 3.0
  19. [HideInInspector] _OutlineColor("Outline Color", Color) = (1,1,0,1)
  20. [HideInInspector] _OutlineReferenceTexWidth("Reference Texture Width", Int) = 1024
  21. [HideInInspector] _ThresholdEnd("Outline Threshold", Range(0,1)) = 0.25
  22. [HideInInspector] _OutlineSmoothness("Outline Smoothness", Range(0,1)) = 1.0
  23. [HideInInspector][MaterialToggle(_USE8NEIGHBOURHOOD_ON)] _Use8Neighbourhood("Sample 8 Neighbours", Float) = 1
  24. [HideInInspector] _OutlineMipLevel("Outline Mip Level", Range(0,3)) = 0
  25. }
  26. SubShader
  27. {
  28. Tags
  29. {
  30. "Queue"="Transparent"
  31. "IgnoreProjector"="True"
  32. "RenderType"="Transparent"
  33. "PreviewType"="Plane"
  34. "CanUseSpriteAtlas"="True"
  35. }
  36. Stencil
  37. {
  38. Ref [_Stencil]
  39. Comp [_StencilComp]
  40. Pass [_StencilOp]
  41. ReadMask [_StencilReadMask]
  42. WriteMask [_StencilWriteMask]
  43. }
  44. Cull Off
  45. Lighting Off
  46. ZWrite Off
  47. ZTest [unity_GUIZTestMode]
  48. Fog { Mode Off }
  49. Blend One OneMinusSrcAlpha
  50. ColorMask [_ColorMask]
  51. Pass
  52. {
  53. Name "Normal"
  54. CGPROGRAM
  55. #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
  56. #pragma shader_feature _ _CANVAS_GROUP_COMPATIBLE
  57. #pragma vertex vert
  58. #pragma fragment frag
  59. #pragma target 2.0
  60. #include "UnityCG.cginc"
  61. #include "UnityUI.cginc"
  62. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  63. struct VertexInput {
  64. float4 vertex : POSITION;
  65. float4 color : COLOR;
  66. float2 texcoord : TEXCOORD0;
  67. UNITY_VERTEX_INPUT_INSTANCE_ID
  68. };
  69. struct VertexOutput {
  70. float4 vertex : SV_POSITION;
  71. fixed4 color : COLOR;
  72. half2 texcoord : TEXCOORD0;
  73. float4 worldPosition : TEXCOORD1;
  74. UNITY_VERTEX_OUTPUT_STEREO
  75. };
  76. fixed4 _Color;
  77. fixed4 _TextureSampleAdd;
  78. float4 _ClipRect;
  79. VertexOutput vert (VertexInput IN) {
  80. VertexOutput OUT;
  81. UNITY_SETUP_INSTANCE_ID(IN);
  82. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  83. OUT.worldPosition = IN.vertex;
  84. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  85. OUT.texcoord = IN.texcoord;
  86. #ifdef UNITY_HALF_TEXEL_OFFSET
  87. OUT.vertex.xy += (_ScreenParams.zw-1.0) * float2(-1,1);
  88. #endif
  89. OUT.color = IN.color * float4(_Color.rgb * _Color.a, _Color.a); // Combine a PMA version of _Color with vertexColor.
  90. return OUT;
  91. }
  92. sampler2D _MainTex;
  93. fixed4 frag (VertexOutput IN) : SV_Target
  94. {
  95. half4 texColor = tex2D(_MainTex, IN.texcoord);
  96. #if defined(_STRAIGHT_ALPHA_INPUT)
  97. texColor.rgb *= texColor.a;
  98. #endif
  99. half4 color = (texColor + _TextureSampleAdd) * IN.color;
  100. #ifdef _CANVAS_GROUP_COMPATIBLE
  101. // CanvasGroup alpha sets vertex color alpha, but does not premultiply it to rgb components.
  102. color.rgb *= IN.color.a;
  103. #endif
  104. color *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  105. #ifdef UNITY_UI_ALPHACLIP
  106. clip (color.a - 0.001);
  107. #endif
  108. return color;
  109. }
  110. ENDCG
  111. }
  112. }
  113. CustomEditor "SpineShaderWithOutlineGUI"
  114. }