webgl_skinning_simple.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - skinning - simple</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> webgl - simple skinning -
  12. <a href="https://github.com/mrdoob/three.js/blob/master/examples/models/skinned/simple/simple.blend" target="_blank" rel="noopener">Blender File</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  19. var stats, mixer, camera, scene, renderer, clock;
  20. init();
  21. animate();
  22. function init() {
  23. var container = document.createElement( 'div' );
  24. document.body.appendChild( container );
  25. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  26. camera.position.set( 24, 8, 24 );
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0xa0a0a0 );
  29. scene.fog = new THREE.Fog( 0xa0a0a0, 70, 100 );
  30. clock = new THREE.Clock();
  31. // ground
  32. var geometry = new THREE.PlaneBufferGeometry( 500, 500 );
  33. var material = new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } );
  34. var ground = new THREE.Mesh( geometry, material );
  35. ground.position.set( 0, - 5, 0 );
  36. ground.rotation.x = - Math.PI / 2;
  37. ground.receiveShadow = true;
  38. scene.add( ground );
  39. var grid = new THREE.GridHelper( 500, 100, 0x000000, 0x000000 );
  40. grid.position.y = - 5;
  41. grid.material.opacity = 0.2;
  42. grid.material.transparent = true;
  43. scene.add( grid );
  44. // lights
  45. var light = new THREE.HemisphereLight( 0xffffff, 0x444444, 0.6 );
  46. light.position.set( 0, 200, 0 );
  47. scene.add( light );
  48. light = new THREE.DirectionalLight( 0xffffff, 0.8 );
  49. light.position.set( 0, 20, 10 );
  50. light.castShadow = true;
  51. light.shadow.camera.top = 18;
  52. light.shadow.camera.bottom = - 10;
  53. light.shadow.camera.left = - 12;
  54. light.shadow.camera.right = 12;
  55. scene.add( light );
  56. //
  57. var loader = new GLTFLoader();
  58. loader.load( './models/gltf/SimpleSkinning.gltf', function ( gltf ) {
  59. scene.add( gltf.scene );
  60. gltf.scene.traverse( function ( child ) {
  61. if ( child.isSkinnedMesh ) child.castShadow = true;
  62. } );
  63. mixer = new THREE.AnimationMixer( gltf.scene );
  64. mixer.clipAction( gltf.animations[ 0 ] ).play();
  65. } );
  66. //
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.shadowMap.enabled = true;
  71. container.appendChild( renderer.domElement );
  72. //
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. var controls = new OrbitControls( camera, renderer.domElement );
  76. controls.enablePan = false;
  77. controls.minDistance = 5;
  78. controls.maxDistance = 50;
  79. }
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. if ( mixer ) mixer.update( clock.getDelta() );
  83. render();
  84. stats.update();
  85. }
  86. function render() {
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>