webgl_animation_skinning_morph.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #meminfo {
  23. margin-top: 8px;
  24. font-size: 10px;
  25. display: none;
  26. }
  27. a {
  28. color: #0af;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div id="container"></div>
  34. <div id="info">
  35. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - clip system
  36. - knight by <a href="http://vimeo.com/36113323">apendua</a>
  37. <div id="meminfo"></div>
  38. </div>
  39. <script src="../build/three.js"></script>
  40. <script src="js/Detector.js"></script>
  41. <script src="js/libs/stats.min.js"></script>
  42. <script src="js/libs/dat.gui.min.js"></script>
  43. <script>
  44. var SCREEN_WIDTH = window.innerWidth;
  45. var SCREEN_HEIGHT = window.innerHeight;
  46. var FLOOR = -250;
  47. var container,stats;
  48. var camera, scene;
  49. var renderer;
  50. var mesh, mesh2, helper;
  51. var mixer, facesClip, bonesClip;
  52. var mouseX = 0, mouseY = 0;
  53. var windowHalfX = window.innerWidth / 2;
  54. var windowHalfY = window.innerHeight / 2;
  55. var clock = new THREE.Clock();
  56. var domMemInfo = document.getElementById( 'meminfo' ),
  57. showMemInfo = false;
  58. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  59. init();
  60. animate();
  61. function init() {
  62. container = document.getElementById( 'container' );
  63. camera = new THREE.PerspectiveCamera( 30, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  64. camera.position.z = 2200;
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0xffffff );
  67. scene.fog = new THREE.Fog( 0xffffff, 2000, 10000 );
  68. scene.add( camera );
  69. // GROUND
  70. var geometry = new THREE.PlaneBufferGeometry( 16000, 16000 );
  71. var material = new THREE.MeshPhongMaterial( { emissive: 0x888888 } );
  72. var ground = new THREE.Mesh( geometry, material );
  73. ground.position.set( 0, FLOOR, 0 );
  74. ground.rotation.x = -Math.PI/2;
  75. scene.add( ground );
  76. ground.receiveShadow = true;
  77. // LIGHTS
  78. scene.add( new THREE.HemisphereLight( 0x111111, 0x444444 ) );
  79. var light = new THREE.DirectionalLight( 0xebf3ff, 1.5 );
  80. light.position.set( 0, 140, 500 ).multiplyScalar( 1.1 );
  81. scene.add( light );
  82. light.castShadow = true;
  83. light.shadow.mapSize.width = 1024;
  84. light.shadow.mapSize.height = 1024;
  85. var d = 390;
  86. light.shadow.camera.left = -d;
  87. light.shadow.camera.right = d;
  88. light.shadow.camera.top = d * 1.5;
  89. light.shadow.camera.bottom = -d;
  90. light.shadow.camera.far = 3500;
  91. // RENDERER
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  95. renderer.domElement.style.position = "relative";
  96. container.appendChild( renderer.domElement );
  97. renderer.gammaInput = true;
  98. renderer.gammaOutput = true;
  99. renderer.shadowMap.enabled = true;
  100. // STATS
  101. stats = new Stats();
  102. container.appendChild( stats.dom );
  103. //
  104. var loader = new THREE.JSONLoader();
  105. loader.load( "models/skinned/knight.js", function ( geometry, materials ) {
  106. createScene( geometry, materials, 0, FLOOR, -300, 60 );
  107. // GUI
  108. initGUI();
  109. } );
  110. //
  111. window.addEventListener( 'resize', onWindowResize, false );
  112. }
  113. function onWindowResize() {
  114. windowHalfX = window.innerWidth / 2;
  115. windowHalfY = window.innerHeight / 2;
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function createScene( geometry, materials, x, y, z, s ) {
  121. geometry.computeBoundingBox();
  122. var bb = geometry.boundingBox;
  123. for ( var i = 0; i < materials.length; i ++ ) {
  124. var m = materials[ i ];
  125. m.skinning = true;
  126. m.morphTargets = true;
  127. m.specular.setHSL( 0, 0, 0.1 );
  128. m.color.setHSL( 0.6, 0, 0.6 );
  129. }
  130. mesh = new THREE.SkinnedMesh( geometry, materials );
  131. mesh.name = "Knight Mesh";
  132. mesh.position.set( x, y - bb.min.y * s, z );
  133. mesh.scale.set( s, s, s );
  134. scene.add( mesh );
  135. mesh.castShadow = true;
  136. mesh.receiveShadow = true;
  137. mesh2 = new THREE.SkinnedMesh( geometry, materials );
  138. mesh2.name = "Lil' Bro Mesh";
  139. mesh2.position.set( x - 240, y - bb.min.y * s, z + 500 );
  140. mesh2.scale.set( s / 2, s / 2, s / 2 );
  141. mesh2.rotation.y = THREE.Math.degToRad( 60 );
  142. mesh2.visible = false;
  143. mesh2.castShadow = true;
  144. mesh2.receiveShadow = true;
  145. scene.add( mesh2 );
  146. helper = new THREE.SkeletonHelper( mesh );
  147. helper.material.linewidth = 3;
  148. helper.visible = false;
  149. scene.add( helper );
  150. mixer = new THREE.AnimationMixer( mesh );
  151. bonesClip = geometry.animations[0];
  152. facesClip = THREE.AnimationClip.CreateFromMorphTargetSequence( 'facialExpressions', mesh.geometry.morphTargets, 3 );
  153. }
  154. function initGUI() {
  155. var API = {
  156. 'show model' : true,
  157. 'show skeleton' : false,
  158. 'show 2nd model' : false,
  159. 'show mem. info' : false
  160. };
  161. var gui = new dat.GUI();
  162. gui.add( API, 'show model' ).onChange( function() {
  163. mesh.visible = API[ 'show model' ];
  164. } );
  165. gui.add( API, 'show skeleton' ).onChange( function() {
  166. helper.visible = API[ 'show skeleton' ];
  167. } );
  168. gui.add( API, 'show 2nd model' ).onChange( function() {
  169. mesh2.visible = API[ 'show 2nd model' ];
  170. } );
  171. gui.add( API, 'show mem. info' ).onChange( function() {
  172. showMemInfo = API[ 'show mem. info' ];
  173. domMemInfo.style.display = showMemInfo ? 'block' : 'none';
  174. } );
  175. // utility function used for drop-down options lists in the GUI
  176. var objectNames = function( objects ) {
  177. var result = [];
  178. for ( var i = 0, n = objects.length; i !== n; ++ i ) {
  179. var obj = objects[ i ];
  180. result.push( obj && obj.name || '&lt;null&gt;' );
  181. }
  182. return result;
  183. };
  184. // creates gui folder with tests / examples for the action API
  185. var clipControl = function clipControl( gui, mixer, clip, rootObjects ) {
  186. var folder = gui.addFolder( "Clip '" + clip.name + "'" ),
  187. rootNames = objectNames( rootObjects ),
  188. rootName = rootNames[ 0 ],
  189. root = rootObjects[ 0 ],
  190. action = null,
  191. API = {
  192. 'play()': function play() {
  193. action = mixer.clipAction( clip, root );
  194. action.play();
  195. },
  196. 'stop()': function() {
  197. action = mixer.clipAction( clip, root );
  198. action.stop();
  199. },
  200. 'reset()': function() {
  201. action = mixer.clipAction( clip, root );
  202. action.reset();
  203. },
  204. get 'time ='() {
  205. return action !== null ? action.time : 0;
  206. },
  207. set 'time ='( value ) {
  208. action = mixer.clipAction( clip, root );
  209. action.time = value;
  210. },
  211. get 'paused ='() {
  212. return action !== null && action.paused;
  213. },
  214. set 'paused ='( value ) {
  215. action = mixer.clipAction( clip, root );
  216. action.paused = value;
  217. },
  218. get 'enabled ='() {
  219. return action !== null && action.enabled;
  220. },
  221. set 'enabled ='( value ) {
  222. action = mixer.clipAction( clip, root );
  223. action.enabled = value;
  224. },
  225. get 'clamp ='() {
  226. return action !== null ? action.clampWhenFinished : false;
  227. },
  228. set 'clamp ='( value ) {
  229. action = mixer.clipAction( clip, root );
  230. action.clampWhenFinished = value;
  231. },
  232. get 'isRunning() ='() {
  233. return action !== null && action.isRunning();
  234. },
  235. set 'isRunning() ='( value ) {
  236. alert( "Read only - this is the result of a method." );
  237. },
  238. 'play delayed': function() {
  239. action = mixer.clipAction( clip, root );
  240. action.startAt( mixer.time + 0.5 ).play();
  241. },
  242. get 'weight ='() {
  243. return action !== null ? action.weight : 1;
  244. },
  245. set 'weight ='( value ) {
  246. action = mixer.clipAction( clip, root );
  247. action.weight = value;
  248. },
  249. get 'eff. weight'() {
  250. return action !== null ? action.getEffectiveWeight() : 1;
  251. },
  252. set 'eff. weight'( value ) {
  253. action = mixer.clipAction( clip, root );
  254. action.setEffectiveWeight( value );
  255. },
  256. 'fade in': function() {
  257. action = mixer.clipAction( clip, root );
  258. action.reset().fadeIn( 0.25 ).play();
  259. },
  260. 'fade out': function() {
  261. action = mixer.clipAction( clip, root );
  262. action.fadeOut( 0.25 ).play();
  263. },
  264. get 'timeScale ='() {
  265. return ( action !== null ) ? action.timeScale : 1;
  266. },
  267. set 'timeScale ='( value ) {
  268. action = mixer.clipAction( clip, root );
  269. action.timeScale = value;
  270. },
  271. get 'eff.T.Scale'() {
  272. return ( action !== null ) ? action.getEffectiveTimeScale() : 1;
  273. },
  274. set 'eff.T.Scale'( value ) {
  275. action = mixer.clipAction( clip, root );
  276. action.setEffectiveTimeScale( value );
  277. },
  278. 'time warp': function() {
  279. action = mixer.clipAction( clip, root );
  280. var timeScaleNow = action.getEffectiveTimeScale();
  281. var destTimeScale = timeScaleNow > 0 ? -1 : 1;
  282. action.warp( timeScaleNow, destTimeScale, 4 ).play();
  283. },
  284. get 'loop mode'() {
  285. return action !== null ? action.loop : THREE.LoopRepeat;
  286. },
  287. set 'loop mode'( value ) {
  288. action = mixer.clipAction( clip, root );
  289. action.loop = + value;
  290. },
  291. get 'repetitions'() {
  292. return action !== null ? action.repetitions : Infinity;
  293. },
  294. set 'repetitions'( value ) {
  295. action = mixer.clipAction( clip, root );
  296. action.repetitions = + value;
  297. },
  298. get 'local root'() { return rootName; },
  299. set 'local root'( value ) {
  300. rootName = value;
  301. root = rootObjects[ rootNames.indexOf( rootName ) ];
  302. action = mixer.clipAction( clip, root );
  303. }
  304. };
  305. folder.add( API, 'play()' );
  306. folder.add( API, 'stop()' );
  307. folder.add( API, 'reset()' );
  308. folder.add( API, 'time =', 0, clip.duration ).listen();
  309. folder.add( API, 'paused =' ).listen();
  310. folder.add( API, 'enabled =' ).listen();
  311. folder.add( API, 'clamp =' );
  312. folder.add( API, 'isRunning() =').listen();
  313. folder.add( API, 'play delayed' );
  314. folder.add( API, 'weight =', 0, 1 ).listen();
  315. folder.add( API, 'eff. weight', 0, 1 ).listen();
  316. folder.add( API, 'fade in' );
  317. folder.add( API, 'fade out' );
  318. folder.add( API, 'timeScale =', -2, 2).listen();
  319. folder.add( API, 'eff.T.Scale', -2, 2).listen();
  320. folder.add( API, 'time warp' );
  321. folder.add( API, 'loop mode', {
  322. "LoopOnce": THREE.LoopOnce,
  323. "LoopRepeat": THREE.LoopRepeat,
  324. "LoopPingPong": THREE.LoopPingPong
  325. } );
  326. folder.add( API, 'repetitions', 0, Infinity );
  327. folder.add( API, 'local root', rootNames );
  328. API[ 'play()' ]();
  329. }; // function clipControl
  330. // one folder per clip
  331. clipControl( gui, mixer, bonesClip, [ null, mesh, mesh2 ] );
  332. clipControl( gui, mixer, facesClip, [ null, mesh, mesh2 ] );
  333. var memoryControl = function( gui, mixer, clips, rootObjects ) {
  334. var clipNames = objectNames( clips ),
  335. rootNames = objectNames( rootObjects );
  336. var folder = gui.addFolder( "Memory Management" ),
  337. clipName = clipNames[ 0 ],
  338. clip = clips[ 0 ],
  339. rootName = rootNames[ 0 ],
  340. root = rootObjects[ 0 ],
  341. API = {
  342. get 'clip'() { return clipName; },
  343. set 'clip'( value ) {
  344. clipName = value;
  345. clip = clips[ clipNames.indexOf( clipName ) ];
  346. },
  347. get 'root'() { return rootName; },
  348. set 'root'( value ) {
  349. rootName = value;
  350. root = rootObjects[ rootNames.indexOf( rootName ) ];
  351. },
  352. 'uncache clip': function() {
  353. mixer.uncacheClip( clip );
  354. },
  355. 'uncache root': function() {
  356. mixer.uncacheRoot( root );
  357. },
  358. 'uncache action': function() {
  359. mixer.uncacheAction( clip, root );
  360. }
  361. };
  362. folder.add( API, 'clip', clipNames );
  363. folder.add( API, 'root', rootNames );
  364. folder.add( API, 'uncache root' );
  365. folder.add( API, 'uncache clip' );
  366. folder.add( API, 'uncache action' );
  367. };
  368. memoryControl( gui, mixer,
  369. [ bonesClip, facesClip ], [ mesh, mesh2 ] );
  370. }
  371. function onDocumentMouseMove( event ) {
  372. mouseX = ( event.clientX - windowHalfX );
  373. mouseY = ( event.clientY - windowHalfY );
  374. }
  375. //
  376. function animate() {
  377. requestAnimationFrame( animate );
  378. stats.begin();
  379. render();
  380. stats.end();
  381. if ( showMemInfo ) {
  382. var s = mixer.stats,
  383. ciS = s.controlInterpolants;
  384. domMemInfo.innerHTML =
  385. s.actions.inUse + " / " + s.actions.total + " actions " +
  386. s.bindings.inUse + " / " + s.bindings.total + " bindings " +
  387. ciS.inUse + " / " + ciS.total + " control interpolants";
  388. }
  389. }
  390. function render() {
  391. var delta = 0.75 * clock.getDelta();
  392. camera.position.x += ( mouseX - camera.position.x ) * .05;
  393. camera.position.y = THREE.Math.clamp( camera.position.y + ( - mouseY - camera.position.y ) * .05, 0, 1000 );
  394. camera.lookAt( scene.position );
  395. if( mixer ) {
  396. mixer.update( delta );
  397. }
  398. renderer.render( scene, camera );
  399. }
  400. </script>
  401. </body>
  402. </html>