webgl_buffergeometry_selective_draw.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - selective - draw</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. <script type="x-shader/x-vertex" id="vertexshader">
  9. attribute float visible;
  10. varying float vVisible;
  11. attribute vec3 vertColor;
  12. varying vec3 vColor;
  13. void main() {
  14. vColor = vertColor;
  15. vVisible = visible;
  16. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  17. }
  18. </script>
  19. <script type="x-shader/x-fragment" id="fragmentshader">
  20. varying float vVisible;
  21. varying vec3 vColor;
  22. void main() {
  23. if ( vVisible > 0.0 ) {
  24. gl_FragColor = vec4( vColor, 1.0 );
  25. } else {
  26. discard;
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <div id="info">
  33. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> buffergeometry - selective - draw
  34. <div id="title"></div>
  35. <div id="ui"><a href="#" id="hideLines">CULL SOME LINES</a> - <a href="#" id="showAllLines">SHOW ALL LINES</a></div>
  36. </div>
  37. <!-- Import maps polyfill -->
  38. <!-- Remove this when import maps will be widely supported -->
  39. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  40. <script type="importmap">
  41. {
  42. "imports": {
  43. "three": "../build/three.module.js",
  44. "three/addons/": "./jsm/"
  45. }
  46. }
  47. </script>
  48. <script type="module">
  49. import * as THREE from 'three';
  50. import Stats from 'three/addons/libs/stats.module.js';
  51. let camera, scene, renderer, stats;
  52. let geometry, mesh;
  53. const numLat = 100;
  54. const numLng = 200;
  55. let numLinesCulled = 0;
  56. init();
  57. animate();
  58. function init() {
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.useLegacyLights = false;
  63. document.body.appendChild( renderer.domElement );
  64. scene = new THREE.Scene();
  65. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
  66. camera.position.z = 3.5;
  67. stats = new Stats();
  68. document.body.appendChild( stats.dom );
  69. window.addEventListener( 'resize', onWindowResize );
  70. addLines( 1.0 );
  71. const hideLinesButton = document.getElementById( 'hideLines' );
  72. hideLinesButton.addEventListener( 'click', hideLines );
  73. const showAllLinesButton = document.getElementById( 'showAllLines' );
  74. showAllLinesButton.addEventListener( 'click', showAllLines );
  75. }
  76. function addLines( radius ) {
  77. geometry = new THREE.BufferGeometry();
  78. const linePositions = new Float32Array( numLat * numLng * 3 * 2 );
  79. const lineColors = new Float32Array( numLat * numLng * 3 * 2 );
  80. const visible = new Float32Array( numLat * numLng * 2 );
  81. for ( let i = 0; i < numLat; ++ i ) {
  82. for ( let j = 0; j < numLng; ++ j ) {
  83. const lat = ( Math.random() * Math.PI ) / 50.0 + i / numLat * Math.PI;
  84. const lng = ( Math.random() * Math.PI ) / 50.0 + j / numLng * 2 * Math.PI;
  85. const index = i * numLng + j;
  86. linePositions[ index * 6 + 0 ] = 0;
  87. linePositions[ index * 6 + 1 ] = 0;
  88. linePositions[ index * 6 + 2 ] = 0;
  89. linePositions[ index * 6 + 3 ] = radius * Math.sin( lat ) * Math.cos( lng );
  90. linePositions[ index * 6 + 4 ] = radius * Math.cos( lat );
  91. linePositions[ index * 6 + 5 ] = radius * Math.sin( lat ) * Math.sin( lng );
  92. const color = new THREE.Color( 0xffffff );
  93. color.setHSL( lat / Math.PI, 1.0, 0.2 );
  94. lineColors[ index * 6 + 0 ] = color.r;
  95. lineColors[ index * 6 + 1 ] = color.g;
  96. lineColors[ index * 6 + 2 ] = color.b;
  97. color.setHSL( lat / Math.PI, 1.0, 0.7 );
  98. lineColors[ index * 6 + 3 ] = color.r;
  99. lineColors[ index * 6 + 4 ] = color.g;
  100. lineColors[ index * 6 + 5 ] = color.b;
  101. // non-0 is visible
  102. visible[ index * 2 + 0 ] = 1.0;
  103. visible[ index * 2 + 1 ] = 1.0;
  104. }
  105. }
  106. geometry.setAttribute( 'position', new THREE.BufferAttribute( linePositions, 3 ) );
  107. geometry.setAttribute( 'vertColor', new THREE.BufferAttribute( lineColors, 3 ) );
  108. geometry.setAttribute( 'visible', new THREE.BufferAttribute( visible, 1 ) );
  109. geometry.computeBoundingSphere();
  110. const shaderMaterial = new THREE.ShaderMaterial( {
  111. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  112. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  113. } );
  114. mesh = new THREE.LineSegments( geometry, shaderMaterial );
  115. scene.add( mesh );
  116. updateCount();
  117. }
  118. function updateCount() {
  119. const str = '1 draw call, ' + numLat * numLng + ' lines, ' + numLinesCulled + ' culled (<a target="_blank" href="http://callum.com">author</a>)';
  120. document.getElementById( 'title' ).innerHTML = str.replace( /\B(?=(\d{3})+(?!\d))/g, ',' );
  121. }
  122. function hideLines() {
  123. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  124. if ( Math.random() > 0.75 ) {
  125. if ( geometry.attributes.visible.array[ i + 0 ] ) {
  126. ++ numLinesCulled;
  127. }
  128. geometry.attributes.visible.array[ i + 0 ] = 0;
  129. geometry.attributes.visible.array[ i + 1 ] = 0;
  130. }
  131. }
  132. geometry.attributes.visible.needsUpdate = true;
  133. updateCount();
  134. }
  135. function showAllLines() {
  136. numLinesCulled = 0;
  137. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  138. geometry.attributes.visible.array[ i + 0 ] = 1;
  139. geometry.attributes.visible.array[ i + 1 ] = 1;
  140. }
  141. geometry.attributes.visible.needsUpdate = true;
  142. updateCount();
  143. }
  144. function onWindowResize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. const time = Date.now() * 0.001;
  152. mesh.rotation.x = time * 0.25;
  153. mesh.rotation.y = time * 0.5;
  154. stats.update();
  155. renderer.render( scene, camera );
  156. }
  157. </script>
  158. </body>
  159. </html>