webgl_buffergeometry_compression.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { GeometryCompressionUtils } from './jsm/utils/GeometryCompressionUtils.js';
  19. import { GUI } from './jsm/libs/dat.gui.module.js';
  20. var statsEnabled = true;
  21. var container, stats, gui;
  22. var camera, scene, renderer, controls;
  23. var lights = [];
  24. // options
  25. var data = {
  26. "wireframe": false,
  27. "texture": false,
  28. "detail": 4,
  29. "rotationSpeed": 0.1,
  30. "quantizeEncodePos": false,
  31. "defaultEncodeNormal": false,
  32. "anglesEncodeNormal": false,
  33. "oct1bytesEncode": false,
  34. "oct2bytesEncode": false,
  35. "defaultEncodeUV": false,
  36. "totalGPUMemory": "0 bytes"
  37. };
  38. var memoryDisplay;
  39. // geometry params
  40. var radius = 100;
  41. // materials
  42. var lineMaterial = new THREE.LineBasicMaterial( { color: 0xaaaaaa, transparent: true, opacity: 0.8 } );
  43. var meshMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
  44. // texture
  45. var texture = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  46. texture.wrapS = THREE.RepeatWrapping;
  47. texture.wrapT = THREE.RepeatWrapping;
  48. //
  49. init();
  50. animate();
  51. function init() {
  52. //
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. //
  60. scene = new THREE.Scene();
  61. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10000000 );
  62. camera.position.x = 2 * radius;
  63. camera.position.y = 2 * radius;
  64. camera.position.z = 2 * radius;
  65. controls = new OrbitControls( camera, renderer.domElement );
  66. //
  67. lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  68. lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  69. lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 );
  70. lights[ 0 ].position.set( 0, 2 * radius, 0 );
  71. lights[ 1 ].position.set( 2 * radius, - 2 * radius, 2 * radius );
  72. lights[ 2 ].position.set( - 2 * radius, - 2 * radius, - 2 * radius );
  73. scene.add( lights[ 0 ] );
  74. scene.add( lights[ 1 ] );
  75. scene.add( lights[ 2 ] );
  76. //
  77. scene.add( new THREE.AxesHelper( radius * 5 ) );
  78. //
  79. var ballGeom = newGeometry();
  80. var ballMesh = new THREE.Mesh( ballGeom, meshMaterial );
  81. var ballLineSegments = new THREE.LineSegments( new THREE.WireframeGeometry( ballGeom ), lineMaterial );
  82. ballLineSegments.visible = data.wireframe;
  83. scene.add( ballMesh );
  84. scene.add( ballLineSegments );
  85. //
  86. gui = new GUI();
  87. gui.width = 350;
  88. function newGeometry() {
  89. var geom = new THREE.IcosahedronBufferGeometry( radius, data.detail );
  90. // var geom = new THREE.CylinderBufferGeometry( 5, 5, 20, 32 );
  91. // var geom = new THREE.OctahedronBufferGeometry( radius, data.detail );
  92. // var geom = new THREE.BoxBufferGeometry(radius, radius, radius, data.detail, data.detail, data.detail);
  93. return geom;
  94. }
  95. function generateGeometry() {
  96. updateGroupGeometry(
  97. ballMesh,
  98. ballLineSegments,
  99. newGeometry(),
  100. data );
  101. }
  102. // updateLineSegments
  103. function updateLineSegments() {
  104. ballLineSegments.visible = data.wireframe;
  105. }
  106. var folder = gui.addFolder( 'Scene' );
  107. folder.open();
  108. folder.add( data, 'wireframe', false ).onChange( updateLineSegments );
  109. folder.add( data, 'texture', false ).onChange( generateGeometry );
  110. folder.add( data, 'detail', 0, 6, 1 ).onChange( generateGeometry );
  111. folder.add( data, 'rotationSpeed', 0, 0.5, 0.1 );
  112. folder = gui.addFolder( 'Position Compression' );
  113. folder.open();
  114. folder.add( data, 'quantizeEncodePos', false ).onChange( generateGeometry );
  115. folder = gui.addFolder( 'Normal Compression' );
  116. folder.open();
  117. folder.add( data, 'defaultEncodeNormal', false ).onChange( generateGeometry );
  118. folder.add( data, 'anglesEncodeNormal', false ).onChange( generateGeometry );
  119. folder.add( data, 'oct1bytesEncode', false ).onChange( generateGeometry );
  120. folder.add( data, 'oct2bytesEncode', false ).onChange( generateGeometry );
  121. folder = gui.addFolder( 'UV Compression' );
  122. folder.open();
  123. folder.add( data, 'defaultEncodeUV', false ).onChange( generateGeometry );
  124. folder = gui.addFolder( 'Memory Info' );
  125. folder.open();
  126. memoryDisplay = folder.add( data, 'totalGPUMemory', "0 bytes" );
  127. computeGPUMemory( ballMesh );
  128. //
  129. if ( statsEnabled ) {
  130. stats = new Stats();
  131. container.appendChild( stats.dom );
  132. }
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. }
  135. //
  136. function onWindowResize() {
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. }
  141. //
  142. function updateLightsPossition() {
  143. lights.forEach( light => {
  144. var direction = light.position.clone();
  145. direction.applyAxisAngle( new THREE.Vector3( 1, 1, 0 ), data.rotationSpeed / 180 * Math.PI );
  146. light.position.add( direction.sub( light.position ) );
  147. } );
  148. }
  149. //
  150. function animate() {
  151. requestAnimationFrame( animate );
  152. controls.update();
  153. updateLightsPossition();
  154. renderer.render( scene, camera );
  155. if ( statsEnabled ) stats.update();
  156. }
  157. //
  158. function updateGroupGeometry( mesh, lineSegments, geometry, data ) {
  159. if ( geometry.isGeometry ) {
  160. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  161. console.warn( 'THREE.GeometryBrowser: Converted Geometry to BufferGeometry.' );
  162. }
  163. lineSegments.geometry.dispose();
  164. mesh.geometry.dispose();
  165. lineSegments.geometry = new THREE.WireframeGeometry( geometry );
  166. mesh.geometry = geometry;
  167. mesh.material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
  168. mesh.material.map = data.texture ? texture : null;
  169. var normalEncode = "";
  170. if ( data.oct1bytesEncode ) {
  171. /**
  172. * It is not recommended to use 1-byte octahedron normals encoding unless you want to extremely reduce the memory usage
  173. * As it makes vertex data not aligned to a 4 byte boundary which may harm some WebGL implementations and sometimes the normal distortion is visible
  174. * Please refer to @zeux 's comments in https://github.com/mrdoob/three.js/pull/18208
  175. */
  176. normalEncode = "OCT1Byte";
  177. } else if ( data.oct2bytesEncode ) {
  178. normalEncode = "OCT2Byte";
  179. } else if ( data.anglesEncodeNormal ) {
  180. normalEncode = "ANGLES";
  181. } else if ( data.defaultEncodeNormal ) {
  182. normalEncode = "DEFAULT";
  183. }
  184. if ( normalEncode != "" ) {
  185. GeometryCompressionUtils.compressNormals( mesh, normalEncode );
  186. }
  187. if ( data.quantizeEncodePos ) {
  188. GeometryCompressionUtils.compressPositions( mesh );
  189. }
  190. if ( data.defaultEncodeUV ) {
  191. GeometryCompressionUtils.compressUvs( mesh );
  192. }
  193. computeGPUMemory( mesh );
  194. }
  195. function computeGPUMemory( mesh ) {
  196. let posBytes = mesh.geometry.attributes.position.bytes || mesh.geometry.attributes.position.array.length * 4;
  197. let normBytes = mesh.geometry.attributes.normal.bytes || mesh.geometry.attributes.normal.array.length * 4;
  198. let uvBytes = mesh.geometry.attributes.uv.bytes || mesh.geometry.attributes.uv.array.length * 4;
  199. memoryDisplay.setValue( posBytes + normBytes + uvBytes + " bytes" );
  200. }
  201. </script>
  202. </body>
  203. </html>