07_Billboards.lua 11 KB

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