webgl_loader_collada.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <meta property="og:title" content="three.js webgl - collada">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_collada.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_collada.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> collada loader<br/>
  17. 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>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { ColladaLoader } from 'three/addons/loaders/ColladaLoader.js';
  31. let container, stats, timer;
  32. let camera, scene, renderer, elf;
  33. init();
  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. timer = new THREE.Timer();
  41. timer.connect( document );
  42. // loading manager
  43. const loadingManager = new THREE.LoadingManager( function () {
  44. scene.add( elf );
  45. } );
  46. // collada
  47. const loader = new ColladaLoader( loadingManager );
  48. loader.load( './models/collada/elf/elf.dae', function ( collada ) {
  49. elf = collada.scene;
  50. } );
  51. //
  52. const ambientLight = new THREE.AmbientLight( 0xffffff );
  53. scene.add( ambientLight );
  54. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  55. directionalLight.position.set( 1, 1, 0 ).normalize();
  56. scene.add( directionalLight );
  57. //
  58. renderer = new THREE.WebGLRenderer();
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.setAnimationLoop( animate );
  62. container.appendChild( renderer.domElement );
  63. //
  64. stats = new Stats();
  65. container.appendChild( stats.dom );
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. }
  74. function animate() {
  75. timer.update();
  76. render();
  77. stats.update();
  78. }
  79. function render() {
  80. const delta = timer.getDelta();
  81. if ( elf !== undefined ) {
  82. elf.rotation.z += delta * 0.5;
  83. }
  84. renderer.render( scene, camera );
  85. }
  86. </script>
  87. </body>
  88. </html>
粤ICP备19079148号