Monday, 16 May 2016
Reworked animated Camera Move script
<button style="margin:10px" onclick="goo.SystemBus.emit('buttonClicked')">
Next camera
</button>
'use strict';
/* global goo */
var setup = function (args, ctx) {
ctx.positions = [];
var keys = Object.keys(args);
for(var i=0; i<keys.length; i++){
if(keys[i].match(/^position\d+$/) && args[keys[i]]){
ctx.positions.push(args[keys[i]]);
}
}
ctx.currentPosition = 0;
ctx.quatA = new goo.Quaternion();
ctx.quatB = new goo.Quaternion();
ctx.lerping = true;
ctx.gooListeners = {};
for(var i=0; i<ctx.positions.length; i++){
ctx.gooListeners[args.setPositionChannel + i] = (function(positionIndex){
return function(){
ctx.currentPosition = positionIndex;
ctx.lerping = true;
};
})(i);
}
for(var key in ctx.gooListeners){
goo.SystemBus.addListener(key, ctx.gooListeners[key]);
}
};
var update = function (args, ctx) {
if(!ctx.positions.length) return;
var factor = 1 - Math.pow(args.smoothing, ctx.world.tpf);
var pos = ctx.positions[ctx.currentPosition];
var distance = ctx.entity.transformComponent.transform.translation.distance(pos.transformComponent.transform.translation);
if(distance < args.minDistance){
ctx.lerping = false;
}
if(!ctx.lerping){
// Copy transform
ctx.entity.transformComponent.transform.copy(
pos.transformComponent.transform
);
} else {
// Lerp translation
ctx.entity.transformComponent.transform.translation.lerp(
pos.transformComponent.transform.translation,
factor
);
// Lerp rotation
ctx.quatA.fromRotationMatrix(ctx.entity.transformComponent.transform.rotation);
ctx.quatB.fromRotationMatrix(pos.transformComponent.transform.rotation);
ctx.quatA.slerp(
ctx.quatB,
factor
);
ctx.entity.transformComponent.transform.rotation.copyQuaternion(ctx.quatA);
}
ctx.entity.transformComponent.transform.update();
ctx.entity.transformComponent.setUpdated();
};
var cleanup = function(args, ctx){
for(var key in ctx.gooListeners){
goo.SystemBus.removeListener(key, ctx.gooListeners[key]);
}
};
var parameters = [{
key: 'smoothing',
type: 'float',
'default': 0.001,
step: 0.01,
min: 0,
max: 1
},{
key: 'minDistance',
type: 'float',
'default': 0.1,
min: 0
},{
key: 'setPositionChannel',
type: 'string',
'default': 'setCameraPosition'
}];
for(var i=0; i<5; i++){
parameters.push({
key: 'position' + i,
type: 'entity'
});
}
Subscribe to:
Post Comments (Atom)







No comments:
Post a Comment