SplinePath.cpp 9.2 KB

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