webgl_loader_mmd.html 5.4 KB

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