webgl_performance_nodes.html 5.4 KB

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