webgl_loader_draco.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. <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> -
  13. <a href="https://github.com/google/draco" target="_blank" rel="noopener">DRACO</a> loader
  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 { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  28. let camera, scene, renderer;
  29. const container = document.querySelector( '#container' );
  30. // Configure and create Draco decoder.
  31. const dracoLoader = new DRACOLoader();
  32. dracoLoader.setDecoderPath( 'js/libs/draco/' );
  33. dracoLoader.setDecoderConfig( { type: 'js' } );
  34. init();
  35. animate();
  36. function init() {
  37. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  38. camera.position.set( 3, 0.25, 3 );
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x443333 );
  41. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  42. // Ground
  43. const plane = new THREE.Mesh(
  44. new THREE.PlaneGeometry( 8, 8 ),
  45. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  46. );
  47. plane.rotation.x = - Math.PI / 2;
  48. plane.position.y = 0.03;
  49. plane.receiveShadow = true;
  50. scene.add( plane );
  51. // Lights
  52. const hemiLight = new THREE.HemisphereLight( 0x443333, 0x111122 );
  53. scene.add( hemiLight );
  54. const spotLight = new THREE.SpotLight();
  55. spotLight.angle = Math.PI / 16;
  56. spotLight.penumbra = 0.5;
  57. spotLight.castShadow = true;
  58. spotLight.position.set( - 1, 1, 1 );
  59. scene.add( spotLight );
  60. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  61. geometry.computeVertexNormals();
  62. const material = new THREE.MeshStandardMaterial( { color: 0x606060 } );
  63. const mesh = new THREE.Mesh( geometry, material );
  64. mesh.castShadow = true;
  65. mesh.receiveShadow = true;
  66. scene.add( mesh );
  67. // Release decoder resources.
  68. dracoLoader.dispose();
  69. } );
  70. // renderer
  71. renderer = new THREE.WebGLRenderer( { antialias: true } );
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.outputEncoding = THREE.sRGBEncoding;
  75. renderer.shadowMap.enabled = true;
  76. container.appendChild( renderer.domElement );
  77. window.addEventListener( 'resize', onWindowResize );
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. render();
  86. requestAnimationFrame( animate );
  87. }
  88. function render() {
  89. const timer = Date.now() * 0.0003;
  90. camera.position.x = Math.sin( timer ) * 0.5;
  91. camera.position.z = Math.cos( timer ) * 0.5;
  92. camera.lookAt( 0, 0.1, 0 );
  93. renderer.render( scene, camera );
  94. }
  95. </script>
  96. </body>
  97. </html>