webgl_animation_skinning_morph.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - skinning + morphing [knight]</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #0af;
  24. }
  25. #stats { position: absolute; top:0; left: 0 }
  26. #stats #fps { background: transparent !important }
  27. #stats #fps #fpsText { color: #777 !important }
  28. #stats #fps #fpsGraph { display: none }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="container"></div>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank">three.js</a> webgl - clip system
  35. - knight by <a href="http://vimeo.com/36113323">apendua</a>
  36. </div>
  37. <script src="../build/three.min.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script src="js/libs/dat.gui.min.js"></script>
  41. <script>
  42. var SCREEN_WIDTH = window.innerWidth;
  43. var SCREEN_HEIGHT = window.innerHeight;
  44. var FLOOR = -250;
  45. var container,stats;
  46. var camera, scene;
  47. var renderer;
  48. var mesh, helper;
  49. var mixer, facesAction, bonesAction;
  50. var mouseX = 0, mouseY = 0;
  51. var windowHalfX = window.innerWidth / 2;
  52. var windowHalfY = window.innerHeight / 2;
  53. var clock = new THREE.Clock();
  54. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  55. init();
  56. animate();
  57. function init() {
  58. container = document.getElementById( 'container' );
  59. camera = new THREE.PerspectiveCamera( 30, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  60. camera.position.z = 2200;
  61. scene = new THREE.Scene();
  62. scene.fog = new THREE.Fog( 0xffffff, 2000, 10000 );
  63. scene.add( camera );
  64. // GROUND
  65. var geometry = new THREE.PlaneBufferGeometry( 16000, 16000 );
  66. var material = new THREE.MeshPhongMaterial( { emissive: 0x888888 } );
  67. var ground = new THREE.Mesh( geometry, material );
  68. ground.position.set( 0, FLOOR, 0 );
  69. ground.rotation.x = -Math.PI/2;
  70. scene.add( ground );
  71. ground.receiveShadow = true;
  72. // LIGHTS
  73. scene.add( new THREE.HemisphereLight( 0x111111, 0x444444 ) );
  74. var light = new THREE.DirectionalLight( 0xebf3ff, 1.5 );
  75. light.position.set( 0, 140, 500 ).multiplyScalar( 1.1 );
  76. scene.add( light );
  77. light.castShadow = true;
  78. light.shadowMapWidth = 1024;
  79. light.shadowMapHeight = 1024;
  80. var d = 390;
  81. light.shadowCameraLeft = -d;
  82. light.shadowCameraRight = d;
  83. light.shadowCameraTop = d * 1.5;
  84. light.shadowCameraBottom = -d;
  85. light.shadowCameraFar = 3500;
  86. //light.shadowCameraVisible = true;
  87. // RENDERER
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setClearColor( scene.fog.color );
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  92. renderer.domElement.style.position = "relative";
  93. container.appendChild( renderer.domElement );
  94. renderer.gammaInput = true;
  95. renderer.gammaOutput = true;
  96. renderer.shadowMap.enabled = true;
  97. // STATS
  98. stats = new Stats();
  99. container.appendChild( stats.domElement );
  100. //
  101. var loader = new THREE.JSONLoader();
  102. loader.load( "models/skinned/knight.js", function ( geometry, materials ) {
  103. createScene( geometry, materials, 0, FLOOR, -300, 60 )
  104. // GUI
  105. initGUI();
  106. } );
  107. //
  108. window.addEventListener( 'resize', onWindowResize, false );
  109. }
  110. function onWindowResize() {
  111. windowHalfX = window.innerWidth / 2;
  112. windowHalfY = window.innerHeight / 2;
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function createScene( geometry, materials, x, y, z, s ) {
  118. //ensureLoop( geometry.animation );
  119. geometry.computeBoundingBox();
  120. var bb = geometry.boundingBox;
  121. var path = "textures/cube/Park2/";
  122. var format = '.jpg';
  123. var urls = [
  124. path + 'posx' + format, path + 'negx' + format,
  125. path + 'posy' + format, path + 'negy' + format,
  126. path + 'posz' + format, path + 'negz' + format
  127. ];
  128. for ( var i = 0; i < materials.length; i ++ ) {
  129. var m = materials[ i ];
  130. m.skinning = true;
  131. m.morphTargets = true;
  132. m.specular.setHSL( 0, 0, 0.1 );
  133. m.color.setHSL( 0.6, 0, 0.6 );
  134. //m.map = map;
  135. //m.envMap = envMap;
  136. //m.bumpMap = bumpMap;
  137. //m.bumpScale = 2;
  138. //m.combine = THREE.MixOperation;
  139. //m.reflectivity = 0.75;
  140. }
  141. mesh = new THREE.SkinnedMesh( geometry, new THREE.MeshFaceMaterial( materials ) );
  142. mesh.position.set( x, y - bb.min.y * s, z );
  143. mesh.scale.set( s, s, s );
  144. scene.add( mesh );
  145. mesh.castShadow = true;
  146. mesh.receiveShadow = true;
  147. helper = new THREE.SkeletonHelper( mesh );
  148. helper.material.linewidth = 3;
  149. helper.visible = false;
  150. scene.add( helper );
  151. mixer = new THREE.AnimationMixer( mesh );
  152. var clipBones = geometry.animations[0];
  153. bonesAction = new THREE.AnimationAction( clipBones );
  154. var clipMorpher = THREE.AnimationClip.CreateFromMorphTargetSequence( 'facialExpressions', mesh.geometry.morphTargets, 3 );
  155. facesAction = new THREE.AnimationAction( clipMorpher );
  156. }
  157. function initGUI() {
  158. var API = {
  159. 'show model' : true,
  160. 'show skeleton' : false,
  161. 'bones action' : true, // use false to see initial allocation
  162. 'bones enable' : true,
  163. 'faces action' : true,
  164. 'faces enable' : true,
  165. 'release props' : function() { mixer.releaseCachedBindings( true ); },
  166. 'purge cache' : function() { mixer.releaseCachedBindings(); }
  167. };
  168. var gui = new dat.GUI();
  169. gui.add( API, 'show model' ).onChange( function() { mesh.visible = API[ 'show model' ]; } );
  170. gui.add( API, 'show skeleton' ).onChange( function() { helper.visible = API[ 'show skeleton' ]; } );
  171. // Note: .add/removeAction and .enabled = true / false have
  172. // different performance characteristics:
  173. //
  174. // The former changes dynamic data structures in the mixer,
  175. // therefore the switch is more expensive but removes the
  176. // per-action base cost caused by the unique property
  177. // bindings it uses.
  178. //
  179. // The latter is a zero-cost switch, but the per-frame base
  180. // cost for having the action added to the mixer remains.
  181. function actionControls( key, action ) {
  182. var guiNameAddRemove = key + ' action';
  183. var guiNameEnabled = key + ' enable';
  184. // set initial state
  185. if ( API[ guiNameAddRemove ] ) {
  186. action.enabled = API[ guiNameEnabled ];
  187. mixer.addAction( action );
  188. }
  189. // attach controls
  190. gui.add( API, guiNameAddRemove ).onChange( function() {
  191. if ( API[ guiNameAddRemove ] ) {
  192. mixer.addAction( action );
  193. } else {
  194. mixer.removeAction( action );
  195. }
  196. } );
  197. gui.add( API, guiNameEnabled ).onChange( function() {
  198. action.enabled = API[ guiNameEnabled ];
  199. } );
  200. }
  201. actionControls( 'bones', bonesAction );
  202. actionControls( 'faces', facesAction );
  203. gui.add( API, 'release props' );
  204. gui.add( API, 'purge cache' );
  205. }
  206. function onDocumentMouseMove( event ) {
  207. mouseX = ( event.clientX - windowHalfX );
  208. mouseY = ( event.clientY - windowHalfY );
  209. }
  210. //
  211. function animate() {
  212. requestAnimationFrame( animate );
  213. render();
  214. stats.update();
  215. }
  216. function render() {
  217. var delta = 0.75 * clock.getDelta();
  218. camera.position.x += ( mouseX - camera.position.x ) * .05;
  219. camera.position.y = THREE.Math.clamp( camera.position.y + ( - mouseY - camera.position.y ) * .05, 0, 1000 );
  220. camera.lookAt( scene.position );
  221. if( mixer ) {
  222. mixer.update( delta );
  223. helper.update();
  224. }
  225. renderer.render( scene, camera );
  226. }
  227. </script>
  228. </body>
  229. </html>