getDisplayMask.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. return {
  2. tag = 'headset',
  3. summary = 'Get a mesh that masks out the visible display area.',
  4. description = [[
  5. Returns a list of 2D triangle vertices that represents areas of the headset display that will
  6. never be seen by the user (due to the circular nature of the lenses). This area can be masked
  7. out by rendering it to the depth buffer or stencil buffer. Then, Further drawing operations can
  8. skip rendering to those pixels using the depth test (`lovr.graphics.setDepthTest`) or stencil
  9. test (`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 position(mat4 projection, mat4 transform, vec4 vertex) {
  24. // Rescale mesh coordinates from (0,1) to (-1,1)
  25. vertex.xy *= 2.;
  26. vertex.xy -= 1.;
  27. // Flip the mesh if it's being drawn in the right eye
  28. if (lovrViewID == 1) {
  29. vertex.x = -vertex.x;
  30. }
  31. return vertex;
  32. }
  33. ]], [[
  34. // The fragment shader returns solid black for illustration purposes. It could be transparent.
  35. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  36. return vec4(0., 0., 0., 1.);
  37. }
  38. ]])
  39. mask = lovr.headset.getDisplayMask()
  40. if mask then
  41. mesh = lovr.graphics.newMesh({ { 'lovrPosition', 'float', 2 } }, mask, 'triangles')
  42. end
  43. end
  44. function lovr.draw()
  45. if mask then
  46. -- Mask out parts of the display that aren't visible to skip rendering those pixels later
  47. lovr.graphics.setShader(shader)
  48. mesh:draw()
  49. lovr.graphics.setShader()
  50. -- Draw a red cube
  51. lovr.graphics.setColor(0xff0000)
  52. lovr.graphics.cube('fill', 0, 1.7, -1, .5, lovr.timer.getTime())
  53. lovr.graphics.setColor(0xffffff)
  54. else
  55. lovr.graphics.setColor(0x000000)
  56. lovr.graphics.print('No mask found.', 0, 1.7, -3, .2)
  57. lovr.graphics.setColor(0xffffff)
  58. end
  59. end
  60. ]=],
  61. related = {
  62. 'lovr.graphics.newMesh',
  63. 'lovr.graphics.setDepthTest',
  64. 'lovr.graphics.setStencilTest'
  65. }
  66. }