AngelScriptIntegration.cpp 7.3 KB

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