webgl_loader_mmd_audio.html 4.9 KB

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