Advertisement
Guest User

Untitled

a guest
May 31st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. module(..., package.seeall)
  2. debug.ReloadScripts.allowReload(...)
  3. require "socket"
  4.  
  5. local cam;
  6.  
  7. local frameRate = 25; -- In ms. So 40 fps
  8. local frame = 0;
  9. local keyFrames = {};
  10. local diff = {};
  11.  
  12. -- ---------------------- Tools
  13.  
  14. function findEntity(name)
  15. local thingy = common.CommonUtils.getSceneInstanceManager():findInstanceByName(name)
  16. if thingy then
  17. logger:info("findEntity: Found " .. name)
  18. else
  19. logger:error("findEntity: Could not find " .. name)
  20. end
  21. return thingy
  22. end
  23.  
  24. function shift(object, vector)
  25. local toShift = object:getTransformComponent()
  26.  
  27. toShift:setPosition(toShift:getPosition()+vector)
  28. end
  29.  
  30. function rotate(object, quat)
  31. local toRotate = object:getTransformComponent()
  32.  
  33. toRotate:setRotation(toRotate:getRotation()+quat)
  34. end
  35.  
  36. function shiftCam(vectorDiff)
  37. shift(cam, vectorDiff);
  38. end
  39.  
  40. function rotateCam(quatDiff)
  41. rotate(cam, quatDiff);
  42. end
  43.  
  44. -- ---------------------- Actual Animation Stuff
  45.  
  46. -- Init function. Call before caling animate() so it puts everything in place
  47. function init()
  48. -- Set the freely movable camera (=F12 in editor)
  49. local gameCameraManager = findEntity("TrineCameraManagerInst");
  50. if(gameCameraManager == nil) then
  51. logger:error("Could not init animation: gameCameraManager is not known by TrineCameraManagerInst.");
  52. return false;
  53. end
  54. gameCameraManager:setFreelyMovableCameraEnabled(true);
  55.  
  56. -- Get the newly created camera
  57. cam = findEntity("freelyMovableCamera");
  58. if(cam == nil) then
  59. logger:error("Could not init animation: camera not found. Where did you put it?");
  60. return false;
  61. end
  62.  
  63. frame = 0;
  64.  
  65. -- Animation frames
  66. -- Basically, every "frameRate" ms, the animate function below is called.
  67. -- The camera is moved around in a way that at every key frames (listed here) it has the position / rotation set
  68. -- by the key frame. Regular frame are just interpolating (cf animate).
  69. -- This is the list of key frames
  70. keyFrames[0] = {position = VC3(-78.269, 12.324, 4.9834); rotation=QUAT(-0.23812, 0.16529, -0.0411626, 0.956182); time=30;};
  71. keyFrames[30] = {position = VC3(-78.269, 12.324, 4.9834); rotation=QUAT(-0.23812, 0.16529, -0.0411626, 0.956182); time=330;};
  72. keyFrames[360] = {position = VC3(-66.337, 9.9743, 5.4069); rotation=QUAT(-0.2522231, 0.1660301, -0.04397416, 0.9523045); time=60;};
  73. keyFrames[420] = {position = VC3(-38.829, 12.448, 14.617); rotation=QUAT(-0.1355198, 0.847332, -0.2589855, 0.443384); time=100;};
  74. keyFrames[520] = {position = VC3(-38.829, 12.448, 14.617); rotation=QUAT(-0.1355198, 0.847332, -0.2589855, 0.443384); time=80;};
  75. keyFrames[600] = {position = VC3(-38.598, 12.019, 14.332); rotation=QUAT(-0.3215925, 0.3436646, -0.1265711, 0.8731853); time=60;};
  76. keyFrames[660] = {position = VC3(-35.598, 11.138, 23.756); rotation=QUAT(-0.1336917, 0.7892254, -0.1850834, 0.5700824); time=120;};
  77. keyFrames[780] = {position = VC3(-35.355, 10.59, 35.814); rotation=QUAT(-0.01365454, 0.9678761, -0.05389808, 0.2452028); time=100;};
  78. keyFrames[880] = {position = VC3(-45.063, 10.59, 38.527); rotation=QUAT(0.0126225, 0.985425, -0.0845883, -0.147047); time=80;};
  79. --keyFrames[1000] = {position = VC3(-57.211, 10.732, 36.288); rotation=QUAT(-0.109577, -0.8585938, 0.2061301, 0.4564207); time=40; lastFrame = true;};
  80. keyFrames[960] = {position = VC3(-42.053, 12.717, 35.474); rotation=QUAT(-0.1705808, 0.8099872, -0.2863436, 0.482525); time=60;};
  81. keyFrames[1020] = {position = VC3(-43.918, 14.467, 36.515); rotation=QUAT(-0.351287, 0.471999, -0.212531, 0.780156); time=280;};
  82. keyFrames[1300] = {position = VC3(-51.206, 48.584, 38.84); rotation=QUAT(-0.5032319, -0.2626835, 0.1638486, 0.8067892); lastFrame = true;};
  83. -- TODO get rid of the time thing
  84.  
  85. -- local prevFrame;
  86. -- for frame, settings in next,keyFrames,nil do
  87. -- logger:info("Frame "..frame..": "..settings.position.x);
  88. -- -- if(prevFrame ~= nil) then
  89. -- -- prevFrame.time =
  90. -- -- end
  91. -- -- prevFrame = settings;
  92. -- end
  93.  
  94. -- Init camera
  95. cam:getTransformComponent():setPosition(keyFrames[0].position);
  96. cam:getTransformComponent():setRotation(keyFrames[0].rotation);
  97.  
  98. return true;
  99. end
  100.  
  101.  
  102. -- Where the actual magic happens
  103. function animate()
  104. local keyFrame = keyFrames[frame];
  105. if(keyFrame ~= nil and not keyFrame.lastFrame) then
  106. logger:info("New KeyFrame at "..frame);
  107.  
  108. -- Should already be the case but ensure
  109. cam:getTransformComponent():setPosition(keyFrame.position);
  110. cam:getTransformComponent():setRotation(keyFrame.rotation);
  111.  
  112. local nextKeyFrame = keyFrames[frame + keyFrame.time];
  113.  
  114. -- Compute diff per frame
  115. diff.position = (nextKeyFrame.position - keyFrame.position) / keyFrame.time;
  116. diff.rotation = (nextKeyFrame.rotation - keyFrame.rotation) / keyFrame.time;
  117. end
  118.  
  119. -- Shift camera
  120. shiftCam(diff.position);
  121. rotateCam(diff.rotation);
  122.  
  123. if(keyFrame == nil or not keyFrame.lastFrame) then
  124. --logger:info("Frame")
  125. frame = frame + 1;
  126. --cinematic.CinematicUtil.delayedCall(frameRate, "leoscripts.animate()");
  127. state:runLuaFuncWithDelay(frameRate, animate)
  128. else
  129. logger:info("End of animation")
  130. end
  131. end
  132.  
  133. -- ---------------------- Debug
  134. function showFrame(f)
  135. local kf = keyFrames[f];
  136. if(kf == nil) then
  137. return logger:error(f.." is not a key frame");
  138. end
  139. cam:getTransformComponent():setPosition(kf.position);
  140. cam:getTransformComponent():setRotation(kf.rotation);
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement