webgl_buffergeometry_selective_draw.html 6.1 KB

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