(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.BVHLoader = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.BVHLoader = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * Description: reads BVH files and outputs a single Skeleton and an AnimationClip * * Currently only supports bvh files containing a single root. * */ var BVHLoader = /*#__PURE__*/function (_Loader) { _inherits(BVHLoader, _Loader); var _super = _createSuper(BVHLoader); function BVHLoader(manager) { var _this; _classCallCheck(this, BVHLoader); _this = _super.call(this, manager); _this.animateBonePositions = true; _this.animateBoneRotations = true; return _this; } _createClass(BVHLoader, [{ key: "load", value: function load(url, onLoad, onProgress, onError) { var scope = this; var loader = new _three.FileLoader(scope.manager); loader.setPath(scope.path); loader.setRequestHeader(scope.requestHeader); loader.setWithCredentials(scope.withCredentials); loader.load(url, function (text) { try { onLoad(scope.parse(text)); } catch (e) { if (onError) { onError(e); } else { console.error(e); } scope.manager.itemError(url); } }, onProgress, onError); } }, { key: "parse", value: function parse(text) { /* reads a string array (lines) from a BVH file and outputs a skeleton structure including motion data returns thee root node: { name: '', channels: [], children: [] } */ function readBvh(lines) { // read model structure if (nextLine(lines) !== 'HIERARCHY') { console.error('THREE.BVHLoader: HIERARCHY expected.'); } var list = []; // collects flat array of all bones var root = readNode(lines, nextLine(lines), list); // read motion data if (nextLine(lines) !== 'MOTION') { console.error('THREE.BVHLoader: MOTION expected.'); } // number of frames var tokens = nextLine(lines).split(/[\s]+/); var numFrames = parseInt(tokens[1]); if (isNaN(numFrames)) { console.error('THREE.BVHLoader: Failed to read number of frames.'); } // frame time tokens = nextLine(lines).split(/[\s]+/); var frameTime = parseFloat(tokens[2]); if (isNaN(frameTime)) { console.error('THREE.BVHLoader: Failed to read frame time.'); } // read frame data line by line for (var i = 0; i < numFrames; i++) { tokens = nextLine(lines).split(/[\s]+/); readFrameData(tokens, i * frameTime, root); } return list; } /* Recursively reads data from a single frame into the bone hierarchy. The passed bone hierarchy has to be structured in the same order as the BVH file. keyframe data is stored in bone.frames. - data: splitted string array (frame values), values are shift()ed so this should be empty after parsing the whole hierarchy. - frameTime: playback time for this keyframe. - bone: the bone to read frame data from. */ function readFrameData(data, frameTime, bone) { // end sites have no motion data if (bone.type === 'ENDSITE') return; // add keyframe var keyframe = { time: frameTime, position: new _three.Vector3(), rotation: new _three.Quaternion() }; bone.frames.push(keyframe); var quat = new _three.Quaternion(); var vx = new _three.Vector3(1, 0, 0); var vy = new _three.Vector3(0, 1, 0); var vz = new _three.Vector3(0, 0, 1); // parse values for each channel in node for (var i = 0; i < bone.channels.length; i++) { switch (bone.channels[i]) { case 'Xposition': keyframe.position.x = parseFloat(data.shift().trim()); break; case 'Yposition': keyframe.position.y = parseFloat(data.shift().trim()); break; case 'Zposition': keyframe.position.z = parseFloat(data.shift().trim()); break; case 'Xrotation': quat.setFromAxisAngle(vx, parseFloat(data.shift().trim()) * Math.PI / 180); keyframe.rotation.multiply(quat); break; case 'Yrotation': quat.setFromAxisAngle(vy, parseFloat(data.shift().trim()) * Math.PI / 180); keyframe.rotation.multiply(quat); break; case 'Zrotation': quat.setFromAxisAngle(vz, parseFloat(data.shift().trim()) * Math.PI / 180); keyframe.rotation.multiply(quat); break; default: console.warn('THREE.BVHLoader: Invalid channel type.'); } } // parse child nodes for (var _i = 0; _i < bone.children.length; _i++) { readFrameData(data, frameTime, bone.children[_i]); } } /* Recursively parses the HIERACHY section of the BVH file - lines: all lines of the file. lines are consumed as we go along. - firstline: line containing the node type and name e.g. 'JOINT hip' - list: collects a flat list of nodes returns: a BVH node including children */ function readNode(lines, firstline, list) { var node = { name: '', type: '', frames: [] }; list.push(node); // parse node type and name var tokens = firstline.split(/[\s]+/); if (tokens[0].toUpperCase() === 'END' && tokens[1].toUpperCase() === 'SITE') { node.type = 'ENDSITE'; node.name = 'ENDSITE'; // bvh end sites have no name } else { node.name = tokens[1]; node.type = tokens[0].toUpperCase(); } if (nextLine(lines) !== '{') { console.error('THREE.BVHLoader: Expected opening { after type & name'); } // parse OFFSET tokens = nextLine(lines).split(/[\s]+/); if (tokens[0] !== 'OFFSET') { console.error('THREE.BVHLoader: Expected OFFSET but got: ' + tokens[0]); } if (tokens.length !== 4) { console.error('THREE.BVHLoader: Invalid number of values for OFFSET.'); } var offset = new _three.Vector3(parseFloat(tokens[1]), parseFloat(tokens[2]), parseFloat(tokens[3])); if (isNaN(offset.x) || isNaN(offset.y) || isNaN(offset.z)) { console.error('THREE.BVHLoader: Invalid values of OFFSET.'); } node.offset = offset; // parse CHANNELS definitions if (node.type !== 'ENDSITE') { tokens = nextLine(lines).split(/[\s]+/); if (tokens[0] !== 'CHANNELS') { console.error('THREE.BVHLoader: Expected CHANNELS definition.'); } var numChannels = parseInt(tokens[1]); node.channels = tokens.splice(2, numChannels); node.children = []; } // read children while (true) { var line = nextLine(lines); if (line === '}') { return node; } else { node.children.push(readNode(lines, line, list)); } } } /* recursively converts the internal bvh node structure to a Bone hierarchy source: the bvh root node list: pass an empty array, collects a flat list of all converted THREE.Bones returns the root Bone */ function toTHREEBone(source, list) { var bone = new _three.Bone(); list.push(bone); bone.position.add(source.offset); bone.name = source.name; if (source.type !== 'ENDSITE') { for (var i = 0; i < source.children.length; i++) { bone.add(toTHREEBone(source.children[i], list)); } } return bone; } /* builds a AnimationClip from the keyframe data saved in each bone. bone: bvh root node returns: a AnimationClip containing position and quaternion tracks */ function toTHREEAnimation(bones) { var tracks = []; // create a position and quaternion animation track for each node for (var i = 0; i < bones.length; i++) { var bone = bones[i]; if (bone.type === 'ENDSITE') continue; // track data var times = []; var positions = []; var rotations = []; for (var j = 0; j < bone.frames.length; j++) { var frame = bone.frames[j]; times.push(frame.time); // the animation system animates the position property, // so we have to add the joint offset to all values positions.push(frame.position.x + bone.offset.x); positions.push(frame.position.y + bone.offset.y); positions.push(frame.position.z + bone.offset.z); rotations.push(frame.rotation.x); rotations.push(frame.rotation.y); rotations.push(frame.rotation.z); rotations.push(frame.rotation.w); } if (scope.animateBonePositions) { tracks.push(new _three.VectorKeyframeTrack('.bones[' + bone.name + '].position', times, positions)); } if (scope.animateBoneRotations) { tracks.push(new _three.QuaternionKeyframeTrack('.bones[' + bone.name + '].quaternion', times, rotations)); } } return new _three.AnimationClip('animation', -1, tracks); } /* returns the next non-empty line in lines */ function nextLine(lines) { var line; // skip empty lines while ((line = lines.shift().trim()).length === 0) {} return line; } var scope = this; var lines = text.split(/[\r\n]+/g); var bones = readBvh(lines); var threeBones = []; toTHREEBone(bones[0], threeBones); var threeClip = toTHREEAnimation(bones); return { skeleton: new _three.Skeleton(threeBones), clip: threeClip }; } }]); return BVHLoader; }(_three.Loader); _exports.BVHLoader = BVHLoader; });