misc_exporter_draco.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - draco</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - draco
  12. </div>
  13. <script src="js/libs/draco/draco_encoder.js"></script>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. import { DRACOExporter } from './jsm/exporters/DRACOExporter.js';
  28. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  29. let scene, camera, renderer, exporter, mesh;
  30. const params = {
  31. export: exportFile
  32. };
  33. init();
  34. animate();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  37. camera.position.set( 200, 100, 200 );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0xa0a0a0 );
  40. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  41. exporter = new DRACOExporter();
  42. //
  43. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  44. hemiLight.position.set( 0, 200, 0 );
  45. scene.add( hemiLight );
  46. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  47. directionalLight.position.set( 0, 200, 100 );
  48. directionalLight.castShadow = true;
  49. directionalLight.shadow.camera.top = 180;
  50. directionalLight.shadow.camera.bottom = - 100;
  51. directionalLight.shadow.camera.left = - 120;
  52. directionalLight.shadow.camera.right = 120;
  53. scene.add( directionalLight );
  54. // ground
  55. const ground = new THREE.Mesh(
  56. new THREE.PlaneGeometry( 2000, 2000 ),
  57. new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } )
  58. );
  59. ground.rotation.x = - Math.PI / 2;
  60. ground.position.y = - 75;
  61. ground.receiveShadow = true;
  62. scene.add( ground );
  63. const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  64. grid.material.opacity = 0.2;
  65. grid.material.transparent = true;
  66. grid.position.y = - 75;
  67. scene.add( grid );
  68. // export mesh
  69. const geometry = new THREE.TorusKnotGeometry( 50, 15, 200, 30 );
  70. const material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
  71. mesh = new THREE.Mesh( geometry, material );
  72. mesh.castShadow = true;
  73. mesh.position.y = 25;
  74. scene.add( mesh );
  75. //
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.shadowMap.enabled = true;
  80. document.body.appendChild( renderer.domElement );
  81. //
  82. const controls = new OrbitControls( camera, renderer.domElement );
  83. controls.target.set( 0, 25, 0 );
  84. controls.update();
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. const gui = new GUI();
  88. gui.add( params, 'export' ).name( 'Export DRC' );
  89. gui.open();
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. renderer.render( scene, camera );
  99. }
  100. function exportFile() {
  101. const result = exporter.parse( mesh );
  102. saveArrayBuffer( result, 'file.drc' );
  103. }
  104. const link = document.createElement( 'a' );
  105. link.style.display = 'none';
  106. document.body.appendChild( link );
  107. function save( blob, filename ) {
  108. link.href = URL.createObjectURL( blob );
  109. link.download = filename;
  110. link.click();
  111. }
  112. function saveArrayBuffer( buffer, filename ) {
  113. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  114. }
  115. </script>
  116. </body>
  117. </html>