Browse Source

create Mixer recreation of classic ThreeJS examples.

Ben Houston 10 years ago
parent
commit
728c30580a

+ 5 - 70
examples/webgl_animation_track_clip_mixer.html → examples/webgl_animation_skinning_morph_mixer.html

@@ -185,23 +185,6 @@
 
 			}
 
-			/*function ensureLoop( animation ) {
-
-				for ( var i = 0; i < animation.hierarchy.length; i ++ ) {
-
-					var bone = animation.hierarchy[ i ];
-
-					var first = bone.keys[ 0 ];
-					var last = bone.keys[ bone.keys.length - 1 ];
-
-					last.pos = first.pos;
-					last.rot = first.rot;
-					last.scl = first.scl;
-
-				}
-
-			}*/
-
 			function createScene( geometry, materials, x, y, z, s ) {
 
 				//ensureLoop( geometry.animation );
@@ -250,35 +233,13 @@
 				helper.visible = false;
 				scene.add( helper );
 
-				console.log( 'geometry.animation', geometry.animation );
-
-				var animation = new THREE.Animation( mesh, geometry.animation );
-				//animation.play();
-
 				mixer = new THREE.AnimationMixer( mesh );
+		
+				var clipMorpher = THREE.AnimationClipCreator.CreateMorphAnimation( mesh.geometry.morphTargets, 3 );
+				mixer.addAction( new THREE.AnimationAction( clipMorpher, 0, 1, 1, true ) );
 
-				var clip1 = THREE.AnimationClipCreator.CreateShakeAnimation( 10, new THREE.Vector3( 10, 10, 10 ) );
-				//mixer.addAction( new THREE.AnimationAction( clip1, 0, 1, 1, true ) );
-				var clip2 = THREE.AnimationClipCreator.CreatePulsationAnimation( 10, 100 );
-				//mixer.addAction( new THREE.AnimationAction( clip2, 0, 1, 1, true ) );
-				var clip3 = THREE.AnimationClipCreator.CreateRotationAnimation( 100, 'y' );
-				//mixer.addAction( new THREE.AnimationAction( clip3, 0, 1, 1, true ) );
-				var clip4 = THREE.AnimationClipCreator.CreateScaleAxisAnimation( 10, 'x' );
-				//mixer.addAction( new THREE.AnimationAction( clip4, 0, 1, 1, true ) );
-				var clip5 = THREE.AnimationClipCreator.CreateMaterialColorAnimation( 10, [ new THREE.Color( 0xffffff ), new THREE.Color( 0xff0000 ), new THREE.Color( 0xff00ff ) ] );
-				//mixer.addAction( new THREE.AnimationAction( clip5, 0, 1, 1, true ) );
-
-				//var clip6 = THREE.AnimationClip.CreateVisibilityAnimation( 10 );
-				//mixer.addAction( new THREE.AnimationAction( clip6, 0, 1, 1, true ) );
-				
-				var clip7 = THREE.AnimationClipCreator.CreateMorphAnimation( mesh.geometry.morphTargets, 3 );
-				mixer.addAction( new THREE.AnimationAction( clip7, 0, 1, 1, true ) );
-
-				var clip8 = THREE.AnimationClip.FromJSONLoaderAnimation( geometry );
-				mixer.addAction( new THREE.AnimationAction( clip8, 0, 1, 1, true ) );
-
-				console.log( clip8.getAt( 0 ) );
-
+				var clipBones = THREE.AnimationClip.FromJSONLoaderAnimation( geometry );
+				mixer.addAction( new THREE.AnimationAction( clipBones, 0, 1, 1, true ) );
 			}
 
 			function initGUI() {
@@ -323,35 +284,9 @@
 
 				camera.lookAt( scene.position );
 
-				// update skinning
-
-				//THREE.AnimationHandler.update( delta );
-
 				if( mixer ) {
 					mixer.update( delta );
 				}
-				
-				//if ( helper !== undefined ) helper.update();
-
-				// update morphs
-/*
-				if ( mesh ) {
-
-					var time = Date.now() * 0.001;
-
-					// mouth
-
-					mesh.morphTargetInfluences[ 1 ] = ( 1 + Math.sin( 4 * time ) ) / 2;
-
-					// frown ?
-
-					mesh.morphTargetInfluences[ 2 ] = ( 1 + Math.sin( 2 * time ) ) / 2;
-
-					// eyes
-
-					mesh.morphTargetInfluences[ 3 ] = ( 1 + Math.cos( 4 * time ) ) / 2;
-
-				}*/
 
 				renderer.render( scene, camera );
 

+ 148 - 0
examples/webgl_morphtargets_horse_mixer.html

@@ -0,0 +1,148 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - morph targets - horse</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				font-family: Monospace;
+				background-color: #f0f0f0;
+				margin: 0px;
+				overflow: hidden;
+			}
+		</style>
+	</head>
+	<body>
+
+		<script src="../build/three.min.js"></script>
+		<script src="js/libs/stats.min.js"></script>
+		<script src="js/AnimationClipCreator.js"></script>
+
+		<script>
+
+			var container, stats;
+			var camera, scene, projector, renderer;
+			var mesh, mixer;
+
+			init();
+			animate();
+
+			function init() {
+
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				var info = document.createElement( 'div' );
+				info.style.position = 'absolute';
+				info.style.top = '10px';
+				info.style.width = '100%';
+				info.style.textAlign = 'center';
+				info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - morph targets - horse. model by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a>';
+				container.appendChild( info );
+
+				//
+
+				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
+				camera.position.y = 300;
+				camera.target = new THREE.Vector3( 0, 150, 0 );
+
+				scene = new THREE.Scene();
+
+				//
+
+				var light = new THREE.DirectionalLight( 0xefefff, 2 );
+				light.position.set( 1, 1, 1 ).normalize();
+				scene.add( light );
+
+				var light = new THREE.DirectionalLight( 0xffefef, 2 );
+				light.position.set( -1, -1, -1 ).normalize();
+				scene.add( light );
+
+				var loader = new THREE.JSONLoader();
+				loader.load( "models/animated/horse.js", function( geometry ) {
+
+					mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
+					mesh.scale.set( 1.5, 1.5, 1.5 );
+					scene.add( mesh );
+
+					mixer = new THREE.AnimationMixer( mesh );
+
+					var clipMorphTargets = THREE.AnimationClipCreator.CreateMorphAnimation( geometry.morphTargets, 1.0 );
+					mixer.addAction( new THREE.AnimationAction( clipMorphTargets, 0, 1, 1, true ) );
+
+				} );
+
+				//
+
+				renderer = new THREE.WebGLRenderer();
+				renderer.setClearColor( 0xf0f0f0 );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				container.appendChild(renderer.domElement);
+
+				//
+
+				stats = new Stats();
+				stats.domElement.style.position = 'absolute';
+				stats.domElement.style.top = '0px';
+				container.appendChild( stats.domElement );
+
+				//
+
+				window.addEventListener( 'resize', onWindowResize, false );
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			//
+
+			function animate() {
+
+				requestAnimationFrame( animate );
+
+				render();
+				stats.update();
+
+			}
+
+			var radius = 600;
+			var theta = 0;
+
+			var prevTime = Date.now();
+
+			function render() {
+
+				theta += 0.1;
+
+				camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
+				camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
+
+				camera.lookAt( camera.target );
+
+				if ( mixer ) {
+
+					var time = Date.now();
+
+					mixer.update( ( time - prevTime ) * 0.001 );
+
+					prevTime = time;
+
+				}
+
+				renderer.render( scene, camera );
+
+			}
+
+		</script>
+
+	</body>
+</html>

+ 1 - 1
src/animation/KeyframeTrack.js

@@ -19,7 +19,7 @@ THREE.KeyframeTrack = function ( name, keys ) {
 
 	this.sort();
 	this.validate();
-	//this.optimize();
+	this.optimize();
 };
 
 THREE.KeyframeTrack.prototype = {