// // walkToolsBVHPlayer.js // version 0.1 // // Created by David Wooldridge, Summer 2015 // Copyright © 2015 - 2016 David Wooldridge. // // Loads a bvh file and plays it. Important: bvh file MUST represent the animation minus the avatar pre-rotations // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // // Include BVH parser library Script.include("./libraries/jaanga-bvh-parser-hifi-version.js"); // ECMAScript 6 specification ready string.contains() function if (!('contains' in String.prototype)) { String.prototype.contains = function(str, startIndex) { return ''.indexOf.call(this, str, startIndex) !== -1; }; } // bvh player object WalkToolsBVHPlayer = function() { var that = {}; var _visible = false; var _isLive = false; var _bvhFrameNumber = 0; var _bvhStartTime = 0; var _frameTimeModifier = 1; var _lastURL = ""; // web window var _innerWidth = Window.innerWidth; var _innerHeight = Window.innerHeight; const PLAYER_WIDTH = 665; const PLAYER_HEIGHT = 190; const MARGIN_TOP = 107; var _url = Script.resolvePath('../html/walkToolsBVHPlayer.html'); var _webView = new WebWindow('walkTools BVH Player', _url, PLAYER_WIDTH, PLAYER_HEIGHT, false); _webView.setPosition((_innerWidth / 4) - (PLAYER_WIDTH / 2), MARGIN_TOP); _webView.setVisible(_visible); // translation scale is ratio of hips to feet (metres, in Interface) and hips to feet (units, source animation) var _translationScale = 1.0 / 107.08; // i.e. hips to feet in Interface / Hips translation in MB var _rawBVHData = null; function loadBVHFile(fileURL) { print('Downloading bvh file: '+fileURL); var _XMLHttpRequest = new XMLHttpRequest(); _XMLHttpRequest.open("GET", fileURL, false); _XMLHttpRequest.send(); if (_XMLHttpRequest.status == 200) { var responseText = _XMLHttpRequest.responseText; _rawBVHData = responseText; Bvh.parseData(responseText, _translationScale); print('bvh file parsed using Jaanga\'s parser.'); _isLive = true; _bvhStartTime = new Date().getTime(); _webView.eventBridge.emitScriptEvent(JSON.stringify({ type: "bvhPlayerEvent", action: "bvhFileLoaded" })); // turn off walk.js animation and walkTools if (_isLive) { motion.isLive = false; walkTools.enableWalkTools(false); } } else { print("Error loading bvh file. Status: " + _XMLHttpRequest.status + " Status Text: " + _XMLHttpRequest.statusText + " Error Code: " + _XMLHttpRequest.errorCode); } } // events from webWindow arrive here _webView.eventBridge.webEventReceived.connect(function(data) { data = JSON.parse(data); if (data.type === "bvhPlayerEvent") { switch (data.action) { case "openBVHFile": if (data.url === _lastURL) { motion.isLive = false; if (walkTools) { walkTools.enableWalkTools(false); } _isLive = true; _webView.eventBridge.emitScriptEvent(JSON.stringify({ type: "bvhPlayerEvent", action: "bvhFileLoaded" })); } else { loadBVHFile(data.url); _lastURL = data.url; } return; case "playBVHFaster": _frameTimeModifier += 0.1; if (_frameTimeModifier > 10) { _frameTimeModifier = 10; } return; case "playBVHSlower": _frameTimeModifier -= 0.1; if (_frameTimeModifier < 0.1) { _frameTimeModifier = 0.1; } return; case "stop": _isLive = false; motion.isLive = true; if (walkTools) { walkTools.enableWalkTools(true); } return; case "export": /*Window.alert('Unable to export BVH file. The exporter is not installed.'); */ if (_rawBVHData) { Script.include("./walkTools/walkToolsBVHConverter.js"); walkToolsBVHConverter.exportJaanga(_rawBVHData, _translationScale); } else { alert('Unable to export BVH file. Please load a BVH file using the Jaanga parser to continue.'); } return; } } }); const MILLISECONDS = 1000; Script.update.connect( function( deltaTime ) { // preview raw bvh data if ( _isLive ) { _bvhFrameNumber = ( _frameTimeModifier * ( Date.now() - Bvh.startTime ) / Bvh.secsPerFrame / MILLISECONDS ) | 0; Bvh.animate(_bvhFrameNumber); } }); that.setVisible = function( visible ) { _visible = visible; _webView.setVisible( _visible ); if ( _visible ) { Window.setFocus(); } }; return that; }; walkToolsBVHPlayer = WalkToolsBVHPlayer();