main.lua 5.7 KB

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