webgl_loader_mmd.html 6.0 KB

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