CTI URP Billboard.hlsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if defined(UNITY_PASS_SHADOWCASTER)
  2. uniform float4 unity_BillboardCameraParams;
  3. #define unity_BillboardCameraPosition (unity_BillboardCameraParams.xyz)
  4. #endif
  5. float3 unity_BillboardSize;
  6. float4 _CTI_SRP_Wind;
  7. float _CTI_SRP_Turbulence;
  8. #if defined(_PARALLAXMAP)
  9. float2 _CTI_TransFade;
  10. #endif
  11. float4 SmoothCurve(float4 x) {
  12. return x * x * (3.0 - 2.0 * x);
  13. }
  14. float4 TriangleWave(float4 x) {
  15. return abs(frac(x + 0.5) * 2.0 - 1.0);
  16. }
  17. float4 CTISmoothTriangleWave(float4 x) {
  18. return (SmoothCurve(TriangleWave(x)) - 0.5) * 2.0;
  19. }
  20. // Billboard Vertex Function
  21. void CTIBillboardVert (inout Attributes v, float3 lightDir, out half4 color) {
  22. // Init color
  23. color = 0;
  24. float4 position = v.positionOS;
  25. float3 worldPos = v.positionOS.xyz + UNITY_MATRIX_M._m03_m13_m23;
  26. float3 TreeWorldPos = abs(worldPos.xyz * 0.125f);
  27. // Store Color Variation
  28. #if !defined(SHADOWCASTERPASS)
  29. color.r = saturate((frac(TreeWorldPos.x + TreeWorldPos.y + TreeWorldPos.z) + frac((TreeWorldPos.x + TreeWorldPos.y + TreeWorldPos.z) * 3.3)) * 0.5);
  30. #endif
  31. // #if defined(_PARALLAXMAP)
  32. // float3 distVec = _WorldSpaceCameraPos - worldPos;
  33. // float distSq = dot(distVec, distVec);
  34. // color.b = saturate( (_CTI_TransFade.x - distSq) / _CTI_TransFade.y);
  35. // #endif
  36. // ////////////////////////////////////
  37. // Set vertex position
  38. #if defined(SHADOWCASTERPASS)
  39. float3 eyeVec = -lightDir;
  40. #else
  41. float3 eyeVec = normalize(_WorldSpaceCameraPos - worldPos);
  42. #endif
  43. float3 billboardTangent = normalize(float3(-eyeVec.z, 0, eyeVec.x));
  44. float3 billboardNormal = float3(billboardTangent.z, 0, -billboardTangent.x);
  45. float2 percent = v.texcoord.xy;
  46. float3 billboardPos = (percent.x - 0.5) * unity_BillboardSize.x * v.texcoord1.x * billboardTangent;
  47. // billboardPos.y += (percent.y * unity_BillboardSize.y * 2.0 + unity_BillboardSize.z) * v.texcoord1.y;
  48. // Nope: not y * 2 other wise billbords get culled too early: Double the height in the bb asset!
  49. billboardPos.y += (percent.y * unity_BillboardSize.y * _BillboardScale + unity_BillboardSize.z) * v.texcoord1.y;
  50. position.xyz += billboardPos;
  51. v.positionOS.xyz = position.xyz;
  52. v.positionOS.w = 1.0f;
  53. // Wind
  54. float sinuswave = _SinTime.z;
  55. float4 vOscillations = CTISmoothTriangleWave(float4(TreeWorldPos.x + sinuswave, TreeWorldPos.z + sinuswave * 0.8, 0.0, 0.0));
  56. float fOsc = vOscillations.x + (vOscillations.y * vOscillations.y);
  57. fOsc = 0.75 + (fOsc + 3.33) * 0.33;
  58. // Saturate added to stop warning on dx11...
  59. v.positionOS.xyz += _CTI_SRP_Wind.w * _CTI_SRP_Wind.xyz * _WindStrength * fOsc * pow(saturate(percent.y), 1.5); // pow(y,1.5) matches the wind baked to the mesh trees
  60. // ////////////////////////////////////
  61. // Get billboard texture coords
  62. float angle = atan2(billboardNormal.z, billboardNormal.x); // signed angle between billboardNormal to {0,0,1}
  63. angle += angle < 0 ? 2 * PI : 0;
  64. // Set Rotation
  65. angle += v.texcoord1.z;
  66. // Write final billboard texture coords
  67. const float invDelta = 1.0 / (45.0 * ((PI * 2.0) / 360.0));
  68. float imageIndex = fmod(floor(angle * invDelta + 0.5f), 8);
  69. float2 column_row;
  70. column_row.x = imageIndex * 0.25; // we do not care about the horizontal coord that much as our billboard texture tiles
  71. column_row.y = saturate(4 - imageIndex) * 0.5;
  72. v.texcoord.xy = column_row + v.texcoord.xy * float2(0.25, 0.5);
  73. // ////////////////////////////////////
  74. // Set Normal and Tangent
  75. v.normalOS = billboardNormal.xyz;
  76. #if !defined(SHADOWCASTERPASS)
  77. v.tangentOS = float4(billboardTangent.xyz, -1.0);
  78. #endif
  79. }