Skip to content

Instantly share code, notes, and snippets.

@LeonardA-L
Created May 31, 2016 18:58
Show Gist options
  • Select an option

  • Save LeonardA-L/180e25568f37c9c237c6cc251db9cf7c to your computer and use it in GitHub Desktop.

Select an option

Save LeonardA-L/180e25568f37c9c237c6cc251db9cf7c to your computer and use it in GitHub Desktop.
Allows to create camera interpolations in the Shadwen editor
module(..., package.seeall)
debug.ReloadScripts.allowReload(...)
require "socket"
local cam;
local frameRate = 25; -- In ms. So 40 fps
local frame = 0;
local keyFrames = {};
local diff = {};
-- ---------------------- Tools
function findEntity(name)
local thingy = common.CommonUtils.getSceneInstanceManager():findInstanceByName(name)
if thingy then
logger:info("findEntity: Found " .. name)
else
logger:error("findEntity: Could not find " .. name)
end
return thingy
end
function shift(object, vector)
local toShift = object:getTransformComponent()
toShift:setPosition(toShift:getPosition()+vector)
end
function rotate(object, quat)
local toRotate = object:getTransformComponent()
toRotate:setRotation(toRotate:getRotation()+quat)
end
function shiftCam(vectorDiff)
shift(cam, vectorDiff);
end
function rotateCam(quatDiff)
rotate(cam, quatDiff);
end
-- ---------------------- Actual Animation Stuff
-- Init function. Call before caling animate() so it puts everything in place
function init()
-- Set the freely movable camera (=F12 in editor)
local gameCameraManager = findEntity("TrineCameraManagerInst");
if(gameCameraManager == nil) then
logger:error("Could not init animation: gameCameraManager is not known by TrineCameraManagerInst.");
return false;
end
gameCameraManager:setFreelyMovableCameraEnabled(true);
-- Get the newly created camera
cam = findEntity("freelyMovableCamera");
if(cam == nil) then
logger:error("Could not init animation: camera not found. Where did you put it?");
return false;
end
frame = 0;
-- Animation frames
-- Basically, every "frameRate" ms, the animate function below is called.
-- The camera is moved around in a way that at every key frames (listed here) it has the position / rotation set
-- by the key frame. Regular frame are just interpolating (cf animate).
-- This is the list of key frames
keyFrames[0] = {position = VC3(-78.269, 12.324, 4.9834); rotation=QUAT(-0.23812, 0.16529, -0.0411626, 0.956182); time=30;};
keyFrames[30] = {position = VC3(-78.269, 12.324, 4.9834); rotation=QUAT(-0.23812, 0.16529, -0.0411626, 0.956182); time=330;};
keyFrames[360] = {position = VC3(-66.337, 9.9743, 5.4069); rotation=QUAT(-0.2522231, 0.1660301, -0.04397416, 0.9523045); time=60;};
keyFrames[420] = {position = VC3(-38.829, 12.448, 14.617); rotation=QUAT(-0.1355198, 0.847332, -0.2589855, 0.443384); time=100;};
keyFrames[520] = {position = VC3(-38.829, 12.448, 14.617); rotation=QUAT(-0.1355198, 0.847332, -0.2589855, 0.443384); time=80;};
keyFrames[600] = {position = VC3(-38.598, 12.019, 14.332); rotation=QUAT(-0.3215925, 0.3436646, -0.1265711, 0.8731853); time=60;};
keyFrames[660] = {position = VC3(-35.598, 11.138, 23.756); rotation=QUAT(-0.1336917, 0.7892254, -0.1850834, 0.5700824); time=120;};
keyFrames[780] = {position = VC3(-35.355, 10.59, 35.814); rotation=QUAT(-0.01365454, 0.9678761, -0.05389808, 0.2452028); time=100;};
keyFrames[880] = {position = VC3(-45.063, 10.59, 38.527); rotation=QUAT(0.0126225, 0.985425, -0.0845883, -0.147047); time=80;};
--keyFrames[1000] = {position = VC3(-57.211, 10.732, 36.288); rotation=QUAT(-0.109577, -0.8585938, 0.2061301, 0.4564207); time=40; lastFrame = true;};
keyFrames[960] = {position = VC3(-42.053, 12.717, 35.474); rotation=QUAT(-0.1705808, 0.8099872, -0.2863436, 0.482525); time=60;};
keyFrames[1020] = {position = VC3(-43.918, 14.467, 36.515); rotation=QUAT(-0.351287, 0.471999, -0.212531, 0.780156); time=280;};
keyFrames[1300] = {position = VC3(-51.206, 48.584, 38.84); rotation=QUAT(-0.5032319, -0.2626835, 0.1638486, 0.8067892); lastFrame = true;};
-- TODO get rid of the time thing
-- local prevFrame;
-- for frame, settings in next,keyFrames,nil do
-- logger:info("Frame "..frame..": "..settings.position.x);
-- -- if(prevFrame ~= nil) then
-- -- prevFrame.time =
-- -- end
-- -- prevFrame = settings;
-- end
-- Init camera
cam:getTransformComponent():setPosition(keyFrames[0].position);
cam:getTransformComponent():setRotation(keyFrames[0].rotation);
return true;
end
-- Where the actual magic happens
function animate()
local keyFrame = keyFrames[frame];
if(keyFrame ~= nil and not keyFrame.lastFrame) then
logger:info("New KeyFrame at "..frame);
-- Should already be the case but ensure
cam:getTransformComponent():setPosition(keyFrame.position);
cam:getTransformComponent():setRotation(keyFrame.rotation);
local nextKeyFrame = keyFrames[frame + keyFrame.time];
-- Compute diff per frame
diff.position = (nextKeyFrame.position - keyFrame.position) / keyFrame.time;
diff.rotation = (nextKeyFrame.rotation - keyFrame.rotation) / keyFrame.time;
end
-- Shift camera
shiftCam(diff.position);
rotateCam(diff.rotation);
if(keyFrame == nil or not keyFrame.lastFrame) then
--logger:info("Frame")
frame = frame + 1;
--cinematic.CinematicUtil.delayedCall(frameRate, "leoscripts.animate()");
state:runLuaFuncWithDelay(frameRate, animate)
else
logger:info("End of animation")
end
end
-- ---------------------- Debug
function showFrame(f)
local kf = keyFrames[f];
if(kf == nil) then
return logger:error(f.." is not a key frame");
end
cam:getTransformComponent():setPosition(kf.position);
cam:getTransformComponent():setRotation(kf.rotation);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment