webgl_loader_mmd_audio.html 5.0 KB

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