05_AnimatingScene.as 7.1 KB

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