123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- THREE.BVHLoader = function( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- this.animateBonePositions = true;
- this.animateBoneRotations = true;
- };
- THREE.BVHLoader.prototype = {
- constructor: THREE.BVHLoader,
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.FileLoader( scope.manager );
- loader.load( url, function( text ) {
- onLoad( scope.parse( text ) );
- }, onProgress, onError );
- },
- parse: function ( text ) {
-
- function readBvh( lines ) {
-
- if ( nextLine( lines ) !== 'HIERARCHY' ) {
- console.error( 'THREE.BVHLoader: HIERARCHY expected.' );
- }
- var list = [];
- var root = readNode( lines, nextLine( lines ), list );
-
- if ( nextLine( lines ) !== 'MOTION' ) {
- console.error( 'THREE.BVHLoader: MOTION expected.' );
- }
-
- var tokens = nextLine( lines ).split( /[\s]+/ );
- var numFrames = parseInt( tokens[ 1 ] );
- if ( isNaN( numFrames ) ) {
- console.error( 'THREE.BVHLoader: Failed to read number of frames.' );
- }
-
- tokens = nextLine( lines ).split( /[\s]+/ );
- var frameTime = parseFloat( tokens[ 2 ] );
- if ( isNaN( frameTime ) ) {
- console.error( 'THREE.BVHLoader: Failed to read frame time.' );
- }
-
- for ( var i = 0; i < numFrames; i ++ ) {
- tokens = nextLine( lines ).split( /[\s]+/ );
- readFrameData( tokens, i * frameTime, root );
- }
- return list;
- }
-
- function readFrameData( data, frameTime, bone ) {
-
- if ( bone.type === 'ENDSITE' ) return;
-
- 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 );
-
- 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.' );
- }
- }
-
- for ( var i = 0; i < bone.children.length; i ++ ) {
- readFrameData( data, frameTime, bone.children[ i ] );
- }
- }
-
- function readNode( lines, firstline, list ) {
- var node = { name: '', type: '', frames: [] };
- list.push( node );
-
- var tokens = firstline.split( /[\s]+/ );
- if ( tokens[ 0 ].toUpperCase() === 'END' && tokens[ 1 ].toUpperCase() === 'SITE' ) {
- node.type = 'ENDSITE';
- node.name = 'ENDSITE';
- } else {
- node.name = tokens[ 1 ];
- node.type = tokens[ 0 ].toUpperCase();
- }
- if ( nextLine( lines ) !== '{' ) {
- console.error( 'THREE.BVHLoader: Expected opening { after type & name' );
- }
-
- 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;
-
- 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 = [];
- }
-
- while ( true ) {
- var line = nextLine( lines );
- if ( line === '}' ) {
- return node;
- } else {
- node.children.push( readNode( lines, line, list ) );
- }
- }
- }
-
- 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;
- }
-
- function toTHREEAnimation( bones ) {
- var tracks = [];
-
- for ( var i = 0; i < bones.length; i ++ ) {
- var bone = bones[ i ];
- if ( bone.type === 'ENDSITE' )
- continue;
-
- var times = [];
- var positions = [];
- var rotations = [];
- for ( var j = 0; j < bone.frames.length; j ++ ) {
- var frame = bone.frames[ j ];
- times.push( frame.time );
-
-
- 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 );
- }
-
- function nextLine( lines ) {
- var line;
-
- 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
- };
- }
- };
|