webgl_loader_mmd.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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( 0xaaaaaa, 3 );
  67. scene.add( ambient );
  68. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  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. renderer.useLegacyLights = false;
  76. container.appendChild( renderer.domElement );
  77. effect = new OutlineEffect( renderer );
  78. // STATS
  79. stats = new Stats();
  80. container.appendChild( stats.dom );
  81. // model
  82. function onProgress( xhr ) {
  83. if ( xhr.lengthComputable ) {
  84. const percentComplete = xhr.loaded / xhr.total * 100;
  85. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  86. }
  87. }
  88. const modelFile = 'models/mmd/miku/miku_v2.pmd';
  89. const vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
  90. helper = new MMDAnimationHelper( {
  91. afterglow: 2.0
  92. } );
  93. const loader = new MMDLoader();
  94. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  95. mesh = mmd.mesh;
  96. mesh.position.y = - 10;
  97. scene.add( mesh );
  98. helper.add( mesh, {
  99. animation: mmd.animation,
  100. physics: true
  101. } );
  102. ikHelper = helper.objects.get( mesh ).ikSolver.createHelper();
  103. ikHelper.visible = false;
  104. scene.add( ikHelper );
  105. physicsHelper = helper.objects.get( mesh ).physics.createHelper();
  106. physicsHelper.visible = false;
  107. scene.add( physicsHelper );
  108. initGui();
  109. }, onProgress, null );
  110. const controls = new OrbitControls( camera, renderer.domElement );
  111. controls.minDistance = 10;
  112. controls.maxDistance = 100;
  113. window.addEventListener( 'resize', onWindowResize );
  114. function initGui() {
  115. const api = {
  116. 'animation': true,
  117. 'ik': true,
  118. 'outline': true,
  119. 'physics': true,
  120. 'show IK bones': false,
  121. 'show rigid bodies': false
  122. };
  123. const gui = new GUI();
  124. gui.add( api, 'animation' ).onChange( function () {
  125. helper.enable( 'animation', api[ 'animation' ] );
  126. } );
  127. gui.add( api, 'ik' ).onChange( function () {
  128. helper.enable( 'ik', api[ 'ik' ] );
  129. } );
  130. gui.add( api, 'outline' ).onChange( function () {
  131. effect.enabled = api[ 'outline' ];
  132. } );
  133. gui.add( api, 'physics' ).onChange( function () {
  134. helper.enable( 'physics', api[ 'physics' ] );
  135. } );
  136. gui.add( api, 'show IK bones' ).onChange( function () {
  137. ikHelper.visible = api[ 'show IK bones' ];
  138. } );
  139. gui.add( api, 'show rigid bodies' ).onChange( function () {
  140. if ( physicsHelper !== undefined ) physicsHelper.visible = api[ 'show rigid bodies' ];
  141. } );
  142. }
  143. }
  144. function onWindowResize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. effect.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. //
  150. function animate() {
  151. requestAnimationFrame( animate );
  152. stats.begin();
  153. render();
  154. stats.end();
  155. }
  156. function render() {
  157. helper.update( clock.getDelta() );
  158. effect.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>