SpeedTree7BillboardPasses.hlsl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef UNIVERSAL_SPEEDTREE7BILLBOARD_PASSES_INCLUDED
  2. #define UNIVERSAL_SPEEDTREE7BILLBOARD_PASSES_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "SpeedTree7CommonPasses.hlsl"
  5. void InitializeData(inout SpeedTreeVertexInput input, out half2 outUV, out half outHueVariation)
  6. {
  7. // assume no scaling & rotation
  8. float3 worldPos = input.vertex.xyz + float3(UNITY_MATRIX_M[0].w, UNITY_MATRIX_M[1].w, UNITY_MATRIX_M[2].w);
  9. #ifdef BILLBOARD_FACE_CAMERA_POS
  10. float3 eyeVec = normalize(unity_BillboardCameraPosition - worldPos);
  11. float3 billboardTangent = normalize(float3(-eyeVec.z, 0, eyeVec.x)); // cross(eyeVec, {0,1,0})
  12. float3 billboardNormal = float3(billboardTangent.z, 0, -billboardTangent.x); // cross({0,1,0},billboardTangent)
  13. float angle = atan2(billboardNormal.z, billboardNormal.x); // signed angle between billboardNormal to {0,0,1}
  14. angle += angle < 0 ? 2 * SPEEDTREE_PI : 0;
  15. #else
  16. float3 billboardTangent = unity_BillboardTangent;
  17. float3 billboardNormal = unity_BillboardNormal;
  18. float angle = unity_BillboardCameraXZAngle;
  19. #endif
  20. float widthScale = input.texcoord1.x;
  21. float heightScale = input.texcoord1.y;
  22. float rotation = input.texcoord1.z;
  23. float2 percent = input.texcoord.xy;
  24. float3 billboardPos = (percent.x - 0.5f) * unity_BillboardSize.x * widthScale * billboardTangent;
  25. billboardPos.y += (percent.y * unity_BillboardSize.y + unity_BillboardSize.z) * heightScale;
  26. #ifdef ENABLE_WIND
  27. if (_WindQuality * _WindEnabled > 0)
  28. {
  29. billboardPos = GlobalWind(billboardPos, worldPos, true, _ST_WindVector.xyz, input.texcoord1.w);
  30. }
  31. #endif
  32. input.vertex.xyz += billboardPos;
  33. input.vertex.w = 1.0f;
  34. input.normal = billboardNormal.xyz;
  35. input.tangent = float4(billboardTangent.xyz, -1);
  36. float slices = unity_BillboardInfo.x;
  37. float invDelta = unity_BillboardInfo.y;
  38. angle += rotation;
  39. float imageIndex = fmod(floor(angle * invDelta + 0.5f), slices);
  40. float4 imageTexCoords = unity_BillboardImageTexCoords[imageIndex];
  41. if (imageTexCoords.w < 0)
  42. {
  43. outUV = imageTexCoords.xy - imageTexCoords.zw * percent.yx;
  44. }
  45. else
  46. {
  47. outUV = imageTexCoords.xy + imageTexCoords.zw * percent;
  48. }
  49. #ifdef EFFECT_HUE_VARIATION
  50. float hueVariationAmount = frac(worldPos.x + worldPos.y + worldPos.z);
  51. outHueVariation = saturate(hueVariationAmount * _HueVariation.a);
  52. #else
  53. outHueVariation = 0;
  54. #endif
  55. }
  56. SpeedTreeVertexOutput SpeedTree7Vert(SpeedTreeVertexInput input)
  57. {
  58. SpeedTreeVertexOutput output = (SpeedTreeVertexOutput)0;
  59. UNITY_SETUP_INSTANCE_ID(input);
  60. UNITY_TRANSFER_INSTANCE_ID(input, output);
  61. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  62. // handle speedtree wind and lod
  63. InitializeData(input, output.uvHueVariation.xy, output.uvHueVariation.z);
  64. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  65. half3 normalWS = input.normal; // Already calculated in world space. Can probably get rid of the world space transform in GetVertexPositionInputs too.
  66. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalWS);
  67. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  68. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  69. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  70. #ifdef EFFECT_BUMP
  71. real sign = input.tangent.w * GetOddNegativeScale();
  72. output.normalWS.xyz = TransformObjectToWorldNormal(input.normal);
  73. output.tangentWS.xyz = TransformObjectToWorldDir(input.tangent.xyz);
  74. output.bitangentWS.xyz = cross(output.normalWS.xyz, output.tangentWS.xyz) * sign;
  75. // View dir packed in w.
  76. output.normalWS.w = viewDirWS.x;
  77. output.tangentWS.w = viewDirWS.y;
  78. output.bitangentWS.w = viewDirWS.z;
  79. #else
  80. output.normalWS.xyz = TransformObjectToWorldNormal(input.normal);
  81. output.viewDirWS = viewDirWS;
  82. #endif
  83. output.positionWS = vertexInput.positionWS;
  84. output.clipPos = vertexInput.positionCS;
  85. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  86. output.shadowCoord = GetShadowCoord(vertexInput);
  87. #endif
  88. return output;
  89. }
  90. SpeedTreeVertexDepthOutput SpeedTree7VertDepth(SpeedTreeVertexInput input)
  91. {
  92. SpeedTreeVertexDepthOutput output = (SpeedTreeVertexDepthOutput)0;
  93. UNITY_SETUP_INSTANCE_ID(input);
  94. UNITY_TRANSFER_INSTANCE_ID(input, output);
  95. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  96. // handle speedtree wind and lod
  97. InitializeData(input, output.uvHueVariation.xy, output.uvHueVariation.z);
  98. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  99. #ifdef SHADOW_CASTER
  100. half3 normalWS = TransformObjectToWorldNormal(input.normal);
  101. output.clipPos = TransformWorldToHClip(ApplyShadowBias(vertexInput.positionWS, normalWS, _LightDirection));
  102. #else
  103. output.clipPos = vertexInput.positionCS;
  104. #endif
  105. return output;
  106. }
  107. #endif