Advertisement
SarahNorthway

SpritesDiffuseWave

Feb 21st, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2. // from 2017.1.1 built-in Sprites-Diffuse Shader
  3. // and https://forum.unity.com/threads/shader-moving-trees-grass-in-wind-outside-of-terrain.230911/
  4. Shader "Northway/SpritesDiffuseWave" {
  5. Properties {
  6. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  7. _Color ("Tint", Color) = (1,1,1,1)
  8. [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
  9. [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
  10. [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
  11. [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
  12. [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
  13. _ShakeDisplacement ("Displacement", Range (0, 1.0)) = 1.0
  14. _ShakeTime ("Shake Time", Range (0, 1.0)) = 1.0
  15. _ShakeWindspeed ("Shake Windspeed", Range (0, 1.0)) = 1.0
  16. _ShakeBending ("Shake Bending", Range (0, 1.0)) = 1.0
  17. }
  18.  
  19. SubShader {
  20. Tags {
  21. "Queue"="Transparent"
  22. "IgnoreProjector"="True"
  23. "RenderType"="Transparent"
  24. "PreviewType"="Plane"
  25. "CanUseSpriteAtlas"="True"
  26. }
  27.  
  28. Cull Off
  29. Lighting Off
  30. ZWrite Off
  31. Blend One OneMinusSrcAlpha
  32.  
  33. CGPROGRAM
  34. #pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
  35. #pragma multi_compile _ PIXELSNAP_ON
  36. #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
  37. #include "UnitySprites.cginc"
  38.  
  39. float _ShakeDisplacement;
  40. float _ShakeTime;
  41. float _ShakeWindspeed;
  42. float _ShakeBending;
  43.  
  44. struct Input {
  45. float2 uv_MainTex;
  46. fixed4 color;
  47. };
  48.  
  49. void FastSinCos (float4 val, out float4 s, out float4 c) {
  50. val = val * 6.408849 - 3.1415927;
  51. float4 r5 = val * val;
  52. float4 r6 = r5 * r5;
  53. float4 r7 = r6 * r5;
  54. float4 r8 = r6 * r5;
  55. float4 r1 = r5 * val;
  56. float4 r2 = r1 * r5;
  57. float4 r3 = r2 * r5;
  58. float4 sin7 = {1, -0.16161616, 0.0083333, -0.00019841} ;
  59. float4 cos8 = {-0.5, 0.041666666, -0.0013888889, 0.000024801587} ;
  60. s = val + r1 * sin7.y + r2 * sin7.z + r3 * sin7.w;
  61. c = 1 + r5 * cos8.x + r6 * cos8.y + r7 * cos8.z + r8 * cos8.w;
  62. }
  63.  
  64. void vert (inout appdata_full v, out Input o) {
  65. //v.vertex.xy *= _Flip.xy;
  66.  
  67. //#if defined(PIXELSNAP_ON)
  68. //v.vertex = UnityPixelSnap (v.vertex);
  69. //#endif
  70.  
  71. // waving
  72. float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
  73. const float _WindSpeed = (_ShakeWindspeed + v.color.g );
  74. const float _WaveScale = _ShakeDisplacement;
  75. const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
  76. const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
  77. const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
  78. float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
  79. float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
  80. float4 waves;
  81. waves = v.vertex.x * _waveXSize;
  82. waves += v.vertex.z * _waveZSize;
  83. //waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b ) * waveSpeed *_WindSpeed;
  84. waves += 0.5 * (1 - _ShakeTime * 2 - v.color.b ) * waveSpeed *_WindSpeed;
  85. float4 s, c;
  86. waves = frac (waves);
  87. FastSinCos (waves, s,c);
  88. float waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
  89. s *= waveAmount;
  90. s *= normalize (waveSpeed);
  91. s = s * s;
  92. float fade = dot (s, 1.3);
  93. s = s * s;
  94. float3 waveMove = float3 (0,0,0);
  95. waveMove.x = dot (s, _waveXmove);
  96. waveMove.z = dot (s, _waveZmove);
  97. //v.vertex.xz -= mul ((float3x3)unity_WorldToObject, waveMove).xz;
  98. v.vertex.xz -= waveMove.xz;
  99.  
  100. //UNITY_INITIALIZE_OUTPUT(Input, o);
  101. o = (Input)0;
  102. o.color = v.color * _Color * _RendererColor;
  103. }
  104.  
  105. void surf (Input IN, inout SurfaceOutput o) {
  106. fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
  107. o.Albedo = c.rgb * c.a;
  108. o.Alpha = c.a;
  109. }
  110. ENDCG
  111. }
  112.  
  113. Fallback "Transparent/VertexLit"
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement