webgl_buffergeometry_selective_draw.html 5.9 KB

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