misc_exporter_draco.html 4.0 KB

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