BabylonCamera.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "stdafx.h"
  2. #include "BabylonCamera.h"
  3. #include "NodeHelpers.h"
  4. #include "GlobalSettings.h"
  5. web::json::value BabylonCamera::toJson() const
  6. {
  7. auto jobj = web::json::value::object();
  8. jobj[L"name"] = web::json::value::string(name);
  9. jobj[L"id"] = web::json::value::string(id);
  10. if(parentId.size()>0)
  11. jobj[L"parentId"] = web::json::value::string(parentId);
  12. if(lockedTargetId.size()>0)
  13. jobj[L"lockedTargetId"] = web::json::value::string(lockedTargetId);
  14. jobj[L"type"] = web::json::value::string(type);
  15. writeVector3(jobj, L"position", position);
  16. writeVector3(jobj, L"rotation", babylon_vector3(0,0,0));
  17. writeVector4(jobj, L"rotationQuaternion", rotationQuaternion);
  18. if(lockedTargetId.size()==0)
  19. writeVector3(jobj, L"target", target);
  20. jobj[L"fov"] = web::json::value::number(fov);
  21. jobj[L"minZ"] = web::json::value::number(minZ);
  22. jobj[L"maxZ"] = web::json::value::number(maxZ);
  23. jobj[L"speed"] = web::json::value::number(speed);
  24. jobj[L"inertia"] = web::json::value::number(inertia);
  25. jobj[L"checkCollisions"] = web::json::value::boolean(checkCollisions);
  26. jobj[L"applyGravity"] = web::json::value::boolean(applyGravity);
  27. writeVector3(jobj, L"ellipsoid", ellipsoid);
  28. if (animations.size() == 0 && quatAnimations.size() == 0){
  29. jobj[L"autoAnimate"] = web::json::value::boolean(false);
  30. jobj[L"autoAnimateLoop"] = web::json::value::boolean(false);
  31. jobj[L"autoAnimateFrom"] = web::json::value::number(0);
  32. jobj[L"autoAnimateTo"] = web::json::value::number(0);
  33. }
  34. else if (animations.size()>0){
  35. jobj[L"autoAnimate"] = web::json::value::boolean(animations[0]->autoAnimate);
  36. jobj[L"autoAnimateLoop"] = web::json::value::boolean(animations[0]->autoAnimateLoop);
  37. jobj[L"autoAnimateFrom"] = web::json::value::number(animations[0]->autoAnimateFrom);
  38. jobj[L"autoAnimateTo"] = web::json::value::number(animations[0]->autoAnimateTo);
  39. }
  40. else{
  41. jobj[L"autoAnimate"] = web::json::value::boolean(quatAnimations[0]->autoAnimate);
  42. jobj[L"autoAnimateLoop"] = web::json::value::boolean(quatAnimations[0]->autoAnimateLoop);
  43. jobj[L"autoAnimateFrom"] = web::json::value::number(quatAnimations[0]->autoAnimateFrom);
  44. jobj[L"autoAnimateTo"] = web::json::value::number(quatAnimations[0]->autoAnimateTo);
  45. }
  46. auto janimations = web::json::value::array();
  47. for (const auto& anim : animations){
  48. janimations[janimations.size()] = anim->toJson();
  49. }for (const auto& anim : quatAnimations){
  50. janimations[janimations.size()] = anim->toJson();
  51. }
  52. jobj[L"animations"] = janimations;
  53. return jobj;
  54. }
  55. BabylonCamera::BabylonCamera()
  56. {
  57. }
  58. BabylonCamera buildCameraFromBoundingBox(const babylon_boundingbox& box){
  59. BabylonCamera result;
  60. result.name = L"defaultcamera";
  61. result.id = L"defaultcamera";
  62. result.target = box.getCenter();
  63. result.position = babylon_vector3(result.target.x, result.target.y, result.target.z - 2 * std::max(box.getWidth(), std::max(box.getHeight(), box.getDepth())));
  64. result.fov = 0.8576f;
  65. result.minZ = -0.01f*result.position.z;
  66. result.maxZ = -5 * result.position.z;
  67. result.speed = (-result.position.z - result.target.z) / 10;
  68. result.inertia = 0.9f;
  69. result.checkCollisions = false;
  70. result.applyGravity = false;
  71. result.ellipsoid = babylon_vector3(.2f, .9f, .2f);
  72. return result;
  73. }
  74. BabylonCamera::BabylonCamera(BabylonNode& babnode)
  75. {
  76. auto node = babnode.fbxNode();
  77. std::string ansiName = node->GetName();
  78. name = std::wstring(ansiName.begin(), ansiName.end());
  79. id = getNodeId(node);
  80. auto parent = node->GetParent();
  81. if (parent) {
  82. parentId = getNodeId(parent);
  83. }
  84. auto camera = node->GetCamera();
  85. if (!camera) {
  86. return;
  87. }
  88. type = L"FreeCamera";
  89. auto targetNode = node->GetTarget();
  90. if (targetNode) {
  91. lockedTargetId = getNodeId(targetNode);
  92. }
  93. else {
  94. target = camera->InterestPosition.Get();
  95. }
  96. auto localTransformAtStart = babnode.GetLocal();
  97. position = localTransformAtStart.translation();
  98. rotationQuaternion = localTransformAtStart.rotationQuaternion();
  99. fov = static_cast<float>(camera->FieldOfViewY * Euler2Rad);
  100. minZ = static_cast<float>(camera->NearPlane.Get());
  101. maxZ = static_cast<float>(camera->FarPlane.Get());
  102. auto hasAnimStack = node->GetScene()->GetSrcObjectCount<FbxAnimStack>() > 0;
  103. if (!hasAnimStack){
  104. return;
  105. }
  106. auto animStack = node->GetScene()->GetCurrentAnimationStack();
  107. FbxString animStackName = animStack->GetName();
  108. //FbxTakeInfo* takeInfo = node->GetScene()->GetTakeInfo(animStackName);
  109. auto animTimeMode = GlobalSettings::Current().AnimationsTimeMode;
  110. auto animFrameRate = GlobalSettings::Current().AnimationsFrameRate();
  111. auto startFrame = animStack->GetLocalTimeSpan().GetStart().GetFrameCount(animTimeMode);
  112. auto endFrame = animStack->GetLocalTimeSpan().GetStop().GetFrameCount(animTimeMode);
  113. auto animLengthInFrame = endFrame - startFrame + 1;
  114. auto posAnim = std::make_shared<BabylonAnimation<babylon_vector3>>(BabylonAnimationBase::loopBehavior_Cycle, static_cast<int>(animFrameRate), L"position", L"position", true, 0, static_cast<int>(animLengthInFrame), true);
  115. auto rotAnim = std::make_shared<BabylonAnimation<babylon_vector4>>(BabylonAnimationBase::loopBehavior_Cycle, static_cast<int>(animFrameRate), L"rotation", L"rotation", true, 0, static_cast<int>(animLengthInFrame), true);
  116. auto targetAnim = std::make_shared<BabylonAnimation<babylon_vector3>>(BabylonAnimationBase::loopBehavior_Cycle, static_cast<int>(animFrameRate), L"target", L"target", true, 0, static_cast<int>(animLengthInFrame), true);
  117. if (node->LclRotation.GetCurveNode() || node->LclScaling.GetCurveNode() || node->LclTranslation.GetCurveNode() || camera->InterestPosition.GetCurveNode()) {
  118. for (auto ix = 0; ix < animLengthInFrame; ix++) {
  119. FbxTime currTime;
  120. currTime.SetFrame(startFrame + ix, animTimeMode);
  121. babylon_animation_key<babylon_vector3> poskey;
  122. babylon_animation_key<babylon_vector4> rotkey;
  123. poskey.frame = ix;
  124. rotkey.frame = ix;
  125. auto transformAtT = babnode.GetLocal(currTime);
  126. poskey.values = transformAtT.translation();
  127. rotkey.values = transformAtT.rotationQuaternion();
  128. posAnim->appendKey(poskey);
  129. rotAnim->appendKey(rotkey);
  130. if (lockedTargetId.size() == 0) {
  131. babylon_animation_key<babylon_vector3> targetKey;
  132. targetKey.frame = ix;
  133. targetKey.values = camera->InterestPosition.EvaluateValue(currTime);
  134. targetAnim->appendKey(targetKey);
  135. }
  136. }
  137. }
  138. if (!posAnim->isConstant()){
  139. animations.push_back(posAnim);
  140. }
  141. if (!rotAnim->isConstant()){
  142. quatAnimations.push_back(rotAnim);
  143. }
  144. if (!targetAnim->isConstant()){
  145. animations.push_back(targetAnim);
  146. }
  147. }
  148. BabylonCamera::BabylonCamera(BabylonCamera && moved) :
  149. name(std::move(moved.name)),
  150. id(std::move(moved.id)),
  151. parentId(std::move(moved.parentId)),
  152. lockedTargetId(std::move(moved.lockedTargetId)),
  153. type(std::move(moved.type)),
  154. position(std::move(moved.position)),
  155. rotationQuaternion(std::move(moved.rotationQuaternion)),
  156. target(std::move(moved.target)),
  157. fov(std::move(moved.fov)),
  158. minZ(std::move(moved.minZ)),
  159. maxZ(std::move(moved.maxZ)),
  160. speed(std::move(moved.speed)),
  161. inertia(std::move(moved.inertia)),
  162. checkCollisions(std::move(moved.checkCollisions)),
  163. applyGravity(std::move(moved.applyGravity)),
  164. ellipsoid(std::move(moved.ellipsoid)),
  165. autoAnimate(std::move(moved.autoAnimate)),
  166. autoAnimateFrom(std::move(moved.autoAnimateFrom)),
  167. autoAnimateTo(std::move(moved.autoAnimateTo)),
  168. autoAnimateLoop(std::move(moved.autoAnimateLoop)),
  169. animations(std::move(moved.animations)),
  170. quatAnimations(std::move(moved.quatAnimations))
  171. {
  172. }
  173. BabylonCamera::~BabylonCamera()
  174. {
  175. }