webgl_skinning_simple.html 3.3 KB

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