webgl_animation_skinning_morph.html 14 KB

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