05_AnimatingScene.as 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Animating 3D scene example.
  2. // This sample demonstrates:
  3. // - Creating a 3D scene and using a script component to animate the objects
  4. // - Controlling scene ambience with the Zone component
  5. // - Attaching a light to an object (the camera)
  6. #include "Scripts/Utilities/Sample.as"
  7. Scene@ scene_;
  8. Node@ cameraNode;
  9. float yaw = 0.0f;
  10. float pitch = 0.0f;
  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. scene_ = Scene();
  27. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  28. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  29. scene_.CreateComponent("Octree");
  30. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  31. // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
  32. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  33. Node@ zoneNode = scene_.CreateChild("Zone");
  34. Zone@ zone = zoneNode.CreateComponent("Zone");
  35. // Set same volume as the Octree, set a close bluish fog and some ambient light
  36. zone.boundingBox = BoundingBox(-1000.0f, 1000.0f);
  37. zone.ambientColor = Color(0.05f, 0.1f, 0.15f);
  38. zone.fogColor = Color(0.1f, 0.2f, 0.3f);
  39. zone.fogStart = 10.0f;
  40. zone.fogEnd = 100.0f;
  41. // Create randomly positioned and oriented box StaticModels in the scene
  42. const uint NUM_OBJECTS = 2000;
  43. for (uint i = 0; i < NUM_OBJECTS; ++i)
  44. {
  45. Node@ boxNode = scene_.CreateChild("Box");
  46. boxNode.position = Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f);
  47. // Orient using random pitch, yaw and roll Euler angles
  48. boxNode.rotation = Quaternion(Random(360.0f), Random(360.0f), Random(360.0f));
  49. StaticModel@ boxObject = boxNode.CreateComponent("StaticModel");
  50. boxObject.model = cache.GetResource("Model", "Models/Box.mdl");
  51. boxObject.material = cache.GetResource("Material", "Materials/Stone.xml");
  52. // Add the Rotator script object which will rotate the scene node each frame, when the scene sends its update event.
  53. // This requires the C++ component ScriptInstance in the scene node, which acts as a container. We need to tell the
  54. // script file and class name to instantiate the object (scriptFile is a global property which refers to the currently
  55. // executing script file.) There is also a shortcut for creating the ScriptInstance component and the script object,
  56. // which is shown in a later sample, but this is what happens "under the hood."
  57. ScriptInstance@ instance = boxNode.CreateComponent("ScriptInstance");
  58. instance.CreateObject(scriptFile, "Rotator");
  59. // Retrieve the created script object and set its rotation speed member variable
  60. Rotator@ rotator = cast<Rotator>(instance.object);
  61. rotator.rotationSpeed = Vector3(10.0f, 20.0f, 30.0f);
  62. }
  63. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  64. // bring the far clip plane closer for more effective culling of distant objects
  65. cameraNode = scene_.CreateChild("Camera");
  66. Camera@ camera = cameraNode.CreateComponent("Camera");
  67. camera.farClip = 100.0f;
  68. // Create a point light to the camera scene node
  69. Light@ light = cameraNode.CreateComponent("Light");
  70. light.lightType = LIGHT_POINT;
  71. light.range = 30.0f;
  72. }
  73. void CreateInstructions()
  74. {
  75. // Construct new Text object, set string to display and font to use
  76. Text@ instructionText = ui.root.CreateChild("Text");
  77. instructionText.text = "Use WASD keys and mouse to move";
  78. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  79. // Position the text relative to the screen center
  80. instructionText.horizontalAlignment = HA_CENTER;
  81. instructionText.verticalAlignment = VA_CENTER;
  82. instructionText.SetPosition(0, ui.root.height / 4);
  83. }
  84. void SetupViewport()
  85. {
  86. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  87. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  88. // use, but now we just use full screen and default render path configured in the engine command line options
  89. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  90. renderer.viewports[0] = viewport;
  91. }
  92. void MoveCamera(float timeStep)
  93. {
  94. // Do not move if the UI has a focused element (the console)
  95. if (ui.focusElement !is null)
  96. return;
  97. // Movement speed as world units per second
  98. const float MOVE_SPEED = 20.0f;
  99. // Mouse sensitivity as degrees per pixel
  100. const float MOUSE_SENSITIVITY = 0.1f;
  101. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  102. IntVector2 mouseMove = input.mouseMove;
  103. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  104. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  105. pitch = Clamp(pitch, -90.0f, 90.0f);
  106. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  107. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  108. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  109. if (input.keyDown['W'])
  110. cameraNode.TranslateRelative(Vector3(0.0f, 0.0f, 1.0f) * MOVE_SPEED * timeStep);
  111. if (input.keyDown['S'])
  112. cameraNode.TranslateRelative(Vector3(0.0f, 0.0f, -1.0f) * MOVE_SPEED * timeStep);
  113. if (input.keyDown['A'])
  114. cameraNode.TranslateRelative(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  115. if (input.keyDown['D'])
  116. cameraNode.TranslateRelative(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  117. }
  118. void SubscribeToEvents()
  119. {
  120. // Subscribe HandleUpdate() function for processing update events
  121. SubscribeToEvent("Update", "HandleUpdate");
  122. }
  123. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  124. {
  125. // Take the frame time step, which is stored as a float
  126. float timeStep = eventData["TimeStep"].GetFloat();
  127. // Move the camera, scale movement with time step
  128. MoveCamera(timeStep);
  129. }
  130. // Rotator script object class. Script objects to be added to a scene node must implement the empty ScriptObject interface
  131. class Rotator : ScriptObject
  132. {
  133. Vector3 rotationSpeed;
  134. // Update is called during the variable timestep scene update
  135. void Update(float timeStep)
  136. {
  137. node.Rotate(Quaternion(rotationSpeed.x * timeStep, rotationSpeed.y * timeStep, rotationSpeed.z * timeStep));
  138. }
  139. }