05_AnimatingScene.as 7.1 KB

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