webgl_performance_nodes.html 5.6 KB

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