misc_exporter_draco.html 4.3 KB

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