webgl_buffergeometry_compression.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - compression</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> - BufferGeometry Compression<br />
  12. Octahedron and Quantization encoding methods from Tarek Sherif
  13. </div>
  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. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  28. import * as GeometryCompressionUtils from './jsm/utils/GeometryCompressionUtils.js';
  29. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  30. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  31. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  32. const statsEnabled = true;
  33. let container, stats, gui;
  34. let camera, scene, renderer, controls;
  35. const lights = [];
  36. // options
  37. const data = {
  38. "model": "Icosahedron",
  39. "wireframe": false,
  40. "texture": false,
  41. "detail": 4,
  42. "rotationSpeed": 0.1,
  43. "QuantizePosEncoding": false,
  44. "NormEncodingMethods": "None", // for normal encodings
  45. "DefaultUVEncoding": false,
  46. "totalGPUMemory": "0 bytes"
  47. };
  48. let memoryDisplay;
  49. // geometry params
  50. const radius = 100;
  51. // materials
  52. const lineMaterial = new THREE.LineBasicMaterial( { color: 0xaaaaaa, transparent: true, opacity: 0.8 } );
  53. const meshMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
  54. // texture
  55. const texture = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  56. texture.wrapS = THREE.RepeatWrapping;
  57. texture.wrapT = THREE.RepeatWrapping;
  58. //
  59. init();
  60. animate();
  61. function init() {
  62. //
  63. container = document.createElement( 'div' );
  64. document.body.appendChild( container );
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild( renderer.domElement );
  69. //
  70. scene = new THREE.Scene();
  71. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10000000 );
  72. camera.position.x = 2 * radius;
  73. camera.position.y = 2 * radius;
  74. camera.position.z = 2 * radius;
  75. controls = new OrbitControls( camera, renderer.domElement );
  76. //
  77. lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  78. lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  79. lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  80. lights[ 0 ].position.set( 0, 2 * radius, 0 );
  81. lights[ 1 ].position.set( 2 * radius, - 2 * radius, 2 * radius );
  82. lights[ 2 ].position.set( - 2 * radius, - 2 * radius, - 2 * radius );
  83. scene.add( lights[ 0 ] );
  84. scene.add( lights[ 1 ] );
  85. scene.add( lights[ 2 ] );
  86. //
  87. scene.add( new THREE.AxesHelper( radius * 5 ) );
  88. //
  89. let geom = newGeometry( data );
  90. const mesh = new THREE.Mesh( geom, meshMaterial );
  91. const lineSegments = new THREE.LineSegments( new THREE.WireframeGeometry( geom ), lineMaterial );
  92. lineSegments.visible = data.wireframe;
  93. scene.add( mesh );
  94. scene.add( lineSegments );
  95. //
  96. gui = new GUI();
  97. gui.width = 350;
  98. function newGeometry( data ) {
  99. switch ( data.model ) {
  100. case "Icosahedron":
  101. return new THREE.IcosahedronGeometry( radius, data.detail );
  102. case "Cylinder":
  103. return new THREE.CylinderGeometry( radius, radius, radius * 2, data.detail * 6 );
  104. case "Teapot":
  105. return new TeapotGeometry( radius, data.detail * 3, true, true, true, true, true );
  106. case "TorusKnot":
  107. return new THREE.TorusKnotGeometry( radius, 10, data.detail * 20, data.detail * 6, 3, 4 );
  108. }
  109. }
  110. function generateGeometry() {
  111. geom = newGeometry( data );
  112. updateGroupGeometry(
  113. mesh,
  114. lineSegments,
  115. geom,
  116. data );
  117. }
  118. // updateLineSegments
  119. function updateLineSegments() {
  120. lineSegments.visible = data.wireframe;
  121. }
  122. let folder = gui.addFolder( 'Scene' );
  123. folder.add( data, 'model', [ "Icosahedron", "Cylinder", "TorusKnot", "Teapot" ] ).onChange( generateGeometry );
  124. folder.add( data, 'wireframe', false ).onChange( updateLineSegments );
  125. folder.add( data, 'texture', false ).onChange( generateGeometry );
  126. folder.add( data, 'detail', 1, 8, 1 ).onChange( generateGeometry );
  127. folder.add( data, 'rotationSpeed', 0, 0.5, 0.1 );
  128. folder.open();
  129. folder = gui.addFolder( 'Position Compression' );
  130. folder.add( data, 'QuantizePosEncoding', false ).onChange( generateGeometry );
  131. folder.open();
  132. folder = gui.addFolder( 'Normal Compression' );
  133. folder.add( data, 'NormEncodingMethods', [ "None", "DEFAULT", "OCT1Byte", "OCT2Byte", "ANGLES" ] ).onChange( generateGeometry );
  134. folder.open();
  135. folder = gui.addFolder( 'UV Compression' );
  136. folder.add( data, 'DefaultUVEncoding', false ).onChange( generateGeometry );
  137. folder.open();
  138. folder = gui.addFolder( 'Memory Info' );
  139. folder.open();
  140. memoryDisplay = folder.add( data, 'totalGPUMemory', "0 bytes" );
  141. computeGPUMemory( mesh );
  142. //
  143. if ( statsEnabled ) {
  144. stats = new Stats();
  145. container.appendChild( stats.dom );
  146. }
  147. window.addEventListener( 'resize', onWindowResize );
  148. }
  149. //
  150. function onWindowResize() {
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. }
  155. //
  156. function updateLightsPossition() {
  157. lights.forEach( light => {
  158. const direction = light.position.clone();
  159. direction.applyAxisAngle( new THREE.Vector3( 1, 1, 0 ), data.rotationSpeed / 180 * Math.PI );
  160. light.position.add( direction.sub( light.position ) );
  161. } );
  162. }
  163. //
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. controls.update();
  167. updateLightsPossition();
  168. renderer.render( scene, camera );
  169. if ( statsEnabled ) stats.update();
  170. }
  171. //
  172. function updateGroupGeometry( mesh, lineSegments, geometry, data ) {
  173. // dispose first
  174. lineSegments.geometry.dispose();
  175. mesh.geometry.dispose();
  176. lineSegments.geometry = new THREE.WireframeGeometry( geometry );
  177. mesh.geometry = geometry;
  178. mesh.material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
  179. mesh.material.map = data.texture ? texture : null;
  180. if ( data[ "QuantizePosEncoding" ] ) {
  181. GeometryCompressionUtils.compressPositions( mesh );
  182. }
  183. if ( data[ "NormEncodingMethods" ] !== "None" ) {
  184. GeometryCompressionUtils.compressNormals( mesh, data[ "NormEncodingMethods" ] );
  185. }
  186. if ( data[ "DefaultUVEncoding" ] ) {
  187. GeometryCompressionUtils.compressUvs( mesh );
  188. }
  189. computeGPUMemory( mesh );
  190. }
  191. function computeGPUMemory( mesh ) {
  192. // Use BufferGeometryUtils to do memory calculation
  193. memoryDisplay.setValue( BufferGeometryUtils.estimateBytesUsed( mesh.geometry ) + " bytes" );
  194. }
  195. </script>
  196. </body>
  197. </html>