webgl_performance_nodes.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - performance [nodes]</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script src="../build/three.js"></script>
  11. <script src="js/libs/stats.min.js"></script>
  12. <div id="info">
  13. <a href="http://threejs.org" target="_blank">three.js</a><span class="white"> - NodeMaterial Performance</span><br />
  14. <br>
  15. <b>Node Material System</b>
  16. <br>
  17. <div>
  18. Standard<b>Node</b>Material |
  19. <span id="node" class="white">None</span>
  20. </div>
  21. <div>
  22. MeshStandard<b>Node</b>Material |
  23. <span id="nodeBased" class="white">None</span>
  24. </div>
  25. <br>
  26. <b>Current Material System</b>
  27. <br>
  28. <div>
  29. MeshStandardMaterial |
  30. <span id="default" class="white">None</span>
  31. </div>
  32. <br>
  33. <a id="bench" href="javascript:void(0);">Click to benchmark</a>
  34. </div>
  35. <script type="module">
  36. import './js/nodes/THREE.Nodes.js';
  37. var container, stats;
  38. var camera, scene, renderer;
  39. var geometry;
  40. var meshes = [];
  41. var mouseX = 0, mouseY = 0;
  42. var windowHalfX = window.innerWidth / 2;
  43. var windowHalfY = window.innerHeight / 2;
  44. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  45. init();
  46. animate();
  47. function createScene( MaterialClass, count ) {
  48. count = count !== undefined ? count : 70;
  49. var i = 0;
  50. for ( i = 0; i < meshes.length; i ++ ) {
  51. meshes[ i ].material.dispose();
  52. scene.remove( meshes[ i ] );
  53. }
  54. meshes = [];
  55. for ( i = 0; i < count; i ++ ) {
  56. var material = new MaterialClass(),
  57. color = 0xFFFFFF * Math.random();
  58. if ( material.color.isNode ) material.color.value.setHex( color );
  59. else material.color.setHex( color );
  60. // prevent share code
  61. material.defines.UUID = material.uuid;
  62. var mesh = new THREE.Mesh( geometry, material );
  63. mesh.position.x = Math.random() * 1000 - 500;
  64. mesh.position.y = Math.random() * 1000 - 500;
  65. mesh.position.z = Math.random() * 1000 - 500;
  66. mesh.rotation.x = Math.random() * 2 * Math.PI;
  67. mesh.rotation.y = Math.random() * 2 * Math.PI;
  68. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  69. mesh.matrixAutoUpdate = false;
  70. mesh.updateMatrix();
  71. scene.add( mesh );
  72. meshes.push( mesh );
  73. }
  74. }
  75. function benchmark() {
  76. var time, benchmarkTime;
  77. // Stabilizes CPU
  78. createScene( THREE.MeshStandardMaterial, 10 );
  79. render();
  80. // Standard *Node* Material
  81. time = performance.now();
  82. createScene( THREE.StandardNodeMaterial );
  83. render();
  84. benchmarkTime = ( performance.now() - time ) / 1000;
  85. document.getElementById( 'node' ).textContent = benchmarkTime.toFixed( 3 ) + " seconds";
  86. // Mesh Standard *Node* Material
  87. time = performance.now();
  88. createScene( THREE.MeshStandardNodeMaterial );
  89. render();
  90. benchmarkTime = ( performance.now() - time ) / 1000;
  91. document.getElementById( 'nodeBased' ).textContent = benchmarkTime.toFixed( 3 ) + " seconds";
  92. // Mesh Standard Material
  93. time = performance.now();
  94. createScene( THREE.MeshStandardMaterial );
  95. render();
  96. benchmarkTime = ( performance.now() - time ) / 1000;
  97. document.getElementById( 'default' ).textContent = benchmarkTime.toFixed( 3 ) + " seconds";
  98. }
  99. document.getElementById( 'bench' ).addEventListener( 'click', function () {
  100. if ( geometry ) {
  101. benchmark();
  102. }
  103. } );
  104. function init() {
  105. container = document.createElement( 'div' );
  106. document.body.appendChild( container );
  107. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  108. camera.position.z = 3200;
  109. scene = new THREE.Scene();
  110. scene.add( new THREE.PointLight( 0xFFFFFF ) );
  111. //scene.background = new THREE.Color( 0xffffff );
  112. var loader = new THREE.BufferGeometryLoader();
  113. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geo ) {
  114. geo.computeVertexNormals();
  115. geometry = geo;
  116. } );
  117. renderer = new THREE.WebGLRenderer();
  118. renderer.setPixelRatio( window.devicePixelRatio );
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. //renderer.sortObjects = false;
  121. container.appendChild( renderer.domElement );
  122. stats = new Stats();
  123. container.appendChild( stats.dom );
  124. //
  125. window.addEventListener( 'resize', onWindowResize, false );
  126. }
  127. function onWindowResize() {
  128. windowHalfX = window.innerWidth / 2;
  129. windowHalfY = window.innerHeight / 2;
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function onDocumentMouseMove( event ) {
  135. mouseX = ( event.clientX - windowHalfX ) * 10;
  136. mouseY = ( event.clientY - windowHalfY ) * 10;
  137. }
  138. //
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. render();
  142. stats.update();
  143. }
  144. function render() {
  145. camera.position.x += ( mouseX - camera.position.x ) * .05;
  146. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  147. camera.lookAt( scene.position );
  148. renderer.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>