webgl_loader_mmd_audio.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. color: #444;
  11. background: #fff;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="overlay">
  20. <button id="startButton">Play</button>
  21. </div>
  22. <div id="info">
  23. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MMDLoader test<br />
  24. <a href="https://github.com/mrdoob/three.js/tree/master/examples/models/mmd#readme" target="_blank" rel="noopener">MMD Assets license</a><br />
  25. Copyright
  26. <a href="https://sites.google.com/view/evpvp/" target="_blank" rel="noopener">Model Data</a>
  27. <a href="http://www.nicovideo.jp/watch/sm13147122" target="_blank" rel="noopener">Dance Data</a>
  28. <a href="http://www.nicovideo.jp/watch/sm11938255" target="_blank" rel="noopener">Audio Data</a><br />
  29. Camera is customized from <a href="http://www.nicovideo.jp/watch/sm19168559" target="_blank" rel="noopener">this Data</a>
  30. </div>
  31. <script src="jsm/libs/ammo.wasm.js"></script>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.module.js",
  36. "three/addons/": "./jsm/"
  37. }
  38. }
  39. </script>
  40. <script type="module">
  41. import * as THREE from 'three';
  42. import { OutlineEffect } from 'three/addons/effects/OutlineEffect.js';
  43. import { MMDLoader } from 'three/addons/loaders/MMDLoader.js';
  44. import { MMDAnimationHelper } from 'three/addons/animation/MMDAnimationHelper.js';
  45. let mesh, camera, scene, renderer, effect;
  46. let helper;
  47. let ready = false;
  48. const clock = new THREE.Clock();
  49. const startButton = document.getElementById( 'startButton' );
  50. startButton.addEventListener( 'click', function () {
  51. Ammo().then( function () {
  52. init();
  53. } );
  54. } );
  55. function init() {
  56. const overlay = document.getElementById( 'overlay' );
  57. overlay.remove();
  58. const container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  61. // scene
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0xffffff );
  64. scene.add( new THREE.PolarGridHelper( 30, 0 ) );
  65. const listener = new THREE.AudioListener();
  66. camera.add( listener );
  67. scene.add( camera );
  68. const ambient = new THREE.AmbientLight( 0xaaaaaa, 3 );
  69. scene.add( ambient );
  70. const directionalLight = new THREE.DirectionalLight( 0xffffff, 3 );
  71. directionalLight.position.set( - 1, 1, 1 ).normalize();
  72. scene.add( directionalLight );
  73. //
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.setAnimationLoop( animate );
  78. container.appendChild( renderer.domElement );
  79. effect = new OutlineEffect( renderer );
  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. const cameraFiles = [ 'models/mmd/vmds/wavefile_camera.vmd' ];
  90. const audioFile = 'models/mmd/audios/wavefile_short.mp3';
  91. const audioParams = { delayTime: 160 * 1 / 30 };
  92. helper = new MMDAnimationHelper();
  93. const loader = new MMDLoader();
  94. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  95. mesh = mmd.mesh;
  96. helper.add( mesh, {
  97. animation: mmd.animation,
  98. physics: true
  99. } );
  100. loader.loadAnimation( cameraFiles, camera, function ( cameraAnimation ) {
  101. helper.add( camera, {
  102. animation: cameraAnimation
  103. } );
  104. new THREE.AudioLoader().load( audioFile, function ( buffer ) {
  105. const audio = new THREE.Audio( listener ).setBuffer( buffer );
  106. helper.add( audio, audioParams );
  107. scene.add( mesh );
  108. ready = true;
  109. }, onProgress, null );
  110. }, onProgress, null );
  111. }, onProgress, null );
  112. //
  113. window.addEventListener( 'resize', onWindowResize );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. effect.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. //
  121. function animate() {
  122. if ( ready ) {
  123. helper.update( clock.getDelta() );
  124. }
  125. effect.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>