Skybox.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Graphics/Batch.h"
  6. #include "../Graphics/Camera.h"
  7. #include "../Graphics/Skybox.h"
  8. #include "../Scene/Node.h"
  9. #include "../DebugNew.h"
  10. namespace Urho3D
  11. {
  12. extern const char* GEOMETRY_CATEGORY;
  13. Skybox::Skybox(Context* context) :
  14. StaticModel(context),
  15. lastFrame_(0)
  16. {
  17. }
  18. Skybox::~Skybox() = default;
  19. void Skybox::RegisterObject(Context* context)
  20. {
  21. context->RegisterFactory<Skybox>(GEOMETRY_CATEGORY);
  22. URHO3D_COPY_BASE_ATTRIBUTES(StaticModel);
  23. }
  24. void Skybox::ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results)
  25. {
  26. // Do not record a raycast result for a skybox, as it would block all other results
  27. }
  28. void Skybox::UpdateBatches(const FrameInfo& frame)
  29. {
  30. distance_ = 0.0f;
  31. if (frame.frameNumber_ != lastFrame_)
  32. {
  33. customWorldTransforms_.Clear();
  34. lastFrame_ = frame.frameNumber_;
  35. }
  36. // Add camera position to fix the skybox in space. Use effective world transform to take reflection into account
  37. Matrix3x4 customWorldTransform = node_->GetWorldTransform();
  38. customWorldTransform.SetTranslation(node_->GetWorldPosition() + frame.camera_->GetEffectiveWorldTransform().Translation());
  39. HashMap<Camera*, Matrix3x4>::Iterator it = customWorldTransforms_.Insert(MakePair(frame.camera_, customWorldTransform));
  40. for (SourceBatch& batch : batches_)
  41. {
  42. batch.worldTransform_ = &it->second_;
  43. batch.distance_ = 0.0f;
  44. }
  45. }
  46. void Skybox::OnWorldBoundingBoxUpdate()
  47. {
  48. // The skybox is supposed to be visible everywhere, so set a humongous bounding box
  49. worldBoundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  50. }
  51. }