webgl_buffergeometry_compression.html 7.0 KB

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