Guest User

ToonLitSwayingProp

a guest
Jan 9th, 2019
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. Shader "Toon/Lit Swaying Prop" {
  2. Properties{
  3. _Color("Color Primary", Color) = (0.5,0.5,0.5,1)
  4. _ColorSec("Color Secondary", Color) = (0.5,0.5,0.5,1)
  5. _ColorTert("Color Tertiary", Color) = (0.5,0.5,0.5,1)
  6. _MainTex("Main Texture", 2D) = "white" {}
  7. _Mask("Mask: R= Prim, G = Sec, B = Tert ", 2D) = "black" {}
  8. _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
  9. _Speed("MoveSpeed", Range(20,50)) = 25 // speed of the swaying
  10. _Rigidness("Rigidness", Range(0,1)) = 25 // lower makes it look more "liquid" higher makes it look rigid
  11. _SwayMax("Sway Max", Range(0, 0.1)) = .005 // how far the swaying goes
  12. _YOffset("Y offset", float) = 0.5// y offset, below this is no animation
  13.  
  14. }
  15.  
  16. SubShader{
  17. Tags { "RenderType" = "Opaque" }
  18. LOD 200
  19.  
  20.  
  21. CGPROGRAM
  22. #pragma surface surf ToonRamp vertex:vert addshadow // addshadow applies shadow after vertex animation
  23.  
  24.  
  25. sampler2D _Ramp;
  26.  
  27. // custom lighting function that uses a texture ramp based
  28. // on angle between light direction and normal
  29. #pragma lighting ToonRamp exclude_path:prepass
  30. #pragma multi_compile_instancing
  31.  
  32. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  33. {
  34. #ifndef USING_DIRECTIONAL_LIGHT
  35. lightDir = normalize(lightDir);
  36. #endif
  37.  
  38. half d = dot(s.Normal, lightDir)*0.5 + 0.5;
  39. half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
  40.  
  41. half4 c;
  42. c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
  43. c.a = 0;
  44. return c;
  45. }
  46.  
  47.  
  48. sampler2D _MainTex, _Mask;
  49. float _Speed;
  50. float _SwayMax;
  51. float _YOffset;
  52. float _Rigidness;
  53.  
  54.  
  55. struct Input {
  56. float2 uv_MainTex : TEXCOORD0;
  57. };
  58.  
  59. void vert(inout appdata_full v)//
  60. {
  61.  
  62. float3 wpos = mul(unity_ObjectToWorld, v.vertex).xyz;// world position
  63. float wind = lerp(-1, 1, (wpos.x * _Rigidness) + _Time.x); // wind over work
  64. float z = sin(_Speed*wind);
  65. float x = cos(_Speed*wind);
  66. v.vertex.x += step(0,v.vertex.y - _YOffset) * x * _SwayMax ;// apply the movement if the vertex's y above the YOffset
  67. v.vertex.z += step(0,v.vertex.y - _YOffset) * z * _SwayMax ;
  68.  
  69. }
  70.  
  71. UNITY_INSTANCING_BUFFER_START(Props)
  72. UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
  73. UNITY_DEFINE_INSTANCED_PROP(fixed4, _ColorSec)
  74. UNITY_DEFINE_INSTANCED_PROP(fixed4, _ColorTert)
  75. UNITY_INSTANCING_BUFFER_END(Props)
  76.  
  77. void surf(Input IN, inout SurfaceOutput o) {
  78. half4 c = tex2D(_MainTex, IN.uv_MainTex);
  79. half4 m = tex2D(_Mask, IN.uv_MainTex);
  80. half4 prim = m.r * UNITY_ACCESS_INSTANCED_PROP(Props, _Color) * c;
  81. half4 sec = m.g * UNITY_ACCESS_INSTANCED_PROP(Props, _ColorSec) * c;
  82. half4 tert = m.b* UNITY_ACCESS_INSTANCED_PROP(Props, _ColorTert) * c;
  83. half4 noMask = c * (1 - (m.r + m.g + m.b));
  84.  
  85. o.Albedo = noMask + prim + sec + tert;
  86. o.Alpha = c.a;
  87. }
  88. ENDCG
  89.  
  90. }
  91.  
  92. Fallback "Diffuse"
  93. }
Advertisement
Add Comment
Please, Sign In to add comment