Vegetation.hlsl 5.1 KB

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