webgl_loader_x.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <!DOCTYPE html>
  2. <html lang='en'>
  3. <head>
  4. <title>three.js webgl - loaders - X-File loader</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: #ffffff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color: #fff;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. a {
  24. color: #ff0000
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - X-File Loader</div>
  30. <div id="container"></div>
  31. <script src='../build/three.js'></script>
  32. <script src='js/controls/OrbitControls.js'></script>
  33. <script src='js/loaders/XLoader.js'></script>
  34. <script src='js/Detector.js'></script>
  35. <script src='js/libs/stats.min.js'></script>
  36. <script src='js/libs/dat.gui.min.js'></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var stats;
  40. var camera, scene, renderer;
  41. var clock = new THREE.Clock();
  42. var gui = new dat.GUI();
  43. var mixers = [];
  44. var models = [];
  45. var animates = [];
  46. var actions = [];
  47. init();
  48. function init() {
  49. var container = document.getElementById( 'container' );
  50. document.body.appendChild( container );
  51. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  52. camera.position.set( 2, 20, 30 );
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0x666666 );
  55. // grid
  56. var gridHelper = new THREE.GridHelper( 20, 20 );
  57. scene.add( gridHelper );
  58. // stats
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. // model
  62. var loader = new THREE.XLoader();
  63. // read (download) model file
  64. loader.load( [ 'models/xfile/SSR06_Born2.x', false ], function ( object ) {
  65. for ( var i = 0; i < object.FrameInfo.length; i ++ ) {
  66. models.push( object.FrameInfo[ i ] );
  67. var model = models[ i ];
  68. scene.add( model );
  69. if ( model instanceof THREE.SkinnedMesh ) {
  70. var skeletonHelper = new THREE.SkeletonHelper( model );
  71. scene.add( skeletonHelper );
  72. if ( object.XAnimationObj !== undefined && object.XAnimationObj.length !== 0 ) {
  73. model.geometry.animations = [];
  74. model.geometry.animations.push( THREE.AnimationClip.parseAnimation( splitAnimation( object.XAnimationObj[ 0 ], 'stand', 10 * object.XAnimationObj[ 0 ].fps, 11 * object.XAnimationObj[ 0 ].fps), model.skeleton.bones ) );
  75. model.geometry.animations.push( THREE.AnimationClip.parseAnimation( splitAnimation( object.XAnimationObj[ 0 ], 'walk', 50 * object.XAnimationObj[ 0 ].fps, 80 * object.XAnimationObj[ 0 ].fps), model.skeleton.bones ) );
  76. model.geometry.animations.push( THREE.AnimationClip.parseAnimation( splitAnimation( object.XAnimationObj[ 0 ], 'dash', 140 * object.XAnimationObj[ 0 ].fps, 160 * object.XAnimationObj[ 0 ].fps), model.skeleton.bones ) );
  77. model.geometry.animations.push( THREE.AnimationClip.parseAnimation( splitAnimation( object.XAnimationObj[ 0 ], 'dashing', 160 * object.XAnimationObj[ 0 ].fps, 165 * object.XAnimationObj[ 0 ].fps), model.skeleton.bones ) );
  78. model.geometry.animations.push( THREE.AnimationClip.parseAnimation( splitAnimation( object.XAnimationObj[ 0 ], 'damage', 500 * object.XAnimationObj[ 0 ].fps, 530 * object.XAnimationObj[ 0 ].fps), model.skeleton.bones ) );
  79. model.mixer = new THREE.AnimationMixer( model );
  80. animates.push( model.mixer );
  81. var stand = model.mixer.clipAction( 'stand' );
  82. stand.setLoop( THREE.LoopRepeat );
  83. actions[ 'stand' ] = stand;
  84. var walk = model.mixer.clipAction( 'walk' );
  85. walk.setLoop( THREE.LoopRepeat );
  86. walk.play();
  87. actions[ 'walk' ] = walk;
  88. var dash = model.mixer.clipAction( 'dash' );
  89. dash.setLoop( THREE.LoopRepeat );
  90. actions[ 'dash' ] = dash;
  91. var dashing = model.mixer.clipAction( 'dashing' );
  92. dashing.setLoop( THREE.LoopPingPong );
  93. actions[ 'dashing' ] = dashing;
  94. var damage = model.mixer.clipAction( 'damage' );
  95. damage.setLoop( THREE.LoopRepeat );
  96. actions[ 'damage' ] = damage;
  97. var actionKeys = Object.keys( actions );
  98. var dmy = {};
  99. dmy.gui = '';
  100. dmy.action = '';
  101. gui.add( dmy, 'action', actionKeys ).onChange( function ( v ) {
  102. animates[ 0 ].stopAllAction();
  103. actions[ v ].play();
  104. });
  105. }
  106. }
  107. }
  108. object = null;
  109. } );
  110. renderer = new THREE.WebGLRenderer();
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. container.appendChild( renderer.domElement );
  114. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  115. controls.target.set( 0, 6, 0 );
  116. controls.update();
  117. var directionalLight1 = new THREE.DirectionalLight( 0xeeeeff, 2 );
  118. directionalLight1.position.set( 10, 100, 1 ).normalize();
  119. scene.add( directionalLight1 );
  120. var directionalLight2 = new THREE.DirectionalLight( 0x555588 );
  121. directionalLight2.position.set( - 1, - 1, - 1 ).normalize();
  122. scene.add( directionalLight2 );
  123. var ambientLight = new THREE.AmbientLight( 0x999999 );
  124. scene.add( ambientLight );
  125. window.addEventListener( 'resize', onWindowResize, false );
  126. animate();
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. //
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. var delta = clock.getDelta();
  137. if ( animates ) {
  138. for ( var i = 0; i < animates.length; i ++ ) {
  139. animates[ i ].update( delta * 1000 );
  140. }
  141. }
  142. stats.update();
  143. render();
  144. }
  145. function render() {
  146. renderer.render( scene, camera );
  147. }
  148. // this is not must mount codes.
  149. // split One and Long Animation, for time
  150. function splitAnimation( _baseAnime, _name, _beginTime, _endTime ) {
  151. var animation = {};
  152. animation.fps = _baseAnime.fps;
  153. animation.name = _name;
  154. animation.length = _endTime - _beginTime;
  155. animation.hierarchy = [];
  156. for ( var i = 0; i < _baseAnime.hierarchy.length; i ++ ) {
  157. var firstKey = -1;
  158. var lastKey = -1;
  159. var frame = {};
  160. frame.name = _baseAnime.hierarchy[ i ].name;
  161. frame.parent = _baseAnime.hierarchy[ i ].parent;
  162. frame.keys = [];
  163. for ( var m = 1; m < _baseAnime.hierarchy[ i ].keys.length; m ++ ) {
  164. if ( _baseAnime.hierarchy[ i ].keys[ m ].time > _beginTime ) {
  165. if ( firstKey === -1 ) {
  166. firstKey = m - 1;
  167. frame.keys.push( _baseAnime.hierarchy[ i ].keys[ m - 1 ] );
  168. }
  169. frame.keys.push( _baseAnime.hierarchy[ i ].keys[ m ] );
  170. }
  171. if ( _endTime <= _baseAnime.hierarchy[ i ].keys[ m ].time || m >= _baseAnime.hierarchy[ i ].keys.length - 1 ) {
  172. break;
  173. }
  174. }
  175. for ( var m = 0; m < frame.keys.length; m ++ ) {
  176. frame.keys[ m ].time -= _beginTime;
  177. }
  178. animation.hierarchy.push( frame );
  179. }
  180. return animation;
  181. }
  182. </script>
  183. </body>
  184. </html>