misc_exporter_draco.html 4.0 KB

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