webgl_loader_mmd.html 5.8 KB

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