webgl_performance_nodes.html 5.3 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="startButton" 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. let stats;
  41. let camera, scene, renderer;
  42. let geometry;
  43. let mouseX = 0, mouseY = 0;
  44. let windowHalfX = window.innerWidth / 2;
  45. let windowHalfY = window.innerHeight / 2;
  46. const meshes = [];
  47. document.addEventListener( 'mousemove', onDocumentMouseMove );
  48. init();
  49. animate();
  50. function createScene( MaterialClass, count ) {
  51. count = count !== undefined ? count : 70;
  52. let i = 0;
  53. for ( i = 0; i < meshes.length; i ++ ) {
  54. meshes[ i ].material.dispose();
  55. scene.remove( meshes[ i ] );
  56. }
  57. meshes.length = 0;
  58. for ( i = 0; i < count; i ++ ) {
  59. const 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. const 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. let 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( 'startButton' ).addEventListener( 'click', function () {
  103. if ( geometry ) {
  104. benchmark();
  105. }
  106. } );
  107. function init() {
  108. const 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 = 1800;
  112. scene = new THREE.Scene();
  113. scene.add( new THREE.PointLight( 0xFFFFFF ) );
  114. //scene.background = new THREE.Color( 0xffffff );
  115. const 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. container.appendChild( renderer.domElement );
  124. stats = new Stats();
  125. container.appendChild( stats.dom );
  126. //
  127. window.addEventListener( 'resize', onWindowResize );
  128. }
  129. function onWindowResize() {
  130. windowHalfX = window.innerWidth / 2;
  131. windowHalfY = window.innerHeight / 2;
  132. camera.aspect = window.innerWidth / window.innerHeight;
  133. camera.updateProjectionMatrix();
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. }
  136. function onDocumentMouseMove( event ) {
  137. mouseX = ( event.clientX - windowHalfX ) * 10;
  138. mouseY = ( event.clientY - windowHalfY ) * 10;
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. camera.position.x += ( mouseX - camera.position.x ) * .05;
  148. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  149. camera.lookAt( scene.position );
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>