webgl_performance_nodes.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. <!-- Import maps polyfill -->
  34. <!-- Remove this when import maps will be widely supported -->
  35. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  36. <script type="importmap">
  37. {
  38. "imports": {
  39. "three": "../build/three.module.js"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import Stats from './jsm/libs/stats.module.js';
  46. import {
  47. StandardNodeMaterial,
  48. MeshStandardNodeMaterial
  49. } from './jsm/nodes/Nodes.js';
  50. let stats;
  51. let camera, scene, renderer;
  52. let geometry;
  53. let mouseX = 0, mouseY = 0;
  54. let windowHalfX = window.innerWidth / 2;
  55. let windowHalfY = window.innerHeight / 2;
  56. const meshes = [];
  57. document.addEventListener( 'mousemove', onDocumentMouseMove );
  58. init();
  59. animate();
  60. function createScene( MaterialClass, count ) {
  61. count = count !== undefined ? count : 70;
  62. let i = 0;
  63. for ( i = 0; i < meshes.length; i ++ ) {
  64. meshes[ i ].material.dispose();
  65. scene.remove( meshes[ i ] );
  66. }
  67. meshes.length = 0;
  68. for ( i = 0; i < count; i ++ ) {
  69. const material = new MaterialClass(),
  70. color = 0xFFFFFF * Math.random();
  71. if ( material.color.isNode ) material.color.value.setHex( color );
  72. else material.color.setHex( color );
  73. // prevent share code
  74. material.defines.UUID = material.uuid;
  75. const mesh = new THREE.Mesh( geometry, material );
  76. mesh.position.x = Math.random() * 1000 - 500;
  77. mesh.position.y = Math.random() * 1000 - 500;
  78. mesh.position.z = Math.random() * 1000 - 500;
  79. mesh.rotation.x = Math.random() * 2 * Math.PI;
  80. mesh.rotation.y = Math.random() * 2 * Math.PI;
  81. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  82. mesh.matrixAutoUpdate = false;
  83. mesh.updateMatrix();
  84. scene.add( mesh );
  85. meshes.push( mesh );
  86. }
  87. }
  88. function benchmark() {
  89. let time, benchmarkTime;
  90. // Stabilizes CPU
  91. createScene( THREE.MeshStandardMaterial, 10 );
  92. render();
  93. // Standard *Node* Material
  94. time = performance.now();
  95. createScene( StandardNodeMaterial );
  96. render();
  97. benchmarkTime = ( performance.now() - time ) / 1000;
  98. document.getElementById( 'node' ).textContent = benchmarkTime.toFixed( 3 ) + ' seconds';
  99. // Mesh Standard *Node* Material
  100. time = performance.now();
  101. createScene( MeshStandardNodeMaterial );
  102. render();
  103. benchmarkTime = ( performance.now() - time ) / 1000;
  104. document.getElementById( 'nodeBased' ).textContent = benchmarkTime.toFixed( 3 ) + ' seconds';
  105. // Mesh Standard Material
  106. time = performance.now();
  107. createScene( THREE.MeshStandardMaterial );
  108. render();
  109. benchmarkTime = ( performance.now() - time ) / 1000;
  110. document.getElementById( 'default' ).textContent = benchmarkTime.toFixed( 3 ) + ' seconds';
  111. }
  112. document.getElementById( 'startButton' ).addEventListener( 'click', function () {
  113. if ( geometry ) {
  114. benchmark();
  115. }
  116. } );
  117. function init() {
  118. const container = document.createElement( 'div' );
  119. document.body.appendChild( container );
  120. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  121. camera.position.z = 1800;
  122. scene = new THREE.Scene();
  123. scene.add( new THREE.PointLight( 0xFFFFFF ) );
  124. //scene.background = new THREE.Color( 0xffffff );
  125. const loader = new THREE.BufferGeometryLoader();
  126. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geo ) {
  127. geo.computeVertexNormals();
  128. geometry = geo;
  129. } );
  130. renderer = new THREE.WebGLRenderer();
  131. renderer.setPixelRatio( window.devicePixelRatio );
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. container.appendChild( renderer.domElement );
  134. stats = new Stats();
  135. container.appendChild( stats.dom );
  136. //
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. windowHalfX = window.innerWidth / 2;
  141. windowHalfY = window.innerHeight / 2;
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function onDocumentMouseMove( event ) {
  147. mouseX = ( event.clientX - windowHalfX ) * 10;
  148. mouseY = ( event.clientY - windowHalfY ) * 10;
  149. }
  150. //
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. render();
  154. stats.update();
  155. }
  156. function render() {
  157. camera.position.x += ( mouseX - camera.position.x ) * .05;
  158. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  159. camera.lookAt( scene.position );
  160. renderer.render( scene, camera );
  161. }
  162. </script>
  163. </body>
  164. </html>