webgl_buffergeometry_selective_draw.html 5.8 KB

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