webgl_loader_mmd_pose.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - MMD 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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MMDLoader test<br />
  21. <a href="https://github.com/mrdoob/three.js/tree/master/examples/models/mmd#readme" target="_blank" rel="noopener">MMD Assets license</a><br />
  22. Copyright
  23. <a href="http://www.geocities.jp/higuchuu4/index_e.htm" target="_blank" rel="noopener">Model Data</a>
  24. <a href="http://seiga.nicovideo.jp/seiga/im5162984" target="_blank" rel="noopener">Pose Data</a>
  25. </div>
  26. <script src="js/libs/ammo.js"></script>
  27. <script type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import { GUI } from './jsm/libs/dat.gui.module.js';
  30. import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
  31. import { MMDLoader } from './jsm/loaders/MMDLoader.js';
  32. import { MMDAnimationHelper } from './jsm/animation/MMDAnimationHelper.js';
  33. var container;
  34. var camera, scene, renderer, effect;
  35. var mesh, helper;
  36. var vpds = [];
  37. Ammo().then( function ( AmmoLib ) {
  38. Ammo = AmmoLib;
  39. init();
  40. animate();
  41. } );
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  46. camera.position.z = 25;
  47. // scene
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0xffffff );
  50. var ambient = new THREE.AmbientLight( 0x666666 );
  51. scene.add( ambient );
  52. var directionalLight = new THREE.DirectionalLight( 0x887766 );
  53. directionalLight.position.set( - 1, 1, 1 ).normalize();
  54. scene.add( directionalLight );
  55. //
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. container.appendChild( renderer.domElement );
  60. effect = new OutlineEffect( renderer );
  61. // model
  62. function onProgress( xhr ) {
  63. if ( xhr.lengthComputable ) {
  64. var percentComplete = xhr.loaded / xhr.total * 100;
  65. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  66. }
  67. }
  68. var modelFile = 'models/mmd/miku/miku_v2.pmd';
  69. var vpdFiles = [
  70. 'models/mmd/vpds/01.vpd',
  71. 'models/mmd/vpds/02.vpd',
  72. 'models/mmd/vpds/03.vpd',
  73. 'models/mmd/vpds/04.vpd',
  74. 'models/mmd/vpds/05.vpd',
  75. 'models/mmd/vpds/06.vpd',
  76. 'models/mmd/vpds/07.vpd',
  77. 'models/mmd/vpds/08.vpd',
  78. //'models/mmd/vpds/09.vpd',
  79. //'models/mmd/vpds/10.vpd',
  80. 'models/mmd/vpds/11.vpd'
  81. ];
  82. helper = new MMDAnimationHelper();
  83. var loader = new MMDLoader();
  84. loader.load( modelFile, function ( object ) {
  85. mesh = object;
  86. mesh.position.y = - 10;
  87. scene.add( mesh );
  88. var vpdIndex = 0;
  89. function loadVpd() {
  90. var vpdFile = vpdFiles[ vpdIndex ];
  91. loader.loadVPD( vpdFile, false, function ( vpd ) {
  92. vpds.push( vpd );
  93. vpdIndex ++;
  94. if ( vpdIndex < vpdFiles.length ) {
  95. loadVpd();
  96. } else {
  97. initGui();
  98. }
  99. }, onProgress, null );
  100. }
  101. loadVpd();
  102. }, onProgress, null );
  103. //
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. function initGui() {
  106. var gui = new GUI();
  107. var dictionary = mesh.morphTargetDictionary;
  108. var controls = {};
  109. var keys = [];
  110. var poses = gui.addFolder( 'Poses' );
  111. var morphs = gui.addFolder( 'Morphs' );
  112. function getBaseName( s ) {
  113. return s.slice( s.lastIndexOf( '/' ) + 1 );
  114. }
  115. function initControls() {
  116. for ( var key in dictionary ) {
  117. controls[ key ] = 0.0;
  118. }
  119. controls.pose = - 1;
  120. for ( var i = 0; i < vpdFiles.length; i ++ ) {
  121. controls[ getBaseName( vpdFiles[ i ] ) ] = false;
  122. }
  123. }
  124. function initKeys() {
  125. for ( var key in dictionary ) {
  126. keys.push( key );
  127. }
  128. }
  129. function initPoses() {
  130. var files = { default: - 1 };
  131. for ( var i = 0; i < vpdFiles.length; i ++ ) {
  132. files[ getBaseName( vpdFiles[ i ] ) ] = i;
  133. }
  134. poses.add( controls, 'pose', files ).onChange( onChangePose );
  135. }
  136. function initMorphs() {
  137. for ( var key in dictionary ) {
  138. morphs.add( controls, key, 0.0, 1.0, 0.01 ).onChange( onChangeMorph );
  139. }
  140. }
  141. function onChangeMorph() {
  142. for ( var i = 0; i < keys.length; i ++ ) {
  143. var key = keys[ i ];
  144. var value = controls[ key ];
  145. mesh.morphTargetInfluences[ i ] = value;
  146. }
  147. }
  148. function onChangePose() {
  149. var index = parseInt( controls.pose );
  150. if ( index === - 1 ) {
  151. mesh.pose();
  152. } else {
  153. helper.pose( mesh, vpds[ index ] );
  154. }
  155. }
  156. initControls();
  157. initKeys();
  158. initPoses();
  159. initMorphs();
  160. onChangeMorph();
  161. onChangePose();
  162. poses.open();
  163. morphs.open();
  164. }
  165. }
  166. function onWindowResize() {
  167. camera.aspect = window.innerWidth / window.innerHeight;
  168. camera.updateProjectionMatrix();
  169. effect.setSize( window.innerWidth, window.innerHeight );
  170. }
  171. //
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. render();
  175. }
  176. function render() {
  177. effect.render( scene, camera );
  178. }
  179. </script>
  180. </body>
  181. </html>