misc_exporter_draco.html 3.9 KB

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