20_HugeObjectCount.as 8.9 KB

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