Vegetation.hlsl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. #include "Fog.hlsl"
  7. #ifndef D3D11
  8. // D3D9 uniforms
  9. uniform float cWindHeightFactor;
  10. uniform float cWindHeightPivot;
  11. uniform float cWindPeriod;
  12. uniform float2 cWindWorldSpacing;
  13. #else
  14. // D3D11 constant buffer
  15. cbuffer CustomVS : register(b6)
  16. {
  17. float cWindHeightFactor;
  18. float cWindHeightPivot;
  19. float cWindPeriod;
  20. float2 cWindWorldSpacing;
  21. }
  22. #endif
  23. void VS(float4 iPos : POSITION,
  24. #if !defined(BILLBOARD) && !defined(TRAILFACECAM)
  25. float3 iNormal : NORMAL,
  26. #endif
  27. #ifndef NOUV
  28. float2 iTexCoord : TEXCOORD0,
  29. #endif
  30. #ifdef VERTEXCOLOR
  31. float4 iColor : COLOR0,
  32. #endif
  33. #if defined(LIGHTMAP) || defined(AO)
  34. float2 iTexCoord2 : TEXCOORD1,
  35. #endif
  36. #if (defined(NORMALMAP) || defined(TRAILFACECAM) || defined(TRAILBONE)) && !defined(BILLBOARD) && !defined(DIRBILLBOARD)
  37. float4 iTangent : TANGENT,
  38. #endif
  39. #ifdef SKINNED
  40. float4 iBlendWeights : BLENDWEIGHT,
  41. int4 iBlendIndices : BLENDINDICES,
  42. #endif
  43. #ifdef INSTANCED
  44. float4x3 iModelInstance : TEXCOORD4,
  45. #endif
  46. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  47. float2 iSize : TEXCOORD1,
  48. #endif
  49. #ifndef NORMALMAP
  50. out float2 oTexCoord : TEXCOORD0,
  51. #else
  52. out float4 oTexCoord : TEXCOORD0,
  53. out float4 oTangent : TEXCOORD3,
  54. #endif
  55. out float3 oNormal : TEXCOORD1,
  56. out float4 oWorldPos : TEXCOORD2,
  57. #ifdef PERPIXEL
  58. #ifdef SHADOW
  59. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  60. #endif
  61. #ifdef SPOTLIGHT
  62. out float4 oSpotPos : TEXCOORD5,
  63. #endif
  64. #ifdef POINTLIGHT
  65. out float3 oCubeMaskVec : TEXCOORD5,
  66. #endif
  67. #else
  68. out float3 oVertexLight : TEXCOORD4,
  69. out float4 oScreenPos : TEXCOORD5,
  70. #ifdef ENVCUBEMAP
  71. out float3 oReflectionVec : TEXCOORD6,
  72. #endif
  73. #if defined(LIGHTMAP) || defined(AO)
  74. out float2 oTexCoord2 : TEXCOORD7,
  75. #endif
  76. #endif
  77. #ifdef VERTEXCOLOR
  78. out float4 oColor : COLOR0,
  79. #endif
  80. #if defined(D3D11) && defined(CLIPPLANE)
  81. out float oClip : SV_CLIPDISTANCE0,
  82. #endif
  83. out float4 oPos : OUTPOSITION)
  84. {
  85. // Define a 0,0 UV coord if not expected from the vertex data
  86. #ifdef NOUV
  87. float2 iTexCoord = float2(0.0, 0.0);
  88. #endif
  89. float4x3 modelMatrix = iModelMatrix;
  90. float3 worldPos = GetWorldPos(modelMatrix);
  91. float height = worldPos.y - modelMatrix._m31;
  92. float windStrength = max(height - cWindHeightPivot, 0.0) * cWindHeightFactor;
  93. float windPeriod = cElapsedTime * cWindPeriod + dot(worldPos.xz, cWindWorldSpacing);
  94. worldPos.x += windStrength * sin(windPeriod);
  95. worldPos.z -= windStrength * cos(windPeriod);
  96. oPos = GetClipPos(worldPos);
  97. oNormal = GetWorldNormal(modelMatrix);
  98. oWorldPos = float4(worldPos, GetDepth(oPos));
  99. #if defined(D3D11) && defined(CLIPPLANE)
  100. oClip = dot(oPos, cClipPlane);
  101. #endif
  102. #ifdef VERTEXCOLOR
  103. oColor = iColor;
  104. #endif
  105. #ifdef NORMALMAP
  106. float4 tangent = GetWorldTangent(modelMatrix);
  107. float3 bitangent = cross(tangent.xyz, oNormal) * tangent.w;
  108. oTexCoord = float4(GetTexCoord(iTexCoord), bitangent.xy);
  109. oTangent = float4(tangent.xyz, bitangent.z);
  110. #else
  111. oTexCoord = GetTexCoord(iTexCoord);
  112. #endif
  113. #ifdef PERPIXEL
  114. // Per-pixel forward lighting
  115. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  116. #ifdef SHADOW
  117. // Shadow projection: transform from world space to shadow space
  118. GetShadowPos(projWorldPos, oNormal, oShadowPos);
  119. #endif
  120. #ifdef SPOTLIGHT
  121. // Spotlight projection: transform from world space to projector texture coordinates
  122. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  123. #endif
  124. #ifdef POINTLIGHT
  125. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  126. #endif
  127. #else
  128. // Ambient & per-vertex lighting
  129. #if defined(LIGHTMAP) || defined(AO)
  130. // If using lightmap, disregard zone ambient light
  131. // If using AO, calculate ambient in the PS
  132. oVertexLight = float3(0.0, 0.0, 0.0);
  133. oTexCoord2 = iTexCoord2;
  134. #else
  135. oVertexLight = GetAmbient(GetZonePos(worldPos));
  136. #endif
  137. #ifdef NUMVERTEXLIGHTS
  138. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  139. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  140. #endif
  141. oScreenPos = GetScreenPos(oPos);
  142. #ifdef ENVCUBEMAP
  143. oReflectionVec = worldPos - cCameraPos;
  144. #endif
  145. #endif
  146. }