CTI_Builtin4xTreeLibraryTumbling.cginc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // Upgrade NOTE: upgraded instancing buffer 'CTIProperties' to new syntax.
  2. #ifndef CTI_BUILTIN_4X_TREE_LIBRARY_INCLUDED
  3. #define CTI_BUILTIN_4X_TREE_LIBRARY_INCLUDED
  4. #include "Tessellation.cginc"
  5. struct appdata_ctitree {
  6. float4 vertex : POSITION;
  7. float4 tangent : TANGENT;
  8. float3 normal : NORMAL;
  9. float2 texcoord : TEXCOORD0;
  10. float4 texcoord1 : TEXCOORD1;
  11. // #if !defined(IS_BARK)
  12. float3 texcoord2 : TEXCOORD2;
  13. // #esle
  14. // float2 texcoord2 : TEXCOORD2;
  15. // #endif
  16. fixed4 color : COLOR0;
  17. #if defined(CTIBARKTESS)
  18. //UNITY_VERTEX_INPUT_INSTANCE_ID // does not work, so we have to do it manually
  19. #if defined(INSTANCING_ON)
  20. #ifdef SHADER_API_PSSL
  21. uint instanceID;
  22. #else
  23. uint instanceID : SV_InstanceID;
  24. #endif
  25. #endif
  26. #else
  27. UNITY_VERTEX_INPUT_INSTANCE_ID
  28. #endif
  29. };
  30. #if defined(GEOM_TYPE_BRANCH) || defined(GEOM_TYPE_BRANCH_DETAIL) || defined(GEOM_TYPE_FROND)
  31. #if !defined(CTIBARKTESS)
  32. float4 _DetailAlbedoMap_ST;
  33. #endif
  34. #endif
  35. float _TumbleStrength;
  36. float _TumbleFrequency;
  37. float _TimeOffset;
  38. float _LeafTurbulence;
  39. float _EdgeFlutterInfluence;
  40. float4 _TerrainLODWind;
  41. float _FadeOutAllLeaves;
  42. float _FadeOutWind;
  43. #if defined(CTITESS)
  44. float _Tess;
  45. float _minDist;
  46. float _maxDist;
  47. float _ExtrudeRange;
  48. float _Displacement;
  49. float _bendBounds;
  50. sampler2D _DispTex;
  51. #endif
  52. // As we do not include the UnityBuiltin3xTreeLibrary we have to also declare the following params:
  53. fixed4 _HueVariation; //_Color;
  54. fixed3 _TranslucencyColor;
  55. fixed _TranslucencyViewDependency;
  56. half _ShadowStrength;
  57. // As we do not include the TerrainEngine:
  58. UNITY_INSTANCING_BUFFER_START (CTIProperties)
  59. UNITY_DEFINE_INSTANCED_PROP (float4, _Wind)
  60. #define _Wind_arr CTIProperties
  61. UNITY_INSTANCING_BUFFER_END(CTIProperties)
  62. CBUFFER_START(CTITerrain)
  63. // trees
  64. fixed4 _TreeInstanceColor;
  65. float4 _TreeInstanceScale;
  66. float4x4 _TerrainEngineBendTree;
  67. float4 _SquashPlaneNormal;
  68. float _SquashAmount;
  69. CBUFFER_END
  70. #if defined(_PARALLAXMAP)
  71. float2 _CTI_TransFade;
  72. #endif
  73. struct LeafSurfaceOutput {
  74. fixed3 Albedo;
  75. fixed3 Normal;
  76. fixed3 Emission;
  77. fixed Translucency;
  78. half Specular;
  79. fixed Gloss;
  80. fixed Alpha;
  81. };
  82. inline half4 LightingTreeLeaf (LeafSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
  83. {
  84. half3 h = normalize (lightDir + viewDir);
  85. half nl = dot (s.Normal, lightDir);
  86. half nh = max (0, dot (s.Normal, h));
  87. half spec = pow (nh, s.Specular * 128.0) * s.Gloss;
  88. // view dependent back contribution for translucency
  89. fixed backContrib = saturate(dot(viewDir, -lightDir));
  90. // normally translucency is more like -nl, but looks better when it's view dependent
  91. backContrib = lerp(saturate(-nl), backContrib, _TranslucencyViewDependency);
  92. fixed3 translucencyColor = backContrib * s.Translucency * _TranslucencyColor;
  93. // wrap-around diffuse
  94. nl = max(0, nl * 0.6 + 0.4);
  95. fixed4 c;
  96. /////@TODO: what is is this multiply 2x here???
  97. c.rgb = s.Albedo * (translucencyColor * 2 + nl);
  98. c.rgb = c.rgb * _LightColor0.rgb + spec;
  99. // For directional lights, apply less shadow attenuation
  100. // based on shadow strength parameter.
  101. #if defined(DIRECTIONAL) || defined(DIRECTIONAL_COOKIE)
  102. c.rgb *= lerp(1, atten, _ShadowStrength);
  103. #else
  104. c.rgb *= atten;
  105. #endif
  106. c.a = s.Alpha;
  107. return c;
  108. }
  109. // Expand billboard and modify normal + tangent to fit
  110. inline void ExpandBillboard (in float4x4 mat, inout float4 pos, inout float3 normal, inout float4 tangent)
  111. {
  112. // tangent.w = 0 if this is a billboard
  113. float isBillboard = 1.0f - abs(tangent.w);
  114. // billboard normal
  115. float3 norb = normalize(mul(float4(normal, 0), mat)).xyz;
  116. // billboard tangent
  117. float3 tanb = normalize(mul(float4(tangent.xyz, 0.0f), mat)).xyz;
  118. pos += mul(float4(normal.xy, 0, 0), mat) * isBillboard;
  119. normal = lerp(normal, norb, isBillboard);
  120. tangent = lerp(tangent, float4(tanb, -1.0f), isBillboard);
  121. }
  122. inline float4 Squash(in float4 pos)
  123. {
  124. float3 planeNormal = _SquashPlaneNormal.xyz;
  125. float3 projectedVertex = pos.xyz - (dot(planeNormal.xyz, pos.xyz) + _SquashPlaneNormal.w) * planeNormal;
  126. pos = float4(lerp(projectedVertex, pos.xyz, _SquashAmount), 1);
  127. return pos;
  128. }
  129. float4 SmoothCurve( float4 x ) {
  130. return x * x *( 3.0 - 2.0 * x );
  131. }
  132. float4 TriangleWave( float4 x ) {
  133. return abs( frac( x + 0.5 ) * 2.0 - 1.0 );
  134. }
  135. float4 SmoothTriangleWave( float4 x ) {
  136. return SmoothCurve( TriangleWave( x ) );
  137. }
  138. // End of declarations formerly covered by TerrainEngine.cginc or UnityBuiltin3xTreeLibrary.cginc
  139. struct Input {
  140. #if defined(CTIBARKTESS)
  141. float2 uv_MainTex;
  142. float2 uv2_DetailAlbedoMap;
  143. #else
  144. #if !defined(IS_CTIARRAY)
  145. float2 uv_MainTex;
  146. #else
  147. float2 uv_MainTexArray;
  148. #endif
  149. #endif
  150. #if defined(GEOM_TYPE_BRANCH) || defined(GEOM_TYPE_BRANCH_DETAIL) || defined(GEOM_TYPE_FROND)
  151. float2 ctiuv2_DetailAlbedoMap;
  152. #endif
  153. // #if !defined(UNITY_PASS_SHADOWCASTER) && !defined (DEPTH_NORMAL) && !defined(CTIBARKTESS) || defined (DEBUG)
  154. fixed4 color : COLOR; // color.a = AO
  155. // #endif
  156. #if !defined (DEPTH_NORMAL)
  157. #if UNITY_VERSION < 2017
  158. #ifdef LOD_FADE_CROSSFADE
  159. // CTIBARKTESS needs both – but only screenPos gets setup
  160. #if defined(CTIBARKTESS)
  161. float4 screenPos;
  162. #endif
  163. half3 ditherScreenPos;
  164. #endif
  165. #endif
  166. #endif
  167. //UNITY_DITHER_CROSSFADE_COORDS
  168. #ifdef USE_VFACE
  169. float FacingSign : FACE;
  170. #endif
  171. #if defined (DEBUG)
  172. float2 my_uv2;
  173. float3 my_uv3;
  174. #endif
  175. };
  176. half3 CTI_UnpackScaleNormal(half4 packednormal, half bumpScale)
  177. {
  178. half3 normal;
  179. normal.xy = (packednormal.wy * 2 - 1);
  180. #if (SHADER_TARGET >= 30)
  181. // SM2.0: instruction count limitation
  182. // SM2.0: normal scaler is not supported
  183. normal.xy *= bumpScale;
  184. #endif
  185. normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
  186. return normal;
  187. }
  188. float4 AfsSmoothTriangleWave( float4 x ) {
  189. return (SmoothCurve( TriangleWave( x )) - 0.5) * 2.0;
  190. }
  191. // see http://www.neilmendoza.com/glsl-rotation-about-an-arbitrary-axis/
  192. // 13fps
  193. float3x3 AfsRotationMatrix(float3 axis, float angle)
  194. {
  195. //axis = normalize(axis); // moved to calling function
  196. float s = sin(angle);
  197. float c = cos(angle);
  198. float oc = 1.0 - c;
  199. return float3x3 ( oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
  200. oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
  201. oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c);
  202. }
  203. // Scriptable Render Loop
  204. // 7.7fps
  205. float3 Rotate(/*float3 pivot, */float3 position, float3 rotationAxis, float angle)
  206. {
  207. //rotationAxis = normalize(rotationAxis); // moved to calling function
  208. float3 cpa = /*pivot + */rotationAxis * dot(rotationAxis, position/* - pivot*/);
  209. return cpa + ((position - cpa) * cos(angle) + cross(rotationAxis, (position - cpa)) * sin(angle));
  210. }
  211. // https://twitter.com/SebAaltonen/status/878250919879639040
  212. #define FLT_MAX 3.402823466e+38 // Maximum representable floating-point number
  213. float3 FastSign(float3 x)
  214. {
  215. return saturate(x * FLT_MAX + 0.5) * 2.0 - 1.0;
  216. }
  217. // Detail bending
  218. void CTI_AnimateVertex( inout appdata_ctitree v, float4 pos, float3 normal, float4 animParams, float3 pivot, float tumbleInfluence, float4 Wind, float packedBranchAxis) {
  219. // animParams.x = branch phase
  220. // animParams.y = edge flutter factor
  221. // animParams.z = primary factor UV2.x
  222. // animParams.w = secondary factor UV2.y
  223. float fDetailAmp = 0.1f;
  224. float fBranchAmp = 0.3f; // 0.3f;
  225. float fade = (_FadeOutWind == 1 && unity_LODFade.x > 0 ) ? unity_LODFade.x : 1.0;
  226. // Add extra animation to make it fit speedtree
  227. float3 TreeWorldPos = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
  228. // fern issue / this does not seem to fix the problem... / float3 TreeWorldPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
  229. TreeWorldPos.xyz = abs(TreeWorldPos.xyz * 0.125f);
  230. float sinuswave = _SinTime.z;
  231. // float4 vOscillations = AfsSmoothTriangleWave(float4(TreeWorldPos.x + sinuswave , TreeWorldPos.z + sinuswave * 0.8, 0.0, 0.0));
  232. #if defined (LEAFTUMBLING)
  233. float shiftedsinuswave = sin(_Time.y * 0.5 + _TimeOffset);
  234. float4 vOscillations = AfsSmoothTriangleWave(float4(TreeWorldPos.x + sinuswave, TreeWorldPos.z + sinuswave * 0.7, TreeWorldPos.x + shiftedsinuswave, TreeWorldPos.z + shiftedsinuswave * 0.8));
  235. #else
  236. float4 vOscillations = AfsSmoothTriangleWave(float4(TreeWorldPos.x + sinuswave, TreeWorldPos.z + sinuswave * 0.7, 0.0, 0.0));
  237. #endif
  238. // vOscillations.xz = lerp(vOscillations.xz, 1, vOscillations.xz );
  239. // x used for main wind bending / y used for tumbling
  240. float2 fOsc = vOscillations.xz + (vOscillations.yw * vOscillations.yw);
  241. fOsc = 0.75 + (fOsc + 3.33) * 0.33;
  242. Wind.w *= fade;
  243. Wind.xyz *= fade;
  244. float absWindStrength = length(Wind.xyz);
  245. // Phases (object, vertex, branch)
  246. // float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
  247. // new
  248. float fObjPhase = abs ( frac( (TreeWorldPos.x + TreeWorldPos.z) * 0.5 ) * 2 - 1 );
  249. float fBranchPhase = fObjPhase + animParams.x;
  250. float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
  251. // x is used for edges; y is used for branches
  252. float2 vWavesIn = _Time.yy + float2(fVtxPhase, fBranchPhase );
  253. // 1.975, 0.793, 0.375, 0.193 are good frequencies
  254. float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
  255. vWaves = SmoothTriangleWave( vWaves );
  256. float2 vWavesSum = vWaves.xz + vWaves.yw;
  257. // Tumbling / Should be done before all other deformations
  258. #if defined (LEAFTUMBLING)
  259. // pos.w: upper bit = lodfade
  260. // Separate lodfade and twigPhase: lodfade stored in highest bit / twigphase compressed to 7 bits
  261. // moved to #ifs
  262. tumbleInfluence = frac(pos.w * 2.0);
  263. // Move point to 0,0,0
  264. pos.xyz -= pivot;
  265. float tumble = (_TumbleStrength == 0) ? 0 : 1;
  266. if ( (_TumbleStrength || _LeafTurbulence /*> 0*/) && absWindStrength * tumbleInfluence > 0 ) {
  267. // _Wind.w is turbulence
  268. // Add variance to the different leaf planes
  269. // good for palms and bananas - but we do it later
  270. // float3 fracs = frac( pivot * 33.3 + animParams.x * frac(fObjPhase) * 0.25 ); //fBranchPhase * 0.1); // + pos.w
  271. // good for trees
  272. float3 fracs = frac( pivot * 33.3 ); //fBranchPhase * 0.1); // + pos.w
  273. float offset = fracs.x + fracs.y + fracs.z; ;
  274. float tFrequency = _TumbleFrequency * (_Time.y /* new */ + fObjPhase * 10 );
  275. // Add different speeds: (1.0 + offset * 0.25)
  276. // float4 vWaves1 = SmoothTriangleWave( float4( (tFrequency + offset) * (1.0 + offset * 0.25), tFrequency * 0.75 - offset, tFrequency * 0.05 + offset, tFrequency * 1.5 + offset));
  277. // less sharp
  278. float4 vWaves1 = SmoothTriangleWave( float4( (tFrequency + offset) * (1.0 + offset * 0.25), tFrequency * 0.75 + offset, tFrequency * 0.5 + offset, tFrequency * 1.5 + offset));
  279. // float4 vWaves1 = SmoothTriangleWave( float4( (tFrequency + offset), tFrequency * 0.75 - offset, tFrequency * 0.05 + offset, tFrequency * 2.5 + offset));
  280. float3 windDir = normalize (Wind.xyz);
  281. #if defined (_EMISSION)
  282. // This was the root of the fern issue: branchAxes slightly varied on different LODs!
  283. float3 branchAxis = frac( packedBranchAxis * float3(1.0, 256.0f, 65536.0f) );
  284. branchAxis = branchAxis * 2.0 - 1.0;
  285. branchAxis = normalize(branchAxis);
  286. // we can do better in case we have the baked branch main axis
  287. float facingWind = (dot(branchAxis, windDir));
  288. #else
  289. float facingWind = (dot(normalize(float3(pos.x, 0, pos.z)), windDir)); //saturate
  290. #endif
  291. float3 windTangent = float3(-windDir.z, windDir.y, windDir.x);
  292. float twigPhase = vWaves1.x + vWaves1.y + (vWaves1.z * vWaves1.z);
  293. float windStrength = dot(abs(Wind.xyz), 1) * tumbleInfluence * (1.35 - facingWind) * Wind.w + absWindStrength; // Use abs(_Wind)!!!!!!
  294. // turbulence
  295. #if defined (_EMISSION)
  296. // if(_LeafTurbulence) {
  297. float angle =
  298. // center rotation so the leaves rotate leftwards as well as rightwards according to the incoming waves
  299. // ((twigPhase + vWaves1.w + fBranchPhase) * 0.2 - 0.5) // not so good to add fBranchPhase here...
  300. ((twigPhase + vWaves1.w ) * 0.25 - 0.5)
  301. // make rotation strength depend on absWindStrength and all other inputs
  302. * 4.0 * absWindStrength * _LeafTurbulence * tumbleInfluence * (0.5 + animParams.w) * saturate(lerp(1.0, animParams.y * 8, _EdgeFlutterInfluence))
  303. ;
  304. //branchAxis = normalize(branchAxis); // branch axis should be mostly normalized...
  305. float3x3 turbulenceRot = AfsRotationMatrix( -branchAxis, angle);
  306. pos.xyz = mul( turbulenceRot, pos.xyz);
  307. #if defined(_NORMALMAP)
  308. v.normal = mul( turbulenceRot, v.normal.xyz );
  309. #endif
  310. // #else
  311. // pos.xyz = Rotate(pos.xyz, -branchAxis, angle);
  312. // #endif
  313. //}
  314. #endif
  315. // tumbling
  316. // As used by the debug shader
  317. #if !defined (EFFECT_HUE_VARIATION)
  318. //if (_TumbleStrength) {
  319. // tumbleInfluence = frac(pos.w * 2.0);
  320. // + 1 is correct for trees/palm / -1 is correct for fern? allow negative values in the material inspector
  321. float angleTumble = ( windStrength * (twigPhase + fBranchPhase * 0.25) * _TumbleStrength * tumbleInfluence * fOsc.y );
  322. // windTangent should be normalized
  323. float3x3 tumbleRot = AfsRotationMatrix( windTangent, angleTumble);
  324. pos.xyz = mul( tumbleRot, pos.xyz);
  325. #if defined(_NORMALMAP)
  326. v.normal = mul( tumbleRot, v.normal.xyz );
  327. #endif
  328. //#else
  329. // pos.xyz = Rotate(pos.xyz, windTangent, angleTumble);
  330. //#endif
  331. //}
  332. #endif
  333. }
  334. // crossfade – in case anybody uses it...
  335. // #if defined(LOD_FADE_CROSSFADE)
  336. // if (unity_LODFade.x != 0.0 && lodfade == 1.0) {
  337. // pos.xyz *= unity_LODFade.x;
  338. // }
  339. // #endif
  340. // fade in/out leave planes
  341. #if defined(LOD_FADE_PERCENTAGE)
  342. //float lodfade = ceil(pos.w - 0.51);
  343. float lodfade = (pos.w > 0.5) ? 1 : 0;
  344. //lodfade += _FadeOutAllLeaves;
  345. if (/*unity_LODFade.x < 1.0 && */ lodfade) {
  346. pos.xyz *= 1.0 - unity_LODFade.x;
  347. }
  348. #endif
  349. // Move point back to origin
  350. pos.xyz += pivot;
  351. #endif
  352. // Preserve Length
  353. float origLength = length(pos.xyz);
  354. Wind.xyz *= fOsc.x;
  355. // Edge (xz) and branch bending (y)
  356. #if !defined(IS_BARK)
  357. float3 bend = animParams.y * fDetailAmp * normal.xyz
  358. #if !defined(USE_VFACE)
  359. * FastSign(normal)
  360. #endif
  361. ;
  362. // Old style turbulence // bend.y = (animParams.w + animParams.y * _LeafTurbulence) * fBranchAmp;
  363. bend.y = (animParams.y + animParams.w) * fBranchAmp;
  364. #else
  365. float3 bend = float3(0,0,0);
  366. bend.y = (animParams.w) * fBranchAmp;
  367. #endif
  368. // This gets never zero even if there is no wind. So we have to multiply it by length(Wind.xyz)
  369. // if not disabled in debug shader
  370. #if !defined(EFFECT_BUMP)
  371. // this is still fucking sharp!!!!!
  372. pos.xyz += ( ((vWavesSum.xyx * bend) + (Wind.xyz * vWavesSum.y * animParams.w)) * Wind.w) * absWindStrength;
  373. #endif
  374. // Preserve Length
  375. // Doing it 2 times might help though
  376. // pos.xyz = normalize(pos.xyz) * origLength;
  377. // Primary bending / Displace position
  378. #if !defined (ENABLE_WIND)
  379. pos.xyz += animParams.z * Wind.xyz;
  380. #endif
  381. // Preserve Length
  382. // This is not a good idea, so we skip it!
  383. // #if defined(LOD_FADE_PERCENTAGE) && defined (LEAFTUMBLING)
  384. // pos.xyz = lerp(normalize(pos.xyz) * origLength, pos.xyz, lodfade * (unity_LODFade.x) ) ;
  385. // #else
  386. pos.xyz = normalize(pos.xyz) * origLength;
  387. // #endif
  388. v.vertex.xyz = pos.xyz;
  389. // Store Variation
  390. #if !defined(UNITY_PASS_SHADOWCASTER) && defined (IS_LODTREE) && !defined (DEBUG)
  391. v.color.r = saturate ( ( frac(TreeWorldPos.x + TreeWorldPos.y + TreeWorldPos.z) + frac( (TreeWorldPos.x + TreeWorldPos.y + TreeWorldPos.z) * 3.3 ) ) * 0.5 );
  392. #endif
  393. }
  394. // ---------------------------
  395. // Leaves
  396. #if !defined(IS_BARK)
  397. #if defined (DEPTH_NORMAL)
  398. void CTI_TreeVertLeaf (inout appdata_ctitree v)
  399. {
  400. #else
  401. void CTI_TreeVertLeaf (inout appdata_ctitree v, out Input OUT)
  402. {
  403. UNITY_INITIALIZE_OUTPUT(Input, OUT);
  404. #endif
  405. #if !defined(IS_LODTREE)
  406. ExpandBillboard (UNITY_MATRIX_IT_MV, v.vertex, v.normal, v.tangent);
  407. v.vertex.xyz *= _TreeInstanceScale.xyz;
  408. #endif
  409. // Decode UV3
  410. float3 pivot;
  411. #if defined(LEAFTUMBLING)
  412. // 15bit compression 2 components only, important: sign of y
  413. pivot.xz = (frac(float2(1.0f, 32768.0f) * v.texcoord2.xx) * 2) - 1;
  414. pivot.y = sqrt(1 - saturate(dot(pivot.xz, pivot.xz)));
  415. pivot *= v.texcoord2.y;
  416. #if !defined(IS_LODTREE)
  417. pivot *= _TreeInstanceScale.xyz;
  418. #endif
  419. #endif
  420. #if defined(_METALLICGLOSSMAP)
  421. float4 TerrainLODWind = _TerrainLODWind;
  422. TerrainLODWind.xyz = mul((float3x3)unity_WorldToObject, _TerrainLODWind.xyz);
  423. CTI_AnimateVertex( v, float4(v.vertex.xyz, v.color.b), v.normal, float4(v.color.xy, v.texcoord1.xy), pivot, v.color.b, TerrainLODWind, v.texcoord2.z);
  424. #else
  425. CTI_AnimateVertex(v, float4(v.vertex.xyz, v.color.b), v.normal, float4(v.color.xy, v.texcoord1.xy), pivot, v.color.b, UNITY_ACCESS_INSTANCED_PROP(_Wind_arr, _Wind), v.texcoord2.z);
  426. #endif
  427. #if !defined(IS_LODTREE)
  428. v.vertex = Squash(v.vertex);
  429. #if !defined(UNITY_PASS_SHADOWCASTER) && !defined (DEBUG)
  430. v.color.rgb = _TreeInstanceColor.rgb; // *_Color.rgb;
  431. #endif
  432. #endif
  433. v.normal = normalize(v.normal);
  434. v.tangent.xyz = normalize(v.tangent.xyz);
  435. //UNITY_TRANSFER_DITHER_CROSSFADE(OUT, v.vertex)
  436. #if UNITY_VERSION < 2017
  437. #if !defined(DEPTH_NORMAL)
  438. #ifdef LOD_FADE_CROSSFADE
  439. OUT.ditherScreenPos = ComputeDitherScreenPos(UnityObjectToClipPos(v.vertex));
  440. #endif
  441. #endif
  442. #endif
  443. #if defined(_PARALLAXMAP)
  444. float3 worldPos = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
  445. float3 distVec = _WorldSpaceCameraPos - worldPos;
  446. float distSq = dot(distVec, distVec);
  447. v.color.b = saturate( (_CTI_TransFade.x - distSq) / _CTI_TransFade.y);
  448. #endif
  449. #if defined (DEBUG)
  450. OUT.my_uv2 = v.texcoord1.xy;
  451. OUT.my_uv3 = v.texcoord2.xyz;
  452. // tumbleinfluence
  453. //v.color.b = saturate( frac(v.color.b * 2.0) - 10/255);
  454. v.color.b = /*frac*/(v.color.b * 2.0);
  455. #endif
  456. }
  457. #endif
  458. // ---------------------------
  459. // Bark
  460. #if defined (DEPTH_NORMAL)
  461. void CTI_TreeVertBark (inout appdata_ctitree v)
  462. {
  463. #else
  464. void CTI_TreeVertBark (inout appdata_ctitree v, out Input OUT)
  465. {
  466. UNITY_INITIALIZE_OUTPUT(Input, OUT);
  467. #endif
  468. #if !defined(DEPTH_NORMAL)
  469. #if defined(GEOM_TYPE_BRANCH) || defined(GEOM_TYPE_BRANCH_DETAIL) || defined(GEOM_TYPE_FROND)
  470. OUT.ctiuv2_DetailAlbedoMap = v.texcoord1.zw; // * _DetailAlbedoMap_ST.xy;
  471. #endif
  472. #endif
  473. #if !defined(IS_LODTREE)
  474. ExpandBillboard (UNITY_MATRIX_IT_MV, v.vertex, v.normal, v.tangent);
  475. v.vertex.xyz *= _TreeInstanceScale.xyz;
  476. #endif
  477. #if defined(_METALLICGLOSSMAP)
  478. float4 TerrainLODWind = _TerrainLODWind;
  479. TerrainLODWind.xyz = mul((float3x3)unity_WorldToObject, _TerrainLODWind.xyz);
  480. CTI_AnimateVertex( v, float4(v.vertex.xyz, v.color.b), v.normal, float4(v.color.xy, v.texcoord1.xy), float3(0,0,0), 0, TerrainLODWind, 0); //v.texcoord2.z);
  481. #else
  482. CTI_AnimateVertex( v, float4(v.vertex.xyz, v.color.b), v.normal, float4(v.color.xy, v.texcoord1.xy), float3(0,0,0), 0, UNITY_ACCESS_INSTANCED_PROP(_Wind_arr, _Wind), 0); //v.texcoord2.z);
  483. #endif
  484. #if !defined(IS_LODTREE)
  485. v.vertex = Squash(v.vertex);
  486. #if !defined(UNITY_PASS_SHADOWCASTER) && !defined (DEBUG)
  487. v.color.rgb = _TreeInstanceColor.rgb; // *_Color.rgb;
  488. #endif
  489. #endif
  490. v.normal = normalize(v.normal);
  491. v.tangent.xyz = normalize(v.tangent.xyz);
  492. #if UNITY_VERSION < 2017
  493. #if !defined(DEPTH_NORMAL) && !defined(CTIBARKTESS)
  494. #ifdef LOD_FADE_CROSSFADE
  495. OUT.ditherScreenPos = ComputeDitherScreenPos(UnityObjectToClipPos(v.vertex));
  496. #endif
  497. #endif
  498. #endif
  499. }
  500. #endif // CTI_BUILTIN_4X_TREE_LIBRARY_INCLUDED