Brak opisu

instancing.vert 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. // Vertex attributes
  5. layout (location = 0) in vec3 inPos;
  6. layout (location = 1) in vec3 inNormal;
  7. layout (location = 2) in vec2 inUV;
  8. layout (location = 3) in vec3 inColor;
  9. // Instanced attributes
  10. layout (location = 4) in vec3 instancePos;
  11. layout (location = 5) in vec3 instanceRot;
  12. layout (location = 6) in float instanceScale;
  13. layout (location = 7) in int instanceTexIndex;
  14. layout (binding = 0) uniform UBO
  15. {
  16. mat4 projection;
  17. mat4 modelview;
  18. vec4 lightPos;
  19. float locSpeed;
  20. float globSpeed;
  21. } ubo;
  22. layout (location = 0) out vec3 outNormal;
  23. layout (location = 1) out vec3 outColor;
  24. layout (location = 2) out vec3 outUV;
  25. layout (location = 3) out vec3 outViewVec;
  26. layout (location = 4) out vec3 outLightVec;
  27. void main()
  28. {
  29. outColor = inColor;
  30. outUV = vec3(inUV, instanceTexIndex);
  31. mat3 mx, my, mz;
  32. // rotate around x
  33. float s = sin(instanceRot.x + ubo.locSpeed);
  34. float c = cos(instanceRot.x + ubo.locSpeed);
  35. mx[0] = vec3(c, s, 0.0);
  36. mx[1] = vec3(-s, c, 0.0);
  37. mx[2] = vec3(0.0, 0.0, 1.0);
  38. // rotate around y
  39. s = sin(instanceRot.y + ubo.locSpeed);
  40. c = cos(instanceRot.y + ubo.locSpeed);
  41. my[0] = vec3(c, 0.0, s);
  42. my[1] = vec3(0.0, 1.0, 0.0);
  43. my[2] = vec3(-s, 0.0, c);
  44. // rot around z
  45. s = sin(instanceRot.z + ubo.locSpeed);
  46. c = cos(instanceRot.z + ubo.locSpeed);
  47. mz[0] = vec3(1.0, 0.0, 0.0);
  48. mz[1] = vec3(0.0, c, s);
  49. mz[2] = vec3(0.0, -s, c);
  50. mat3 rotMat = mz * my * mx;
  51. mat4 gRotMat;
  52. s = sin(instanceRot.y + ubo.globSpeed);
  53. c = cos(instanceRot.y + ubo.globSpeed);
  54. gRotMat[0] = vec4(c, 0.0, s, 0.0);
  55. gRotMat[1] = vec4(0.0, 1.0, 0.0, 0.0);
  56. gRotMat[2] = vec4(-s, 0.0, c, 0.0);
  57. gRotMat[3] = vec4(0.0, 0.0, 0.0, 1.0);
  58. vec4 locPos = vec4(inPos.xyz * rotMat, 1.0);
  59. vec4 pos = vec4((locPos.xyz * instanceScale) + instancePos, 1.0);
  60. gl_Position = ubo.projection * ubo.modelview * gRotMat * pos;
  61. outNormal = mat3(ubo.modelview * gRotMat) * inverse(rotMat) * inNormal;
  62. pos = ubo.modelview * vec4(inPos.xyz + instancePos, 1.0);
  63. vec3 lPos = mat3(ubo.modelview) * ubo.lightPos.xyz;
  64. outLightVec = lPos - pos.xyz;
  65. outViewVec = -pos.xyz;
  66. }