webgl_loader_x.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank">three.js</a> - X-File Loader <br>
  12. <a href="https://github.com/adrs2002/threeXfileLoader" target="_new">see more info and samples</a> <br /><br/>
  13. <select id="mech1_anime">
  14. <option value="stand">stand</option>
  15. <option value="wark">walk</option>
  16. <option value="attack">attack</option>
  17. </select>
  18. </div>
  19. <div id="canvase3d"></div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import Stats from './jsm/libs/stats.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { XLoader } from './jsm/loaders/XLoader.js';
  25. var container, stats, controls;
  26. var camera, scene, renderer;
  27. var clock = new THREE.Clock();
  28. var manager = null;
  29. var skeletonHelper = null;
  30. var animates = [];
  31. var actions = [];
  32. var Models = [];
  33. init();
  34. var onProgress = function ( xhr ) {
  35. if ( xhr.lengthComputable ) {
  36. var percentComplete = xhr.loaded / xhr.total * 100;
  37. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  38. }
  39. };
  40. var onError = function () {};
  41. function init() {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. scene = new THREE.Scene();
  45. scene.add( new THREE.AmbientLight( 0xaaaaaa ) );
  46. // grid
  47. var gridHelper = new THREE.GridHelper( 14, 1, 0x303030, 0x303030 );
  48. scene.add( gridHelper );
  49. // stats
  50. stats = new Stats();
  51. container.appendChild( stats.dom );
  52. renderer = new THREE.WebGLRenderer();
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.setClearColor( 0x666666 );
  56. container.appendChild( renderer.domElement );
  57. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  58. camera.position.set( 2, 10, - 28 );
  59. camera.up.set( 0, 1, 0 );
  60. controls = new OrbitControls( camera, renderer.domElement );
  61. controls.target.set( 0, 5, 0 );
  62. controls.update();
  63. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  64. light.position.set( 10, 100, - 10 ).normalize();
  65. scene.add( light );
  66. var light2 = new THREE.DirectionalLight( 0x777666, 1 );
  67. light2.position.set( - 1, - 1, - 1 ).normalize();
  68. scene.add( light2 );
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. animate();
  71. // model loading
  72. manager = new THREE.LoadingManager();
  73. manager.onProgress = function ( item, loaded, total ) {
  74. console.log( item, loaded, total );
  75. };
  76. var loader = new XLoader( manager );
  77. actions[ 0 ] = {};
  78. //download Model file
  79. loader.load( [ 'models/xfile/SSR06_model.x' ], function ( object ) {
  80. for ( var i = 0; i < object.models.length; i ++ ) {
  81. var model = object.models[ i ];
  82. model.scale.x *= - 1;
  83. Models.push( model );
  84. }
  85. loadAnimation( 'stand', 0, () => {
  86. scene.add( Models[ 0 ] );
  87. if ( Models[ 0 ] instanceof THREE.SkinnedMesh ) {
  88. skeletonHelper = new THREE.SkeletonHelper( Models[ 0 ] );
  89. scene.add( skeletonHelper );
  90. }
  91. actions[ 0 ][ 'stand' ].play();
  92. } );
  93. object = null;
  94. }, onProgress, onError );
  95. //
  96. var animationSelection = document.getElementById( 'mech1_anime' );
  97. animationSelection.addEventListener( 'change', mech1_changeAnime );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. var delta = clock.getDelta();
  107. if ( animates != null && animates.length > 0 ) {
  108. for ( var i = 0; i < animates.length; i ++ ) {
  109. animates[ i ].update( delta * 1000 );
  110. }
  111. }
  112. stats.update();
  113. render();
  114. }
  115. function render() {
  116. renderer.render( scene, camera );
  117. }
  118. /////////////////////////////////////////////////////////////////////////////////
  119. function loadAnimation( animeName, modelId, _callback ) {
  120. if ( actions[ modelId ][ animeName ] ) {
  121. if ( _callback ) {
  122. _callback();
  123. }
  124. } else {
  125. var loader2 = new XLoader( manager );
  126. loader2.load( [ 'models/xfile/' + animeName + '.x', { putPos: false, putScl: false } ], function () {
  127. // !! important!
  128. // associate divided model and animation.
  129. loader2.assignAnimation( Models[ modelId ] );
  130. if ( ! animates[ modelId ] ) {
  131. animates[ modelId ] = Models[ modelId ].animationMixer;
  132. }
  133. actions[ modelId ][ animeName ] = Models[ modelId ].animationMixer.clipAction( animeName );
  134. if ( animeName == 'stand' ) {
  135. actions[ modelId ][ animeName ].setLoop( THREE.LoopOnce );
  136. }
  137. actions[ modelId ][ animeName ].clampWhenFinished = true;
  138. if ( _callback ) {
  139. _callback(); return;
  140. }
  141. actions[ modelId ][ animeName ].play();
  142. }, onProgress, onError );
  143. }
  144. }
  145. function mech1_changeAnime( event ) {
  146. var val = event.target.value;
  147. loadAnimation( val, 0, function () {
  148. Object.keys( actions[ 0 ] ).forEach( function ( p ) {
  149. if ( p == val ) {
  150. actions[ 0 ][ p ].play();
  151. } else {
  152. actions[ 0 ][ p ].stop();
  153. }
  154. } );
  155. } );
  156. }
  157. </script>
  158. </body>
  159. </html>