123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - loaders - MMD loader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #fff;
- color: #444;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MMDLoader test<br />
- <a href="https://github.com/mrdoob/three.js/tree/master/examples/models/mmd#readme" target="_blank" rel="noopener">MMD Assets license</a><br />
- Copyright
- <a href="http://www.geocities.jp/higuchuu4/index_e.htm" target="_blank" rel="noopener">Model Data</a>
- <a href="http://www.nicovideo.jp/watch/sm13147122" target="_blank" rel="noopener">Dance Data</a>
- </div>
- <script src="js/libs/ammo.js"></script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
- import { MMDLoader } from './jsm/loaders/MMDLoader.js';
- import { MMDAnimationHelper } from './jsm/animation/MMDAnimationHelper.js';
- var container, stats;
- var mesh, camera, scene, renderer, effect;
- var helper, ikHelper, physicsHelper;
- var clock = new THREE.Clock();
- Ammo().then( function ( AmmoLib ) {
- Ammo = AmmoLib;
- init();
- animate();
- } );
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.z = 30;
- // scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- var gridHelper = new THREE.PolarGridHelper( 30, 10 );
- gridHelper.position.y = - 10;
- scene.add( gridHelper );
- var ambient = new THREE.AmbientLight( 0x666666 );
- scene.add( ambient );
- var directionalLight = new THREE.DirectionalLight( 0x887766 );
- directionalLight.position.set( - 1, 1, 1 ).normalize();
- scene.add( directionalLight );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- effect = new OutlineEffect( renderer );
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- // model
- function onProgress( xhr ) {
- if ( xhr.lengthComputable ) {
- var percentComplete = xhr.loaded / xhr.total * 100;
- console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
- }
- }
- var modelFile = 'models/mmd/miku/miku_v2.pmd';
- var vmdFiles = [ 'models/mmd/vmds/wavefile_v2.vmd' ];
- helper = new MMDAnimationHelper( {
- afterglow: 2.0
- } );
- var loader = new MMDLoader();
- loader.loadWithAnimation( modelFile, vmdFiles, function ( mmd ) {
- mesh = mmd.mesh;
- mesh.position.y = - 10;
- scene.add( mesh );
- helper.add( mesh, {
- animation: mmd.animation,
- physics: true
- } );
- ikHelper = helper.objects.get( mesh ).ikSolver.createHelper();
- ikHelper.visible = false;
- scene.add( ikHelper );
- physicsHelper = helper.objects.get( mesh ).physics.createHelper();
- physicsHelper.visible = false;
- scene.add( physicsHelper );
- initGui();
- }, onProgress, null );
- var controls = new OrbitControls( camera, renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- var phongMaterials;
- var originalMaterials;
- function makePhongMaterials( materials ) {
- var array = [];
- for ( var i = 0, il = materials.length; i < il; i ++ ) {
- var m = new THREE.MeshPhongMaterial();
- m.copy( materials[ i ] );
- m.needsUpdate = true;
- array.push( m );
- }
- phongMaterials = array;
- }
- function initGui() {
- var api = {
- 'animation': true,
- 'gradient mapping': true,
- 'ik': true,
- 'outline': true,
- 'physics': true,
- 'show IK bones': false,
- 'show rigid bodies': false
- };
- var gui = new GUI();
- gui.add( api, 'animation' ).onChange( function () {
- helper.enable( 'animation', api[ 'animation' ] );
- } );
- gui.add( api, 'gradient mapping' ).onChange( function () {
- if ( originalMaterials === undefined ) originalMaterials = mesh.material;
- if ( phongMaterials === undefined ) makePhongMaterials( mesh.material );
- if ( api[ 'gradient mapping' ] ) {
- mesh.material = originalMaterials;
- } else {
- mesh.material = phongMaterials;
- }
- } );
- gui.add( api, 'ik' ).onChange( function () {
- helper.enable( 'ik', api[ 'ik' ] );
- } );
- gui.add( api, 'outline' ).onChange( function () {
- effect.enabled = api[ 'outline' ];
- } );
- gui.add( api, 'physics' ).onChange( function () {
- helper.enable( 'physics', api[ 'physics' ] );
- } );
- gui.add( api, 'show IK bones' ).onChange( function () {
- ikHelper.visible = api[ 'show IK bones' ];
- } );
- gui.add( api, 'show rigid bodies' ).onChange( function () {
- if ( physicsHelper !== undefined ) physicsHelper.visible = api[ 'show rigid bodies' ];
- } );
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- effect.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- helper.update( clock.getDelta() );
- effect.render( scene, camera );
- }
- </script>
- </body>
- </html>
|