20_HugeObjectCount.lua 7.4 KB

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