init.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. return {
  2. summary = 'An offscreen render target.',
  3. description = [[
  4. A Canvas is also known as a framebuffer or render-to-texture. It allows you to render to a
  5. texture instead of directly to the screen. This lets you postprocess or transform the results
  6. later before finally rendering them to the screen.
  7. After creating a Canvas, you can attach Textures to it using `Canvas:setTexture`.
  8. ]],
  9. constructors = {
  10. 'lovr.graphics.newCanvas'
  11. },
  12. notes = [[
  13. Up to four textures can be attached to a Canvas and anything rendered to the Canvas will be
  14. broadcast to all attached Textures. If you want to do render different things to different
  15. textures, use the `multicanvas` shader flag when creating your shader and implement the `void
  16. colors` function instead of the usual `vec4 color` function. You can then assign different
  17. output colors to `lovrCanvas[0]`, `lovrCanvas[1]`, etc. instead of returning a single color.
  18. Each color written to the array will end up in the corresponding texture attached to the Canvas.
  19. ]],
  20. example = {
  21. description = 'Apply a postprocessing effect (wave) using a Canvas and a fragment shader.',
  22. code = [=[
  23. function lovr.load()
  24. lovr.graphics.setBackgroundColor(.1, .1, .1)
  25. canvas = lovr.graphics.newCanvas(lovr.headset.getDisplayDimensions())
  26. wave = lovr.graphics.newShader([[
  27. vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
  28. return vertex;
  29. }
  30. ]], [[
  31. uniform float time;
  32. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  33. uv.x += sin(uv.y * 10 + time * 4) * .01;
  34. uv.y += cos(uv.x * 10 + time * 4) * .01;
  35. return graphicsColor * lovrDiffuseColor * vertexColor * texture(image, uv);
  36. }
  37. ]])
  38. end
  39. function lovr.update(dt)
  40. wave:send('time', lovr.timer.getTime())
  41. end
  42. function lovr.draw()
  43. -- Render the scene to the canvas instead of the headset.
  44. canvas:renderTo(function()
  45. lovr.graphics.clear()
  46. local size = 5
  47. for i = 1, size do
  48. for j = 1, size do
  49. for k = 1, size do
  50. lovr.graphics.setColor(i / size, j / size, k / size)
  51. local x, y, z = i - size / 2, j - size / 2, k - size / 2
  52. lovr.graphics.cube('fill', x, y, z, .5)
  53. end
  54. end
  55. end
  56. end)
  57. -- Render the canvas to the headset using a shader.
  58. lovr.graphics.setColor(1, 1, 1)
  59. lovr.graphics.setShader(wave)
  60. lovr.graphics.fill(canvas:getTexture())
  61. lovr.graphics.setShader()
  62. end
  63. ]=]
  64. }
  65. }