webgl2_sandbox.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl2 sandbox</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. <style>
  8. body {
  9. background:#000;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family:Monospace;
  21. font-size:13px;
  22. text-align:center;
  23. z-index:1000;
  24. }
  25. a {
  26. color: #ffffff;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl2 sandbox.</div>
  32. <script type="module">
  33. import { PerspectiveCamera } from '../src/cameras/PerspectiveCamera.js';
  34. import { SphereBufferGeometry } from '../src/geometries/SphereGeometry.js';
  35. import { MeshNormalMaterial } from '../src/materials/MeshNormalMaterial.js';
  36. import { PointLight } from '../src/lights/PointLight.js';
  37. import { Color } from '../src/math/Color.js';
  38. import { Mesh } from '../src/objects/Mesh.js';
  39. import { Fog } from '../src/scenes/Fog.js';
  40. import { Scene } from '../src/scenes/Scene.js';
  41. import { WebGL2Renderer } from '../src/renderers/WebGL2Renderer.js';
  42. //
  43. var camera, scene, renderer;
  44. var mouseX = 0, mouseY = 0;
  45. var windowHalfX = window.innerWidth / 2;
  46. var windowHalfY = window.innerHeight / 2;
  47. init();
  48. animate();
  49. function init() {
  50. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  51. camera.position.z = 3200;
  52. scene = new Scene();
  53. scene.background = new Color( 0, 0, 0.5 );
  54. scene.fog = new Fog( 0x000000, 1, 20000 );
  55. var light = new PointLight( 0xffffff );
  56. scene.add( light );
  57. var geometry = new SphereBufferGeometry( 50, 32, 16 );
  58. var material = new MeshNormalMaterial();
  59. for ( var i = 0; i < 5000; i ++ ) {
  60. var mesh = new Mesh( geometry, material );
  61. mesh.position.x = Math.random() * 10000 - 5000;
  62. mesh.position.y = Math.random() * 10000 - 5000;
  63. mesh.position.z = Math.random() * 10000 - 5000;
  64. mesh.rotation.y = Math.random() * 2 * Math.PI;
  65. mesh.scale.setScalar( Math.random() * 4 + 1 );
  66. scene.add( mesh );
  67. }
  68. renderer = new WebGL2Renderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. document.body.appendChild( renderer.domElement );
  72. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  73. //
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. windowHalfX = window.innerWidth / 2;
  78. windowHalfY = window.innerHeight / 2;
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function onDocumentMouseMove(event) {
  84. mouseX = ( event.clientX - windowHalfX ) * 10;
  85. mouseY = ( event.clientY - windowHalfY ) * 10;
  86. }
  87. //
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. }
  92. function render() {
  93. camera.position.x += ( mouseX - camera.position.x ) * .05;
  94. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  95. camera.lookAt( scene.position );
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>