webgl_loader_mmd_audio.html 4.4 KB

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