20_HugeObjectCount.lua 8.8 KB

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