main.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. scale = .75,
  56. aspect = textureHeight / textureWidth,
  57. material = lovr.graphics.newMaterial( texture )
  58. }
  59. -- Configure the shader
  60. if useCanvas then
  61. local width, height = lovr.headset.getDisplayDimensions()
  62. -- Compile the shader
  63. screenShader = lovr.graphics.newShader(screenShaderVertex, screenShaderFragment)
  64. -- Set the resolution uniform
  65. screenShader:send("resolution", {width, height})
  66. -- Create two temporary canvases
  67. tempCanvas = {
  68. lovr.graphics.newCanvas(width, height),
  69. lovr.graphics.newCanvas(width, height)
  70. }
  71. tempCanvas[1]:getTexture():setWrap('clamp')
  72. tempCanvas[2]:getTexture():setWrap('clamp')
  73. end
  74. end
  75. -- The scene is drawn in this callback
  76. local function sceneDraw()
  77. lovr.graphics.clear() -- Because we are drawing to a canvas, we must manually clear
  78. lovr.graphics.setShader(nil)
  79. -- Draw text on the left and right
  80. for _, sign in ipairs {-1, 1} do
  81. lovr.graphics.push()
  82. lovr.graphics.rotate(sign * math.pi/2, 0, 1, 0)
  83. lovr.graphics.print("MOVE CLOSER", 0, 0, -10, 5)
  84. lovr.graphics.pop()
  85. end
  86. lovr.graphics.plane(eyechart.material, 0, 1.7, -1, eyechart.scale, eyechart.scale * eyechart.aspect)
  87. end
  88. -- This simple callback is used to draw one canvas onto another
  89. local function fullScreenDraw(source)
  90. lovr.graphics.fill(source)
  91. end
  92. function lovr.draw()
  93. if not useCanvas then
  94. -- No-postprocessing path: Call scene-draw callback without doing anything fancy
  95. sceneDraw()
  96. else
  97. -- Start by drawing the scene to one of our temp canvases.
  98. tempCanvas[1]:renderTo(sceneDraw)
  99. -- We now have the scene in a texture (a canvas), which means we can apply a full-screen effect
  100. -- by rendering the texture with a shader material. However, because our blur is separable,
  101. -- we will need to do this twice, once for horizontal blur and once for vertical.
  102. -- We would also like to do multiple blur passes at larger and larger scales, to get a blurrier blur.
  103. -- To achieve these many passes we will render from canvas A into B, and then B back into A, and repeat.
  104. lovr.graphics.setShader(screenShader)
  105. screenShader:send("direction", {1, 0})
  106. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  107. screenShader:send("direction", {0, 1})
  108. tempCanvas[1]:renderTo(fullScreenDraw, tempCanvas[2])
  109. screenShader:send("direction", {2, 0})
  110. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  111. screenShader:send("direction", {0, 2})
  112. tempCanvas[1]:renderTo(fullScreenDraw, tempCanvas[2])
  113. screenShader:send("direction", {4, 0})
  114. tempCanvas[2]:renderTo(fullScreenDraw, tempCanvas[1])
  115. screenShader:send("direction", {0, 4})
  116. lovr.graphics.fill(tempCanvas[2]) -- On the final pass, render directly to the screen.
  117. end
  118. end