webgl_animation_skinning_morph.html 14 KB

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