SplinePath.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Scene/Scene.h"
  26. #include "../Scene/SplinePath.h"
  27. namespace Urho3D
  28. {
  29. extern const char* interpolationModeNames[];
  30. extern const char* LOGIC_CATEGORY;
  31. static const StringVector controlPointsStructureElementNames =
  32. {
  33. "Control Point Count",
  34. " NodeID"
  35. };
  36. SplinePath::SplinePath(Context* context) :
  37. Component(context),
  38. spline_(BEZIER_CURVE),
  39. speed_(1.f),
  40. elapsedTime_(0.f),
  41. traveled_(0.f),
  42. length_(0.f),
  43. dirty_(false),
  44. controlledIdAttr_(0)
  45. {
  46. UpdateNodeIds();
  47. }
  48. void SplinePath::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<SplinePath>(LOGIC_CATEGORY);
  51. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Interpolation Mode", GetInterpolationMode, SetInterpolationMode, InterpolationMode,
  52. interpolationModeNames, BEZIER_CURVE, AM_FILE);
  53. URHO3D_ATTRIBUTE("Speed", float, speed_, 1.f, AM_FILE);
  54. URHO3D_ATTRIBUTE("Traveled", float, traveled_, 0.f, AM_FILE | AM_NOEDIT);
  55. URHO3D_ATTRIBUTE("Elapsed Time", float, elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
  56. URHO3D_ACCESSOR_ATTRIBUTE("Controlled", GetControlledIdAttr, SetControlledIdAttr, unsigned, 0, AM_FILE | AM_NODEID);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Control Points", GetControlPointIdsAttr, SetControlPointIdsAttr,
  58. VariantVector, Variant::emptyVariantVector, AM_FILE | AM_NODEIDVECTOR)
  59. .SetMetadata(AttributeMetadata::P_VECTOR_STRUCT_ELEMENTS, controlPointsStructureElementNames);
  60. }
  61. void SplinePath::ApplyAttributes()
  62. {
  63. if (!dirty_)
  64. return;
  65. // Remove all old instance nodes before searching for new. Can not call RemoveAllInstances() as that would modify
  66. // the ID list on its own
  67. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  68. {
  69. Node* node = controlPoints_[i];
  70. if (node)
  71. node->RemoveListener(this);
  72. }
  73. controlPoints_.Clear();
  74. spline_.Clear();
  75. Scene* scene = GetScene();
  76. if (scene)
  77. {
  78. // The first index stores the number of IDs redundantly. This is for editing
  79. for (unsigned i = 1; i < controlPointIdsAttr_.Size(); ++i)
  80. {
  81. Node* node = scene->GetNode(controlPointIdsAttr_[i].GetUInt());
  82. if (node)
  83. {
  84. WeakPtr<Node> controlPoint(node);
  85. node->AddListener(this);
  86. controlPoints_.Push(controlPoint);
  87. spline_.AddKnot(node->GetWorldPosition());
  88. }
  89. }
  90. Node* node = scene->GetNode(controlledIdAttr_);
  91. if (node)
  92. {
  93. WeakPtr<Node> controlled(node);
  94. controlledNode_ = controlled;
  95. }
  96. }
  97. CalculateLength();
  98. dirty_ = false;
  99. }
  100. void SplinePath::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  101. {
  102. if (debug && node_ && IsEnabledEffective())
  103. {
  104. if (spline_.GetKnots().Size() > 1)
  105. {
  106. Vector3 a = spline_.GetPoint(0.f).GetVector3();
  107. for (float f = 0.01f; f <= 1.0f; f = f + 0.01f)
  108. {
  109. Vector3 b = spline_.GetPoint(f).GetVector3();
  110. debug->AddLine(a, b, Color::GREEN);
  111. a = b;
  112. }
  113. }
  114. for (Vector<WeakPtr<Node> >::ConstIterator i = controlPoints_.Begin(); i != controlPoints_.End(); ++i)
  115. debug->AddNode(*i);
  116. if (controlledNode_)
  117. debug->AddNode(controlledNode_);
  118. }
  119. }
  120. void SplinePath::AddControlPoint(Node* point, unsigned index)
  121. {
  122. if (!point)
  123. return;
  124. WeakPtr<Node> controlPoint(point);
  125. point->AddListener(this);
  126. controlPoints_.Insert(index, controlPoint);
  127. spline_.AddKnot(point->GetWorldPosition(), index);
  128. UpdateNodeIds();
  129. CalculateLength();
  130. }
  131. void SplinePath::RemoveControlPoint(Node* point)
  132. {
  133. if (!point)
  134. return;
  135. WeakPtr<Node> controlPoint(point);
  136. point->RemoveListener(this);
  137. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  138. {
  139. if (controlPoints_[i] == controlPoint)
  140. {
  141. controlPoints_.Erase(i);
  142. spline_.RemoveKnot(i);
  143. break;
  144. }
  145. }
  146. UpdateNodeIds();
  147. CalculateLength();
  148. }
  149. void SplinePath::ClearControlPoints()
  150. {
  151. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  152. {
  153. Node* node = controlPoints_[i];
  154. if (node)
  155. node->RemoveListener(this);
  156. }
  157. controlPoints_.Clear();
  158. spline_.Clear();
  159. UpdateNodeIds();
  160. CalculateLength();
  161. }
  162. void SplinePath::SetControlledNode(Node* controlled)
  163. {
  164. if (controlled)
  165. controlledNode_ = WeakPtr<Node>(controlled);
  166. }
  167. void SplinePath::SetInterpolationMode(InterpolationMode interpolationMode)
  168. {
  169. spline_.SetInterpolationMode(interpolationMode);
  170. CalculateLength();
  171. }
  172. void SplinePath::SetPosition(float factor)
  173. {
  174. float t = factor;
  175. if (t < 0.f)
  176. t = 0.0f;
  177. else if (t > 1.0f)
  178. t = 1.0f;
  179. traveled_ = t;
  180. }
  181. Vector3 SplinePath::GetPoint(float factor) const
  182. {
  183. return spline_.GetPoint(factor).GetVector3();
  184. }
  185. void SplinePath::Move(float timeStep)
  186. {
  187. if (traveled_ >= 1.0f || length_ <= 0.0f || controlledNode_.Null())
  188. return;
  189. elapsedTime_ += timeStep;
  190. // 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.
  191. float distanceCovered = elapsedTime_ * speed_;
  192. traveled_ = distanceCovered / length_;
  193. controlledNode_->SetWorldPosition(GetPoint(traveled_));
  194. }
  195. void SplinePath::Reset()
  196. {
  197. traveled_ = 0.f;
  198. elapsedTime_ = 0.f;
  199. }
  200. void SplinePath::SetControlPointIdsAttr(const VariantVector& value)
  201. {
  202. // Just remember the node IDs. They need to go through the SceneResolver, and we actually find the nodes during
  203. // ApplyAttributes()
  204. if (value.Size())
  205. {
  206. controlPointIdsAttr_.Clear();
  207. unsigned index = 0;
  208. unsigned numInstances = value[index++].GetUInt();
  209. // Prevent crash on entering negative value in the editor
  210. if (numInstances > M_MAX_INT)
  211. numInstances = 0;
  212. controlPointIdsAttr_.Push(numInstances);
  213. while (numInstances--)
  214. {
  215. // If vector contains less IDs than should, fill the rest with zeros
  216. if (index < value.Size())
  217. controlPointIdsAttr_.Push(value[index++].GetUInt());
  218. else
  219. controlPointIdsAttr_.Push(0);
  220. }
  221. dirty_ = true;
  222. }
  223. else
  224. {
  225. controlPointIdsAttr_.Clear();
  226. controlPointIdsAttr_.Push(0);
  227. dirty_ = true;
  228. }
  229. }
  230. void SplinePath::SetControlledIdAttr(unsigned value)
  231. {
  232. if (value > 0 && value < M_MAX_UNSIGNED)
  233. controlledIdAttr_ = value;
  234. dirty_ = true;
  235. }
  236. void SplinePath::OnMarkedDirty(Node* point)
  237. {
  238. if (!point)
  239. return;
  240. WeakPtr<Node> controlPoint(point);
  241. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  242. {
  243. if (controlPoints_[i] == controlPoint)
  244. {
  245. spline_.SetKnot(point->GetWorldPosition(), i);
  246. break;
  247. }
  248. }
  249. CalculateLength();
  250. }
  251. void SplinePath::OnNodeSetEnabled(Node* point)
  252. {
  253. if (!point)
  254. return;
  255. WeakPtr<Node> controlPoint(point);
  256. for (unsigned i = 0; i < controlPoints_.Size(); ++i)
  257. {
  258. if (controlPoints_[i] == controlPoint)
  259. {
  260. if (point->IsEnabled())
  261. spline_.AddKnot(point->GetWorldPosition(), i);
  262. else
  263. spline_.RemoveKnot(i);
  264. break;
  265. }
  266. }
  267. CalculateLength();
  268. }
  269. void SplinePath::UpdateNodeIds()
  270. {
  271. unsigned numInstances = controlPoints_.Size();
  272. controlPointIdsAttr_.Clear();
  273. controlPointIdsAttr_.Push(numInstances);
  274. for (unsigned i = 0; i < numInstances; ++i)
  275. {
  276. Node* node = controlPoints_[i];
  277. controlPointIdsAttr_.Push(node ? node->GetID() : 0);
  278. }
  279. }
  280. void SplinePath::CalculateLength()
  281. {
  282. if (spline_.GetKnots().Size() <= 0)
  283. return;
  284. length_ = 0.f;
  285. Vector3 a = spline_.GetKnot(0).GetVector3();
  286. for (float f = 0.000f; f <= 1.000f; f += 0.001f)
  287. {
  288. Vector3 b = spline_.GetPoint(f).GetVector3();
  289. length_ += Abs((a - b).Length());
  290. a = b;
  291. }
  292. }
  293. }