20_HugeObjectCount.as 7.5 KB

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