webgl_skinning_simple.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a {
  25. color: #046;
  26. font-weight: bold;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - simple skinning -
  33. <a href="https://github.com/mrdoob/three.js/blob/master/examples/models/skinned/simple/simple.blend" target="_blank" rel="noopener">Blender File</a>
  34. </div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script src="js/controls/OrbitControls.js"></script>
  38. <script src="js/loaders/GLTFLoader.js"></script>
  39. <script>
  40. var stats, mixer, camera, scene, renderer, clock;
  41. init();
  42. animate();
  43. function init() {
  44. var container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 24, 8, 24 );
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0xa0a0a0 );
  50. scene.fog = new THREE.Fog( 0xa0a0a0, 70, 100 );
  51. clock = new THREE.Clock();
  52. // ground
  53. var geometry = new THREE.PlaneBufferGeometry( 500, 500 );
  54. var material = new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } );
  55. var ground = new THREE.Mesh( geometry, material );
  56. ground.position.set( 0, - 5, 0 );
  57. ground.rotation.x = - Math.PI / 2;
  58. ground.receiveShadow = true;
  59. scene.add( ground );
  60. var grid = new THREE.GridHelper( 500, 100, 0x000000, 0x000000 );
  61. grid.position.y = - 5;
  62. grid.material.opacity = 0.2;
  63. grid.material.transparent = true;
  64. scene.add( grid );
  65. // lights
  66. var light = new THREE.HemisphereLight( 0xffffff, 0x444444, 0.6 );
  67. light.position.set( 0, 200, 0 );
  68. scene.add( light );
  69. light = new THREE.DirectionalLight( 0xffffff, 0.8 );
  70. light.position.set( 0, 20, 10 );
  71. light.castShadow = true;
  72. light.shadow.camera.top = 18;
  73. light.shadow.camera.bottom = - 10;
  74. light.shadow.camera.left = - 12;
  75. light.shadow.camera.right = 12;
  76. scene.add( light );
  77. //
  78. var loader = new THREE.GLTFLoader();
  79. loader.load( './models/gltf/SimpleSkinning.gltf', function ( gltf ) {
  80. scene.add( gltf.scene );
  81. gltf.scene.traverse( function ( child ) {
  82. if ( child.isSkinnedMesh ) child.castShadow = true;
  83. } );
  84. mixer = new THREE.AnimationMixer( gltf.scene );
  85. mixer.clipAction( gltf.animations[ 0 ] ).play();
  86. } );
  87. //
  88. renderer = new THREE.WebGLRenderer();
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. renderer.shadowMap.enabled = true;
  92. container.appendChild( renderer.domElement );
  93. //
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  97. controls.enablePan = false;
  98. controls.minDistance = 5;
  99. controls.maxDistance = 50;
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. if ( mixer ) mixer.update( clock.getDelta() );
  104. render();
  105. stats.update();
  106. }
  107. function render() {
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>