webgl_loader_collada_skinning.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from './jsm/libs/stats.module.js';
  28. import { ColladaLoader } from './jsm/loaders/ColladaLoader.js';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. let container, stats, clock, controls;
  31. let camera, scene, renderer, mixer;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.getElementById( 'container' );
  36. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 1, 1000 );
  37. camera.position.set( 15, 10, - 15 );
  38. scene = new THREE.Scene();
  39. clock = new THREE.Clock();
  40. // collada
  41. const loader = new ColladaLoader();
  42. loader.load( './models/collada/stormtrooper/stormtrooper.dae', function ( collada ) {
  43. const avatar = collada.scene;
  44. const animations = avatar.animations;
  45. avatar.traverse( function ( node ) {
  46. if ( node.isSkinnedMesh ) {
  47. node.frustumCulled = false;
  48. }
  49. } );
  50. mixer = new THREE.AnimationMixer( avatar );
  51. mixer.clipAction( animations[ 0 ] ).play();
  52. scene.add( avatar );
  53. } );
  54. //
  55. const gridHelper = new THREE.GridHelper( 10, 20, 0x888888, 0x444444 );
  56. scene.add( gridHelper );
  57. //
  58. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2 );
  59. scene.add( ambientLight );
  60. const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  61. scene.add( camera );
  62. camera.add( pointLight );
  63. //
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. //
  69. controls = new OrbitControls( camera, renderer.domElement );
  70. controls.screenSpacePanning = true;
  71. controls.minDistance = 5;
  72. controls.maxDistance = 40;
  73. controls.target.set( 0, 2, 0 );
  74. controls.update();
  75. //
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. render();
  89. stats.update();
  90. }
  91. function render() {
  92. const delta = clock.getDelta();
  93. if ( mixer !== undefined ) {
  94. mixer.update( delta );
  95. }
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>