webgl_buffergeometry_selective_draw.html 5.8 KB

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