main.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. -- This demo renders a scene to a canvas, then renders the canvas to screen filtered through a shader.
  2. -- Set this to false to see the scene with no postprocessing.
  3. local useCanvas = true
  4. -- A shader program consists of a vertex shader (which describes how to transform polygons)
  5. -- and a fragment shader (which describes how to color pixels).
  6. -- For a full-screen shader, the vertex shader should just pass the polygon through unaltered.
  7. -- This is the same as the "default" full-screen shader used by lovr.graphics.plane:
  8. local screenShaderVertex = [[
  9. vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
  10. return vertex;
  11. }
  12. ]]
  13. -- For the fragment shader: We are going to create a separable gaussian blur.
  14. -- A "separable" blur means we first blur horizontally, then blur vertically to get a 2D blur.
  15. local screenShaderFragment = [[
  16. // This one-dimensional blur filter samples five points and averages them by different amounts.
  17. // Weights and offsets taken from http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
  18. // The weights for the center, one-point-out, and two-point-out samples
  19. #define WEIGHT0 0.2270270270
  20. #define WEIGHT1 0.3162162162
  21. #define WEIGHT2 0.0702702703
  22. // The distances-from-center for the samples
  23. #define OFFSET1 1.3846153846
  24. #define OFFSET2 3.2307692308
  25. // UVs are sampled from a texture over the range 0..1.
  26. // This uniform is set outside the shader so we know what UV distance "one pixel" is.
  27. uniform vec2 resolution;
  28. // This uniform will be set every draw to determine whether we are sampling horizontally or vertically.
  29. uniform vec2 direction;
  30. // lovr's shader architecture will automatically supply a main(), which will call this color() function
  31. vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
  32. vec2 pixelOff = direction / resolution;
  33. vec4 color = vec4(0.0);
  34. color += texture(image, uv) * WEIGHT0;
  35. color += texture(image, uv + pixelOff * OFFSET1) * WEIGHT1;
  36. color += texture(image, uv - pixelOff * OFFSET1) * WEIGHT1;
  37. color += texture(image, uv + pixelOff * OFFSET2) * WEIGHT2;
  38. color += texture(image, uv - pixelOff * OFFSET2) * WEIGHT2;
  39. return color;
  40. }
  41. ]]
  42. -- The vertex and fragment shaders will be combined together into a shader program
  43. local screenShader
  44. -- Image of an eyechart
  45. local eyechart
  46. -- This table will contain two canvases we will use as scratch space
  47. local tempCanvas
  48. function lovr.load()
  49. -- Load the eyechart image
  50. -- Source: https://www.publicdomainpictures.net/view-image.php?image=244244&picture=eye-chart-test-vintage
  51. -- Creative Commons 0 / Public Domain license
  52. local texture = lovr.graphics.newTexture('eye-chart-test-vintage-cc0.jpg')
  53. local textureWidth, textureHeight = texture:getDimensions()
  54. eyechart = {
  55. aspect = textureHeight / textureWidth,
  56. material = lovr.graphics.newMaterial( texture )
  57. }
  58. -- Configure the shader
  59. if useCanvas then
  60. local width, height = lovr.headset.getDisplayDimensions()
  61. -- Compile the shader
  62. screenShader = lovr.graphics.newShader(screenShaderVertex, screenShaderFragment)
  63. -- Set the resolution uniform
  64. screenShader:send("resolution", {width, height})
  65. -- Create two temporary canvases
  66. tempCanvas = {
  67. lovr.graphics.newCanvas(width, height),
  68. lovr.graphics.newCanvas(width, height)
  69. }
  70. tempCanvas[1]:setWrap('clamp')
  71. tempCanvas[2]:setWrap('clamp')
  72. end
  73. end
  74. -- The scene is drawn in this callback
  75. local function sceneDraw()
  76. lovr.graphics.clear() -- Because we are drawing to a canvas, we must manually clear
  77. lovr.graphics.setShader(nil)
  78. -- Draw text on the left and right
  79. for _, sign in ipairs {-1, 1} do
  80. lovr.graphics.push()
  81. lovr.graphics.rotate(sign * math.pi/2, 0, 1, 0)
  82. lovr.graphics.print("MOVE CLOSER", 0, 0, -10, 5)
  83. lovr.graphics.pop()
  84. end
  85. -- Because the eyechart image is non-square, we must stretch its plane
  86. lovr.graphics.push()
  87. lovr.graphics.scale(1, eyechart.aspect, 1)
  88. lovr.graphics.translate(0, 0.5, -0.75)
  89. lovr.graphics.plane(eyechart.material, 0, 0, 0)
  90. lovr.graphics.pop()
  91. end
  92. -- This simple callback is used to draw one canvas onto another
  93. local function fullScreenDraw(source)
  94. lovr.graphics.fill(source)
  95. end
  96. function lovr.draw()
  97. if not useCanvas then
  98. -- No-postprocessing path: Call scene-draw callback without doing anything fancy
  99. sceneDraw()
  100. else
  101. -- Start by drawing the scene to one of our temp canvases.
  102. tempCanvas[1]:renderTo(sceneDraw)
  103. -- We now have the scene in a texture (a canvas), which means we can apply a full-screen effect
  104. -- by rendering the texture with a shader material. However, because our blur is separable,
  105. -- we will need to do this twice, once for horizontal blur and once for vertical.
  106. -- We would also like to do multiple blur passes at larger and larger scales, to get a blurrier blur.
  107. -- To achieve these many passes we will render from canvas A into B, and then B back into A, and repeat.
  108. lovr.graphics.setShader(screenShader)
  109. screenShader:send("direction", {1, 0})
  110. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  111. screenShader:send("direction", {0, 1})
  112. tempCanvas[1]:renderTo(fullScreenDraw, tempCanvas[2])
  113. screenShader:send("direction", {2, 0})
  114. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  115. screenShader:send("direction", {0, 2})
  116. tempCanvas[1]:renderTo(fullScreenDraw, tempCanvas[2])
  117. screenShader:send("direction", {4, 0})
  118. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  119. screenShader:send("direction", {0, 4})
  120. lovr.graphics.fill(tempCanvas[2]) -- On the final pass, render directly to the screen.
  121. end
  122. end