2
0

webgl_loader_mmd_audio.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, 1 );
  73. scene.add( ambient );
  74. const directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  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. container.appendChild( renderer.domElement );
  82. effect = new OutlineEffect( renderer );
  83. // model
  84. function onProgress( xhr ) {
  85. if ( xhr.lengthComputable ) {
  86. const percentComplete = xhr.loaded / xhr.total * 100;
  87. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  88. }
  89. }
  90. const modelFile = 'models/mmd/miku/miku_v2.pmd';
  91. const vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
  92. const cameraFiles = [ 'models/mmd/vmds/wavefile_camera.vmd' ];
  93. const audioFile = 'models/mmd/audios/wavefile_short.mp3';
  94. const audioParams = { delayTime: 160 * 1 / 30 };
  95. helper = new MMDAnimationHelper();
  96. const loader = new MMDLoader();
  97. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  98. mesh = mmd.mesh;
  99. helper.add( mesh, {
  100. animation: mmd.animation,
  101. physics: true
  102. } );
  103. loader.loadAnimation( cameraFiles, camera, function ( cameraAnimation ) {
  104. helper.add( camera, {
  105. animation: cameraAnimation
  106. } );
  107. new THREE.AudioLoader().load( audioFile, function ( buffer ) {
  108. const audio = new THREE.Audio( listener ).setBuffer( buffer );
  109. helper.add( audio, audioParams );
  110. scene.add( mesh );
  111. ready = true;
  112. }, onProgress, null );
  113. }, onProgress, null );
  114. }, onProgress, null );
  115. //
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. effect.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. }
  128. function render() {
  129. if ( ready ) {
  130. helper.update( clock.getDelta() );
  131. }
  132. effect.render( scene, camera );
  133. }
  134. </script>
  135. </body>
  136. </html>