Vegetation.glsl 3.6 KB

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