Vegetation.glsl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "Uniforms.glsl"
  2. #include "Transform.glsl"
  3. #include "ScreenPos.glsl"
  4. #include "Lighting.glsl"
  5. uniform float cWindHeightFactor;
  6. uniform float cWindHeightPivot;
  7. uniform float cWindPeriod;
  8. uniform vec2 cWindWorldSpacing;
  9. #ifdef NORMALMAP
  10. varying vec4 vTexCoord;
  11. varying vec4 vTangent;
  12. #else
  13. varying vec2 vTexCoord;
  14. #endif
  15. varying vec3 vNormal;
  16. varying vec4 vWorldPos;
  17. #ifdef PERPIXEL
  18. #ifdef SHADOW
  19. varying vec4 vShadowPos[NUMCASCADES];
  20. #endif
  21. #ifdef SPOTLIGHT
  22. varying vec4 vSpotPos;
  23. #endif
  24. #ifdef POINTLIGHT
  25. varying vec3 vCubeMaskVec;
  26. #endif
  27. #else
  28. varying vec3 vVertexLight;
  29. varying vec4 vScreenPos;
  30. #ifdef ENVCUBEMAP
  31. varying vec3 vReflectionVec;
  32. #endif
  33. #if defined(LIGHTMAP) || defined(AO)
  34. varying vec2 vTexCoord2;
  35. #endif
  36. #endif
  37. void VS()
  38. {
  39. mat4 modelMatrix = iModelMatrix;
  40. vec3 worldPos = GetWorldPos(modelMatrix);
  41. float height = worldPos.y - cModel[3][1];
  42. float windStrength = max(height - cWindHeightPivot, 0.0) * cWindHeightFactor;
  43. float windPeriod = cElapsedTime * cWindPeriod + dot(worldPos.xz, cWindWorldSpacing);
  44. worldPos.x += windStrength * sin(windPeriod);
  45. worldPos.z -= windStrength * cos(windPeriod);
  46. gl_Position = GetClipPos(worldPos);
  47. vNormal = GetWorldNormal(modelMatrix);
  48. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  49. #ifdef NORMALMAP
  50. vec3 tangent = GetWorldTangent(modelMatrix);
  51. vec3 bitangent = cross(tangent, vNormal) * iTangent.w;
  52. vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  53. vTangent = vec4(tangent, bitangent.z);
  54. #else
  55. vTexCoord = GetTexCoord(iTexCoord);
  56. #endif
  57. #ifdef PERPIXEL
  58. // Per-pixel forward lighting
  59. vec4 projWorldPos = vec4(worldPos, 1.0);
  60. #ifdef SHADOW
  61. // Shadow projection: transform from world space to shadow space
  62. for (int i = 0; i < NUMCASCADES; i++)
  63. vShadowPos[i] = GetShadowPos(i, projWorldPos);
  64. #endif
  65. #ifdef SPOTLIGHT
  66. // Spotlight projection: transform from world space to projector texture coordinates
  67. vSpotPos = cLightMatrices[0] * projWorldPos;
  68. #endif
  69. #ifdef POINTLIGHT
  70. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * (worldPos - cLightPos.xyz);
  71. #endif
  72. #else
  73. // Ambient & per-vertex lighting
  74. #if defined(LIGHTMAP) || defined(AO)
  75. // If using lightmap, disregard zone ambient light
  76. // If using AO, calculate ambient in the PS
  77. vVertexLight = vec3(0.0, 0.0, 0.0);
  78. vTexCoord2 = iTexCoord2;
  79. #else
  80. vVertexLight = GetAmbient(GetZonePos(worldPos));
  81. #endif
  82. #ifdef NUMVERTEXLIGHTS
  83. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  84. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  85. #endif
  86. vScreenPos = GetScreenPos(gl_Position);
  87. #ifdef ENVCUBEMAP
  88. vReflectionVec = worldPos - cCameraPos;
  89. #endif
  90. #endif
  91. }