webgl_loader_mmd.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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="https://sites.google.com/view/evpvp/" target="_blank" rel="noopener">Model Data</a>
  24. <a href="http://www.nicovideo.jp/watch/sm13147122" target="_blank" rel="noopener">Dance Data</a>
  25. </div>
  26. <script src="jsm/libs/ammo.wasm.js"></script>
  27. <!-- Import maps polyfill -->
  28. <!-- Remove this when import maps will be widely supported -->
  29. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  30. <script type="importmap">
  31. {
  32. "imports": {
  33. "three": "../build/three.module.js",
  34. "three/addons/": "./jsm/"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three';
  40. import Stats from 'three/addons/libs/stats.module.js';
  41. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  42. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  43. import { OutlineEffect } from 'three/addons/effects/OutlineEffect.js';
  44. import { MMDLoader } from 'three/addons/loaders/MMDLoader.js';
  45. import { MMDAnimationHelper } from 'three/addons/animation/MMDAnimationHelper.js';
  46. let stats;
  47. let mesh, camera, scene, renderer, effect;
  48. let helper, ikHelper, physicsHelper;
  49. const clock = new THREE.Clock();
  50. Ammo().then( function ( AmmoLib ) {
  51. Ammo = AmmoLib;
  52. init();
  53. animate();
  54. } );
  55. function init() {
  56. const container = document.createElement( 'div' );
  57. document.body.appendChild( container );
  58. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  59. camera.position.z = 30;
  60. // scene
  61. scene = new THREE.Scene();
  62. scene.background = new THREE.Color( 0xffffff );
  63. const gridHelper = new THREE.PolarGridHelper( 30, 0 );
  64. gridHelper.position.y = - 10;
  65. scene.add( gridHelper );
  66. const ambient = new THREE.AmbientLight( 0x666666 );
  67. scene.add( ambient );
  68. const directionalLight = new THREE.DirectionalLight( 0x887766 );
  69. directionalLight.position.set( - 1, 1, 1 ).normalize();
  70. scene.add( directionalLight );
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. container.appendChild( renderer.domElement );
  76. effect = new OutlineEffect( renderer );
  77. // STATS
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. // model
  81. function onProgress( xhr ) {
  82. if ( xhr.lengthComputable ) {
  83. const percentComplete = xhr.loaded / xhr.total * 100;
  84. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  85. }
  86. }
  87. const modelFile = 'models/mmd/miku/miku_v2.pmd';
  88. const vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
  89. helper = new MMDAnimationHelper( {
  90. afterglow: 2.0
  91. } );
  92. const loader = new MMDLoader();
  93. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  94. mesh = mmd.mesh;
  95. mesh.position.y = - 10;
  96. scene.add( mesh );
  97. helper.add( mesh, {
  98. animation: mmd.animation,
  99. physics: true
  100. } );
  101. ikHelper = helper.objects.get( mesh ).ikSolver.createHelper();
  102. ikHelper.visible = false;
  103. scene.add( ikHelper );
  104. physicsHelper = helper.objects.get( mesh ).physics.createHelper();
  105. physicsHelper.visible = false;
  106. scene.add( physicsHelper );
  107. initGui();
  108. }, onProgress, null );
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.minDistance = 10;
  111. controls.maxDistance = 100;
  112. window.addEventListener( 'resize', onWindowResize );
  113. function initGui() {
  114. const api = {
  115. 'animation': true,
  116. 'ik': true,
  117. 'outline': true,
  118. 'physics': true,
  119. 'show IK bones': false,
  120. 'show rigid bodies': false
  121. };
  122. const gui = new GUI();
  123. gui.add( api, 'animation' ).onChange( function () {
  124. helper.enable( 'animation', api[ 'animation' ] );
  125. } );
  126. gui.add( api, 'ik' ).onChange( function () {
  127. helper.enable( 'ik', api[ 'ik' ] );
  128. } );
  129. gui.add( api, 'outline' ).onChange( function () {
  130. effect.enabled = api[ 'outline' ];
  131. } );
  132. gui.add( api, 'physics' ).onChange( function () {
  133. helper.enable( 'physics', api[ 'physics' ] );
  134. } );
  135. gui.add( api, 'show IK bones' ).onChange( function () {
  136. ikHelper.visible = api[ 'show IK bones' ];
  137. } );
  138. gui.add( api, 'show rigid bodies' ).onChange( function () {
  139. if ( physicsHelper !== undefined ) physicsHelper.visible = api[ 'show rigid bodies' ];
  140. } );
  141. }
  142. }
  143. function onWindowResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. effect.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. //
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. stats.begin();
  152. render();
  153. stats.end();
  154. }
  155. function render() {
  156. helper.update( clock.getDelta() );
  157. effect.render( scene, camera );
  158. }
  159. </script>
  160. </body>
  161. </html>