misc_exporter_draco.html 5.1 KB

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