SplinePath.cpp 9.0 KB

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