webgl_loader_mmd.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. </div>
  26. <script src="js/libs/ammo.js"></script>
  27. <script type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import Stats from './jsm/libs/stats.module.js';
  30. import { GUI } from './jsm/libs/dat.gui.module.js';
  31. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  32. import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
  33. import { MMDLoader } from './jsm/loaders/MMDLoader.js';
  34. import { MMDAnimationHelper } from './jsm/animation/MMDAnimationHelper.js';
  35. var container, stats;
  36. var mesh, camera, scene, renderer, effect;
  37. var helper, ikHelper, physicsHelper;
  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. camera.position.z = 30;
  49. // scene
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0xffffff );
  52. var gridHelper = new THREE.PolarGridHelper( 30, 10 );
  53. gridHelper.position.y = - 10;
  54. scene.add( gridHelper );
  55. var ambient = new THREE.AmbientLight( 0x666666 );
  56. scene.add( ambient );
  57. var directionalLight = new THREE.DirectionalLight( 0x887766 );
  58. directionalLight.position.set( - 1, 1, 1 ).normalize();
  59. scene.add( directionalLight );
  60. //
  61. renderer = new THREE.WebGLRenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. container.appendChild( renderer.domElement );
  65. effect = new OutlineEffect( renderer );
  66. // STATS
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. // model
  70. function onProgress( xhr ) {
  71. if ( xhr.lengthComputable ) {
  72. var percentComplete = xhr.loaded / xhr.total * 100;
  73. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  74. }
  75. }
  76. var modelFile = 'models/mmd/miku/miku_v2.pmd';
  77. var vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
  78. helper = new MMDAnimationHelper( {
  79. afterglow: 2.0
  80. } );
  81. var loader = new MMDLoader();
  82. loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
  83. mesh = mmd.mesh;
  84. mesh.position.y = - 10;
  85. scene.add( mesh );
  86. helper.add( mesh, {
  87. animation: mmd.animation,
  88. physics: true
  89. } );
  90. ikHelper = helper.objects.get( mesh ).ikSolver.createHelper();
  91. ikHelper.visible = false;
  92. scene.add( ikHelper );
  93. physicsHelper = helper.objects.get( mesh ).physics.createHelper();
  94. physicsHelper.visible = false;
  95. scene.add( physicsHelper );
  96. initGui();
  97. }, onProgress, null );
  98. var controls = new OrbitControls( camera, renderer.domElement );
  99. window.addEventListener( 'resize', onWindowResize, false );
  100. var phongMaterials;
  101. var originalMaterials;
  102. function makePhongMaterials( materials ) {
  103. var array = [];
  104. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  105. var m = new THREE.MeshPhongMaterial();
  106. m.copy( materials[ i ] );
  107. m.needsUpdate = true;
  108. array.push( m );
  109. }
  110. phongMaterials = array;
  111. }
  112. function initGui() {
  113. var api = {
  114. 'animation': true,
  115. 'gradient mapping': true,
  116. 'ik': true,
  117. 'outline': true,
  118. 'physics': true,
  119. 'show IK bones': false,
  120. 'show rigid bodies': false
  121. };
  122. var gui = new GUI();
  123. gui.add( api, 'animation' ).onChange( function () {
  124. helper.enable( 'animation', api[ 'animation' ] );
  125. } );
  126. gui.add( api, 'gradient mapping' ).onChange( function () {
  127. if ( originalMaterials === undefined ) originalMaterials = mesh.material;
  128. if ( phongMaterials === undefined ) makePhongMaterials( mesh.material );
  129. if ( api[ 'gradient mapping' ] ) {
  130. mesh.material = originalMaterials;
  131. } else {
  132. mesh.material = phongMaterials;
  133. }
  134. } );
  135. gui.add( api, 'ik' ).onChange( function () {
  136. helper.enable( 'ik', api[ 'ik' ] );
  137. } );
  138. gui.add( api, 'outline' ).onChange( function () {
  139. effect.enabled = api[ 'outline' ];
  140. } );
  141. gui.add( api, 'physics' ).onChange( function () {
  142. helper.enable( 'physics', api[ 'physics' ] );
  143. } );
  144. gui.add( api, 'show IK bones' ).onChange( function () {
  145. ikHelper.visible = api[ 'show IK bones' ];
  146. } );
  147. gui.add( api, 'show rigid bodies' ).onChange( function () {
  148. if ( physicsHelper !== undefined ) physicsHelper.visible = api[ 'show rigid bodies' ];
  149. } );
  150. }
  151. }
  152. function onWindowResize() {
  153. camera.aspect = window.innerWidth / window.innerHeight;
  154. camera.updateProjectionMatrix();
  155. effect.setSize( window.innerWidth, window.innerHeight );
  156. }
  157. //
  158. function animate() {
  159. requestAnimationFrame( animate );
  160. stats.begin();
  161. render();
  162. stats.end();
  163. }
  164. function render() {
  165. helper.update( clock.getDelta() );
  166. effect.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>