123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - buffergeometry - selective - draw</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <script type="x-shader/x-vertex" id="vertexshader">
- attribute float visible;
- varying float vVisible;
- attribute vec3 vertColor;
- varying vec3 vColor;
- void main() {
- vColor = vertColor;
- vVisible = visible;
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentshader">
- varying float vVisible;
- varying vec3 vColor;
- void main() {
- if ( vVisible > 0.0 ) {
- gl_FragColor = vec4( vColor, 1.0 );
- } else {
- discard;
- }
- }
- </script>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> buffergeometry - selective - draw
- <div id="title"></div>
- <div id="ui"><a href="#" id="hideLines">CULL SOME LINES</a> - <a href="#" id="showAllLines">SHOW ALL LINES</a></div>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var camera, scene, renderer, stats;
- var geometry, mesh;
- var numLat = 100;
- var numLng = 200;
- var numLinesCulled = 0;
- init();
- animate();
- function init() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 10 );
- camera.position.z = 3.5;
- scene.add( new THREE.AmbientLight( 0x444444 ) );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- addLines( 1.0 );
- var hideLinesButton = document.getElementById( 'hideLines' );
- hideLinesButton.addEventListener( 'click', hideLines );
- var showAllLinesButton = document.getElementById( 'showAllLines' );
- showAllLinesButton.addEventListener( 'click', showAllLines );
- }
- function addLines( radius ) {
- geometry = new THREE.BufferGeometry();
- var linePositions = new Float32Array( numLat * numLng * 3 * 2 );
- var lineColors = new Float32Array( numLat * numLng * 3 * 2 );
- var visible = new Float32Array( numLat * numLng * 2 );
- for ( var i = 0; i < numLat; ++ i ) {
- for ( var j = 0; j < numLng; ++ j ) {
- var lat = ( Math.random() * Math.PI ) / 50.0 + i / numLat * Math.PI;
- var lng = ( Math.random() * Math.PI ) / 50.0 + j / numLng * 2 * Math.PI;
- var index = i * numLng + j;
- linePositions[ index * 6 + 0 ] = 0;
- linePositions[ index * 6 + 1 ] = 0;
- linePositions[ index * 6 + 2 ] = 0;
- linePositions[ index * 6 + 3 ] = radius * Math.sin( lat ) * Math.cos( lng );
- linePositions[ index * 6 + 4 ] = radius * Math.cos( lat );
- linePositions[ index * 6 + 5 ] = radius * Math.sin( lat ) * Math.sin( lng );
- var color = new THREE.Color( 0xffffff );
- color.setHSL( lat / Math.PI, 1.0, 0.2 );
- lineColors[ index * 6 + 0 ] = color.r;
- lineColors[ index * 6 + 1 ] = color.g;
- lineColors[ index * 6 + 2 ] = color.b;
- color.setHSL( lat / Math.PI, 1.0, 0.7 );
- lineColors[ index * 6 + 3 ] = color.r;
- lineColors[ index * 6 + 4 ] = color.g;
- lineColors[ index * 6 + 5 ] = color.b;
- // non-0 is visible
- visible[ index * 2 + 0 ] = 1.0;
- visible[ index * 2 + 1 ] = 1.0;
- }
- }
- geometry.setAttribute( 'position', new THREE.BufferAttribute( linePositions, 3 ) );
- geometry.setAttribute( 'vertColor', new THREE.BufferAttribute( lineColors, 3 ) );
- geometry.setAttribute( 'visible', new THREE.BufferAttribute( visible, 1 ) );
- geometry.computeBoundingSphere();
- var shaderMaterial = new THREE.ShaderMaterial( {
- vertexShader: document.getElementById( 'vertexshader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentshader' ).textContent
- } );
- mesh = new THREE.LineSegments( geometry, shaderMaterial );
- scene.add( mesh );
- updateCount();
- }
- function updateCount() {
- var str = '1 draw call, ' + numLat * numLng + ' lines, ' + numLinesCulled + ' culled (<a target="_blank" href="http://callum.com">author</a>)';
- document.getElementById( 'title' ).innerHTML = str.replace( /\B(?=(\d{3})+(?!\d))/g, "," );
- }
- function hideLines() {
- for ( var i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
- if ( Math.random() > 0.75 ) {
- if ( geometry.attributes.visible.array[ i + 0 ] ) {
- ++ numLinesCulled;
- }
- geometry.attributes.visible.array[ i + 0 ] = 0;
- geometry.attributes.visible.array[ i + 1 ] = 0;
- }
- }
- geometry.attributes.visible.needsUpdate = true;
- updateCount();
- }
- function showAllLines() {
- numLinesCulled = 0;
- for ( var i = 0; i < geometry.attributes.visible.array.length; i += 2 ) {
- geometry.attributes.visible.array[ i + 0 ] = 1;
- geometry.attributes.visible.array[ i + 1 ] = 1;
- }
- geometry.attributes.visible.needsUpdate = true;
- updateCount();
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate( time ) {
- requestAnimationFrame( animate );
- var time = Date.now() * 0.001;
- mesh.rotation.x = time * 0.25;
- mesh.rotation.y = time * 0.5;
- stats.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|