webgl_skinning_simple.html 3.6 KB

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