main.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. -- This demo renders a scene with several uses of stencil maps (skip down to comment "Stencils here")
  2. --
  3. -- Sample contributed by andi mcc
  4. local scene = {}
  5. function scene.load()
  6. -- So we can see the effects of the stencils, we want to put some things in our scene.
  7. -- A checkerboard floor:
  8. scene.floorSize = 6
  9. -- A series of sideways-drifting cubes (these will be stenciled)
  10. scene.driftCubeCount = 60
  11. scene.boundMin = lovr.math.newVec3(-10, -1, -10)
  12. scene.boundMax = lovr.math.newVec3( 10, 9, 10)
  13. scene.speed = 1
  14. scene.driftCubeSize = 0.6
  15. scene.driftCubes = {}
  16. for i=1,scene.driftCubeCount do
  17. scene.generateDriftCube(i, true)
  18. end
  19. -- A 3x3 cube made of two different stencil types
  20. scene.stencilCubeCenter = lovr.math.newVec3(0, 1.5, -0.5)
  21. scene.stencilCubeSize = 0.25
  22. scene.stencilCubeRotate = 0
  23. scene.stencilCubeRotateSpeed = 1
  24. scene.stencilCubes = {}
  25. for z=-1,1 do for y=-1,1 do for x=-1,1 do -- Iterate over every cube
  26. if not (x==0 and y==0 and z==0) then -- Except the center
  27. table.insert(scene.stencilCubes, {lovr.math.newVec3(x,y,z), math.random(1,2)}) -- Cube center and stencil type
  28. end
  29. end end end
  30. -- Three cubemap skyboxes, of different colors
  31. scene.skybox = {}
  32. local skyboxTextureSize = 32
  33. local bandSize=3
  34. for cube_index,colors in ipairs{
  35. {{1, 0.5, 1}, {1,1,1}}, -- Fuschia and white
  36. {{1, 1, 0.5}, {0,0,0}}, -- Yellow and black
  37. {{1,1,1}, {0.9,0.9,0.9}}, -- White and silver
  38. } do
  39. local layers = {}
  40. for layer=1,6 do -- 6 layers to a cubemap
  41. local data = lovr.data.newImage(skyboxTextureSize, skyboxTextureSize, "rgba8")
  42. for y=1,skyboxTextureSize do for x=1,skyboxTextureSize do
  43. local isBorder = x==1 or x==skyboxTextureSize or y==1 or y==skyboxTextureSize -- Solid color in corners
  44. local direction = cube_index==3 and -1 or 1 -- Reverse direction on third cubemap
  45. local whichColor = (isBorder or ((x+direction*y-2)%(bandSize*2)>=bandSize)) and 1 or 2 -- Diagonal stripes
  46. data:setPixel(x-1,y-1,unpack(colors[whichColor]))
  47. end end
  48. table.insert(layers, data)
  49. end
  50. table.insert(scene.skybox, lovr.graphics.newTexture(layers))
  51. end
  52. scene.sampler = lovr.graphics.newSampler({filter="nearest"})
  53. end
  54. local function randomQuaternion() -- Generate one random rotation
  55. -- Formula from http://planning.cs.uiuc.edu/node198.html
  56. local u,v,w = math.random(), math.random(), math.random()
  57. return lovr.math.newQuat(
  58. math.sqrt(1-u)*math.sin(2*v*math.pi),
  59. math.sqrt(1-u)*math.cos(2*v*math.pi),
  60. math.sqrt(u)*math.sin(2*w*math.pi),
  61. math.sqrt(u)*math.cos(2*w*math.pi),
  62. true -- Raw components
  63. )
  64. end
  65. function scene.generateDriftCube(i, randomX) -- Generate one cube with random position and color and a random rotational velocity
  66. local cube = {}
  67. cube.at = lovr.math.newVec3()
  68. if randomX then
  69. cube.at.x = scene.boundMin.x + math.random()*(scene.boundMax.x-scene.boundMin.x)
  70. else
  71. cube.at.x = scene.boundMin.x
  72. end
  73. cube.at.y = scene.boundMin.y + math.random()*(scene.boundMax.y-scene.boundMin.y)
  74. cube.at.z = scene.boundMin.z + math.random()*(scene.boundMax.z-scene.boundMin.z)
  75. cube.rotateBasis = randomQuaternion()
  76. cube.rotateTarget = lovr.math.newQuat(cube.rotateBasis:conjugate())
  77. cube.rotate = cube.rotateBasis
  78. scene.driftCubes[i] = cube
  79. end
  80. function scene.update(dt) -- On each frame, move each cube and spin it a little
  81. for i,cube in ipairs(scene.driftCubes) do
  82. cube.at.x = cube.at.x + scene.speed*dt
  83. if cube.at.x > scene.boundMax.x then -- If cube left the scene bounds respawn it
  84. scene.generateDriftCube(i)
  85. else
  86. local rotateAmount = (cube.at.x - scene.boundMin.x)/(scene.boundMax.x-scene.boundMin.x)
  87. cube.rotate = cube.rotateBasis:slerp( cube.rotateTarget, rotateAmount )
  88. end
  89. end
  90. -- Also rotate the center cube
  91. scene.stencilCubeRotate = scene.stencilCubeRotate + dt*scene.stencilCubeRotateSpeed
  92. end
  93. function scene.draw(pass)
  94. -- Drawing without culling can make stencils or transparency look weird. We'll be using both...
  95. pass:setCullMode('back')
  96. -- First, draw the skybox
  97. pass:setSampler(scene.sampler)
  98. pass:skybox(scene.skybox[3])
  99. -- Next, draw a floor
  100. local floorRecenter = scene.floorSize/2 + 0.5
  101. for x=1,scene.floorSize do for y=1,scene.floorSize do
  102. if (x+y)%2==0 then
  103. pass:setColor(0.25,0.25,0.25)
  104. else
  105. pass:setColor(0.35,0.35,0.35)
  106. end
  107. pass:plane(x-floorRecenter,0,y-floorRecenter, 1,1, -math.pi/2,1,0,0) -- Face up
  108. end end
  109. pass:setColor(1,1,1,1)
  110. -- Stencils here
  111. -- Using stencils involves drawing twice, once with a stencil write set and once with a stencil test set.
  112. -- Example 1: Using stencils to "paint" scenes
  113. -- Each sub-cube in our 3x3 cube will write a different value to the stencil buffer, 1 or 2.
  114. pass:setColorWrite() -- In the color spectrum, these cubes are completely invisible! They write only stencil and depth.
  115. pass:push() -- Position ourselves in the right place
  116. pass:translate(scene.stencilCubeCenter)
  117. pass:rotate(scene.stencilCubeRotate, 0,1,0)
  118. for _, cube in ipairs(scene.stencilCubes) do
  119. local center, stencilValue = unpack(cube)
  120. -- Draw to stencil (but only when we pass the depth test)
  121. pass:setStencilWrite({"keep", "keep", "replace"}, stencilValue)
  122. pass:cube(center*scene.stencilCubeSize, scene.stencilCubeSize)
  123. end
  124. pass:pop()
  125. pass:setStencilWrite() -- Reset stencil write
  126. pass:setColorWrite(true) -- Reset color write
  127. -- Now that we've painted the stencil buffer, let's draw something with depth-- like a skybox
  128. pass:setDepthTest() -- Turn off depth test because the skybox is "behind" the cubes
  129. for stencilValue=1,2 do
  130. pass:setStencilTest("equal", stencilValue) -- Commands after here will only draw on pixels where the stencil value is right
  131. pass:skybox(scene.skybox[stencilValue])
  132. end
  133. pass:setDepthTest("gequal") -- Turn depth test back on
  134. -- Example 2: Using stencils to prevent collision
  135. -- Here we will write AND test the stencil at the same time! In this step we want to draw a bunch of 50%-transparent cubes,
  136. -- But we don't want any cubes to overlap each other. We want each cube to look like a "world of shadow".
  137. -- The cubes can darken the skybox and the 3x3 cube, but not any pixel where another cube has already drawn.
  138. pass:setStencilTest("notequal", 3) -- We will write the value "3", but refuse to write any pixel where a 3 is already present.
  139. pass:setStencilWrite("replace", 3) -- Note we haven't cleared the stencil buffer, so we can't reuse values 1 or 2.
  140. for _,cube in ipairs(scene.driftCubes) do
  141. pass:setColor(0.75,0.5,0.5,0.5)
  142. pass:cube(cube.at.x, cube.at.y, cube.at.z, scene.driftCubeSize, cube.rotate:unpack())
  143. end
  144. -- The stencil state will reset at the end of this lovr.draw, but let's clear it anyway.
  145. pass:setStencilWrite()
  146. pass:setStencilTest()
  147. end
  148. -- Handle lovr
  149. function lovr.load()
  150. lovr.graphics.setBackgroundColor(1,0,0) -- Red to show up clearly if something goes wrong
  151. scene.load()
  152. end
  153. function lovr.update(dt)
  154. scene.update(dt)
  155. end
  156. function lovr.draw(pass)
  157. scene.draw(pass)
  158. end