webgl_loader_collada.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
  27. let container, stats, clock;
  28. let camera, scene, renderer, elf;
  29. init();
  30. function init() {
  31. container = document.getElementById( 'container' );
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  33. camera.position.set( 8, 10, 8 );
  34. camera.lookAt( 0, 3, 0 );
  35. scene = new THREE.Scene();
  36. clock = new THREE.Clock();
  37. // loading manager
  38. const loadingManager = new THREE.LoadingManager( function () {
  39. scene.add( elf );
  40. } );
  41. // collada
  42. const loader = new ColladaLoader( loadingManager );
  43. loader.load( './models/collada/elf/elf.dae', function ( collada ) {
  44. elf = collada.scene;
  45. } );
  46. //
  47. const ambientLight = new THREE.AmbientLight( 0xffffff );
  48. scene.add( ambientLight );
  49. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  50. directionalLight.position.set( 1, 1, 0 ).normalize();
  51. scene.add( directionalLight );
  52. //
  53. renderer = new THREE.WebGLRenderer();
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.setAnimationLoop( animate );
  57. container.appendChild( renderer.domElement );
  58. //
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. //
  62. window.addEventListener( 'resize', onWindowResize );
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. }
  69. function animate() {
  70. render();
  71. stats.update();
  72. }
  73. function render() {
  74. const delta = clock.getDelta();
  75. if ( elf !== undefined ) {
  76. elf.rotation.z += delta * 0.5;
  77. }
  78. renderer.render( scene, camera );
  79. }
  80. </script>
  81. </body>
  82. </html>