20_HugeObjectCount.as 8.9 KB

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