Advertisement
Guest User

Untitled

a guest
May 4th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.63 KB | None | 0 0
  1.  
  2. /*
  3.     This is a modification of "Nature/Tree Soft Occlusion Leaves" shader from 2017 LTS version shader source
  4.  
  5. */
  6.  
  7. Shader "Heavens Curse/Trees/TreeCanopy_AO_Animated"
  8. {
  9.     Properties
  10.     {
  11.         _Color("Main Color", Color) = (1,1,1,1)
  12.         // Shadow Modifier will affect final shadow multiplier
  13.         _ShadowModifier("Shadow Modifier", Color) = (0.3,0.3,0.3)
  14.  
  15.         _MainTex("Main Texture", 2D) = "white" {  }
  16.         _Cutoff("Alpha cutoff", Range(0.25,0.9)) = 0.5
  17.         _BaseLight("Base Light", Range(0, 1)) = 0.35
  18.         _AO("Amb. Occlusion", Range(0, 10)) = 2.4
  19.         _Occlusion("Dir Occlusion", Range(0, 20)) = 7.5
  20.  
  21.             // Modification from Diffuse Shake shader
  22.         _ShakeDisplacement("Displacement", Range(0, 1.0)) = 1.0
  23.         _ShakeTime("Shake Time", Range(0, 1.0)) = 1.0
  24.         _ShakeWindspeed("Shake Windspeed", Range(0, 1.0)) = 1.0
  25.         _ShakeBending("Shake Bending", Range(0, 1.0)) = 1.0
  26.  
  27.  
  28.             // Copied from Tree Soft
  29.         [HideInInspector] _TreeInstanceColor("TreeInstanceColor", Vector) = (1,1,1,1)
  30.         [HideInInspector] _TreeInstanceScale("TreeInstanceScale", Vector) = (1,1,1,1)
  31.         [HideInInspector] _SquashAmount("Squash", Float) = 1
  32.     }
  33.  
  34.     SubShader
  35.     {
  36.         Tags
  37.         {
  38.             "Queue" = "AlphaTest"
  39.             "IgnoreProjector" = "True"
  40.             "RenderType" = "TreeTransparentCutout"
  41.  
  42.             // Not sure why this was there in original shader, seems to work without
  43.             //"DisableBatching" = "True"
  44.         }
  45.         Cull Off
  46.         ColorMask RGB
  47.  
  48.         Pass
  49.         {
  50.             Lighting On
  51.             Tags {"RenderMode" = "ForwardBase"}
  52.  
  53.             CGPROGRAM
  54.             #pragma vertex leaves_custom
  55.             #pragma fragment frag_custom
  56.             #pragma multi_compile_fog
  57.             #pragma multi_compile_fwdbase
  58.             #pragma multi_compile_instancing
  59.  
  60.             #include "UnityCG.cginc"
  61.             #include "AutoLight.cginc"
  62.             #include "UnityBuiltin2xTreeLibrary.cginc"
  63.  
  64.             sampler2D _MainTex;
  65.             fixed _Cutoff;
  66.  
  67.             float _ShakeDisplacement;
  68.             float _ShakeTime;
  69.             float _ShakeWindspeed;
  70.             float _ShakeBending;
  71.  
  72.             struct appdata_tree_custom
  73.             {
  74.                 float4 vertex : POSITION;       // position
  75.                 float4 tangent : TANGENT;       // directional AO
  76.                 float3 normal : NORMAL;         // normal
  77.                 fixed4 color : COLOR;           // .w = bend factor
  78.                 float4 texcoord : TEXCOORD0;    // UV
  79.                 UNITY_VERTEX_INPUT_INSTANCE_ID
  80.             };
  81.  
  82.             struct v2f_custom
  83.             {
  84.                 float4 pos : SV_POSITION;
  85.                 float4 uv : TEXCOORD0;
  86.                 half4 color : TEXCOORD1;
  87.                 UNITY_FOG_COORDS(2)
  88.                 UNITY_VERTEX_OUTPUT_STEREO
  89.                 SHADOW_COORDS(4)
  90.             };
  91.  
  92.             v2f_custom leaves_custom(appdata_tree_custom v)
  93.             {
  94.                 v2f_custom o;
  95.                 UNITY_SETUP_INSTANCE_ID(v);
  96.                 //UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  97.                 //TerrainAnimateTree(v.vertex, v.color.w);
  98.  
  99.                 // Terrain Animate Tree function has been replaced with the following
  100.                 // modified piece from TerrainWaveGrass from TerrainEngine.cginc
  101.                 float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
  102.  
  103.                 const float _WindSpeed = (_ShakeWindspeed + v.color.g);
  104.                 const float _WaveScale = _ShakeDisplacement;
  105.  
  106.                 const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
  107.                 const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
  108.                 const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
  109.  
  110.                 float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
  111.                 float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
  112.  
  113.                 float4 waves;
  114.                 waves = v.vertex.x * _waveXSize;
  115.                 waves += v.vertex.z * _waveZSize;
  116.  
  117.                 waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b) * waveSpeed *_WindSpeed;
  118.  
  119.                 float4 s, c;
  120.                 waves = frac(waves);
  121.                 FastSinCos(waves, s, c);
  122.  
  123.                 float waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
  124.                 s *= waveAmount;
  125.  
  126.                 s *= normalize(waveSpeed);
  127.  
  128.                 s = s * s;
  129.                 float fade = dot(s, 1.3);
  130.                 s = s * s;
  131.                 float3 waveMove = float3 (0, 0, 0);
  132.                 waveMove.x = dot(s, _waveXmove);
  133.                 waveMove.z = dot(s, _waveZmove);
  134.                 v.vertex.xz -= mul((float3x3)unity_WorldToObject, waveMove).xz;
  135.  
  136.                 // Perform a modification on Vertex Normal so it always points towards main directional light
  137.                 // this way the lighting across all quads in canopy will be consistent
  138.                 v.normal = normalize(mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz);
  139.  
  140.                 // Following is "leaves" function from UnityBuiltin2xTreeLibrary
  141.                 float3 viewpos = UnityObjectToViewPos(v.vertex);
  142.                 o.pos = UnityObjectToClipPos(v.vertex);
  143.                 o.uv = v.texcoord;
  144.  
  145.                 float4 lightDir = 0;
  146.                 float4 lightColor = 0;
  147.                 lightDir.w = _AO;
  148.  
  149.                 // Mod: Changed UNITY_LIGHTMODEL_AMBIENT because it's obsolete
  150.                 /*
  151.                     Possible candidates are:
  152.                     unity_AmbientSky
  153.                     unity_AmbientEquator
  154.                     unity_AmbientGround
  155.                 */
  156.                 float4 light = unity_AmbientSky;
  157.  
  158.                 for (int i = 0; i < 4; i++)
  159.                 {
  160.                     float atten = 1.0;
  161.                     float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
  162.                     toLight.z *= -1.0;
  163.  
  164.                     lightDir.xyz = mul((float3x3)unity_CameraToWorld, normalize(toLight));
  165.                     float lengthSq = dot(toLight, toLight);
  166.                     atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
  167.  
  168.                     lightColor.rgb = unity_LightColor[i].rgb;
  169.  
  170.                
  171.                     // Modification to lightDir so it always points towards main directional light.
  172.                     // We need this to affect the direction of fake "AO" of the tree. Direction of
  173.                     // shifted AO via _Occlusion will be correct instead of fixed
  174.                     //lightDir.xyz = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;
  175.                     lightDir.xyz = v.normal.xyz;
  176.  
  177.                     lightDir.xyz *= _Occlusion;
  178.                     float occ = dot(v.tangent, lightDir);
  179.                     occ = max(0, occ);
  180.                     occ += _BaseLight;
  181.                     light += lightColor * (occ * atten);
  182.                 }
  183.  
  184.                 o.color = light * _Color;// *_TreeInstanceColor;
  185.                 //o.color.a = 0.5 * _HalfOverCutoff;
  186.  
  187.                 // Do main light shadow:
  188.                 TRANSFER_SHADOW(o);
  189.                 UNITY_TRANSFER_FOG(o, o.pos);
  190.                 return o;
  191.             }
  192.  
  193.             uniform fixed3 _ShadowModifier;
  194.  
  195.             fixed4 frag_custom(v2f_custom input) : SV_Target
  196.             {
  197.                 fixed4 c = tex2D(_MainTex, input.uv.xy);
  198.                 // Mod: clip before applying rgb.
  199.                 // No real reason, really.
  200.                 clip(c.a - _Cutoff);
  201.  
  202.                 c.rgb *= input.color.rgb;
  203.  
  204.                 //return LIGHT_ATTENUATION(input);
  205.                 // Modify the final color via shadow.
  206.                 // Simply multiplying with shadow will give us pitch black shadows across the canopy
  207.                 c.rgb *= LIGHT_ATTENUATION(input) + _ShadowModifier;
  208.  
  209.                 UNITY_APPLY_FOG(input.fogCoord, c);
  210.                 return c;
  211.             }
  212.             ENDCG
  213.         }
  214.  
  215.         Pass
  216.         {
  217.             Name "ShadowCaster"
  218.             Tags{ "LightMode" = "ShadowCaster" }
  219.  
  220.             CGPROGRAM
  221.             #pragma vertex vert
  222.             #pragma fragment frag
  223.             #pragma multi_compile_shadowcaster
  224.             #include "UnityCG.cginc"
  225.             #include "TerrainEngine.cginc"
  226.  
  227.             struct v2f
  228.             {
  229.                 V2F_SHADOW_CASTER;
  230.                 float2 uv : TEXCOORD1;
  231.                 UNITY_VERTEX_OUTPUT_STEREO
  232.             };
  233.  
  234.             struct appdata
  235.             {
  236.                 float4 vertex : POSITION;
  237.                 float3 normal : NORMAL;
  238.                 fixed4 color : COLOR;
  239.                 float4 texcoord : TEXCOORD0;
  240.                 UNITY_VERTEX_INPUT_INSTANCE_ID
  241.             };
  242.  
  243.             float _ShakeDisplacement;
  244.             float _ShakeTime;
  245.             float _ShakeWindspeed;
  246.             float _ShakeBending;
  247.  
  248.             v2f vert(appdata v)
  249.             {
  250.                 v2f o;
  251.                 UNITY_SETUP_INSTANCE_ID(v);
  252.                 //UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  253.  
  254.                 //TerrainAnimateTree(v.vertex, v.color.w);
  255.                 // Apply same animation thing in ShadowCaster pass because we want shadows to be consistent!
  256.                 float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
  257.  
  258.                 const float _WindSpeed = (_ShakeWindspeed + v.color.g);
  259.                 const float _WaveScale = _ShakeDisplacement;
  260.  
  261.                 const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
  262.                 const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
  263.                 const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
  264.  
  265.                 float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
  266.                 float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
  267.  
  268.                 float4 waves;
  269.                 waves = v.vertex.x * _waveXSize;
  270.                 waves += v.vertex.z * _waveZSize;
  271.  
  272.                 waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b) * waveSpeed *_WindSpeed;
  273.  
  274.                 float4 s, c;
  275.                 waves = frac(waves);
  276.                 FastSinCos(waves, s, c);
  277.  
  278.                 float waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
  279.                 s *= waveAmount;
  280.  
  281.                 s *= normalize(waveSpeed);
  282.  
  283.                 s = s * s;
  284.                 float fade = dot(s, 1.3);
  285.                 s = s * s;
  286.                 float3 waveMove = float3 (0, 0, 0);
  287.                 waveMove.x = dot(s, _waveXmove);
  288.                 waveMove.z = dot(s, _waveZmove);
  289.                 v.vertex.xz -= mul((float3x3)unity_WorldToObject, waveMove).xz;
  290.  
  291.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
  292.                     o.uv = v.texcoord;
  293.                 return o;
  294.             }
  295.  
  296.             sampler2D _MainTex;
  297.             fixed _Cutoff;
  298.  
  299.             float4 frag(v2f i) : SV_Target
  300.             {
  301.                 fixed4 texcol = tex2D(_MainTex, i.uv);
  302.                 clip(texcol.a - _Cutoff);
  303.                 SHADOW_CASTER_FRAGMENT(i)
  304.             }
  305.         ENDCG
  306.         }
  307.     }      
  308.     Fallback Off
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement