SplinePath.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../IO/Log.h"
  6. #include "../Scene/Scene.h"
  7. #include "../Scene/SplinePath.h"
  8. namespace Urho3D
  9. {
  10. extern const char* interpolationModeNames[];
  11. extern const char* LOGIC_CATEGORY;
  12. static const StringVector controlPointsStructureElementNames =
  13. {
  14. "Control Point Count",
  15. " NodeID"
  16. };
  17. SplinePath::SplinePath(Context* context) :
  18. Component(context),
  19. spline_(BEZIER_CURVE),
  20. speed_(1.f),
  21. elapsedTime_(0.f),
  22. traveled_(0.f),
  23. length_(0.f),
  24. dirty_(false),
  25. controlledIdAttr_(0)
  26. {
  27. UpdateNodeIds();
  28. }
  29. void SplinePath::RegisterObject(Context* context)
  30. {
  31. context->RegisterFactory<SplinePath>(LOGIC_CATEGORY);
  32. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Interpolation Mode", GetInterpolationMode, SetInterpolationMode,
  33. interpolationModeNames, BEZIER_CURVE, AM_FILE);
  34. URHO3D_ATTRIBUTE("Speed", speed_, 1.f, AM_FILE);
  35. URHO3D_ATTRIBUTE("Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
  36. URHO3D_ATTRIBUTE("Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
  37. URHO3D_ACCESSOR_ATTRIBUTE("Controlled", GetControlledIdAttr, SetControlledIdAttr, 0, AM_FILE | AM_NODEID);
  38. URHO3D_ACCESSOR_ATTRIBUTE("Control Points", GetControlPointIdsAttr, SetControlPointIdsAttr,
  39. Variant::emptyVariantVector, AM_FILE | AM_NODEIDVECTOR)
  40. .SetMetadata(AttributeMetadata::P_VECTOR_STRUCT_ELEMENTS, controlPointsStructureElementNames);
  41. }
  42. void SplinePath::ApplyAttributes()
  43. {
  44. if (!dirty_)
  45. return;
  46. // Remove all old instance nodes before searching for new. Can not call RemoveAllInstances() as that would modify
  47. // the ID list on its own
  48. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  49. {
  50. Node* node = controlPoints_[i];
  51. if (node)
  52. node->RemoveListener(this);
  53. }
  54. controlPoints_.Clear();
  55. spline_.Clear();
  56. Scene* scene = GetScene();
  57. if (scene)
  58. {
  59. // The first index stores the number of IDs redundantly. This is for editing
  60. for (unsigned i = 1; i < controlPointIdsAttr_.Size(); ++i)
  61. {
  62. Node* node = scene->GetNode(controlPointIdsAttr_[i].GetU32());
  63. if (node)
  64. {
  65. WeakPtr<Node> controlPoint(node);
  66. node->AddListener(this);
  67. controlPoints_.Push(controlPoint);
  68. spline_.AddKnot(node->GetWorldPosition());
  69. }
  70. }
  71. Node* node = scene->GetNode(controlledIdAttr_);
  72. if (node)
  73. {
  74. WeakPtr<Node> controlled(node);
  75. controlledNode_ = controlled;
  76. }
  77. }
  78. CalculateLength();
  79. dirty_ = false;
  80. }
  81. void SplinePath::DrawDebugGeometry(DebugRenderer* debug, bool /*depthTest*/)
  82. {
  83. if (debug && node_ && IsEnabledEffective())
  84. {
  85. if (spline_.GetKnots().Size() > 1)
  86. {
  87. Vector3 a = spline_.GetPoint(0.f).GetVector3();
  88. for (auto i = 1; i <= 100; ++i)
  89. {
  90. Vector3 b = spline_.GetPoint(i / 100.f).GetVector3();
  91. debug->AddLine(a, b, Color::GREEN);
  92. a = b;
  93. }
  94. }
  95. for (Vector<WeakPtr<Node>>::ConstIterator i = controlPoints_.Begin(); i != controlPoints_.End(); ++i)
  96. debug->AddNode(*i);
  97. if (controlledNode_)
  98. debug->AddNode(controlledNode_);
  99. }
  100. }
  101. void SplinePath::AddControlPoint(Node* point, unsigned index)
  102. {
  103. if (!point)
  104. return;
  105. WeakPtr<Node> controlPoint(point);
  106. point->AddListener(this);
  107. controlPoints_.Insert(index, controlPoint);
  108. spline_.AddKnot(point->GetWorldPosition(), index);
  109. UpdateNodeIds();
  110. CalculateLength();
  111. }
  112. void SplinePath::RemoveControlPoint(Node* point)
  113. {
  114. if (!point)
  115. return;
  116. WeakPtr<Node> controlPoint(point);
  117. point->RemoveListener(this);
  118. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  119. {
  120. if (controlPoints_[i] == controlPoint)
  121. {
  122. controlPoints_.Erase(i);
  123. spline_.RemoveKnot(i);
  124. break;
  125. }
  126. }
  127. UpdateNodeIds();
  128. CalculateLength();
  129. }
  130. void SplinePath::ClearControlPoints()
  131. {
  132. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  133. {
  134. Node* node = controlPoints_[i];
  135. if (node)
  136. node->RemoveListener(this);
  137. }
  138. controlPoints_.Clear();
  139. spline_.Clear();
  140. UpdateNodeIds();
  141. CalculateLength();
  142. }
  143. void SplinePath::SetControlledNode(Node* controlled)
  144. {
  145. if (controlled)
  146. controlledNode_ = WeakPtr<Node>(controlled);
  147. }
  148. void SplinePath::SetInterpolationMode(InterpolationMode interpolationMode)
  149. {
  150. spline_.SetInterpolationMode(interpolationMode);
  151. CalculateLength();
  152. }
  153. void SplinePath::SetPosition(float factor)
  154. {
  155. float t = factor;
  156. if (t < 0.f)
  157. t = 0.0f;
  158. else if (t > 1.0f)
  159. t = 1.0f;
  160. traveled_ = t;
  161. }
  162. Vector3 SplinePath::GetPoint(float factor) const
  163. {
  164. return spline_.GetPoint(factor).GetVector3();
  165. }
  166. void SplinePath::Move(float timeStep)
  167. {
  168. if (traveled_ >= 1.0f || length_ <= 0.0f || controlledNode_.Null())
  169. return;
  170. elapsedTime_ += timeStep;
  171. // Calculate where we should be on the spline based on length, speed and time. If that is less than the set traveled_ don't move till caught up.
  172. float distanceCovered = elapsedTime_ * speed_;
  173. traveled_ = distanceCovered / length_;
  174. controlledNode_->SetWorldPosition(GetPoint(traveled_));
  175. }
  176. void SplinePath::Reset()
  177. {
  178. traveled_ = 0.f;
  179. elapsedTime_ = 0.f;
  180. }
  181. void SplinePath::SetControlPointIdsAttr(const VariantVector& value)
  182. {
  183. // Just remember the node IDs. They need to go through the SceneResolver, and we actually find the nodes during
  184. // ApplyAttributes()
  185. if (value.Size())
  186. {
  187. controlPointIdsAttr_.Clear();
  188. unsigned index = 0;
  189. unsigned numInstances = value[index++].GetU32();
  190. // Prevent crash on entering negative value in the editor
  191. if (numInstances > M_MAX_INT)
  192. numInstances = 0;
  193. controlPointIdsAttr_.Push(numInstances);
  194. while (numInstances--)
  195. {
  196. // If vector contains less IDs than should, fill the rest with zeros
  197. if (index < value.Size())
  198. controlPointIdsAttr_.Push(value[index++].GetU32());
  199. else
  200. controlPointIdsAttr_.Push(0);
  201. }
  202. dirty_ = true;
  203. }
  204. else
  205. {
  206. controlPointIdsAttr_.Clear();
  207. controlPointIdsAttr_.Push(0);
  208. dirty_ = true;
  209. }
  210. }
  211. void SplinePath::SetControlledIdAttr(unsigned value)
  212. {
  213. if (value > 0 && value < M_MAX_UNSIGNED)
  214. controlledIdAttr_ = value;
  215. dirty_ = true;
  216. }
  217. void SplinePath::OnMarkedDirty(Node* point)
  218. {
  219. if (!point)
  220. return;
  221. WeakPtr<Node> controlPoint(point);
  222. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  223. {
  224. if (controlPoints_[i] == controlPoint)
  225. {
  226. spline_.SetKnot(point->GetWorldPosition(), i);
  227. break;
  228. }
  229. }
  230. CalculateLength();
  231. }
  232. void SplinePath::OnNodeSetEnabled(Node* point)
  233. {
  234. if (!point)
  235. return;
  236. WeakPtr<Node> controlPoint(point);
  237. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  238. {
  239. if (controlPoints_[i] == controlPoint)
  240. {
  241. if (point->IsEnabled())
  242. spline_.AddKnot(point->GetWorldPosition(), i);
  243. else
  244. spline_.RemoveKnot(i);
  245. break;
  246. }
  247. }
  248. CalculateLength();
  249. }
  250. void SplinePath::UpdateNodeIds()
  251. {
  252. unsigned numInstances = controlPoints_.Size();
  253. controlPointIdsAttr_.Clear();
  254. controlPointIdsAttr_.Push(numInstances);
  255. for (unsigned i = 0; i < numInstances; ++i)
  256. {
  257. Node* node = controlPoints_[i];
  258. controlPointIdsAttr_.Push(node ? node->GetID() : 0);
  259. }
  260. }
  261. void SplinePath::CalculateLength()
  262. {
  263. if (spline_.GetKnots().Size() <= 0)
  264. return;
  265. length_ = 0.f;
  266. Vector3 a = spline_.GetKnot(0).GetVector3();
  267. for (auto i = 0; i <= 1000; ++i)
  268. {
  269. Vector3 b = spline_.GetPoint(i / 1000.f).GetVector3();
  270. length_ += Abs((a - b).Length());
  271. a = b;
  272. }
  273. }
  274. }