webgl_buffergeometry_selective_draw.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js",
  41. "three/addons/": "./jsm/"
  42. }
  43. }
  44. </script>
  45. <script type="module">
  46. import * as THREE from 'three';
  47. import Stats from 'three/addons/libs/stats.module.js';
  48. let camera, scene, renderer, stats;
  49. let geometry, mesh;
  50. const numLat = 100;
  51. const numLng = 200;
  52. let numLinesCulled = 0;
  53. init();
  54. function init() {
  55. scene = new THREE.Scene();
  56. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
  57. camera.position.z = 3.5;
  58. stats = new Stats();
  59. document.body.appendChild( stats.dom );
  60. window.addEventListener( 'resize', onWindowResize );
  61. addLines( 1.0 );
  62. const hideLinesButton = document.getElementById( 'hideLines' );
  63. hideLinesButton.addEventListener( 'click', hideLines );
  64. const showAllLinesButton = document.getElementById( 'showAllLines' );
  65. showAllLinesButton.addEventListener( 'click', showAllLines );
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.setAnimationLoop( animate );
  70. document.body.appendChild( renderer.domElement );
  71. }
  72. function addLines( radius ) {
  73. geometry = new THREE.BufferGeometry();
  74. const linePositions = new Float32Array( numLat * numLng * 3 * 2 );
  75. const lineColors = new Float32Array( numLat * numLng * 3 * 2 );
  76. const visible = new Float32Array( numLat * numLng * 2 );
  77. for ( let i = 0; i < numLat; ++ i ) {
  78. for ( let j = 0; j < numLng; ++ j ) {
  79. const lat = ( Math.random() * Math.PI ) / 50.0 + i / numLat * Math.PI;
  80. const lng = ( Math.random() * Math.PI ) / 50.0 + j / numLng * 2 * Math.PI;
  81. const index = i * numLng + j;
  82. linePositions[ index * 6 + 0 ] = 0;
  83. linePositions[ index * 6 + 1 ] = 0;
  84. linePositions[ index * 6 + 2 ] = 0;
  85. linePositions[ index * 6 + 3 ] = radius * Math.sin( lat ) * Math.cos( lng );
  86. linePositions[ index * 6 + 4 ] = radius * Math.cos( lat );
  87. linePositions[ index * 6 + 5 ] = radius * Math.sin( lat ) * Math.sin( lng );
  88. const color = new THREE.Color( 0xffffff );
  89. color.setHSL( lat / Math.PI, 1.0, 0.2 );
  90. lineColors[ index * 6 + 0 ] = color.r;
  91. lineColors[ index * 6 + 1 ] = color.g;
  92. lineColors[ index * 6 + 2 ] = color.b;
  93. color.setHSL( lat / Math.PI, 1.0, 0.7 );
  94. lineColors[ index * 6 + 3 ] = color.r;
  95. lineColors[ index * 6 + 4 ] = color.g;
  96. lineColors[ index * 6 + 5 ] = color.b;
  97. // non-0 is visible
  98. visible[ index * 2 + 0 ] = 1.0;
  99. visible[ index * 2 + 1 ] = 1.0;
  100. }
  101. }
  102. geometry.setAttribute( 'position', new THREE.BufferAttribute( linePositions, 3 ) );
  103. geometry.setAttribute( 'vertColor', new THREE.BufferAttribute( lineColors, 3 ) );
  104. geometry.setAttribute( 'visible', new THREE.BufferAttribute( visible, 1 ) );
  105. geometry.computeBoundingSphere();
  106. const shaderMaterial = new THREE.ShaderMaterial( {
  107. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  108. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  109. } );
  110. mesh = new THREE.LineSegments( geometry, shaderMaterial );
  111. scene.add( mesh );
  112. updateCount();
  113. }
  114. function updateCount() {
  115. const str = '1 draw call, ' + numLat * numLng + ' lines, ' + numLinesCulled + ' culled (<a target="_blank" href="http://callum.com">author</a>)';
  116. document.getElementById( 'title' ).innerHTML = str.replace( /\B(?=(\d{3})+(?!\d))/g, ',' );
  117. }
  118. function hideLines() {
  119. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  120. if ( Math.random() > 0.75 ) {
  121. if ( geometry.attributes.visible.array[ i + 0 ] ) {
  122. ++ numLinesCulled;
  123. }
  124. geometry.attributes.visible.array[ i + 0 ] = 0;
  125. geometry.attributes.visible.array[ i + 1 ] = 0;
  126. }
  127. }
  128. geometry.attributes.visible.needsUpdate = true;
  129. updateCount();
  130. }
  131. function showAllLines() {
  132. numLinesCulled = 0;
  133. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  134. geometry.attributes.visible.array[ i + 0 ] = 1;
  135. geometry.attributes.visible.array[ i + 1 ] = 1;
  136. }
  137. geometry.attributes.visible.needsUpdate = true;
  138. updateCount();
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. }
  145. function animate() {
  146. const time = Date.now() * 0.001;
  147. mesh.rotation.x = time * 0.25;
  148. mesh.rotation.y = time * 0.5;
  149. renderer.render( scene, camera );
  150. stats.update();
  151. }
  152. </script>
  153. </body>
  154. </html>