webgl_buffergeometry_selective_draw.html 6.2 KB

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