webgl_loader_draco.html 3.2 KB

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