init.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. return {
  2. summary = 'A drawable list of vertices.',
  3. description = [[
  4. A Mesh is a low-level graphics object that stores and renders a list of vertices.
  5. Meshes are really flexible since you can pack pretty much whatever you want in them. This makes
  6. them great for rendering arbitrary geometry, but it also makes them kinda difficult to use since
  7. you have to place each vertex yourself.
  8. It's possible to batch geometry with Meshes too. Instead of drawing a shape 100 times, it's
  9. much faster to pack 100 copies of the shape into a Mesh and draw the Mesh once. Even storing
  10. just one copy in the Mesh and drawing that 100 times is usually faster.
  11. Meshes are also a good choice if you have an object that changes its shape over time.
  12. ]],
  13. constructor = 'lovr.graphics.newMesh',
  14. notes = [[
  15. Each vertex in a Mesh can hold several pieces of data. For example, you might want a vertex to
  16. keep track of its position, color, and a weight. Each one of these pieces of information is
  17. called a vertex **attribute**. A vertex attribute must have a name, a type, and a size. Here's
  18. what the "position" attribute would look like as a Lua table:
  19. { 'vPosition', 'float', 3 } -- 3 floats, one for x, y, and z
  20. Every vertex in a Mesh must have the same set of attributes. We call this set of attributes the
  21. **format** of the Mesh, and it's specified as a simple table of attributes. For example, we
  22. could represent the format described above as:
  23. {
  24. { 'vPosition', 'float', 3 },
  25. { 'vColor', 'byte', 4 },
  26. { 'vWeight', 'int', 1 }
  27. }
  28. When creating a Mesh, you can give it any format you want, or use the default. The default Mesh
  29. format looks like this:
  30. {
  31. { 'lovrPosition', 'float', 3 },
  32. { 'lovrNormal', 'float', 3 },
  33. { 'lovrTexCoord', 'float', 2 }
  34. }
  35. Great, so why do we go through the trouble of naming everything in our vertex and saying what
  36. type and size it is? The cool part is that we can access this data in a Shader. We can write a
  37. vertex Shader that has `in` variables for every vertex attribute in our Mesh:
  38. in vec3 vPosition;
  39. in vec4 vColor;
  40. in int vWeight;
  41. vec4 lovrMain() {
  42. // Here we can access the vPosition, vColor, and vWeight of each vertex in the Mesh!
  43. }
  44. Specifying custom vertex data is really powerful and is often used for lighting, animation, and
  45. more!
  46. For more on the different data types available for the attributes, see `AttributeType`.
  47. ]],
  48. example = {
  49. description = 'Draw a circle using a Mesh.',
  50. code = [[
  51. function lovr.load()
  52. local x, y, z = 0, 1, -2
  53. local radius = .3
  54. local points = 40
  55. -- A table to hold the Mesh data
  56. local vertices = {}
  57. for i = 0, points do
  58. local angle = i / points * 2 * math.pi
  59. local vx = x + math.cos(angle)
  60. local vy = y + math.sin(angle)
  61. table.insert(vertices, { vx, vy, z })
  62. end
  63. mesh = lovr.graphics.newMesh(vertices, 'fan')
  64. end
  65. function lovr.draw()
  66. mesh:draw()
  67. end
  68. ]]
  69. }
  70. }