20_HugeObjectCount.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. -- Huge object count example.
  2. -- This sample demonstrates:
  3. -- - Creating a scene with 250 x 250 simple objects
  4. -- - Competing with http://yosoygames.com.ar/wp/2013/07/ogre-2-0-is-up-to-3x-faster/ :)
  5. -- - Allowing examination of performance hotspots in the rendering code
  6. -- - Optionally speeding up rendering by grouping objects with the StaticModelGroup component
  7. require "LuaScripts/Utilities/Sample"
  8. local scene_ = nil
  9. local cameraNode = nil
  10. local boxNodes = {}
  11. local yaw = 0.0
  12. local pitch = 0.0
  13. local animate = false
  14. local useGroups = false
  15. local context = GetContext()
  16. local cache = GetCache()
  17. local input = GetInput()
  18. local renderer = GetRenderer()
  19. local ui = GetUI()
  20. function Start()
  21. -- Execute the common startup for samples
  22. SampleStart()
  23. -- Create the scene content
  24. CreateScene()
  25. -- Create the UI content
  26. CreateInstructions()
  27. -- Setup the viewport for displaying the scene
  28. SetupViewport()
  29. -- Hook up to the frame update events
  30. SubscribeToEvents()
  31. end
  32. function CreateScene()
  33. if scene_ == nil then
  34. scene_ = Scene(context)
  35. else
  36. scene_:Clear()
  37. boxNodes = {}
  38. end
  39. -- Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  40. -- (-1000, -1000, -1000) to (1000, 1000, 1000)
  41. scene_:CreateComponent("Octree")
  42. -- Create a Zone for ambient light & fog control
  43. local zoneNode = scene_:CreateChild("Zone")
  44. local zone = zoneNode:CreateComponent("Zone")
  45. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  46. zone.fogColor = Color(0.2, 0.2, 0.2)
  47. zone.fogStart = 200.0
  48. zone.fogEnd = 300.0
  49. -- Create a directional light
  50. local lightNode = scene_:CreateChild("DirectionalLight")
  51. lightNode.direction = Vector3(-0.6, -1.0, -0.8) -- The direction vector does not need to be normalized
  52. local light = lightNode:CreateComponent("Light")
  53. light.lightType = LIGHT_DIRECTIONAL
  54. if not useGroups then
  55. light.color = Color(0.7, 0.35, 0.0)
  56. -- Create individual box StaticModels in the scene
  57. for y = -125, 125 do
  58. for x = -125, 125 do
  59. local boxNode = scene_:CreateChild("Box")
  60. boxNode.position = Vector3(x * 0.3, 0.0, y * 0.3)
  61. boxNode:SetScale(0.25)
  62. local boxObject = boxNode:CreateComponent("StaticModel")
  63. boxObject.model = cache:GetResource("Model", "Models/Box.mdl")
  64. table.insert(boxNodes, boxNode)
  65. end
  66. end
  67. else
  68. light.color = Color(0.6, 0.6, 0.6);
  69. light.specularIntensity = 1.5;
  70. -- Create StaticModelGroups in the scene
  71. local lastGroup = nil
  72. for y = -125, 125 do
  73. for x = -125, 125 do
  74. -- Create new group if no group yet, or the group has already "enough" objects. The tradeoff is between culling
  75. -- accuracy and the amount of CPU processing needed for all the objects. Note that the group's own transform
  76. -- does not matter, and it does not render anything if instance nodes are not added to it
  77. if lastGroup == nil or lastGroup.numInstanceNodes >= 25 * 25 then
  78. local boxGroupNode = scene_:CreateChild("BoxGroup")
  79. lastGroup = boxGroupNode:CreateComponent("StaticModelGroup")
  80. lastGroup.model = cache:GetResource("Model", "Models/Box.mdl")
  81. end
  82. local boxNode = scene_:CreateChild("Box");
  83. boxNode.position = Vector3(x * 0.3, 0.0, y * 0.3)
  84. boxNode:SetScale(0.25)
  85. table.insert(boxNodes, boxNode)
  86. lastGroup:AddInstanceNode(boxNode);
  87. end
  88. end
  89. end
  90. -- Create the camera. Create it outside the scene so that we can clear the whole scene without affecting it
  91. if cameraNode == nil then
  92. cameraNode = Node(context)
  93. cameraNode.position = Vector3(0.0, 10.0, -100.0)
  94. local camera = cameraNode:CreateComponent("Camera")
  95. camera.farClip = 300.0
  96. end
  97. end
  98. function CreateInstructions()
  99. -- Construct new Text object, set string to display and font to use
  100. local instructionText = ui.root:CreateChild("Text")
  101. instructionText:SetText("Use WASD keys and mouse to move\n"..
  102. "Space to toggle animation\n"..
  103. "G to toggle object group optimization")
  104. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  105. -- The text has multiple rows. Center them in relation to each other
  106. instructionText.textAlignment = HA_CENTER
  107. -- Position the text relative to the screen center
  108. instructionText.horizontalAlignment = HA_CENTER
  109. instructionText.verticalAlignment = VA_CENTER
  110. instructionText:SetPosition(0, ui.root.height / 4)
  111. end
  112. function SetupViewport()
  113. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  114. local viewport = Viewport:new(context, scene_, cameraNode:GetComponent("Camera"))
  115. renderer:SetViewport(0, viewport)
  116. end
  117. function SubscribeToEvents()
  118. -- Subscribe HandleUpdate() function for processing update events
  119. SubscribeToEvent("Update", "HandleUpdate")
  120. end
  121. function MoveCamera(timeStep)
  122. -- Do not move if the UI has a focused element (the console)
  123. if ui.focusElement ~= nil then
  124. return
  125. end
  126. -- Movement speed as world units per second
  127. local MOVE_SPEED = 20.0
  128. -- Mouse sensitivity as degrees per pixel
  129. local MOUSE_SENSITIVITY = 0.1
  130. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  131. local mouseMove = input.mouseMove
  132. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  133. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  134. pitch = Clamp(pitch, -90.0, 90.0)
  135. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  136. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  137. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  138. if input:GetKeyDown(KEY_W) then
  139. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  140. end
  141. if input:GetKeyDown(KEY_S) then
  142. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  143. end
  144. if input:GetKeyDown(KEY_A) then
  145. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  146. end
  147. if input:GetKeyDown(KEY_D) then
  148. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  149. end
  150. end
  151. function AnimateObjects(timeStep)
  152. local ROTATE_SPEED = 15.0
  153. local delta = ROTATE_SPEED * timeStep
  154. local rotateQuat = Quaternion(delta, Vector3(0.0, 0.0, 1.0))
  155. for i, v in ipairs(boxNodes) do
  156. v:Rotate(rotateQuat)
  157. end
  158. end
  159. function HandleUpdate(eventType, eventData)
  160. -- Take the frame time step, which is stored as a float
  161. local timeStep = eventData:GetFloat("TimeStep")
  162. -- Toggle animation with space
  163. if input:GetKeyPress(KEY_SPACE) then
  164. animate = not animate
  165. end
  166. -- Toggle grouped / ungrouped mode
  167. if input:GetKeyPress(KEY_G) then
  168. useGroups = not useGroups
  169. CreateScene()
  170. end
  171. -- Move the camera, scale movement with time step
  172. MoveCamera(timeStep)
  173. -- Animate scene if enabled
  174. if animate then
  175. AnimateObjects(timeStep)
  176. end
  177. end