webgl_performance_nodes.html 5.4 KB

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