webgl_loader_vrm.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - vrm 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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - VRMLoader<br />
  12. Alicia by
  13. <a href="https://3d.nicovideo.jp/works/td32797" target="_blank" rel="noopener">Dwango</a> /
  14. <a href="http://3d.nicovideo.jp/alicia/rule.html">License</a><br />
  15. </div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import Stats from './jsm/libs/stats.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { VRMLoader } from './jsm/loaders/VRMLoader.js';
  21. var container, stats, controls;
  22. var camera, scene, renderer, light;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  29. camera.position.set( 0, 1.6, - 2.2 );
  30. scene = new THREE.Scene();
  31. light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
  32. light.position.set( 0, 1, 0 );
  33. scene.add( light );
  34. // model
  35. var loader = new VRMLoader();
  36. loader.load( 'models/vrm/Alicia/AliciaSolid.vrm', function ( vrm ) {
  37. // VRMLoader doesn't support VRM Unlit extension yet so
  38. // converting all materials to THREE.MeshBasicMaterial here as workaround so far.
  39. vrm.scene.traverse( function ( object ) {
  40. if ( object.material ) {
  41. if ( Array.isArray( object.material ) ) {
  42. for ( var i = 0, il = object.material.length; i < il; i ++ ) {
  43. var material = new THREE.MeshBasicMaterial();
  44. THREE.Material.prototype.copy.call( material, object.material[ i ] );
  45. material.color.copy( object.material[ i ].color );
  46. material.map = object.material[ i ].map;
  47. material.skinning = object.material[ i ].skinning;
  48. material.morphTargets = object.material[ i ].morphTargets;
  49. material.morphNormals = object.material[ i ].morphNormals;
  50. object.material[ i ] = material;
  51. }
  52. } else {
  53. var material = new THREE.MeshBasicMaterial();
  54. THREE.Material.prototype.copy.call( material, object.material );
  55. material.color.copy( object.material.color );
  56. material.map = object.material.map;
  57. material.skinning = object.material.skinning;
  58. material.morphTargets = object.material.morphTargets;
  59. material.morphNormals = object.material.morphNormals;
  60. object.material = material;
  61. }
  62. }
  63. } );
  64. scene.add( vrm.scene );
  65. } );
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.outputEncoding = THREE.sRGBEncoding;
  70. container.appendChild( renderer.domElement );
  71. controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.set( 0, 0.9, 0 );
  73. controls.update();
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. // stats
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. //
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. renderer.render( scene, camera );
  88. stats.update();
  89. }
  90. </script>
  91. </body>
  92. </html>