overte/script-archive/dancing_bot.js

50 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2014-04-16 20:39:55 -07:00
//
// dancing_bot.js
// examples
//
// Created by Andrzej Kapolka on 4/16/14.
// Copyright 2014 High Fidelity, Inc.
//
// This is an example script that demonstrates an NPC avatar running an FBX animation loop.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var animation = AnimationCache.getAnimation("http://www.fungibleinsight.com/faces/gangnam_style_2.fbx");
2014-04-16 20:39:55 -07:00
Avatar.skeletonModelURL = "http://www.fungibleinsight.com/faces/beta.fst";
2014-04-16 20:39:55 -07:00
Agent.isAvatar = true;
var jointMapping;
2015-10-07 13:02:58 -07:00
var currentFrame = 0.0;
2014-04-16 21:10:59 -07:00
var FRAME_RATE = 30.0; // frames per second
2014-04-16 20:39:55 -07:00
Script.update.connect(function(deltaTime) {
if (!jointMapping) {
var avatarJointNames = Avatar.jointNames;
var animationJointNames = animation.jointNames;
2014-04-17 17:07:02 -07:00
if (avatarJointNames.length === 0 || animationJointNames.length === 0) {
2014-04-16 20:39:55 -07:00
return;
}
2014-04-16 21:10:59 -07:00
jointMapping = new Array(avatarJointNames.length);
for (var i = 0; i < avatarJointNames.length; i++) {
jointMapping[i] = animationJointNames.indexOf(avatarJointNames[i]);
}
}
2015-10-07 13:02:58 -07:00
currentFrame += deltaTime * FRAME_RATE;
2014-04-16 21:10:59 -07:00
var frames = animation.frames;
2015-10-07 13:02:58 -07:00
var rotations = frames[Math.floor(currentFrame) % frames.length].rotations;
2014-04-16 21:10:59 -07:00
for (var j = 0; j < jointMapping.length; j++) {
var rotationIndex = jointMapping[j];
if (rotationIndex != -1) {
Avatar.setJointData(j, rotations[rotationIndex]);
}
2014-04-16 20:39:55 -07:00
}
});