PBRVegetation.hlsl 5.1 KB

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