2
0

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. console.log( 'materials', materials );
  112. createScene( geometry, materials, 0, FLOOR, -300, 60 )
  113. // GUI
  114. initGUI();
  115. } );
  116. //
  117. window.addEventListener( 'resize', onWindowResize, false );
  118. }
  119. function onWindowResize() {
  120. windowHalfX = window.innerWidth / 2;
  121. windowHalfY = window.innerHeight / 2;
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function createScene( geometry, materials, x, y, z, s ) {
  127. //ensureLoop( geometry.animation );
  128. geometry.computeBoundingBox();
  129. var bb = geometry.boundingBox;
  130. var path = "textures/cube/Park2/";
  131. var format = '.jpg';
  132. var urls = [
  133. path + 'posx' + format, path + 'negx' + format,
  134. path + 'posy' + format, path + 'negy' + format,
  135. path + 'posz' + format, path + 'negz' + format
  136. ];
  137. for ( var i = 0; i < materials.length; i ++ ) {
  138. var m = materials[ i ];
  139. m.skinning = true;
  140. m.morphTargets = true;
  141. m.specular.setHSL( 0, 0, 0.1 );
  142. m.color.setHSL( 0.6, 0, 0.6 );
  143. //m.map = map;
  144. //m.envMap = envMap;
  145. //m.bumpMap = bumpMap;
  146. //m.bumpScale = 2;
  147. //m.combine = THREE.MixOperation;
  148. //m.reflectivity = 0.75;
  149. }
  150. mesh = new THREE.SkinnedMesh( geometry, new THREE.MultiMaterial( materials ) );
  151. mesh.name = "Knight Mesh";
  152. mesh.position.set( x, y - bb.min.y * s, z );
  153. mesh.scale.set( s, s, s );
  154. scene.add( mesh );
  155. mesh.castShadow = true;
  156. mesh.receiveShadow = true;
  157. mesh2 = new THREE.SkinnedMesh( geometry, new THREE.MultiMaterial( materials ) );
  158. mesh2.name = "Lil' Bro Mesh";
  159. mesh2.position.set( x - 240, y - bb.min.y * s, z + 500 );
  160. mesh2.scale.set( s / 2, s / 2, s / 2 );
  161. mesh2.rotation.y = THREE.Math.degToRad( 60 );
  162. mesh2.visible = false;
  163. mesh2.castShadow = true;
  164. mesh2.receiveShadow = true;
  165. scene.add( mesh2 );
  166. helper = new THREE.SkeletonHelper( mesh );
  167. helper.material.linewidth = 3;
  168. helper.visible = false;
  169. scene.add( helper );
  170. mixer = new THREE.AnimationMixer( mesh );
  171. bonesClip = geometry.animations[0];
  172. facesClip = THREE.AnimationClip.CreateFromMorphTargetSequence( 'facialExpressions', mesh.geometry.morphTargets, 3 );
  173. }
  174. function initGUI() {
  175. var API = {
  176. 'show model' : true,
  177. 'show skeleton' : false,
  178. 'show 2nd model' : false,
  179. 'show mem. info' : false
  180. };
  181. var gui = new dat.GUI();
  182. gui.add( API, 'show model' ).onChange( function() {
  183. mesh.visible = API[ 'show model' ];
  184. } );
  185. gui.add( API, 'show skeleton' ).onChange( function() {
  186. helper.visible = API[ 'show skeleton' ];
  187. } );
  188. gui.add( API, 'show 2nd model' ).onChange( function() {
  189. mesh2.visible = API[ 'show 2nd model' ];
  190. } );
  191. gui.add( API, 'show mem. info' ).onChange( function() {
  192. showMemInfo = API[ 'show mem. info' ];
  193. domMemInfo.style.display = showMemInfo ? 'block' : 'none';
  194. } );
  195. // utility function used for drop-down options lists in the GUI
  196. var objectNames = function( objects ) {
  197. var result = [];
  198. for ( var i = 0, n = objects.length; i !== n; ++ i ) {
  199. var obj = objects[ i ];
  200. result.push( obj && obj.name || '&lt;null&gt;' );
  201. }
  202. return result;
  203. };
  204. // creates gui folder with tests / examples for the action API
  205. var clipControl = function clipControl( gui, mixer, clip, rootObjects ) {
  206. var folder = gui.addFolder( "Clip '" + clip.name + "'" ),
  207. rootNames = objectNames( rootObjects ),
  208. rootName = rootNames[ 0 ],
  209. root = rootObjects[ 0 ],
  210. action = null,
  211. API = {
  212. 'play()': function play() {
  213. action = mixer.clipAction( clip, root );
  214. action.play();
  215. },
  216. 'stop()': function() {
  217. action = mixer.clipAction( clip, root );
  218. action.stop();
  219. },
  220. 'reset()': function() {
  221. action = mixer.clipAction( clip, root );
  222. action.reset();
  223. },
  224. get 'time ='() {
  225. return action !== null ? action.time : 0;
  226. },
  227. set 'time ='( value ) {
  228. action = mixer.clipAction( clip, root );
  229. action.time = value;
  230. },
  231. get 'paused ='() {
  232. return action !== null && action.paused;
  233. },
  234. set 'paused ='( value ) {
  235. action = mixer.clipAction( clip, root );
  236. action.paused = value;
  237. },
  238. get 'enabled ='() {
  239. return action !== null && action.enabled;
  240. },
  241. set 'enabled ='( value ) {
  242. action = mixer.clipAction( clip, root );
  243. action.enabled = value;
  244. },
  245. get 'clamp ='() {
  246. return action !== null ? action.clampWhenFinished : false;
  247. },
  248. set 'clamp ='( value ) {
  249. action = mixer.clipAction( clip, root );
  250. action.clampWhenFinished = value;
  251. },
  252. get 'isRunning() ='() {
  253. return action !== null && action.isRunning();
  254. },
  255. set 'isRunning() ='( value ) {
  256. alert( "Read only - this is the result of a method." );
  257. },
  258. 'play delayed': function() {
  259. action = mixer.clipAction( clip, root );
  260. action.startAt( mixer.time + 0.5 ).play();
  261. },
  262. get 'weight ='() {
  263. return action !== null ? action.weight : 1;
  264. },
  265. set 'weight ='( value ) {
  266. action = mixer.clipAction( clip, root );
  267. action.weight = value;
  268. },
  269. get 'eff. weight'() {
  270. return action !== null ? action.getEffectiveWeight() : 1;
  271. },
  272. set 'eff. weight'( value ) {
  273. action = mixer.clipAction( clip, root );
  274. action.setEffectiveWeight( value );
  275. },
  276. 'fade in': function() {
  277. action = mixer.clipAction( clip, root );
  278. action.reset().fadeIn( 0.25 ).play();
  279. },
  280. 'fade out': function() {
  281. action = mixer.clipAction( clip, root );
  282. action.fadeOut( 0.25 ).play();
  283. },
  284. get 'timeScale ='() {
  285. return ( action !== null ) ? action.timeScale : 1;
  286. },
  287. set 'timeScale ='( value ) {
  288. action = mixer.clipAction( clip, root );
  289. action.timeScale = value;
  290. },
  291. get 'eff.T.Scale'() {
  292. return ( action !== null ) ? action.getEffectiveTimeScale() : 1;
  293. },
  294. set 'eff.T.Scale'( value ) {
  295. action = mixer.clipAction( clip, root );
  296. action.setEffectiveTimeScale( value );
  297. },
  298. 'time warp': function() {
  299. action = mixer.clipAction( clip, root );
  300. var timeScaleNow = action.getEffectiveTimeScale();
  301. var destTimeScale = timeScaleNow > 0 ? -1 : 1;
  302. action.warp( timeScaleNow, destTimeScale, 4 ).play();
  303. },
  304. get 'loop mode'() {
  305. return action !== null ? action.loop : THREE.LoopRepeat;
  306. },
  307. set 'loop mode'( value ) {
  308. action = mixer.clipAction( clip, root );
  309. action.loop = + value;
  310. },
  311. get 'repetitions'() {
  312. return action !== null ? action.repetitions : Infinity;
  313. },
  314. set 'repetitions'( value ) {
  315. action = mixer.clipAction( clip, root );
  316. action.repetitions = + value;
  317. },
  318. get 'local root'() { return rootName; },
  319. set 'local root'( value ) {
  320. rootName = value;
  321. root = rootObjects[ rootNames.indexOf( rootName ) ];
  322. action = mixer.clipAction( clip, root );
  323. }
  324. };
  325. folder.add( API, 'play()' );
  326. folder.add( API, 'stop()' );
  327. folder.add( API, 'reset()' );
  328. folder.add( API, 'time =', 0, clip.duration ).listen();
  329. folder.add( API, 'paused =' ).listen();
  330. folder.add( API, 'enabled =' ).listen();
  331. folder.add( API, 'clamp =' );
  332. folder.add( API, 'isRunning() =').listen();
  333. folder.add( API, 'play delayed' );
  334. folder.add( API, 'weight =', 0, 1 ).listen();
  335. folder.add( API, 'eff. weight', 0, 1 ).listen();
  336. folder.add( API, 'fade in' );
  337. folder.add( API, 'fade out' );
  338. folder.add( API, 'timeScale =', -2, 2).listen();
  339. folder.add( API, 'eff.T.Scale', -2, 2).listen();
  340. folder.add( API, 'time warp' );
  341. folder.add( API, 'loop mode', {
  342. "LoopOnce": THREE.LoopOnce,
  343. "LoopRepeat": THREE.LoopRepeat,
  344. "LoopPingPong": THREE.LoopPingPong
  345. } );
  346. folder.add( API, 'repetitions', 0, Infinity );
  347. folder.add( API, 'local root', rootNames );
  348. API[ 'play()' ]();
  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>