webgl_loader_mmd_audio.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. <!-- Import maps polyfill -->
  32. <!-- Remove this when import maps will be widely supported -->
  33. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  34. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js"
  38. }
  39. }
  40. </script>
  41. <script type="module">
  42. import * as THREE from 'three';
  43. import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
  44. import { MMDLoader } from './jsm/loaders/MMDLoader.js';
  45. import { MMDAnimationHelper } from './jsm/animation/MMDAnimationHelper.js';
  46. let mesh, camera, scene, renderer, effect;
  47. let helper;
  48. let ready = false;
  49. const clock = new THREE.Clock();
  50. const startButton = document.getElementById( 'startButton' );
  51. startButton.addEventListener( 'click', function () {
  52. Ammo().then( function () {
  53. init();
  54. animate();
  55. } );
  56. } );
  57. function init() {
  58. const overlay = document.getElementById( 'overlay' );
  59. overlay.remove();
  60. const container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  63. // scene
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0xffffff );
  66. scene.add( new THREE.PolarGridHelper( 30, 10 ) );
  67. const listener = new THREE.AudioListener();
  68. camera.add( listener );
  69. scene.add( camera );
  70. const ambient = new THREE.AmbientLight( 0x666666 );
  71. scene.add( ambient );
  72. const directionalLight = new THREE.DirectionalLight( 0x887766 );
  73. directionalLight.position.set( - 1, 1, 1 ).normalize();
  74. scene.add( directionalLight );
  75. //
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. effect = new OutlineEffect( renderer );
  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. const cameraFiles = [ 'models/mmd/vmds/wavefile_camera.vmd' ];
  91. const audioFile = 'models/mmd/audios/wavefile_short.mp3';
  92. const audioParams = { delayTime: 160 * 1 / 30 };
  93. helper = new MMDAnimationHelper();
  94. const loader = new MMDLoader();
  95. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  96. mesh = mmd.mesh;
  97. helper.add( mesh, {
  98. animation: mmd.animation,
  99. physics: true
  100. } );
  101. loader.loadAnimation( cameraFiles, camera, function ( cameraAnimation ) {
  102. helper.add( camera, {
  103. animation: cameraAnimation
  104. } );
  105. new THREE.AudioLoader().load( audioFile, function ( buffer ) {
  106. const audio = new THREE.Audio( listener ).setBuffer( buffer );
  107. helper.add( audio, audioParams );
  108. scene.add( mesh );
  109. ready = true;
  110. }, onProgress, null );
  111. }, onProgress, null );
  112. }, onProgress, null );
  113. //
  114. window.addEventListener( 'resize', onWindowResize );
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. effect.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. //
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. render();
  125. }
  126. function render() {
  127. if ( ready ) {
  128. helper.update( clock.getDelta() );
  129. }
  130. effect.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>