1
0

webgl_buffergeometry_selective_draw.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <meta property="og:title" content="three.js webgl - buffergeometry - selective - draw">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_selective_draw.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_selective_draw.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float visible;
  14. varying float vVisible;
  15. attribute vec3 vertColor;
  16. varying vec3 vColor;
  17. void main() {
  18. vColor = vertColor;
  19. vVisible = visible;
  20. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. varying float vVisible;
  25. varying vec3 vColor;
  26. void main() {
  27. if ( vVisible > 0.0 ) {
  28. gl_FragColor = vec4( vColor, 1.0 );
  29. } else {
  30. discard;
  31. }
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> buffergeometry - selective - draw
  38. <div id="title"></div>
  39. <div id="ui"><a href="#" id="hideLines">CULL SOME LINES</a> - <a href="#" id="showAllLines">SHOW ALL LINES</a></div>
  40. </div>
  41. <script type="importmap">
  42. {
  43. "imports": {
  44. "three": "../build/three.module.js",
  45. "three/addons/": "./jsm/"
  46. }
  47. }
  48. </script>
  49. <script type="module">
  50. import * as THREE from 'three';
  51. import Stats from 'three/addons/libs/stats.module.js';
  52. let camera, scene, renderer, stats;
  53. let geometry, mesh;
  54. const numLat = 100;
  55. const numLng = 200;
  56. let numLinesCulled = 0;
  57. init();
  58. function init() {
  59. scene = new THREE.Scene();
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
  61. camera.position.z = 3.5;
  62. stats = new Stats();
  63. document.body.appendChild( stats.dom );
  64. window.addEventListener( 'resize', onWindowResize );
  65. addLines( 1.0 );
  66. const hideLinesButton = document.getElementById( 'hideLines' );
  67. hideLinesButton.addEventListener( 'click', hideLines );
  68. const showAllLinesButton = document.getElementById( 'showAllLines' );
  69. showAllLinesButton.addEventListener( 'click', showAllLines );
  70. renderer = new THREE.WebGLRenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( animate );
  74. document.body.appendChild( renderer.domElement );
  75. }
  76. function addLines( radius ) {
  77. geometry = new THREE.BufferGeometry();
  78. const linePositions = new Float32Array( numLat * numLng * 3 * 2 );
  79. const lineColors = new Float32Array( numLat * numLng * 3 * 2 );
  80. const visible = new Float32Array( numLat * numLng * 2 );
  81. for ( let i = 0; i < numLat; ++ i ) {
  82. for ( let j = 0; j < numLng; ++ j ) {
  83. const lat = ( Math.random() * Math.PI ) / 50.0 + i / numLat * Math.PI;
  84. const lng = ( Math.random() * Math.PI ) / 50.0 + j / numLng * 2 * Math.PI;
  85. const index = i * numLng + j;
  86. linePositions[ index * 6 + 0 ] = 0;
  87. linePositions[ index * 6 + 1 ] = 0;
  88. linePositions[ index * 6 + 2 ] = 0;
  89. linePositions[ index * 6 + 3 ] = radius * Math.sin( lat ) * Math.cos( lng );
  90. linePositions[ index * 6 + 4 ] = radius * Math.cos( lat );
  91. linePositions[ index * 6 + 5 ] = radius * Math.sin( lat ) * Math.sin( lng );
  92. const color = new THREE.Color( 0xffffff );
  93. color.setHSL( lat / Math.PI, 1.0, 0.2 );
  94. lineColors[ index * 6 + 0 ] = color.r;
  95. lineColors[ index * 6 + 1 ] = color.g;
  96. lineColors[ index * 6 + 2 ] = color.b;
  97. color.setHSL( lat / Math.PI, 1.0, 0.7 );
  98. lineColors[ index * 6 + 3 ] = color.r;
  99. lineColors[ index * 6 + 4 ] = color.g;
  100. lineColors[ index * 6 + 5 ] = color.b;
  101. // non-0 is visible
  102. visible[ index * 2 + 0 ] = 1.0;
  103. visible[ index * 2 + 1 ] = 1.0;
  104. }
  105. }
  106. geometry.setAttribute( 'position', new THREE.BufferAttribute( linePositions, 3 ) );
  107. geometry.setAttribute( 'vertColor', new THREE.BufferAttribute( lineColors, 3 ) );
  108. geometry.setAttribute( 'visible', new THREE.BufferAttribute( visible, 1 ) );
  109. geometry.computeBoundingSphere();
  110. const shaderMaterial = new THREE.ShaderMaterial( {
  111. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  112. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  113. } );
  114. mesh = new THREE.LineSegments( geometry, shaderMaterial );
  115. scene.add( mesh );
  116. updateCount();
  117. }
  118. function updateCount() {
  119. const str = '1 draw call, ' + numLat * numLng + ' lines, ' + numLinesCulled + ' culled (<a target="_blank" href="http://callum.com">author</a>)';
  120. document.getElementById( 'title' ).innerHTML = str.replace( /\B(?=(\d{3})+(?!\d))/g, ',' );
  121. }
  122. function hideLines() {
  123. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  124. if ( Math.random() > 0.75 ) {
  125. if ( geometry.attributes.visible.array[ i + 0 ] ) {
  126. ++ numLinesCulled;
  127. }
  128. geometry.attributes.visible.array[ i + 0 ] = 0;
  129. geometry.attributes.visible.array[ i + 1 ] = 0;
  130. }
  131. }
  132. geometry.attributes.visible.needsUpdate = true;
  133. updateCount();
  134. }
  135. function showAllLines() {
  136. numLinesCulled = 0;
  137. for ( let i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
  138. geometry.attributes.visible.array[ i + 0 ] = 1;
  139. geometry.attributes.visible.array[ i + 1 ] = 1;
  140. }
  141. geometry.attributes.visible.needsUpdate = true;
  142. updateCount();
  143. }
  144. function onWindowResize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. function animate() {
  150. const time = Date.now() * 0.001;
  151. mesh.rotation.x = time * 0.25;
  152. mesh.rotation.y = time * 0.5;
  153. renderer.render( scene, camera );
  154. stats.update();
  155. }
  156. </script>
  157. </body>
  158. </html>
粤ICP备19079148号