LuaIntegration.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Camera.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7. #include <Urho3D/Graphics/Material.h>
  8. #include <Urho3D/Graphics/Model.h>
  9. #include <Urho3D/Graphics/Octree.h>
  10. #include <Urho3D/Graphics/Renderer.h>
  11. #include <Urho3D/Graphics/StaticModel.h>
  12. #include <Urho3D/Graphics/Zone.h>
  13. #include <Urho3D/Input/Input.h>
  14. #include <Urho3D/LuaScript/LuaFile.h>
  15. #include <Urho3D/LuaScript/LuaFunction.h>
  16. #include <Urho3D/LuaScript/LuaScript.h>
  17. #include <Urho3D/LuaScript/LuaScriptInstance.h>
  18. #include <Urho3D/Resource/ResourceCache.h>
  19. #include <Urho3D/Scene/Scene.h>
  20. #include <Urho3D/UI/Font.h>
  21. #include <Urho3D/UI/Text.h>
  22. #include <Urho3D/UI/UI.h>
  23. #include "LuaIntegration.h"
  24. #include <Urho3D/DebugNew.h>
  25. URHO3D_DEFINE_APPLICATION_MAIN(LuaIntegration)
  26. LuaIntegration::LuaIntegration(Context* context) :
  27. Sample(context)
  28. {
  29. // Instantiate and register the Lua script subsystem so that we can use the LuaScriptInstance component
  30. context_->RegisterSubsystem(new LuaScript(context_));
  31. }
  32. void LuaIntegration::Start()
  33. {
  34. // Execute base class startup
  35. Sample::Start();
  36. // Create the scene content
  37. CreateScene();
  38. // Create the UI content
  39. CreateInstructions();
  40. // Setup the viewport for displaying the scene
  41. SetupViewport();
  42. // Hook up to the frame update events
  43. SubscribeToEvents();
  44. // Set the mouse mode to use in the sample
  45. Sample::InitMouseMode(MM_RELATIVE);
  46. }
  47. void LuaIntegration::CreateScene()
  48. {
  49. auto* cache = GetSubsystem<ResourceCache>();
  50. scene_ = new Scene(context_);
  51. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  52. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  53. scene_->CreateComponent<Octree>();
  54. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  55. // 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
  56. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  57. Node* zoneNode = scene_->CreateChild("Zone");
  58. auto* zone = zoneNode->CreateComponent<Zone>();
  59. // Set same volume as the Octree, set a close bluish fog and some ambient light
  60. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  61. zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
  62. zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
  63. zone->SetFogStart(10.0f);
  64. zone->SetFogEnd(100.0f);
  65. auto* scriptFile = cache->GetResource<LuaFile>("LuaScripts/Utilities/Rotator.lua");
  66. if (!scriptFile)
  67. return;
  68. // Create randomly positioned and oriented box StaticModels in the scene
  69. const unsigned NUM_OBJECTS = 2000;
  70. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  71. {
  72. Node* boxNode = scene_->CreateChild("Box");
  73. boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
  74. // Orient using random pitch, yaw and roll Euler angles
  75. boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
  76. auto* boxObject = boxNode->CreateComponent<StaticModel>();
  77. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  78. boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  79. // Add our custom Rotator script object (using the LuaScriptInstance C++ component to instantiate / store it) which will
  80. // rotate the scene node each frame, when the scene sends its update event
  81. auto* instance = boxNode->CreateComponent<LuaScriptInstance>();
  82. instance->CreateObject(scriptFile, "Rotator");
  83. // Call the script object's "SetRotationSpeed" function.
  84. LuaFunction* function = instance->GetScriptObjectFunction("SetRotationSpeed");
  85. if (function && function->BeginCall(instance))
  86. {
  87. function->PushUserType(Vector3(10.0f, 20.0f, 30.0f), "Vector3");
  88. function->EndCall();
  89. }
  90. }
  91. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  92. // bring the far clip plane closer for more effective culling of distant objects
  93. cameraNode_ = scene_->CreateChild("Camera");
  94. auto* camera = cameraNode_->CreateComponent<Camera>();
  95. camera->SetFarClip(100.0f);
  96. // Create a point light to the camera scene node
  97. auto* light = cameraNode_->CreateComponent<Light>();
  98. light->SetLightType(LIGHT_POINT);
  99. light->SetRange(30.0f);
  100. }
  101. void LuaIntegration::CreateInstructions()
  102. {
  103. auto* cache = GetSubsystem<ResourceCache>();
  104. auto* ui = GetSubsystem<UI>();
  105. // Construct new Text object, set string to display and font to use
  106. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  107. instructionText->SetText("Use WASD keys and mouse/touch to move");
  108. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  109. // Position the text relative to the screen center
  110. instructionText->SetHorizontalAlignment(HA_CENTER);
  111. instructionText->SetVerticalAlignment(VA_CENTER);
  112. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  113. }
  114. void LuaIntegration::SetupViewport()
  115. {
  116. auto* renderer = GetSubsystem<Renderer>();
  117. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  118. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  119. renderer->SetViewport(0, viewport);
  120. }
  121. void LuaIntegration::SubscribeToEvents()
  122. {
  123. // Subscribe HandleUpdate() function for processing update events
  124. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(LuaIntegration, HandleUpdate));
  125. }
  126. void LuaIntegration::MoveCamera(float timeStep)
  127. {
  128. // Do not move if the UI has a focused element (the console)
  129. if (GetSubsystem<UI>()->GetFocusElement())
  130. return;
  131. auto* input = GetSubsystem<Input>();
  132. // Movement speed as world units per second
  133. const float MOVE_SPEED = 20.0f;
  134. // Mouse sensitivity as degrees per pixel
  135. const float MOUSE_SENSITIVITY = 0.1f;
  136. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  137. IntVector2 mouseMove = input->GetMouseMove();
  138. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  139. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  140. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  141. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  142. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  143. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  144. if (input->GetKeyDown(KEY_W))
  145. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  146. if (input->GetKeyDown(KEY_S))
  147. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  148. if (input->GetKeyDown(KEY_A))
  149. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  150. if (input->GetKeyDown(KEY_D))
  151. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  152. }
  153. void LuaIntegration::HandleUpdate(StringHash eventType, VariantMap& eventData)
  154. {
  155. using namespace Update;
  156. // Take the frame time step, which is stored as a float
  157. float timeStep = eventData[P_TIMESTEP].GetFloat();
  158. // Move the camera, scale movement with time step
  159. MoveCamera(timeStep);
  160. }