Advertisement
Guest User

Untitled

a guest
Mar 28th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. Shader "Custom/Dither"
  2. {
  3. Properties{
  4. _Threshold("Threshold", Float) = 0.45
  5. _Strength("Strength", Float) = 0.45
  6. _Width("Width", Int) = 0.45
  7. _Height("Height", Int) = 0.45
  8. _Dither("Dither", 2D) = "white" {}
  9. _MainTex("", 2D) = "white" {}
  10. _Color("",Color)=(1,1,1,1)
  11.  
  12. _Amplitude ("Wave Size", Range(0,1)) = 0.4
  13. _Frequency ("Wave Freqency", Range(1, 8)) = 2
  14. _AnimationSpeed ("Animation Speed", Range(0,5)) = 1
  15.  
  16. }
  17.  
  18. SubShader{
  19. Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
  20. LOD 100
  21.  
  22. ZWrite Off
  23. Blend SrcAlpha OneMinusSrcAlpha
  24.  
  25. Lighting Off
  26. ZTest Always
  27. Cull Off
  28. Fog { Mode Off }
  29.  
  30. CGPROGRAM
  31. // Upgrade NOTE: excluded shader from DX11, Xbox360, OpenGL ES 2.0 because it uses unsized arrays
  32. #pragma exclude_renderers flash
  33. #pragma surface surf Standard fullforwardshadows vertex:vert addshadow
  34. #pragma fragmentoption ARB_precision_hint_fastest
  35. #include "UnityCG.cginc"
  36.  
  37. uniform float _Threshold;
  38. uniform float _Strength;
  39. uniform int _Width;
  40. uniform int _Height;
  41. float4 _Color;
  42.  
  43. uniform sampler2D _Dither;
  44. uniform sampler2D _MainTex;
  45.  
  46. float _Amplitude;
  47. float _Frequency;
  48. float _AnimationSpeed;
  49. //input struct which is automatically filled by unity
  50.  
  51. struct Input {
  52. float2 uv_MainTex;
  53. };
  54.  
  55. void vert(inout appdata_full data){
  56. float4 modifiedPos = data.vertex;
  57. modifiedPos.y += sin(data.vertex.x * _Frequency + _Time.y * _AnimationSpeed) * _Amplitude;
  58.  
  59. float3 posPlusTangent = data.vertex + data.tangent * 0.01;
  60. posPlusTangent.y += sin(posPlusTangent.x * _Frequency + _Time.y * _AnimationSpeed) * _Amplitude;
  61.  
  62. float3 bitangent = cross(data.normal, data.tangent);
  63. float3 posPlusBitangent = data.vertex + bitangent * 0.01;
  64. posPlusBitangent.y += sin(posPlusBitangent.x * _Frequency + _Time.y * _AnimationSpeed) * _Amplitude;
  65.  
  66. float3 modifiedTangent = posPlusTangent - modifiedPos;
  67. float3 modifiedBitangent = posPlusBitangent - modifiedPos;
  68.  
  69. float3 modifiedNormal = cross(modifiedTangent, modifiedBitangent);
  70. data.normal = normalize(modifiedNormal);
  71. data.vertex = modifiedPos;
  72. }
  73.  
  74.  
  75. float luma(fixed3 color) {
  76. return dot(color, fixed3(0.299, 0.587, 0.114));
  77. }
  78.  
  79. float luma(fixed4 color) {
  80. return dot(color.rgb, fixed3(0.299, 0.587, 0.114));
  81. }
  82.  
  83. fixed4 ditherBayer2(fixed2 position, float brightness) {
  84. int x = fmod(position.x, 4.0);
  85. int y = fmod(position.y, 4.0);
  86.  
  87. int index = x + y * 4;
  88. float lim = 0.0;
  89.  
  90. if (x < 8) {
  91. if (index == 0) lim = 0.0625;
  92. if (index == 1) lim = 0.5625;
  93. if (index == 2) lim = 0.1875;
  94. if (index == 3) lim = 0.6875;
  95. if (index == 4) lim = 0.8125;
  96. if (index == 5) lim = 0.3125;
  97. if (index == 6) lim = 0.9375;
  98. if (index == 7) lim = 0.4375;
  99. if (index == 8) lim = 0.25;
  100. if (index == 9) lim = 0.75;
  101. if (index == 10) lim = 0.125;
  102. if (index == 11) lim = 0.625;
  103. if (index == 12) lim = 1.0;
  104. if (index == 13) lim = 0.5;
  105. if (index == 14) lim = 0.875;
  106. if (index == 15) lim = 0.375;
  107. }
  108.  
  109. if (brightness < lim * _Strength)
  110. return 0.0;
  111.  
  112. return 1.0;
  113. }
  114.  
  115. fixed3 ditherPattern2(fixed2 position, float brightness) {
  116. int x = fmod(position.x, _Width);
  117. int y = fmod(position.y, _Height);
  118.  
  119. float lim = 0.0;
  120.  
  121. lim = tex2D(_Dither, fixed2(x, y) / _Width).r;
  122.  
  123. if (brightness < lim * _Threshold)
  124. return _Strength;
  125.  
  126. return 1.0;
  127. }
  128.  
  129. fixed3 ditherBayer(fixed2 position, fixed3 col) {
  130. return col * ditherBayer2(position, luma(col));
  131. }
  132.  
  133. fixed3 ditherPattern(fixed2 position, fixed3 col) {
  134. return col * ditherPattern2(position, luma(col));
  135. }
  136. void surf (Input i, inout SurfaceOutputStandard o) {
  137. fixed4 col = tex2D(_MainTex, i.uv_MainTex);
  138. return fixed4(tex2D(ditherPattern(i.pos.xy, col.rgb), col.a)*_Color);
  139. }
  140. fixed4 frag(v2f_img i) : COLOR
  141. {
  142. fixed4 col = (tex2D(_MainTex, i.uv));
  143.  
  144.  
  145. }
  146. ENDCG
  147. }
  148. FallBack "Standard"
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement