ForwardLit.vert 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "Uniforms.vert"
  2. #include "Transform.vert"
  3. varying vec2 vTexCoord;
  4. #ifdef VERTEXCOLOR
  5. varying vec4 vColor;
  6. #endif
  7. varying vec4 vLightVec;
  8. #ifdef SPECULAR
  9. varying vec3 vEyeVec;
  10. #endif
  11. #ifndef NORMALMAP
  12. varying vec3 vNormal;
  13. #endif
  14. #ifdef SHADOW
  15. #if defined(DIRLIGHT)
  16. varying vec4 vShadowPos[4];
  17. #elif defined(SPOTLIGHT)
  18. varying vec4 vShadowPos;
  19. #else
  20. varying vec3 vShadowPos;
  21. #endif
  22. #endif
  23. #ifdef SPOTLIGHT
  24. varying vec4 vSpotPos;
  25. #endif
  26. #ifdef POINTLIGHT
  27. varying vec3 vCubeMaskVec;
  28. #endif
  29. void main()
  30. {
  31. mat4 modelMatrix = iModelMatrix;
  32. vec3 worldPos = GetWorldPos(modelMatrix);
  33. gl_Position = GetClipPos(worldPos);
  34. vTexCoord = GetTexCoord(iTexCoord);
  35. #ifdef VERTEXCOLOR
  36. vColor = iColor;
  37. #endif
  38. #ifdef NORMALMAP
  39. vec3 vNormal;
  40. vec3 vTangent;
  41. vec3 vBitangent;
  42. #endif
  43. vNormal = GetWorldNormal(modelMatrix);
  44. vec4 projWorldPos = vec4(worldPos, 1.0);
  45. #ifdef SHADOW
  46. // Shadow projection: transform from world space to shadow space
  47. #if defined(DIRLIGHT)
  48. vShadowPos[0] = cLightMatrices[0] * projWorldPos;
  49. vShadowPos[1] = cLightMatrices[1] * projWorldPos;
  50. vShadowPos[2] = cLightMatrices[2] * projWorldPos;
  51. vShadowPos[3] = cLightMatrices[3] * projWorldPos;
  52. #elif defined(SPOTLIGHT)
  53. vShadowPos = cLightMatrices[1] * projWorldPos;
  54. #else
  55. vShadowPos = worldPos - cLightPos.xyz;
  56. #endif
  57. #endif
  58. #ifdef SPOTLIGHT
  59. // Spotlight projection: transform from world space to projector texture coordinates
  60. vSpotPos = cLightMatrices[0] * projWorldPos;
  61. #endif
  62. #ifdef POINTLIGHT
  63. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * (cLightPos.xyz - worldPos);
  64. #endif
  65. #ifdef NORMALMAP
  66. vTangent = GetWorldTangent(modelMatrix);
  67. vBitangent = cross(vTangent, vNormal) * iTangent.w;
  68. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  69. #ifdef DIRLIGHT
  70. vLightVec = vec4(cLightDir * tbn, GetDepth(gl_Position));
  71. #else
  72. vLightVec = vec4((cLightPos.xyz - worldPos) * tbn * cLightPos.w, GetDepth(gl_Position));
  73. #endif
  74. #ifdef SPECULAR
  75. vEyeVec = (cCameraPos - worldPos) * tbn;
  76. #endif
  77. #else
  78. #ifdef DIRLIGHT
  79. vLightVec = vec4(cLightDir, GetDepth(gl_Position));
  80. #else
  81. vLightVec = vec4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(gl_Position));
  82. #endif
  83. #ifdef SPECULAR
  84. vEyeVec = cCameraPos - worldPos;
  85. #endif
  86. #endif
  87. }