Vegetation.glsl 3.4 KB

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