webgl_loader_draco.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - Draco loader</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 - loaders - Draco loader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_draco.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_draco.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> -
  17. <a href="https://github.com/google/draco" target="_blank" rel="noopener">DRACO</a> loader
  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 { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  30. let camera, scene, renderer;
  31. const container = document.querySelector( '#container' );
  32. // Configure and create Draco decoder.
  33. const dracoLoader = new DRACOLoader();
  34. init();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  37. camera.position.set( 3, 0.25, 3 );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x443333 );
  40. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  41. // Ground
  42. const plane = new THREE.Mesh(
  43. new THREE.PlaneGeometry( 8, 8 ),
  44. new THREE.MeshLambertMaterial( { color: 0xcbcbcb } )
  45. );
  46. plane.rotation.x = - Math.PI / 2;
  47. plane.position.y = 0.03;
  48. plane.receiveShadow = true;
  49. scene.add( plane );
  50. // Lights
  51. const hemiLight = new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 );
  52. scene.add( hemiLight );
  53. const spotLight = new THREE.SpotLight();
  54. spotLight.intensity = 7;
  55. spotLight.angle = Math.PI / 16;
  56. spotLight.penumbra = 0.5;
  57. spotLight.castShadow = true;
  58. spotLight.shadow.radius = 8;
  59. spotLight.position.set( - 1, 1, 1 );
  60. scene.add( spotLight );
  61. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  62. geometry.computeVertexNormals();
  63. const material = new THREE.MeshStandardMaterial( { color: 0xa5a5a5 } );
  64. const mesh = new THREE.Mesh( geometry, material );
  65. mesh.castShadow = true;
  66. mesh.receiveShadow = true;
  67. scene.add( mesh );
  68. // Release decoder resources.
  69. dracoLoader.dispose();
  70. } );
  71. // renderer
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.setAnimationLoop( animate );
  76. renderer.shadowMap.enabled = true;
  77. container.appendChild( renderer.domElement );
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. const timer = Date.now() * 0.0003;
  87. camera.position.x = Math.sin( timer ) * 0.5;
  88. camera.position.z = Math.cos( timer ) * 0.5;
  89. camera.lookAt( 0, 0.1, 0 );
  90. renderer.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>
粤ICP备19079148号