webgl_animation_skinning_morph.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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">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.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.setClearColor( scene.fog.color );
  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. //ensureLoop( geometry.animation );
  122. geometry.computeBoundingBox();
  123. var bb = geometry.boundingBox;
  124. var path = "textures/cube/Park2/";
  125. var format = '.jpg';
  126. var urls = [
  127. path + 'posx' + format, path + 'negx' + format,
  128. path + 'posy' + format, path + 'negy' + format,
  129. path + 'posz' + format, path + 'negz' + format
  130. ];
  131. for ( var i = 0; i < materials.length; i ++ ) {
  132. var m = materials[ i ];
  133. m.morphTargets = true;
  134. m.specular.setHSL( 0, 0, 0.1 );
  135. m.color.setHSL( 0.6, 0, 0.6 );
  136. //m.map = map;
  137. //m.envMap = envMap;
  138. //m.bumpMap = bumpMap;
  139. //m.bumpScale = 2;
  140. //m.combine = THREE.MixOperation;
  141. //m.reflectivity = 0.75;
  142. }
  143. mesh = new THREE.SkinnedMesh( geometry, new THREE.MultiMaterial( materials ) );
  144. mesh.name = "Knight Mesh";
  145. mesh.position.set( x, y - bb.min.y * s, z );
  146. mesh.scale.set( s, s, s );
  147. scene.add( mesh );
  148. mesh.castShadow = true;
  149. mesh.receiveShadow = true;
  150. mesh2 = new THREE.SkinnedMesh( geometry, new THREE.MultiMaterial( materials ) );
  151. mesh2.name = "Lil' Bro Mesh";
  152. mesh2.position.set( x - 240, y - bb.min.y * s, z + 500 );
  153. mesh2.scale.set( s / 2, s / 2, s / 2 );
  154. mesh2.rotation.y = THREE.Math.degToRad( 60 );
  155. mesh2.visible = false;
  156. mesh2.castShadow = true;
  157. mesh2.receiveShadow = true;
  158. scene.add( mesh2 );
  159. helper = new THREE.SkeletonHelper( mesh );
  160. helper.material.linewidth = 3;
  161. helper.visible = false;
  162. scene.add( helper );
  163. mixer = new THREE.AnimationMixer( mesh );
  164. bonesClip = geometry.animations[0];
  165. facesClip = THREE.AnimationClip.CreateFromMorphTargetSequence( 'facialExpressions', mesh.geometry.morphTargets, 3 );
  166. }
  167. function initGUI() {
  168. var API = {
  169. 'show model' : true,
  170. 'show skeleton' : false,
  171. 'show 2nd model' : false,
  172. 'show mem. info' : false
  173. };
  174. var gui = new dat.GUI();
  175. gui.add( API, 'show model' ).onChange( function() {
  176. mesh.visible = API[ 'show model' ];
  177. } );
  178. gui.add( API, 'show skeleton' ).onChange( function() {
  179. helper.visible = API[ 'show skeleton' ];
  180. } );
  181. gui.add( API, 'show 2nd model' ).onChange( function() {
  182. mesh2.visible = API[ 'show 2nd model' ];
  183. } );
  184. gui.add( API, 'show mem. info' ).onChange( function() {
  185. showMemInfo = API[ 'show mem. info' ];
  186. domMemInfo.style.display = showMemInfo ? 'block' : 'none';
  187. } );
  188. // utility function used for drop-down options lists in the GUI
  189. var objectNames = function( objects ) {
  190. var result = [];
  191. for ( var i = 0, n = objects.length; i !== n; ++ i ) {
  192. var obj = objects[ i ];
  193. result.push( obj && obj.name || '&lt;null&gt;' );
  194. }
  195. return result;
  196. };
  197. // creates gui folder with tests / examples for the action API
  198. var clipControl = function clipControl( gui, mixer, clip, rootObjects ) {
  199. var folder = gui.addFolder( "Clip '" + clip.name + "'" ),
  200. rootNames = objectNames( rootObjects ),
  201. rootName = rootNames[ 0 ],
  202. root = rootObjects[ 0 ],
  203. action = null,
  204. API = {
  205. 'play()': function play() {
  206. action = mixer.clipAction( clip, root );
  207. action.play();
  208. },
  209. 'stop()': function() {
  210. action = mixer.clipAction( clip, root );
  211. action.stop();
  212. },
  213. 'reset()': function() {
  214. action = mixer.clipAction( clip, root );
  215. action.reset();
  216. },
  217. get 'time ='() {
  218. return action !== null ? action.time : 0;
  219. },
  220. set 'time ='( value ) {
  221. action = mixer.clipAction( clip, root );
  222. action.time = value;
  223. },
  224. get 'paused ='() {
  225. return action !== null && action.paused;
  226. },
  227. set 'paused ='( value ) {
  228. action = mixer.clipAction( clip, root );
  229. action.paused = value;
  230. },
  231. get 'enabled ='() {
  232. return action !== null && action.enabled;
  233. },
  234. set 'enabled ='( value ) {
  235. action = mixer.clipAction( clip, root );
  236. action.enabled = value;
  237. },
  238. get 'clamp ='() {
  239. return action !== null ? action.clampWhenFinished : false;
  240. },
  241. set 'clamp ='( value ) {
  242. action = mixer.clipAction( clip, root );
  243. action.clampWhenFinished = value;
  244. },
  245. get 'isRunning() ='() {
  246. return action !== null && action.isRunning();
  247. },
  248. set 'isRunning() ='( value ) {
  249. alert( "Read only - this is the result of a method." );
  250. },
  251. 'play delayed': function() {
  252. action = mixer.clipAction( clip, root );
  253. action.startAt( mixer.time + 0.5 ).play();
  254. },
  255. get 'weight ='() {
  256. return action !== null ? action.weight : 1;
  257. },
  258. set 'weight ='( value ) {
  259. action = mixer.clipAction( clip, root );
  260. action.weight = value;
  261. },
  262. get 'eff. weight'() {
  263. return action !== null ? action.getEffectiveWeight() : 1;
  264. },
  265. set 'eff. weight'( value ) {
  266. action = mixer.clipAction( clip, root );
  267. action.setEffectiveWeight( value );
  268. },
  269. 'fade in': function() {
  270. action = mixer.clipAction( clip, root );
  271. action.reset().fadeIn( 0.25 ).play();
  272. },
  273. 'fade out': function() {
  274. action = mixer.clipAction( clip, root );
  275. action.fadeOut( 0.25 ).play();
  276. },
  277. get 'timeScale ='() {
  278. return ( action !== null ) ? action.timeScale : 1;
  279. },
  280. set 'timeScale ='( value ) {
  281. action = mixer.clipAction( clip, root );
  282. action.timeScale = value;
  283. },
  284. get 'eff.T.Scale'() {
  285. return ( action !== null ) ? action.getEffectiveTimeScale() : 1;
  286. },
  287. set 'eff.T.Scale'( value ) {
  288. action = mixer.clipAction( clip, root );
  289. action.setEffectiveTimeScale( value );
  290. },
  291. 'time warp': function() {
  292. action = mixer.clipAction( clip, root );
  293. var timeScaleNow = action.getEffectiveTimeScale();
  294. var destTimeScale = timeScaleNow > 0 ? -1 : 1;
  295. action.warp( timeScaleNow, destTimeScale, 4 ).play();
  296. },
  297. get 'loop mode'() {
  298. return action !== null ? action.loop : THREE.LoopRepeat;
  299. },
  300. set 'loop mode'( value ) {
  301. action = mixer.clipAction( clip, root );
  302. action.loop = + value;
  303. },
  304. get 'repetitions'() {
  305. return action !== null ? action.repetitions : Infinity;
  306. },
  307. set 'repetitions'( value ) {
  308. action = mixer.clipAction( clip, root );
  309. action.repetitions = + value;
  310. },
  311. get 'local root'() { return rootName; },
  312. set 'local root'( value ) {
  313. rootName = value;
  314. root = rootObjects[ rootNames.indexOf( rootName ) ];
  315. action = mixer.clipAction( clip, root );
  316. }
  317. };
  318. folder.add( API, 'play()' );
  319. folder.add( API, 'stop()' );
  320. folder.add( API, 'reset()' );
  321. folder.add( API, 'time =', 0, clip.duration ).listen();
  322. folder.add( API, 'paused =' ).listen();
  323. folder.add( API, 'enabled =' ).listen();
  324. folder.add( API, 'clamp =' );
  325. folder.add( API, 'isRunning() =').listen();
  326. folder.add( API, 'play delayed' );
  327. folder.add( API, 'weight =', 0, 1 ).listen();
  328. folder.add( API, 'eff. weight', 0, 1 ).listen();
  329. folder.add( API, 'fade in' );
  330. folder.add( API, 'fade out' );
  331. folder.add( API, 'timeScale =', -2, 2).listen();
  332. folder.add( API, 'eff.T.Scale', -2, 2).listen();
  333. folder.add( API, 'time warp' );
  334. folder.add( API, 'loop mode', {
  335. "LoopOnce": THREE.LoopOnce,
  336. "LoopRepeat": THREE.LoopRepeat,
  337. "LoopPingPong": THREE.LoopPingPong
  338. } );
  339. folder.add( API, 'repetitions', 0, Infinity );
  340. folder.add( API, 'local root', rootNames );
  341. API[ 'play()' ]();
  342. }; // function clipControl
  343. // one folder per clip
  344. clipControl( gui, mixer, bonesClip, [ null, mesh, mesh2 ] );
  345. clipControl( gui, mixer, facesClip, [ null, mesh, mesh2 ] );
  346. var memoryControl = function( gui, mixer, clips, rootObjects ) {
  347. var clipNames = objectNames( clips ),
  348. rootNames = objectNames( rootObjects );
  349. var folder = gui.addFolder( "Memory Management" ),
  350. clipName = clipNames[ 0 ],
  351. clip = clips[ 0 ],
  352. rootName = rootNames[ 0 ],
  353. root = rootObjects[ 0 ],
  354. API = {
  355. get 'clip'() { return clipName; },
  356. set 'clip'( value ) {
  357. clipName = value;
  358. clip = clips[ clipNames.indexOf( clipName ) ];
  359. },
  360. get 'root'() { return rootName; },
  361. set 'root'( value ) {
  362. rootName = value;
  363. root = rootObjects[ rootNames.indexOf( rootName ) ];
  364. },
  365. 'uncache clip': function() {
  366. mixer.uncacheClip( clip );
  367. },
  368. 'uncache root': function() {
  369. mixer.uncacheRoot( root );
  370. },
  371. 'uncache action': function() {
  372. mixer.uncacheAction( clip, root );
  373. }
  374. };
  375. folder.add( API, 'clip', clipNames );
  376. folder.add( API, 'root', rootNames );
  377. folder.add( API, 'uncache root' );
  378. folder.add( API, 'uncache clip' );
  379. folder.add( API, 'uncache action' );
  380. };
  381. memoryControl( gui, mixer,
  382. [ bonesClip, facesClip ], [ mesh, mesh2 ] );
  383. }
  384. function onDocumentMouseMove( event ) {
  385. mouseX = ( event.clientX - windowHalfX );
  386. mouseY = ( event.clientY - windowHalfY );
  387. }
  388. //
  389. function animate() {
  390. requestAnimationFrame( animate );
  391. stats.begin();
  392. render();
  393. stats.end();
  394. if ( showMemInfo ) {
  395. var s = mixer.stats,
  396. ciS = s.controlInterpolants;
  397. domMemInfo.innerHTML =
  398. s.actions.inUse + " / " + s.actions.total + " actions " +
  399. s.bindings.inUse + " / " + s.bindings.total + " bindings " +
  400. ciS.inUse + " / " + ciS.total + " control interpolants";
  401. }
  402. }
  403. function render() {
  404. var delta = 0.75 * clock.getDelta();
  405. camera.position.x += ( mouseX - camera.position.x ) * .05;
  406. camera.position.y = THREE.Math.clamp( camera.position.y + ( - mouseY - camera.position.y ) * .05, 0, 1000 );
  407. camera.lookAt( scene.position );
  408. if( mixer ) {
  409. mixer.update( delta );
  410. helper.update();
  411. }
  412. renderer.render( scene, camera );
  413. }
  414. </script>
  415. </body>
  416. </html>