webgl_performance_doublesided.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - performance</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. <script type="module">
  11. import {
  12. AmbientLight,
  13. Color,
  14. CubeTextureLoader,
  15. DoubleSide,
  16. Mesh,
  17. MeshPhongMaterial,
  18. MixOperation,
  19. PerspectiveCamera,
  20. PointLight,
  21. RGBFormat,
  22. Scene,
  23. SphereBufferGeometry,
  24. WebGLRenderer
  25. } from "../build/three.module.js";
  26. import Stats from './jsm/libs/stats.module.js';
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var mouseX = 0, mouseY = 0;
  30. var SCREEN_WIDTH = window.innerWidth,
  31. SCREEN_HEIGHT = window.innerHeight;
  32. var windowHalfX = SCREEN_WIDTH / 2;
  33. var windowHalfY = SCREEN_HEIGHT / 2;
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new PerspectiveCamera( 50, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 20000 );
  40. camera.position.z = 3200;
  41. scene = new Scene();
  42. scene.background = new Color( 0x050505 );
  43. scene.add( new AmbientLight( 0x050505 ) );
  44. var light = new PointLight( 0x0011ff, 1, 5500 );
  45. light.position.set( 4000, 0, 0 );
  46. scene.add( light );
  47. var light = new PointLight( 0xff1100, 1, 5500 );
  48. light.position.set( - 4000, 0, 0 );
  49. scene.add( light );
  50. var light = new PointLight( 0xffaa00, 2, 3000 );
  51. light.position.set( 0, 0, 0 );
  52. scene.add( light );
  53. var path = "textures/cube/SwedishRoyalCastle/";
  54. var format = '.jpg';
  55. var urls = [
  56. path + 'px' + format, path + 'nx' + format,
  57. path + 'py' + format, path + 'ny' + format,
  58. path + 'pz' + format, path + 'nz' + format
  59. ];
  60. var reflectionCube = new CubeTextureLoader().load( urls );
  61. reflectionCube.format = RGBFormat;
  62. var material = new MeshPhongMaterial( { specular: 0x101010, shininess: 100, envMap: reflectionCube, combine: MixOperation, reflectivity: 0.1, side: DoubleSide } );
  63. var geometry = new SphereBufferGeometry( 1, 32, 16, 0, Math.PI );
  64. for ( var i = 0; i < 5000; i ++ ) {
  65. var mesh = new Mesh( geometry, material );
  66. mesh.position.x = Math.random() * 10000 - 5000;
  67. mesh.position.y = Math.random() * 10000 - 5000;
  68. mesh.position.z = Math.random() * 10000 - 5000;
  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. }
  76. renderer = new WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  79. renderer.gammaInput = true;
  80. renderer.gammaOutput = true;
  81. container.appendChild( renderer.domElement );
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. window.addEventListener( 'resize', onWindowResize, false );
  86. }
  87. //
  88. function onWindowResize() {
  89. SCREEN_WIDTH = window.innerWidth;
  90. SCREEN_HEIGHT = window.innerHeight;
  91. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  92. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  93. camera.updateProjectionMatrix();
  94. windowHalfX = SCREEN_WIDTH / 2;
  95. windowHalfY = SCREEN_HEIGHT / 2;
  96. }
  97. function onDocumentMouseMove( event ) {
  98. mouseX = ( event.clientX - windowHalfX ) * 10;
  99. mouseY = ( event.clientY - windowHalfY ) * 10;
  100. }
  101. //
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. render();
  105. stats.update();
  106. }
  107. function render() {
  108. camera.position.x += ( mouseX - camera.position.x ) * .05;
  109. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  110. camera.lookAt( scene.position );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>