webgl_loader_mmd_audio.html 4.6 KB

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