webgl_buffergeometry_selective_draw.html 6.1 KB

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