07_Billboards.lua 11 KB

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