Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wind_pixel_lighting.vert 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. varying vec2 texCoord;
  2. varying vec3 vNormal;
  3. varying vec3 eyePos;
  4. uniform float time;
  5. uniform float strength;
  6. uniform float speed;
  7. uniform float deforming;
  8. uniform float bending;
  9. uniform float ground;
  10. uniform vec2 direction;
  11. uniform mat4 ModelMatrix;
  12. uniform mat4 ViewMatrix;
  13. // ########################################Settings########################################
  14. vec2 windDirection = direction; // Direction of the wind
  15. float windStrength = strength; // Strength of the wind
  16. float groundLevel = ground; // Everything over ground level will have movement
  17. const int windSamples = 3; // Number of wind sine waves to add
  18. // ########################################Functions########################################
  19. vec2 getWind(vec4 worldPos, float height)
  20. // Function to generate the wind
  21. {
  22. vec2 wind;
  23. if (height > groundLevel)
  24. {
  25. float windTime = time*speed;
  26. float windDisplacement = cos(0.375*((17.5+worldPos.x)+(17.5+worldPos.y))+(windTime*1.25));
  27. for (int w = 0; w < windSamples; w++)
  28. {
  29. float rAnd = float(w)+1.0-(float(windSamples)/2.0);
  30. float rCnd = float(w)-1.0+(float(windSamples)/2.0);
  31. windDisplacement += sin(0.5*((17.5+rAnd+worldPos.x)+(rAnd+worldPos.y))+(windTime*(rAnd*0.1+1.75)));
  32. windDisplacement -= cos(0.5*((17.5+rCnd+worldPos.x)+(rAnd+worldPos.y))+(windTime*(rCnd*0.1+1.75)));
  33. }
  34. wind = windStrength*(height-0.25)*sin((worldPos.xy*normalize(windDirection))+vec2(windTime*0.5))*windDisplacement;
  35. wind *= ((height - groundLevel) * bending + 1.0 - bending);
  36. }
  37. else
  38. {
  39. wind = vec2(0.0,0.0);
  40. }
  41. return wind;
  42. }
  43. mat4 addWorldPos(mat4 modelMat, vec3 pos)
  44. {
  45. modelMat[3][0] += pos.x;
  46. modelMat[3][1] += pos.y;
  47. modelMat[3][2] += pos.z;
  48. return modelMat;
  49. }
  50. // ########################################Main-Function########################################
  51. void main()
  52. {
  53. vec4 verts = gl_Vertex;
  54. vec4 worldSpacePos = ModelMatrix * (verts*deforming+1.0-deforming);
  55. vec2 wind = getWind(worldSpacePos, verts.z);
  56. mat4 modelViewMatix = ViewMatrix * addWorldPos(ModelMatrix, vec3(wind, 0.0));
  57. texCoord = gl_MultiTexCoord0.xy;
  58. vNormal = gl_NormalMatrix * gl_Normal;
  59. eyePos = (modelViewMatix * verts).xyz;
  60. gl_Position = gl_ProjectionMatrix * modelViewMatix * verts;
  61. }