20_HugeObjectCount.lua 8.7 KB

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