webgl_buffergeometry_compression.html 7.6 KB

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