View3D.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Graphics/Camera.h"
  6. #include "../Graphics/Graphics.h"
  7. #include "../Graphics/GraphicsEvents.h"
  8. #include "../Graphics/Octree.h"
  9. #include "../Graphics/Zone.h"
  10. #include "../GraphicsAPI/Texture2D.h"
  11. #include "../Scene/Scene.h"
  12. #include "../UI/UI.h"
  13. #include "../UI/UIEvents.h"
  14. #include "../UI/View3D.h"
  15. namespace Urho3D
  16. {
  17. extern const char* UI_CATEGORY;
  18. View3D::View3D(Context* context) :
  19. Window(context),
  20. ownScene_(true),
  21. rttFormat_(Graphics::GetRGBFormat()),
  22. autoUpdate_(true)
  23. {
  24. renderTexture_ = new Texture2D(context_);
  25. depthTexture_ = new Texture2D(context_);
  26. viewport_ = new Viewport(context_);
  27. // Disable mipmaps since the texel ratio should be 1:1
  28. renderTexture_->SetNumLevels(1);
  29. depthTexture_->SetNumLevels(1);
  30. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(View3D, HandleRenderSurfaceUpdate));
  31. }
  32. View3D::~View3D()
  33. {
  34. ResetScene();
  35. }
  36. void View3D::RegisterObject(Context* context)
  37. {
  38. context->RegisterFactory<View3D>(UI_CATEGORY);
  39. URHO3D_COPY_BASE_ATTRIBUTES(Window);
  40. // The texture format is API specific, so do not register it as a serializable attribute
  41. URHO3D_ACCESSOR_ATTRIBUTE("Auto Update", GetAutoUpdate, SetAutoUpdate, true, AM_FILE);
  42. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Clip Children", true);
  43. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  44. }
  45. void View3D::OnResize(const IntVector2& newSize, const IntVector2& delta)
  46. {
  47. int width = newSize.x_;
  48. int height = newSize.y_;
  49. if (width > 0 && height > 0)
  50. {
  51. renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
  52. depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  53. RenderSurface* surface = renderTexture_->GetRenderSurface();
  54. surface->SetViewport(0, viewport_);
  55. surface->SetUpdateMode(SURFACE_MANUALUPDATE);
  56. surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface());
  57. SetTexture(renderTexture_);
  58. SetImageRect(IntRect(0, 0, width, height));
  59. if (!autoUpdate_)
  60. surface->QueueUpdate();
  61. }
  62. }
  63. void View3D::SetView(Scene* scene, Camera* camera, bool ownScene)
  64. {
  65. ResetScene();
  66. scene_ = scene;
  67. cameraNode_ = camera ? camera->GetNode() : nullptr;
  68. ownScene_ = ownScene;
  69. viewport_->SetScene(scene_);
  70. viewport_->SetCamera(camera);
  71. QueueUpdate();
  72. }
  73. void View3D::SetFormat(unsigned format)
  74. {
  75. if (format != rttFormat_)
  76. {
  77. rttFormat_ = format;
  78. OnResize(GetSize(), IntVector2::ZERO);
  79. }
  80. }
  81. void View3D::SetAutoUpdate(bool enable)
  82. {
  83. autoUpdate_ = enable;
  84. }
  85. void View3D::QueueUpdate()
  86. {
  87. RenderSurface* surface = renderTexture_->GetRenderSurface();
  88. if (surface)
  89. surface->QueueUpdate();
  90. }
  91. Scene* View3D::GetScene() const
  92. {
  93. return scene_;
  94. }
  95. Node* View3D::GetCameraNode() const
  96. {
  97. return cameraNode_;
  98. }
  99. Texture2D* View3D::GetRenderTexture() const
  100. {
  101. return renderTexture_;
  102. }
  103. Texture2D* View3D::GetDepthTexture() const
  104. {
  105. return depthTexture_;
  106. }
  107. Viewport* View3D::GetViewport() const
  108. {
  109. return viewport_;
  110. }
  111. void View3D::ResetScene()
  112. {
  113. if (!scene_)
  114. return;
  115. if (!ownScene_)
  116. {
  117. RefCount* refCount = scene_->RefCountPtr();
  118. ++refCount->refs_;
  119. scene_ = nullptr;
  120. --refCount->refs_;
  121. }
  122. else
  123. scene_ = nullptr;
  124. }
  125. void View3D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  126. {
  127. if (autoUpdate_ && IsVisibleEffective())
  128. QueueUpdate();
  129. }
  130. }