Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is a modification of "Nature/Tree Soft Occlusion Leaves" shader from 2017 LTS version shader source
- */
- Shader "Heavens Curse/Trees/TreeCanopy_AO_Animated"
- {
- Properties
- {
- _Color("Main Color", Color) = (1,1,1,1)
- // Shadow Modifier will affect final shadow multiplier
- _ShadowModifier("Shadow Modifier", Color) = (0.3,0.3,0.3)
- _MainTex("Main Texture", 2D) = "white" { }
- _Cutoff("Alpha cutoff", Range(0.25,0.9)) = 0.5
- _BaseLight("Base Light", Range(0, 1)) = 0.35
- _AO("Amb. Occlusion", Range(0, 10)) = 2.4
- _Occlusion("Dir Occlusion", Range(0, 20)) = 7.5
- // Modification from Diffuse Shake shader
- _ShakeDisplacement("Displacement", Range(0, 1.0)) = 1.0
- _ShakeTime("Shake Time", Range(0, 1.0)) = 1.0
- _ShakeWindspeed("Shake Windspeed", Range(0, 1.0)) = 1.0
- _ShakeBending("Shake Bending", Range(0, 1.0)) = 1.0
- // Copied from Tree Soft
- [HideInInspector] _TreeInstanceColor("TreeInstanceColor", Vector) = (1,1,1,1)
- [HideInInspector] _TreeInstanceScale("TreeInstanceScale", Vector) = (1,1,1,1)
- [HideInInspector] _SquashAmount("Squash", Float) = 1
- }
- SubShader
- {
- Tags
- {
- "Queue" = "AlphaTest"
- "IgnoreProjector" = "True"
- "RenderType" = "TreeTransparentCutout"
- // Not sure why this was there in original shader, seems to work without
- //"DisableBatching" = "True"
- }
- Cull Off
- ColorMask RGB
- Pass
- {
- Lighting On
- Tags {"RenderMode" = "ForwardBase"}
- CGPROGRAM
- #pragma vertex leaves_custom
- #pragma fragment frag_custom
- #pragma multi_compile_fog
- #pragma multi_compile_fwdbase
- #pragma multi_compile_instancing
- #include "UnityCG.cginc"
- #include "AutoLight.cginc"
- #include "UnityBuiltin2xTreeLibrary.cginc"
- sampler2D _MainTex;
- fixed _Cutoff;
- float _ShakeDisplacement;
- float _ShakeTime;
- float _ShakeWindspeed;
- float _ShakeBending;
- struct appdata_tree_custom
- {
- float4 vertex : POSITION; // position
- float4 tangent : TANGENT; // directional AO
- float3 normal : NORMAL; // normal
- fixed4 color : COLOR; // .w = bend factor
- float4 texcoord : TEXCOORD0; // UV
- UNITY_VERTEX_INPUT_INSTANCE_ID
- };
- struct v2f_custom
- {
- float4 pos : SV_POSITION;
- float4 uv : TEXCOORD0;
- half4 color : TEXCOORD1;
- UNITY_FOG_COORDS(2)
- UNITY_VERTEX_OUTPUT_STEREO
- SHADOW_COORDS(4)
- };
- v2f_custom leaves_custom(appdata_tree_custom v)
- {
- v2f_custom o;
- UNITY_SETUP_INSTANCE_ID(v);
- //UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
- //TerrainAnimateTree(v.vertex, v.color.w);
- // Terrain Animate Tree function has been replaced with the following
- // modified piece from TerrainWaveGrass from TerrainEngine.cginc
- float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
- const float _WindSpeed = (_ShakeWindspeed + v.color.g);
- const float _WaveScale = _ShakeDisplacement;
- const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
- const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
- const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
- float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
- float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
- float4 waves;
- waves = v.vertex.x * _waveXSize;
- waves += v.vertex.z * _waveZSize;
- waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b) * waveSpeed *_WindSpeed;
- float4 s, c;
- waves = frac(waves);
- FastSinCos(waves, s, c);
- float waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
- s *= waveAmount;
- s *= normalize(waveSpeed);
- s = s * s;
- float fade = dot(s, 1.3);
- s = s * s;
- float3 waveMove = float3 (0, 0, 0);
- waveMove.x = dot(s, _waveXmove);
- waveMove.z = dot(s, _waveZmove);
- v.vertex.xz -= mul((float3x3)unity_WorldToObject, waveMove).xz;
- // Perform a modification on Vertex Normal so it always points towards main directional light
- // this way the lighting across all quads in canopy will be consistent
- v.normal = normalize(mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz);
- // Following is "leaves" function from UnityBuiltin2xTreeLibrary
- float3 viewpos = UnityObjectToViewPos(v.vertex);
- o.pos = UnityObjectToClipPos(v.vertex);
- o.uv = v.texcoord;
- float4 lightDir = 0;
- float4 lightColor = 0;
- lightDir.w = _AO;
- // Mod: Changed UNITY_LIGHTMODEL_AMBIENT because it's obsolete
- /*
- Possible candidates are:
- unity_AmbientSky
- unity_AmbientEquator
- unity_AmbientGround
- */
- float4 light = unity_AmbientSky;
- for (int i = 0; i < 4; i++)
- {
- float atten = 1.0;
- float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
- toLight.z *= -1.0;
- lightDir.xyz = mul((float3x3)unity_CameraToWorld, normalize(toLight));
- float lengthSq = dot(toLight, toLight);
- atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
- lightColor.rgb = unity_LightColor[i].rgb;
- // Modification to lightDir so it always points towards main directional light.
- // We need this to affect the direction of fake "AO" of the tree. Direction of
- // shifted AO via _Occlusion will be correct instead of fixed
- //lightDir.xyz = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;
- lightDir.xyz = v.normal.xyz;
- lightDir.xyz *= _Occlusion;
- float occ = dot(v.tangent, lightDir);
- occ = max(0, occ);
- occ += _BaseLight;
- light += lightColor * (occ * atten);
- }
- o.color = light * _Color;// *_TreeInstanceColor;
- //o.color.a = 0.5 * _HalfOverCutoff;
- // Do main light shadow:
- TRANSFER_SHADOW(o);
- UNITY_TRANSFER_FOG(o, o.pos);
- return o;
- }
- uniform fixed3 _ShadowModifier;
- fixed4 frag_custom(v2f_custom input) : SV_Target
- {
- fixed4 c = tex2D(_MainTex, input.uv.xy);
- // Mod: clip before applying rgb.
- // No real reason, really.
- clip(c.a - _Cutoff);
- c.rgb *= input.color.rgb;
- //return LIGHT_ATTENUATION(input);
- // Modify the final color via shadow.
- // Simply multiplying with shadow will give us pitch black shadows across the canopy
- c.rgb *= LIGHT_ATTENUATION(input) + _ShadowModifier;
- UNITY_APPLY_FOG(input.fogCoord, c);
- return c;
- }
- ENDCG
- }
- Pass
- {
- Name "ShadowCaster"
- Tags{ "LightMode" = "ShadowCaster" }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_shadowcaster
- #include "UnityCG.cginc"
- #include "TerrainEngine.cginc"
- struct v2f
- {
- V2F_SHADOW_CASTER;
- float2 uv : TEXCOORD1;
- UNITY_VERTEX_OUTPUT_STEREO
- };
- struct appdata
- {
- float4 vertex : POSITION;
- float3 normal : NORMAL;
- fixed4 color : COLOR;
- float4 texcoord : TEXCOORD0;
- UNITY_VERTEX_INPUT_INSTANCE_ID
- };
- float _ShakeDisplacement;
- float _ShakeTime;
- float _ShakeWindspeed;
- float _ShakeBending;
- v2f vert(appdata v)
- {
- v2f o;
- UNITY_SETUP_INSTANCE_ID(v);
- //UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
- //TerrainAnimateTree(v.vertex, v.color.w);
- // Apply same animation thing in ShadowCaster pass because we want shadows to be consistent!
- float factor = (1 - _ShakeDisplacement - v.color.r) * 0.5;
- const float _WindSpeed = (_ShakeWindspeed + v.color.g);
- const float _WaveScale = _ShakeDisplacement;
- const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
- const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
- const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
- float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
- float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
- float4 waves;
- waves = v.vertex.x * _waveXSize;
- waves += v.vertex.z * _waveZSize;
- waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b) * waveSpeed *_WindSpeed;
- float4 s, c;
- waves = frac(waves);
- FastSinCos(waves, s, c);
- float waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
- s *= waveAmount;
- s *= normalize(waveSpeed);
- s = s * s;
- float fade = dot(s, 1.3);
- s = s * s;
- float3 waveMove = float3 (0, 0, 0);
- waveMove.x = dot(s, _waveXmove);
- waveMove.z = dot(s, _waveZmove);
- v.vertex.xz -= mul((float3x3)unity_WorldToObject, waveMove).xz;
- TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
- o.uv = v.texcoord;
- return o;
- }
- sampler2D _MainTex;
- fixed _Cutoff;
- float4 frag(v2f i) : SV_Target
- {
- fixed4 texcol = tex2D(_MainTex, i.uv);
- clip(texcol.a - _Cutoff);
- SHADOW_CASTER_FRAGMENT(i)
- }
- ENDCG
- }
- }
- Fallback Off
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement