getDisplayMask.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. return {
  2. tag = 'headset',
  3. summary = 'Get a mesh that masks out the visible display area.',
  4. description = [[
  5. Returns 2D triangle vertices that represent areas of the headset display that will never be seen
  6. by the user (due to the circular lenses). This area can be masked out by rendering it to the
  7. depth buffer or stencil buffer. Then, further drawing operations can skip rendering those
  8. pixels using the depth test (`lovr.graphics.setDepthTest`) or stencil test
  9. (`lovr.graphics.setStencilTest`), which improves performance.
  10. ]],
  11. arguments = {},
  12. returns = {
  13. {
  14. name = 'points',
  15. type = 'table',
  16. description = 'A table of points. Each point is a table with two numbers between 0 and 1.'
  17. }
  18. },
  19. example = [=[
  20. function lovr.load()
  21. lovr.graphics.setBackgroundColor(1, 1, 1)
  22. shader = lovr.graphics.newShader([[
  23. vec4 lovrMain() {
  24. vec4 vertex = lovrVertex;
  25. // Rescale mesh coordinates from (0,1) to (-1,1)
  26. vertex.xy *= 2.;
  27. vertex.xy -= 1.;
  28. // Flip the mesh if it's being drawn in the right eye
  29. if (lovrViewID == 1) {
  30. vertex.x = -vertex.x;
  31. }
  32. return vertex;
  33. }
  34. ]], [[
  35. // The fragment shader returns solid black for illustration purposes. It could be transparent.
  36. vec4 lovrMain() {
  37. return vec4(0., 0., 0., 1.);
  38. }
  39. ]])
  40. mask = lovr.headset.getDisplayMask()
  41. if mask then
  42. mesh = lovr.graphics.newMesh({ { 'lovrPosition', 'float', 2 } }, mask, 'triangles')
  43. end
  44. end
  45. function lovr.draw()
  46. if mask then
  47. -- Mask out parts of the display that aren't visible to skip rendering those pixels later
  48. lovr.graphics.setShader(shader)
  49. mesh:draw()
  50. lovr.graphics.setShader()
  51. -- Draw a red cube
  52. lovr.graphics.setColor(0xff0000)
  53. lovr.graphics.cube('fill', 0, 1.7, -1, .5, lovr.timer.getTime())
  54. lovr.graphics.setColor(0xffffff)
  55. else
  56. lovr.graphics.setColor(0x000000)
  57. lovr.graphics.print('No mask found.', 0, 1.7, -3, .2)
  58. lovr.graphics.setColor(0xffffff)
  59. end
  60. end
  61. ]=],
  62. related = {
  63. 'lovr.graphics.newMesh',
  64. 'lovr.graphics.setDepthTest',
  65. 'lovr.graphics.setStencilTest'
  66. }
  67. }