Vegetation.hlsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. uniform float cWindHeightFactor;
  8. uniform float cWindHeightPivot;
  9. uniform float cWindPeriod;
  10. uniform float2 cWindWorldSpacing;
  11. void VS(float4 iPos : POSITION,
  12. float3 iNormal : NORMAL,
  13. float2 iTexCoord : TEXCOORD0,
  14. #if defined(LIGHTMAP) || defined(AO)
  15. float2 iTexCoord2 : TEXCOORD1,
  16. #endif
  17. #ifdef NORMALMAP
  18. float4 iTangent : TANGENT,
  19. #endif
  20. #ifdef SKINNED
  21. float4 iBlendWeights : BLENDWEIGHT,
  22. int4 iBlendIndices : BLENDINDICES,
  23. #endif
  24. #ifdef INSTANCED
  25. float4x3 iModelInstance : TEXCOORD2,
  26. #endif
  27. #ifdef BILLBOARD
  28. float2 iSize : TEXCOORD1,
  29. #endif
  30. #ifndef NORMALMAP
  31. out float2 oTexCoord : TEXCOORD0,
  32. #else
  33. out float4 oTexCoord : TEXCOORD0,
  34. out float4 oTangent : TEXCOORD3,
  35. #endif
  36. out float3 oNormal : TEXCOORD1,
  37. out float4 oWorldPos : TEXCOORD2,
  38. #ifdef PERPIXEL
  39. #ifdef SHADOW
  40. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  41. #endif
  42. #ifdef SPOTLIGHT
  43. out float4 oSpotPos : TEXCOORD5,
  44. #endif
  45. #ifdef POINTLIGHT
  46. out float3 oCubeMaskVec : TEXCOORD5,
  47. #endif
  48. #else
  49. out float3 oVertexLight : TEXCOORD4,
  50. out float4 oScreenPos : TEXCOORD5,
  51. #ifdef ENVCUBEMAP
  52. out float3 oReflectionVec : TEXCOORD6,
  53. #endif
  54. #if defined(LIGHTMAP) || defined(AO)
  55. out float2 oTexCoord2 : TEXCOORD7,
  56. #endif
  57. #endif
  58. out float4 oPos : POSITION)
  59. {
  60. float4x3 modelMatrix = iModelMatrix;
  61. float3 worldPos = GetWorldPos(modelMatrix);
  62. float height = worldPos.y - cModel._m31;
  63. float windStrength = max(height - cWindHeightPivot, 0.0) * cWindHeightFactor;
  64. float windPeriod = cElapsedTime * cWindPeriod + dot(worldPos.xz, cWindWorldSpacing);
  65. worldPos.x += windStrength * sin(windPeriod);
  66. worldPos.z -= windStrength * cos(windPeriod);
  67. oPos = GetClipPos(worldPos);
  68. oNormal = GetWorldNormal(modelMatrix);
  69. oWorldPos = float4(worldPos, GetDepth(oPos));
  70. #ifdef NORMALMAP
  71. float3 tangent = GetWorldTangent(modelMatrix);
  72. float3 bitangent = cross(tangent, oNormal) * iTangent.w;
  73. oTexCoord = float4(GetTexCoord(iTexCoord), bitangent.xy);
  74. oTangent = float4(tangent, bitangent.z);
  75. #else
  76. oTexCoord = GetTexCoord(iTexCoord);
  77. #endif
  78. #ifdef PERPIXEL
  79. // Per-pixel forward lighting
  80. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  81. #ifdef SHADOW
  82. // Shadow projection: transform from world space to shadow space
  83. GetShadowPos(projWorldPos, oShadowPos);
  84. #endif
  85. #ifdef SPOTLIGHT
  86. // Spotlight projection: transform from world space to projector texture coordinates
  87. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  88. #endif
  89. #ifdef POINTLIGHT
  90. oCubeMaskVec = mul(cLightPos.xyz - worldPos, (float3x3)cLightMatrices[0]);
  91. #endif
  92. #else
  93. // Ambient & per-vertex lighting
  94. #if defined(LIGHTMAP) || defined(AO)
  95. // If using lightmap, disregard zone ambient light
  96. // If using AO, calculate ambient in the PS
  97. oVertexLight = float3(0.0, 0.0, 0.0);
  98. oTexCoord2 = iTexCoord2;
  99. #else
  100. oVertexLight = GetAmbient(GetZonePos(worldPos));
  101. #endif
  102. #ifdef NUMVERTEXLIGHTS
  103. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  104. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  105. #endif
  106. oScreenPos = GetScreenPos(oPos);
  107. #ifdef ENVCUBEMAP
  108. oReflectionVec = worldPos - cCameraPos;
  109. #endif
  110. #endif
  111. }