webgl_animation_skinning_morph.html 14 KB

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