webgl_loader_collada_skinning.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada - skinning</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> collada loader - skinning<br/>
  13. Dancing Stormtrooper by <a href="https://sketchfab.com/strykerdoesgames" target="_blank" rel="noopener">StrykerDoesAnimation</a>, <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">CC Attribution</a>
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. let container, stats, clock, controls;
  32. let camera, scene, renderer, mixer;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.getElementById( 'container' );
  37. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 1, 1000 );
  38. camera.position.set( 15, 10, - 15 );
  39. scene = new THREE.Scene();
  40. clock = new THREE.Clock();
  41. // collada
  42. const loader = new ColladaLoader();
  43. loader.load( './models/collada/stormtrooper/stormtrooper.dae', function ( collada ) {
  44. const avatar = collada.scene;
  45. const animations = avatar.animations;
  46. mixer = new THREE.AnimationMixer( avatar );
  47. mixer.clipAction( animations[ 0 ] ).play();
  48. scene.add( avatar );
  49. } );
  50. //
  51. const gridHelper = new THREE.GridHelper( 10, 20, 0xc1c1c1, 0x8d8d8d );
  52. scene.add( gridHelper );
  53. //
  54. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
  55. scene.add( ambientLight );
  56. const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  57. scene.add( camera );
  58. camera.add( pointLight );
  59. //
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. container.appendChild( renderer.domElement );
  64. //
  65. controls = new OrbitControls( camera, renderer.domElement );
  66. controls.screenSpacePanning = true;
  67. controls.minDistance = 5;
  68. controls.maxDistance = 40;
  69. controls.target.set( 0, 2, 0 );
  70. controls.update();
  71. //
  72. stats = new Stats();
  73. container.appendChild( stats.dom );
  74. //
  75. window.addEventListener( 'resize', onWindowResize );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. render();
  85. stats.update();
  86. }
  87. function render() {
  88. const delta = clock.getDelta();
  89. if ( mixer !== undefined ) {
  90. mixer.update( delta );
  91. }
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>