webgl_loader_draco.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display: block;
  23. }
  24. #info a,
  25. .button {
  26. color: #f00;
  27. font-weight: bold;
  28. text-decoration: underline;
  29. cursor: pointer
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"></div>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  37. <a href="https://github.com/google/draco" target="_blank" rel="noopener">DRACO</a> loader
  38. </div>
  39. <script src="../build/three.js"></script>
  40. <script src="js/loaders/draco/DRACOLoader.js"></script>
  41. <script src="js/loaders/draco/geometry_helper.js"></script>
  42. <script>
  43. var camera, scene, renderer;
  44. // Global Draco decoder type.
  45. var dracoLoader = new THREE.DRACOLoader('js/loaders/draco/');
  46. init();
  47. animate();
  48. function init() {
  49. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  50. camera.position.set( 3, 0.25, 3 );
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x443333 );
  53. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  54. // Ground
  55. var plane = new THREE.Mesh(
  56. new THREE.PlaneBufferGeometry( 8, 8 ),
  57. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  58. );
  59. plane.rotation.x = - Math.PI / 2;
  60. plane.position.y = 0.03;
  61. plane.receiveShadow = true;
  62. scene.add(plane);
  63. // Lights
  64. var light = new THREE.HemisphereLight( 0x443333, 0x111122 );
  65. scene.add( light );
  66. var light = new THREE.SpotLight();
  67. light.angle = Math.PI / 16;
  68. light.penumbra = 0.5;
  69. light.castShadow = true;
  70. light.position.set( - 1, 1, 1 );
  71. scene.add( light );
  72. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  73. geometry.computeVertexNormals();
  74. var material = new THREE.MeshStandardMaterial( { vertexColors: THREE.VertexColors } );
  75. var mesh = new THREE.Mesh( geometry, material );
  76. mesh.castShadow = true;
  77. mesh.receiveShadow = true;
  78. scene.add( mesh );
  79. } );
  80. // renderer
  81. renderer = new THREE.WebGLRenderer( { antialias: true } );
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.gammaInput = true;
  85. renderer.gammaOutput = true;
  86. renderer.shadowMap.enabled = true;
  87. container.appendChild( renderer.domElement );
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function animate() {
  96. render();
  97. requestAnimationFrame( animate );
  98. }
  99. function render() {
  100. var timer = Date.now() * 0.0003;
  101. camera.position.x = Math.sin( timer ) * 0.5;
  102. camera.position.z = Math.cos( timer ) * 0.5;
  103. camera.lookAt( new THREE.Vector3( 0, 0.1, 0 ) );
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>