webgl_loader_mmd_audio.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. THREE.ColorManagement.enabled = false; // TODO: Confirm correct color management.
  49. let mesh, camera, scene, renderer, effect;
  50. let helper;
  51. let ready = false;
  52. const clock = new THREE.Clock();
  53. const startButton = document.getElementById( 'startButton' );
  54. startButton.addEventListener( 'click', function () {
  55. Ammo().then( function () {
  56. init();
  57. animate();
  58. } );
  59. } );
  60. function init() {
  61. const overlay = document.getElementById( 'overlay' );
  62. overlay.remove();
  63. const container = document.createElement( 'div' );
  64. document.body.appendChild( container );
  65. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  66. // scene
  67. scene = new THREE.Scene();
  68. scene.background = new THREE.Color( 0xffffff );
  69. scene.add( new THREE.PolarGridHelper( 30, 0 ) );
  70. const listener = new THREE.AudioListener();
  71. camera.add( listener );
  72. scene.add( camera );
  73. const ambient = new THREE.AmbientLight( 0x666666 );
  74. scene.add( ambient );
  75. const directionalLight = new THREE.DirectionalLight( 0x887766 );
  76. directionalLight.position.set( - 1, 1, 1 ).normalize();
  77. scene.add( directionalLight );
  78. //
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  83. container.appendChild( renderer.domElement );
  84. effect = new OutlineEffect( renderer );
  85. // model
  86. function onProgress( xhr ) {
  87. if ( xhr.lengthComputable ) {
  88. const percentComplete = xhr.loaded / xhr.total * 100;
  89. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  90. }
  91. }
  92. const modelFile = 'models/mmd/miku/miku_v2.pmd';
  93. const vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
  94. const cameraFiles = [ 'models/mmd/vmds/wavefile_camera.vmd' ];
  95. const audioFile = 'models/mmd/audios/wavefile_short.mp3';
  96. const audioParams = { delayTime: 160 * 1 / 30 };
  97. helper = new MMDAnimationHelper();
  98. const loader = new MMDLoader();
  99. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  100. mesh = mmd.mesh;
  101. helper.add( mesh, {
  102. animation: mmd.animation,
  103. physics: true
  104. } );
  105. loader.loadAnimation( cameraFiles, camera, function ( cameraAnimation ) {
  106. helper.add( camera, {
  107. animation: cameraAnimation
  108. } );
  109. new THREE.AudioLoader().load( audioFile, function ( buffer ) {
  110. const audio = new THREE.Audio( listener ).setBuffer( buffer );
  111. helper.add( audio, audioParams );
  112. scene.add( mesh );
  113. ready = true;
  114. }, onProgress, null );
  115. }, onProgress, null );
  116. }, onProgress, null );
  117. //
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. effect.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. }
  130. function render() {
  131. if ( ready ) {
  132. helper.update( clock.getDelta() );
  133. }
  134. effect.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>