PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/webgl_animation_skinning_morph.html

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