webgl_loader_mmd.html 5.5 KB

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