main.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. --[[
  2. Example of basic shadow mapping in LÖVR
  3. Ben Porter, 2022
  4. ]]
  5. local z = -2
  6. local light_pos = lovr.math.newVec3(3.0, 4.0, z)
  7. local light_orthographic = false -- Use orthographic light
  8. local shadow_map_size = 2048
  9. local debug_render_from_light = false -- Enable to render scene from light
  10. local debug_show_shadow_map = false -- Enable to view shadow map in overlap
  11. local shader, render_texture
  12. local shadow_map_texture, shadow_map_sampler
  13. local light_space_matrix
  14. local shadow_map_pass, lighting_pass
  15. local function render_scene(pass)
  16. local t = lovr.timer.getTime()
  17. pass:push()
  18. pass:setColor(0xCBC1AD)
  19. pass:circle(0, 0, z, 5, -math.pi / 2, 1, 0, 0, 'fill', 0, 2 * math.pi, 256)
  20. local count = 2
  21. local min, max = -(count - 1) / 2, (count - 1) / 2
  22. pass:setColor(0xEEEEEE)
  23. pass:translate(0, -min + .55, z)
  24. for i = min, max do
  25. for j = min, max do
  26. for k = min, max do
  27. pass:push()
  28. pass:translate(i, j, k)
  29. pass:rotate(t + i * 0.3 + j * 0.3, 1, 1, 1)
  30. pass:scale(0.45)
  31. if i + j % 2 == 1 then
  32. pass:box()
  33. else
  34. pass:scale(0.7)
  35. pass:sphere()
  36. end
  37. pass:pop()
  38. end
  39. end
  40. end
  41. pass:pop()
  42. end
  43. local function lighting_shader()
  44. local vs = [[
  45. vec4 lovrmain() {
  46. return Projection * View * Transform * VertexPosition;
  47. }
  48. ]]
  49. local fs = [[
  50. uniform vec3 lightPos;
  51. uniform mat4 lightSpaceMatrix;
  52. uniform bool lightOrthographic;
  53. uniform texture2D shadowMapTexture;
  54. vec4 diffuseLighting(vec3 lightDir, vec3 normal, float shadow) {
  55. float diff = max(dot(normal, lightDir), 0.0);
  56. vec4 diffuse = diff * vec4(1.0, 1.0, 0.8, 1.0);
  57. vec4 baseColor = Color * getPixel(ColorTexture, UV);
  58. vec4 ambience = vec4(0.05, 0.05, 0.1, 1.0);
  59. return baseColor * (ambience + (1 - shadow) * diffuse);
  60. }
  61. // Falloff shadow near edge of light bounds/frustum
  62. float shadowFalloff(vec2 uv) {
  63. const float margin = 0.05;
  64. uv = clamp(uv, vec2(0,0), vec2(1,1));
  65. float dx = 1;
  66. if (uv.x < margin) dx = uv.x / margin;
  67. else if (uv.x > 1 - margin) dx = ( 1 - uv.x ) / margin;
  68. float dy = 1;
  69. if (uv.y < margin) dy = uv.y / margin;
  70. else if (uv.y > 1 - margin) dy = ( 1 - uv.y ) / margin;
  71. return dx * dy;
  72. }
  73. vec4 lovrmain() {
  74. vec3 lightDir = normalize(lightPos - PositionWorld);
  75. vec3 normal = normalize(Normal);
  76. vec4 positionLightSpace = lightSpaceMatrix * vec4(PositionWorld, 1);
  77. vec3 positionLightSpaceProj = 0.5 * (positionLightSpace.xyz / positionLightSpace.w) + 0.5;
  78. vec4 shadowMap = getPixel(shadowMapTexture, positionLightSpaceProj.xy);
  79. float closestDepth = shadowMap.r * 0.5 + 0.5;
  80. float currentDepth = positionLightSpaceProj.z;
  81. float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
  82. float falloff = shadowFalloff(positionLightSpaceProj.xy);
  83. float shadow;
  84. if (lightOrthographic) {
  85. shadow = ((currentDepth - bias) >= closestDepth) ? 1.0 : 0.0;
  86. } else {
  87. shadow = ((currentDepth + bias) <= closestDepth) ? 1.0 : 0.0;
  88. }
  89. return diffuseLighting(lightDir, normal, falloff * shadow);
  90. }
  91. ]]
  92. return lovr.graphics.newShader(vs, fs, {})
  93. end
  94. local function render_shadow_map(draw)
  95. local near_plane = 2
  96. local projection
  97. if light_orthographic then
  98. local radius = 3
  99. local far_plane = 15
  100. projection = mat4():orthographic(-radius, radius, -radius, radius, near_plane, far_plane)
  101. else
  102. projection = mat4():perspective(math.pi / 3, 1, near_plane)
  103. end
  104. local view = mat4():lookAt(light_pos, vec3(0, 1, z))
  105. light_space_matrix = mat4(projection):mul(view)
  106. shadow_map_pass:reset()
  107. shadow_map_pass:setProjection(1, projection)
  108. shadow_map_pass:setViewPose(1, view, true)
  109. if light_orthographic then
  110. -- Note for ortho projection with a far plane the depth coord is reversed
  111. shadow_map_pass:setDepthTest('lequal')
  112. end
  113. if debug_render_from_light then
  114. shadow_map_pass:setShader(shader)
  115. shadow_map_pass:send('lightPos', light_pos)
  116. end
  117. draw(shadow_map_pass)
  118. end
  119. local function render_lighting_pass(draw)
  120. lighting_pass:reset()
  121. if lovr.headset then
  122. for i = 1, lovr.headset.getViewCount() do
  123. lighting_pass:setViewPose(i, lovr.headset.getViewPose(i))
  124. lighting_pass:setProjection(i, lovr.headset.getViewAngles(i))
  125. end
  126. else
  127. local t = lovr.timer.getTime()
  128. lighting_pass:setViewPose(1, 0, 3 - math.sin(t * 0.1), 4, -math.pi / 8, 1, 0, 0)
  129. end
  130. lighting_pass:setShader(shader)
  131. lighting_pass:setSampler(shadow_map_sampler)
  132. lighting_pass:send('shadowMapTexture', shadow_map_texture)
  133. lighting_pass:send('lightPos', light_pos)
  134. lighting_pass:send('lightSpaceMatrix', light_space_matrix)
  135. lighting_pass:send('lightOrthographic', light_orthographic)
  136. draw(lighting_pass)
  137. lighting_pass:setShader()
  138. lighting_pass:setColor(1, 1, 1, 1)
  139. lighting_pass:sphere(light_pos, 0.1)
  140. end
  141. local function debug_passes(pass)
  142. pass:setDepthWrite(false)
  143. if debug_render_from_light then
  144. pass:fill(shadow_map_texture)
  145. else
  146. pass:fill(render_texture)
  147. if debug_show_shadow_map then
  148. -- Render shadow map in overlay
  149. local width, height = lovr.system.getWindowDimensions()
  150. pass:setViewport(0, 0, width / 4, height / 4)
  151. pass:fill(shadow_map_texture)
  152. end
  153. end
  154. end
  155. function lovr.load(args)
  156. shader = lighting_shader()
  157. lovr.graphics.setBackgroundColor(0x4782B3)
  158. local shadow_map_format = debug_render_from_light and 'rgba8' or 'd32f'
  159. shadow_map_texture = lovr.graphics.newTexture(shadow_map_size, shadow_map_size, {
  160. format = shadow_map_format,
  161. linear = false,
  162. mipmaps = false
  163. })
  164. shadow_map_sampler = lovr.graphics.newSampler({ wrap = 'clamp' })
  165. if lovr.headset then
  166. local width, height = lovr.headset.getDisplayDimensions()
  167. local layers = lovr.headset.getViewCount()
  168. render_texture = lovr.graphics.newTexture(width, height, layers, { mipmaps = false })
  169. else
  170. local width, height = lovr.system.getWindowDimensions()
  171. render_texture = lovr.graphics.newTexture(width, height, 1, { mipmaps = false })
  172. end
  173. if debug_render_from_light then
  174. shadow_map_pass = lovr.graphics.newPass({ shadow_map_texture, samples = 1 })
  175. else
  176. shadow_map_pass = lovr.graphics.newPass({ depth = shadow_map_texture, samples = 1 })
  177. shadow_map_pass:setClear({ depth = light_orthographic and 1 or 0 })
  178. end
  179. lighting_pass = lovr.graphics.newPass(render_texture)
  180. end
  181. function lovr.update(dt)
  182. local t = lovr.timer.getTime()
  183. light_pos.x = 3 * math.cos(t * 0.4)
  184. light_pos.z = 3 * math.sin(t * 0.4) + z
  185. end
  186. function lovr.draw(pass)
  187. render_shadow_map(render_scene)
  188. render_lighting_pass(render_scene)
  189. debug_passes(pass)
  190. return lovr.graphics.submit({ shadow_map_pass, lighting_pass, pass })
  191. end