webgl_loader_collada_skinning.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. avatar.traverse( function ( node ) {
  47. if ( node.isSkinnedMesh ) {
  48. node.frustumCulled = false;
  49. }
  50. } );
  51. mixer = new THREE.AnimationMixer( avatar );
  52. mixer.clipAction( animations[ 0 ] ).play();
  53. scene.add( avatar );
  54. } );
  55. //
  56. const gridHelper = new THREE.GridHelper( 10, 20, 0x888888, 0x444444 );
  57. scene.add( gridHelper );
  58. //
  59. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
  60. scene.add( ambientLight );
  61. const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  62. scene.add( camera );
  63. camera.add( pointLight );
  64. //
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.outputEncoding = THREE.sRGBEncoding;
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. container.appendChild( renderer.domElement );
  70. //
  71. controls = new OrbitControls( camera, renderer.domElement );
  72. controls.screenSpacePanning = true;
  73. controls.minDistance = 5;
  74. controls.maxDistance = 40;
  75. controls.target.set( 0, 2, 0 );
  76. controls.update();
  77. //
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. //
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. stats.update();
  92. }
  93. function render() {
  94. const delta = clock.getDelta();
  95. if ( mixer !== undefined ) {
  96. mixer.update( delta );
  97. }
  98. renderer.render( scene, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>