/*
Example showing how to use SIQuaternion.Slerp
to blend between two rotations
*/
//
// Code for the example scripted operator
//
strDeclOperatorGlobals = "var gQA, gQB, gQC;" ;
function Slerper_Init( ctx )
{
// The SIQuaternion objects are global
// to avoid recreating them at each update
gQA = XSIMath.CreateQuaternion() ;
gQB = XSIMath.CreateQuaternion() ;
gQC = XSIMath.CreateQuaternion() ;
}
function Slerper_Update( ctx, outC, inA, inB )
{
slerpFactor = ctx.Parameters( "Slerp" ).Value ;
oATrans = inA.Value.Transform ;
oATrans.GetRotationQuaternion( gQA ) ;
oBTrans = inB.Value.Transform ;
oBTrans.GetRotationQuaternion( gQB ) ;
gQA.Normalize() ;
gQB.Normalize() ;
gQC.Slerp( gQA, gQB, slerpFactor ) ;
oCTrans = outC.Value.Transform ;
oCTrans.SetRotationFromQuaternion( gQC ) ;
// Very important: oCTrans is just a temporary
// so we must set it back to change it
outC.Value.Transform = oCTrans ;
}
//
// Demo the scripted operator
//
// Build 3 cones. C will show the blended rotation of A and B
NewScene( null,false) ;
var oConeA = ActiveSceneRoot.AddPrimitive( "Cone", "A" ) ;
var oConeB = ActiveSceneRoot.AddPrimitive( "Cone", "B" ) ;
var oConeC = ActiveSceneRoot.AddPrimitive( "Cone", "C" ) ;
//
// Set some initial position/rotation
//
oTransA = XSIMath.CreateTransform() ;
oTransA.SetTranslationFromValues( 5, 0 , 0 ) ;
oTransA.SetRotationFromXYZAnglesValues( 0, 0 , XSIMath.DegreesToRadians(-90) ) ;
oConeA.Kinematics.Global.Transform = oTransA ;
oTransB = XSIMath.CreateTransform() ;
oTransB.SetTranslationFromValues( -5, 0 , 0 ) ;
oTransB.SetRotationFromXYZAnglesValues( 0, 0 , XSIMath.DegreesToRadians(90) ) ;
oConeB.Kinematics.Global.Transform = oTransB ;
//
// Connect the Kinematics
//
strOpCode = strDeclOperatorGlobals +
Slerper_Init.toString() +
Slerper_Update.toString() ;
oOp = XSIFactory.CreateScriptedOp(
"Slerper",
strOpCode,
"JScript"
) ;
oOp.AddOutputPort( oConeC.Kinematics.Global ) ;
oOp.AddInputPort( oConeA.Kinematics.Global )
oOp.AddInputPort( oConeB.Kinematics.Global ) ;
oSlerp = oOp.AddParameter ( XSIFactory.CreateParamDef2("Slerp", siDouble, 0.5, 0, 1) );
oOp.Connect() ;
// Show the operator so that the Slerp amount can be changed with a slider
InspectObj( oOp ) ; |