123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - instancing - performance</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">
- <style>
- #info {
- background-color: rgba(0,0,0,0.75);
- }
- .dg .folder .gui-stats {
- height: auto;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - instancing - performance
- </div>
- <div id="container"></div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
- var container, stats, gui, guiStatsEl;
- var camera, controls, scene, renderer, material;
- // gui
- var Method = {
- INSTANCED: 'INSTANCED',
- MERGED: 'MERGED',
- NAIVE: 'NAIVE'
- };
- var api = {
- method: Method.INSTANCED,
- count: 1000
- };
- //
- init();
- initMesh();
- animate();
- //
- function clean() {
- var meshes = [];
- scene.traverse( function ( object ) {
- if ( object.isMesh ) meshes.push( object );
- } );
- for ( var i = 0; i < meshes.length; i ++ ) {
- var mesh = meshes[ i ];
- mesh.material.dispose();
- mesh.geometry.dispose();
- scene.remove( mesh );
- }
- }
- var randomizeMatrix = function () {
- var position = new THREE.Vector3();
- var rotation = new THREE.Euler();
- var quaternion = new THREE.Quaternion();
- var scale = new THREE.Vector3();
- return function ( matrix ) {
- position.x = Math.random() * 40 - 20;
- position.y = Math.random() * 40 - 20;
- position.z = Math.random() * 40 - 20;
- rotation.x = Math.random() * 2 * Math.PI;
- rotation.y = Math.random() * 2 * Math.PI;
- rotation.z = Math.random() * 2 * Math.PI;
- quaternion.setFromEuler( rotation );
- scale.x = scale.y = scale.z = Math.random() * 1;
- matrix.compose( position, quaternion, scale );
- };
- }();
- function initMesh() {
- clean();
- // make instances
- new THREE.BufferGeometryLoader()
- .setPath( 'models/json/' )
- .load( 'suzanne_buffergeometry.json', function ( geometry ) {
- material = new THREE.MeshNormalMaterial();
- geometry.computeVertexNormals();
- console.time( api.method + ' (build)' );
- switch ( api.method ) {
- case Method.INSTANCED:
- makeInstanced( geometry );
- break;
- case Method.MERGED:
- makeMerged( geometry );
- break;
- case Method.NAIVE:
- makeNaive( geometry );
- break;
- }
- console.timeEnd( api.method + ' (build)' );
- } );
- }
- function makeInstanced( geometry ) {
- var matrix = new THREE.Matrix4();
- var mesh = new THREE.InstancedMesh( geometry, material, api.count );
- for ( var i = 0; i < api.count; i ++ ) {
- randomizeMatrix( matrix );
- mesh.setMatrixAt( i, matrix );
- }
- scene.add( mesh );
- //
- var geometryByteLength = getGeometryByteLength( geometry );
- guiStatsEl.innerHTML = [
- '<i>GPU draw calls</i>: 1',
- '<i>GPU memory</i>: ' + formatBytes( api.count * 16 + geometryByteLength, 2 )
- ].join( '<br/>' );
- }
- function makeMerged( geometry ) {
- var instanceGeometry;
- var geometries = [];
- var matrix = new THREE.Matrix4();
- for ( var i = 0; i < api.count; i ++ ) {
- randomizeMatrix( matrix );
- var instanceGeometry = geometry.clone();
- instanceGeometry.applyMatrix4( matrix );
- geometries.push( instanceGeometry );
- }
- var mergedGeometry = BufferGeometryUtils.mergeBufferGeometries( geometries );
- scene.add( new THREE.Mesh( mergedGeometry, material ) );
- //
- guiStatsEl.innerHTML = [
- '<i>GPU draw calls</i>: 1',
- '<i>GPU memory</i>: ' + formatBytes( getGeometryByteLength( mergedGeometry ), 2 )
- ].join( '<br/>' );
- }
- function makeNaive( geometry ) {
- var matrix = new THREE.Matrix4();
- for ( var i = 0; i < api.count; i ++ ) {
- randomizeMatrix( matrix );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.applyMatrix4( matrix );
- scene.add( mesh );
- }
- //
- var geometryByteLength = getGeometryByteLength( geometry );
- guiStatsEl.innerHTML = [
- '<i>GPU draw calls</i>: ' + api.count,
- '<i>GPU memory</i>: ' + formatBytes( api.count * 16 + geometryByteLength, 2 )
- ].join( '<br/>' );
- }
- function init() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- // camera
- camera = new THREE.PerspectiveCamera( 70, width / height, 1, 100 );
- camera.position.z = 30;
- // renderer
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( width, height );
- renderer.outputEncoding = THREE.sRGBEncoding;
- container = document.getElementById( 'container' );
- container.appendChild( renderer.domElement );
- // scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- // controls
- controls = new OrbitControls( camera, renderer.domElement );
- controls.autoRotate = true;
- // stats
- stats = new Stats();
- container.appendChild( stats.dom );
- // gui
- gui = new GUI();
- gui.add( api, 'method', Method ).onChange( initMesh );
- gui.add( api, 'count', 1, 10000 ).step( 1 ).onChange( initMesh );
- var perfFolder = gui.addFolder( 'Performance' );
- guiStatsEl = document.createElement( 'li' );
- guiStatsEl.classList.add( 'gui-stats' );
- perfFolder.__ul.appendChild( guiStatsEl );
- perfFolder.open();
- // listeners
- window.addEventListener( 'resize', onWindowResize, false );
- Object.assign(window, {scene});
- }
- //
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- stats.update();
- render();
- }
- function render() {
- renderer.render( scene, camera );
- }
- //
- function getGeometryByteLength( geometry ) {
- var total = 0;
- if ( geometry.index ) total += geometry.index.array.byteLength;
- for ( var name in geometry.attributes ) {
- total += geometry.attributes[ name ].array.byteLength;
- }
- return total;
- }
- // Source: https://stackoverflow.com/a/18650828/1314762
- function formatBytes( bytes, decimals ) {
- if ( bytes === 0 ) return '0 bytes';
- var k = 1024;
- var dm = decimals < 0 ? 0 : decimals;
- var sizes = [ 'bytes', 'KB', 'MB' ];
- var i = Math.floor( Math.log( bytes ) / Math.log( k ) );
- return parseFloat( ( bytes / Math.pow( k, i ) ).toFixed( dm ) ) + ' ' + sizes[ i ];
- }
- </script>
- </body>
- </html>
|