webgl_buffergeometry_selective_draw.html 5.8 KB

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