2
0

webgl_loader_collada.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - collada</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<br/>
  13. Elf Girl by <a href="https://sketchfab.com/yellow09" target="_blank" rel="noopener">halloween</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. let container, stats, clock;
  31. let camera, scene, renderer, elf;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.getElementById( 'container' );
  36. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  37. camera.position.set( 8, 10, 8 );
  38. camera.lookAt( 0, 3, 0 );
  39. scene = new THREE.Scene();
  40. clock = new THREE.Clock();
  41. // loading manager
  42. const loadingManager = new THREE.LoadingManager( function () {
  43. scene.add( elf );
  44. } );
  45. // collada
  46. const loader = new ColladaLoader( loadingManager );
  47. loader.load( './models/collada/elf/elf.dae', function ( collada ) {
  48. elf = collada.scene;
  49. } );
  50. //
  51. const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  52. scene.add( ambientLight );
  53. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
  54. directionalLight.position.set( 1, 1, 0 ).normalize();
  55. scene.add( directionalLight );
  56. //
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. container.appendChild( renderer.domElement );
  61. //
  62. stats = new Stats();
  63. container.appendChild( stats.dom );
  64. //
  65. window.addEventListener( 'resize', onWindowResize );
  66. }
  67. function onWindowResize() {
  68. camera.aspect = window.innerWidth / window.innerHeight;
  69. camera.updateProjectionMatrix();
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. }
  72. function animate() {
  73. requestAnimationFrame( animate );
  74. render();
  75. stats.update();
  76. }
  77. function render() {
  78. const delta = clock.getDelta();
  79. if ( elf !== undefined ) {
  80. elf.rotation.z += delta * 0.5;
  81. }
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>