SplinePath.cpp 8.9 KB

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