textured.vert 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #define LIGHTING
  2. // Attributes
  3. attribute vec4 a_position; // Vertex position (x, y, z, w)
  4. attribute vec3 a_normal; // Vertex normal (x, y, z)
  5. attribute vec2 a_texCoord; // Vertex texture coordinate (u, v)
  6. #if defined(SKINNING)
  7. attribute vec4 a_blendWeights; // Vertex blend weight, up to 4 (0, 1, 2, 3)
  8. attribute vec4 a_blendIndices; // Vertex blend index int u_matrixPalette (0, 1, 2, 3)
  9. #endif
  10. // Uniforms
  11. uniform mat4 u_worldViewProjectionMatrix; // Matrix to transform a position to clip space
  12. uniform mat4 u_inverseTransposeWorldViewMatrix; // Matrix to transform a normal to view space
  13. #if defined(SPECULAR) || defined(SPOT_LIGHT) || defined(POINT_LIGHT)
  14. uniform mat4 u_worldViewMatrix; // Matrix to tranform a position to view space
  15. #endif
  16. #if defined(SKINNING)
  17. uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3]; // Array of 4x3 matrices
  18. #endif
  19. #if defined(SPECULAR)
  20. uniform vec3 u_cameraPosition; // Position of the camera in view space
  21. #endif
  22. #if defined(TEXTURE_REPEAT)
  23. uniform vec2 u_textureRepeat; // Texture repeat for tiling
  24. #endif
  25. #if defined(TEXTURE_OFFSET)
  26. uniform vec2 u_textureOffset; // Texture offset
  27. #endif
  28. #if defined(POINT_LIGHT)
  29. uniform vec3 u_pointLightPosition; // Position of light
  30. uniform float u_pointLightRangeInverse; // Inverse of light range
  31. #elif defined(SPOT_LIGHT)
  32. uniform vec3 u_spotLightPosition; // Position of light
  33. uniform float u_spotLightRangeInverse; // Inverse of light range
  34. uniform vec3 u_spotLightDirection; // Direction of a spot light source
  35. #else
  36. #endif
  37. // Varyings
  38. varying vec3 v_normalVector; // Normal vector in view space
  39. varying vec2 v_texCoord; // Texture coordinate
  40. #if defined(SPECULAR)
  41. varying vec3 v_cameraDirection; // Direction the camera is looking at in tangent space
  42. #endif
  43. #if defined(POINT_LIGHT)
  44. varying vec3 v_vertexToPointLightDirection; // Direction of point light w.r.t current vertex in tangent space
  45. varying float v_pointLightAttenuation; // Attenuation of point light
  46. #include "lighting-point.vert"
  47. #elif defined(SPOT_LIGHT)
  48. varying vec3 v_vertexToSpotLightDirection; // Direction of the spot light w.r.t current vertex in tangent space
  49. varying float v_spotLightAttenuation; // Attenuation of spot light
  50. // Lighting
  51. #include "lighting-spot.vert"
  52. #else
  53. #include "lighting-directional.vert"
  54. #endif
  55. // Skinning
  56. #if defined(SKINNING)
  57. #include "skinning.vert"
  58. #else
  59. #include "skinning-none.vert"
  60. #endif
  61. void main()
  62. {
  63. // Get the position and normal
  64. vec4 position = getPosition();
  65. vec3 normal = getNormal();
  66. // Transform position to clip space.
  67. gl_Position = u_worldViewProjectionMatrix * position;
  68. // Transform normal to view space.
  69. mat3 normalMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
  70. v_normalVector = normalMatrix * normal;
  71. // Apply light.
  72. applyLight(position);
  73. // Texture transformation
  74. v_texCoord = a_texCoord;
  75. #if defined(TEXTURE_REPEAT)
  76. v_texCoord *= u_textureRepeat;
  77. #endif
  78. #if defined(TEXTURE_OFFSET)
  79. v_texCoord += u_textureOffset;
  80. #endif
  81. }