|
@@ -0,0 +1,161 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js canvas - 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>
|
|
|
+
|
|
|
+ var container, stats;
|
|
|
+ var camera, scene, projector, renderer;
|
|
|
+ var mesh;
|
|
|
+
|
|
|
+ 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> canvas - 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( true );
|
|
|
+ loader.load( "models/animated/horse.js", function( geometry ) {
|
|
|
+
|
|
|
+ mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true, overdraw: 0.5 } ) );
|
|
|
+ mesh.scale.set( 1.5, 1.5, 1.5 );
|
|
|
+ scene.add( mesh );
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ renderer = new THREE.CanvasRenderer();
|
|
|
+ renderer.setClearColor( 0xf0f0f0 );
|
|
|
+ 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 duration = 1000;
|
|
|
+ var keyframes = 15, interpolation = duration / keyframes;
|
|
|
+ var lastKeyframe = 0, currentKeyframe = 0;
|
|
|
+
|
|
|
+ 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 ( mesh ) {
|
|
|
+
|
|
|
+ // Alternate morph targets
|
|
|
+
|
|
|
+ var time = Date.now() % duration;
|
|
|
+
|
|
|
+ var keyframe = Math.floor( time / interpolation );
|
|
|
+
|
|
|
+ if ( keyframe != currentKeyframe ) {
|
|
|
+
|
|
|
+ mesh.morphTargetInfluences[ lastKeyframe ] = 0;
|
|
|
+ mesh.morphTargetInfluences[ currentKeyframe ] = 1;
|
|
|
+ mesh.morphTargetInfluences[ keyframe ] = 0;
|
|
|
+
|
|
|
+ lastKeyframe = currentKeyframe;
|
|
|
+ currentKeyframe = keyframe;
|
|
|
+
|
|
|
+ // console.log( mesh.morphTargetInfluences );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ mesh.morphTargetInfluences[ keyframe ] = ( time % interpolation ) / interpolation;
|
|
|
+ mesh.morphTargetInfluences[ lastKeyframe ] = 1 - mesh.morphTargetInfluences[ keyframe ];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ renderer.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+
|
|
|
+ </body>
|
|
|
+</html>
|