07_Billboards.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. -- Billboard example.
  2. -- This sample demonstrates:
  3. -- - Populating a 3D scene with billboard sets and several shadow casting spotlights
  4. -- - Parenting scene nodes to allow more intuitive creation of groups of objects
  5. -- - Examining rendering performance with a somewhat large object and light count
  6. require "LuaScripts/Utilities/Sample"
  7. local scene_ = nil
  8. local cameraNode = nil
  9. local lightNodes = {}
  10. local billboardNodes = {}
  11. local yaw = 0.0
  12. local pitch = 0.0
  13. local drawDebug = false
  14. local context = GetContext()
  15. local cache = GetCache()
  16. local input = GetInput()
  17. local renderer = GetRenderer()
  18. local ui = GetUI()
  19. function Start()
  20. -- Execute the common startup for samples
  21. SampleStart()
  22. -- Create the scene content
  23. CreateScene()
  24. -- Create the UI content
  25. CreateInstructions()
  26. -- Setup the viewport for displaying the scene
  27. SetupViewport()
  28. -- Hook up to the frame update and render post-update events
  29. SubscribeToEvents()
  30. end
  31. function CreateScene()
  32. scene_ = Scene(context)
  33. -- Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  34. -- Also create a DebugRenderer component so that we can draw debug geometry
  35. scene_:CreateComponent("Octree")
  36. scene_:CreateComponent("DebugRenderer")
  37. -- Create a Zone component for ambient lighting & fog control
  38. local zoneNode = scene_:CreateChild("Zone")
  39. local zone = zoneNode:CreateComponent("Zone")
  40. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  41. zone.ambientColor = Color(0.1, 0.1, 0.1)
  42. zone.fogStart = 100.0
  43. zone.fogEnd = 300.0
  44. -- Create a directional light without shadows
  45. local lightNode = scene_:CreateChild("DirectionalLight")
  46. lightNode.direction = Vector3(0.5, -1.0, 0.5)
  47. local light = lightNode:CreateComponent("Light")
  48. light.lightType = LIGHT_DIRECTIONAL
  49. light.color = Color(0.2, 0.2, 0.2)
  50. light.specularIntensity = 1.0
  51. -- Create a "floor" consisting of several tiles
  52. for y = -5, 5 do
  53. for x = -5, 5 do
  54. local floorNode = scene_:CreateChild("FloorTile")
  55. floorNode.position = Vector3(x * 20.5, -0.5, y * 20.5)
  56. floorNode.scale = Vector3(20.0, 1.0, 20.0)
  57. local floorObject = floorNode:CreateComponent("StaticModel")
  58. floorObject.model = cache:GetResource("Model", "Models/Box.mdl")
  59. floorObject.material = cache:GetResource("Material", "Materials/Stone.xml")
  60. end
  61. end
  62. -- Create groups of mushrooms, which act as shadow casters
  63. local NUM_MUSHROOMGROUPS = 25
  64. local NUM_MUSHROOMS = 25
  65. for i = 1, NUM_MUSHROOMGROUPS do
  66. -- First create a scene node for the group. The individual mushrooms nodes will be created as children
  67. local groupNode = scene_:CreateChild("MushroomGroup")
  68. groupNode.position = Vector3(Random(190.0) - 95.0, 0.0, Random(190.0) - 95.0)
  69. for j = 1, NUM_MUSHROOMS do
  70. local mushroomNode = groupNode:CreateChild("Mushroom")
  71. mushroomNode.position = Vector3(Random(25.0) - 12.5, 0.0, Random(25.0) - 12.5)
  72. mushroomNode.rotation = Quaternion(0.0, Random() * 360.0, 0.0)
  73. mushroomNode:SetScale(1.0 + Random() * 4.0)
  74. local mushroomObject = mushroomNode:CreateComponent("StaticModel")
  75. mushroomObject.model = cache:GetResource("Model", "Models/Mushroom.mdl")
  76. mushroomObject.material = cache:GetResource("Material", "Materials/Mushroom.xml")
  77. mushroomObject.castShadows = true
  78. end
  79. end
  80. -- Create billboard sets (floating smoke)
  81. local NUM_BILLBOARDNODES = 25
  82. local NUM_BILLBOARDS = 10
  83. for i = 1, NUM_BILLBOARDNODES do
  84. local smokeNode = scene_:CreateChild("Smoke")
  85. smokeNode.position = Vector3(Random(200.0) - 100.0, Random(20.0) + 10.0, Random(200.0) - 100.0)
  86. local billboardObject = smokeNode:CreateComponent("BillboardSet")
  87. billboardObject.numBillboards = NUM_BILLBOARDS
  88. billboardObject.material = cache:GetResource("Material", "Materials/LitSmoke.xml")
  89. billboardObject.sorted = true
  90. for j = 1, NUM_BILLBOARDS do
  91. local bb = billboardObject:GetBillboard(j - 1)
  92. bb.position = Vector3(Random(12.0) - 6.0, Random(8.0) - 4.0, Random(12.0) - 6.0)
  93. bb.size = Vector2(Random(2.0) + 3.0, Random(2.0) + 3.0)
  94. bb.rotation = Random() * 360.0
  95. bb.enabled = true
  96. end
  97. -- After modifying the billboards, they need to be "commited" so that the BillboardSet updates its internals
  98. billboardObject:Commit()
  99. table.insert(billboardNodes, smokeNode)
  100. end
  101. -- Create shadow casting spotlights
  102. local NUM_LIGHTS = 9
  103. for i = 0, NUM_LIGHTS - 1 do
  104. local lightNode = scene_:CreateChild("SpotLight")
  105. local light = lightNode:CreateComponent("Light")
  106. local angle = 0.0
  107. local position = Vector3((i % 3) * 60.0 - 60.0, 45.0, math.floor(i / 3) * 60.0 - 60.0)
  108. local color = Color(((i + 1) % 2) * 0.5 + 0.5, (math.floor((i + 1) / 2) % 2) * 0.5 + 0.5, (math.floor((i + 1) / 4) % 2) * 0.5 + 0.5)
  109. lightNode.position = position
  110. lightNode.direction = Vector3(math.sin(M_DEGTORAD * angle), -1.5, math.cos(M_DEGTORAD * angle))
  111. light.lightType = LIGHT_SPOT
  112. light.range = 90.0
  113. light.rampTexture = cache:GetResource("Texture2D", "Textures/RampExtreme.png")
  114. light.fov = 45.0
  115. light.color = color
  116. light.specularIntensity = 1.0
  117. light.castShadows = true
  118. light.shadowBias = BiasParameters(0.00002, 0.0)
  119. -- Configure shadow fading for the lights. When they are far away enough, the lights eventually become unshadowed for
  120. -- better GPU performance. Note that we could also set the maximum distance for each object to cast shadows
  121. light.shadowFadeDistance = 100.0 -- Fade start distance
  122. light.shadowDistance = 125.0 -- Fade end distance, shadows are disabled
  123. -- Set half resolution for the shadow maps for increased performance
  124. light.shadowResolution = 0.5
  125. -- The spot lights will not have anything near them, so move the near plane of the shadow camera farther
  126. -- for better shadow depth resolution
  127. light.shadowNearFarRatio = 0.01
  128. table.insert(lightNodes, lightNode)
  129. end
  130. -- Create the camera. Limit far clip distance to match the fog
  131. cameraNode = scene_:CreateChild("Camera")
  132. local camera = cameraNode:CreateComponent("Camera")
  133. camera.farClip = 300.0
  134. -- Set an initial position for the camera scene node above the plane
  135. cameraNode.position = Vector3(0.0, 5.0, 0.0)
  136. end
  137. function CreateInstructions()
  138. -- Construct new Text object, set string to display and font to use
  139. local instructionText = ui.root:CreateChild("Text")
  140. instructionText.text =
  141. "Use WASD keys and mouse to move\n"..
  142. "Space to toggle debug geometry"
  143. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  144. -- The text has multiple rows. Center them in relation to each other
  145. instructionText.textAlignment = HA_CENTER
  146. -- Position the text relative to the screen center
  147. instructionText.horizontalAlignment = HA_CENTER
  148. instructionText.verticalAlignment = VA_CENTER
  149. instructionText:SetPosition(0, ui.root.height / 4)
  150. end
  151. function SetupViewport()
  152. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  153. local viewport = Viewport:new(context, scene_, cameraNode:GetComponent("Camera"))
  154. renderer:SetViewport(0, viewport)
  155. end
  156. function SubscribeToEvents()
  157. -- Subscribe HandleUpdate() function for processing update events
  158. SubscribeToEvent("Update", "HandleUpdate")
  159. -- Subscribe HandlePostRenderUpdate() function for processing the post-render update event, during which we request
  160. -- debug geometry
  161. SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate")
  162. end
  163. function MoveCamera(timeStep)
  164. -- Do not move if the UI has a focused element (the console)
  165. if ui.focusElement ~= nil then
  166. return
  167. end
  168. -- Movement speed as world units per second
  169. local MOVE_SPEED = 20.0
  170. -- Mouse sensitivity as degrees per pixel
  171. local MOUSE_SENSITIVITY = 0.1
  172. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  173. local mouseMove = input.mouseMove
  174. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  175. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  176. pitch = Clamp(pitch, -90.0, 90.0)
  177. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  178. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  179. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  180. if input:GetKeyDown(KEY_W) then
  181. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  182. end
  183. if input:GetKeyDown(KEY_S) then
  184. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  185. end
  186. if input:GetKeyDown(KEY_A) then
  187. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  188. end
  189. if input:GetKeyDown(KEY_D) then
  190. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  191. end
  192. -- Toggle debug geometry with space
  193. if input:GetKeyPress(KEY_SPACE) then
  194. drawDebug = not drawDebug
  195. end
  196. end
  197. function AnimateScene(timeStep)
  198. local LIGHT_ROTATION_SPEED = 20.0
  199. local BILLBOARD_ROTATION_SPEED = 50.0
  200. -- Rotate the lights around the world Y-axis
  201. for i, v in ipairs(lightNodes) do
  202. v:Rotate(Quaternion(0.0, LIGHT_ROTATION_SPEED * timeStep, 0.0), true)
  203. end
  204. -- Rotate the individual billboards within the billboard sets, then recommit to make the changes visible
  205. for i, v in ipairs(billboardNodes) do
  206. local billboardObject = v:GetComponent("BillboardSet")
  207. for j = 1, billboardObject.numBillboards do
  208. local bb = billboardObject:GetBillboard(j - 1)
  209. bb.rotation = bb.rotation + BILLBOARD_ROTATION_SPEED * timeStep
  210. end
  211. billboardObject:Commit()
  212. end
  213. end
  214. function HandleUpdate(eventType, eventData)
  215. -- Take the frame time step, which is stored as a float
  216. local timeStep = eventData:GetFloat("TimeStep")
  217. -- Move the camera and animate the scene, scale movement with time step
  218. MoveCamera(timeStep)
  219. AnimateScene(timeStep)
  220. end
  221. function HandlePostRenderUpdate(eventType, eventData)
  222. -- If draw debug mode is enabled, draw viewport debug geometry. This time use depth test, as otherwise the result becomes
  223. -- hard to interpret due to large object count
  224. if drawDebug then
  225. renderer:DrawDebugGeometry(true)
  226. end
  227. end