Advertisement
Guest User

ScreenTransitionImageEffect

a guest
Jun 1st, 2017
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // The code has a small fix to the original for Direct3D like displays
  2. // UNITY_UV_STARTS_AT_TOP is only set for Direct3D, Metal and Consoles
  3. // There is an issue with the image being flipped on these devices
  4. // The changes below can be seen on the lines with '_MainTex_TexelSize'
  5. // Thanks to Peter77 for this fine shader
  6.  
  7. Shader "Hidden/ScreenTransitionImageEffect"
  8. {
  9. Properties
  10. {
  11. _MainTex ("Texture", 2D) = "white" {}
  12. _MaskTex ("Mask Texture", 2D) = "white" {}
  13. _MaskValue ("Mask Value", Range(0,1)) = 0.5
  14. _MaskColor ("Mask Color", Color) = (0,0,0,1)
  15. [Toggle(INVERT_MASK)] _INVERT_MASK ("Mask Invert", Float) = 0
  16. }
  17. SubShader
  18. {
  19. // No culling or depth
  20. Cull Off ZWrite Off ZTest Always
  21.  
  22. Pass
  23. {
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #include "UnityCG.cginc"
  28.  
  29. #pragma shader_feature INVERT_MASK
  30.  
  31. float4 _MainTex_TexelSize;
  32.  
  33. struct appdata
  34. {
  35. float4 vertex : POSITION;
  36. float2 uv : TEXCOORD0;
  37. };
  38.  
  39. struct v2f
  40. {
  41. float4 vertex : SV_POSITION;
  42. float2 uv : TEXCOORD0;
  43. };
  44.  
  45. v2f vert (appdata v)
  46. {
  47. v2f o;
  48. o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  49. o.uv = v.uv;
  50.  
  51. #if UNITY_UV_STARTS_AT_TOP
  52. if (_MainTex_TexelSize.y < 0)
  53. o.uv.y = 1 - o.uv.y;
  54. #endif
  55.  
  56. return o;
  57. }
  58.  
  59. sampler2D _MainTex;
  60. sampler2D _MaskTex;
  61. float _MaskValue;
  62. float4 _MaskColor;
  63.  
  64. fixed4 frag (v2f i) : SV_Target
  65. {
  66. float4 col = tex2D(_MainTex, i.uv);
  67. float4 mask = tex2D(_MaskTex, i.uv);
  68.  
  69. // Scale 0..255 to 0..254 range.
  70. float alpha = mask.a * (1 - 1/255.0);
  71.  
  72. // If the mask value is greater than the alpha value,
  73. // we want to draw the mask.
  74. float weight = step(_MaskValue, alpha);
  75. #if INVERT_MASK
  76. weight = 1 - weight;
  77. #endif
  78.  
  79. // Blend in mask color depending on the weight
  80. //col.rgb = lerp(_MaskColor, col.rgb, weight);
  81.  
  82. // Blend in mask color depending on the weight
  83. // Additionally also apply a blend between mask and scene
  84. col.rgb = lerp(col.rgb, lerp(_MaskColor.rgb, col.rgb, weight), _MaskColor.a);
  85.  
  86. return col;
  87. }
  88. ENDCG
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement