webgl_loader_draco.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  18. var camera, scene, renderer;
  19. var container = document.querySelector( '#container' );
  20. // Configure and create Draco decoder.
  21. var dracoLoader = new DRACOLoader();
  22. dracoLoader.setDecoderPath( 'js/libs/draco/' );
  23. dracoLoader.setDecoderConfig( { type: 'js' } );
  24. init();
  25. animate();
  26. function init() {
  27. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  28. camera.position.set( 3, 0.25, 3 );
  29. scene = new THREE.Scene();
  30. scene.background = new THREE.Color( 0x443333 );
  31. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  32. // Ground
  33. var plane = new THREE.Mesh(
  34. new THREE.PlaneBufferGeometry( 8, 8 ),
  35. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  36. );
  37. plane.rotation.x = - Math.PI / 2;
  38. plane.position.y = 0.03;
  39. plane.receiveShadow = true;
  40. scene.add( plane );
  41. // Lights
  42. var light = new THREE.HemisphereLight( 0x443333, 0x111122 );
  43. scene.add( light );
  44. var light = new THREE.SpotLight();
  45. light.angle = Math.PI / 16;
  46. light.penumbra = 0.5;
  47. light.castShadow = true;
  48. light.position.set( - 1, 1, 1 );
  49. scene.add( light );
  50. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  51. geometry.computeVertexNormals();
  52. var material = new THREE.MeshStandardMaterial( { color: 0x606060 } );
  53. var mesh = new THREE.Mesh( geometry, material );
  54. mesh.castShadow = true;
  55. mesh.receiveShadow = true;
  56. scene.add( mesh );
  57. // Release decoder resources.
  58. dracoLoader.dispose();
  59. } );
  60. // renderer
  61. renderer = new THREE.WebGLRenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.outputEncoding = THREE.sRGBEncoding;
  65. renderer.shadowMap.enabled = true;
  66. container.appendChild( renderer.domElement );
  67. window.addEventListener( 'resize', onWindowResize, false );
  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. render();
  76. requestAnimationFrame( animate );
  77. }
  78. function render() {
  79. var timer = Date.now() * 0.0003;
  80. camera.position.x = Math.sin( timer ) * 0.5;
  81. camera.position.z = Math.cos( timer ) * 0.5;
  82. camera.lookAt( 0, 0.1, 0 );
  83. renderer.render( scene, camera );
  84. }
  85. </script>
  86. </body>
  87. </html>